blob: 1cd701e61345e7f05280ab46cb564b3f5028b081 [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>
Emeric Brun46591952012-05-18 15:47:34 +020087#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020088#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020089#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010090#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020091
Willy Tarreau9356dac2019-05-10 09:22:53 +020092/* ***** READ THIS before adding code here! *****
93 *
94 * Due to API incompatibilities between multiple OpenSSL versions and their
95 * derivatives, it's often tempting to add macros to (re-)define certain
96 * symbols. Please do not do this here, and do it in common/openssl-compat.h
97 * exclusively so that the whole code consistently uses the same macros.
98 *
99 * Whenever possible if a macro is missing in certain versions, it's better
100 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
101 */
102
Willy Tarreau518cedd2014-02-17 15:43:01 +0100103/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200104#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100105#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100106#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200107#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
108
Emeric Brunf282a812012-09-21 15:27:54 +0200109/* bits 0xFFFF0000 are reserved to store verify errors */
110
111/* Verify errors macros */
112#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
113#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
114#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
115
116#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
117#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
118#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200119
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200120/* ssl_methods flags for ssl options */
121#define MC_SSL_O_ALL 0x0000
122#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
123#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
124#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
125#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200126#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200127
William Lallemand3af48e72020-02-03 17:15:52 +0100128/* file to guess during file loading */
129#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
130#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
131#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
132#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
133#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
William Lallemand4c5adbf2020-02-24 14:23:22 +0100134#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
William Lallemand3af48e72020-02-03 17:15:52 +0100135
William Lallemand4c5adbf2020-02-24 14:23:22 +0100136#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
William Lallemand3af48e72020-02-03 17:15:52 +0100137
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200138/* ssl_methods versions */
139enum {
140 CONF_TLSV_NONE = 0,
141 CONF_TLSV_MIN = 1,
142 CONF_SSLV3 = 1,
143 CONF_TLSV10 = 2,
144 CONF_TLSV11 = 3,
145 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200146 CONF_TLSV13 = 5,
147 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200148};
149
Emeric Brun850efd52014-01-29 12:24:34 +0100150/* server and bind verify method, it uses a global value as default */
151enum {
152 SSL_SOCK_VERIFY_DEFAULT = 0,
153 SSL_SOCK_VERIFY_REQUIRED = 1,
154 SSL_SOCK_VERIFY_OPTIONAL = 2,
155 SSL_SOCK_VERIFY_NONE = 3,
156};
157
Willy Tarreau71b734c2014-01-28 15:19:44 +0100158int sslconns = 0;
159int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100160static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100161int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200162
William Lallemande0f3fd52020-02-25 14:53:06 +0100163static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +0100164static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert);
William Lallemande0f3fd52020-02-25 14:53:06 +0100165
Willy Tarreauef934602016-12-22 23:12:01 +0100166static struct {
167 char *crt_base; /* base directory path for certificates */
168 char *ca_base; /* base directory path for CAs and CRLs */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100169 char *issuers_chain_path; /* from "issuers-chain-path" */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100170
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000171 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100172
173 char *listen_default_ciphers;
174 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200175#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200176 char *listen_default_ciphersuites;
177 char *connect_default_ciphersuites;
178#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100179 int listen_default_ssloptions;
180 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200181 struct tls_version_filter listen_default_sslmethods;
182 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100183
184 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
185 unsigned int life_time; /* SSL session lifetime in seconds */
186 unsigned int max_record; /* SSL max record size */
187 unsigned int default_dh_param; /* SSL maximum DH parameter size */
188 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100189 int capture_cipherlist; /* Size of the cipherlist buffer. */
William Lallemand3af48e72020-02-03 17:15:52 +0100190 int extra_files; /* which files not defined in the configuration file are we looking for */
Willy Tarreauef934602016-12-22 23:12:01 +0100191} global_ssl = {
192#ifdef LISTEN_DEFAULT_CIPHERS
193 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
194#endif
195#ifdef CONNECT_DEFAULT_CIPHERS
196 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
197#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200198#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200199#ifdef LISTEN_DEFAULT_CIPHERSUITES
200 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
201#endif
202#ifdef CONNECT_DEFAULT_CIPHERSUITES
203 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
204#endif
205#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100206 .listen_default_ssloptions = BC_SSL_O_NONE,
207 .connect_default_ssloptions = SRV_SSL_O_NONE,
208
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200209 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
210 .listen_default_sslmethods.min = CONF_TLSV_NONE,
211 .listen_default_sslmethods.max = CONF_TLSV_NONE,
212 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
213 .connect_default_sslmethods.min = CONF_TLSV_NONE,
214 .connect_default_sslmethods.max = CONF_TLSV_NONE,
215
Willy Tarreauef934602016-12-22 23:12:01 +0100216#ifdef DEFAULT_SSL_MAX_RECORD
217 .max_record = DEFAULT_SSL_MAX_RECORD,
218#endif
219 .default_dh_param = SSL_DEFAULT_DH_PARAM,
220 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100221 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100222 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100223};
224
Olivier Houcharda8955d52019-04-07 22:00:38 +0200225static BIO_METHOD *ha_meth;
226
Olivier Houchard66ab4982019-02-26 18:37:15 +0100227struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200228 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100229 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200230 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200231 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100232 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200233 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100234 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100235 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100236 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100237 int sent_early_data; /* Amount of early data we sent so far */
238
Olivier Houchard66ab4982019-02-26 18:37:15 +0100239};
240
241DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
242
Olivier Houchardea8dd942019-05-20 14:02:16 +0200243static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200244static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200245
Olivier Houcharda8955d52019-04-07 22:00:38 +0200246/* Methods to implement OpenSSL BIO */
247static int ha_ssl_write(BIO *h, const char *buf, int num)
248{
249 struct buffer tmpbuf;
250 struct ssl_sock_ctx *ctx;
251 int ret;
252
253 ctx = BIO_get_data(h);
254 tmpbuf.size = num;
255 tmpbuf.area = (void *)(uintptr_t)buf;
256 tmpbuf.data = num;
257 tmpbuf.head = 0;
258 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200259 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200260 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200261 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200262 } else if (ret == 0)
263 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200264 return ret;
265}
266
267static int ha_ssl_gets(BIO *h, char *buf, int size)
268{
269
270 return 0;
271}
272
273static int ha_ssl_puts(BIO *h, const char *str)
274{
275
276 return ha_ssl_write(h, str, strlen(str));
277}
278
279static int ha_ssl_read(BIO *h, char *buf, int size)
280{
281 struct buffer tmpbuf;
282 struct ssl_sock_ctx *ctx;
283 int ret;
284
285 ctx = BIO_get_data(h);
286 tmpbuf.size = size;
287 tmpbuf.area = buf;
288 tmpbuf.data = 0;
289 tmpbuf.head = 0;
290 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200291 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200292 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200293 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200294 } else if (ret == 0)
295 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200296
297 return ret;
298}
299
300static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
301{
302 int ret = 0;
303 switch (cmd) {
304 case BIO_CTRL_DUP:
305 case BIO_CTRL_FLUSH:
306 ret = 1;
307 break;
308 }
309 return ret;
310}
311
312static int ha_ssl_new(BIO *h)
313{
314 BIO_set_init(h, 1);
315 BIO_set_data(h, NULL);
316 BIO_clear_flags(h, ~0);
317 return 1;
318}
319
320static int ha_ssl_free(BIO *data)
321{
322
323 return 1;
324}
325
326
Willy Tarreau5db847a2019-05-09 14:13:35 +0200327#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100328
Emeric Brun821bb9b2017-06-15 16:37:39 +0200329static HA_RWLOCK_T *ssl_rwlocks;
330
331
332unsigned long ssl_id_function(void)
333{
334 return (unsigned long)tid;
335}
336
337void ssl_locking_function(int mode, int n, const char * file, int line)
338{
339 if (mode & CRYPTO_LOCK) {
340 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200342 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200344 }
345 else {
346 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200348 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100349 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200350 }
351}
352
353static int ssl_locking_init(void)
354{
355 int i;
356
357 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
358 if (!ssl_rwlocks)
359 return -1;
360
361 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100362 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200363
364 CRYPTO_set_id_callback(ssl_id_function);
365 CRYPTO_set_locking_callback(ssl_locking_function);
366
367 return 0;
368}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100369
Emeric Brun821bb9b2017-06-15 16:37:39 +0200370#endif
371
William Lallemand150bfa82019-09-19 17:12:49 +0200372__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200373
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100374/* Uncommitted CKCH transaction */
375
376static struct {
377 struct ckch_store *new_ckchs;
378 struct ckch_store *old_ckchs;
379 char *path;
380} ckchs_transaction;
381
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200382/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100383 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200384 */
385struct cafile_entry {
386 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200387 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200388 struct ebmb_node node;
389 char path[0];
390};
391
392static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
393
394static X509_STORE* ssl_store_get0_locations_file(char *path)
395{
396 struct ebmb_node *eb;
397
398 eb = ebst_lookup(&cafile_tree, path);
399 if (eb) {
400 struct cafile_entry *ca_e;
401 ca_e = ebmb_entry(eb, struct cafile_entry, node);
402 return ca_e->ca_store;
403 }
404 return NULL;
405}
406
407static int ssl_store_load_locations_file(char *path)
408{
409 if (ssl_store_get0_locations_file(path) == NULL) {
410 struct cafile_entry *ca_e;
411 X509_STORE *store = X509_STORE_new();
412 if (X509_STORE_load_locations(store, path, NULL)) {
413 int pathlen;
414 pathlen = strlen(path);
415 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
416 if (ca_e) {
417 memcpy(ca_e->path, path, pathlen + 1);
418 ca_e->ca_store = store;
419 ebst_insert(&cafile_tree, &ca_e->node);
420 return 1;
421 }
422 }
423 X509_STORE_free(store);
424 return 0;
425 }
426 return 1;
427}
428
429/* mimic what X509_STORE_load_locations do with store_ctx */
430static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
431{
432 X509_STORE *store;
433 store = ssl_store_get0_locations_file(path);
434 if (store_ctx && store) {
435 int i;
436 X509_OBJECT *obj;
437 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
438 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
439 obj = sk_X509_OBJECT_value(objs, i);
440 switch (X509_OBJECT_get_type(obj)) {
441 case X509_LU_X509:
442 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
443 break;
444 case X509_LU_CRL:
445 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
446 break;
447 default:
448 break;
449 }
450 }
451 return 1;
452 }
453 return 0;
454}
455
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500456/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200457static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
458{
459 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
460 return ssl_set_cert_crl_file(store_ctx, path);
461}
462
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200463/*
464 Extract CA_list from CA_file already in tree.
465 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
466 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
467*/
468static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
469{
470 struct ebmb_node *eb;
471 struct cafile_entry *ca_e;
472
473 eb = ebst_lookup(&cafile_tree, path);
474 if (!eb)
475 return NULL;
476 ca_e = ebmb_entry(eb, struct cafile_entry, node);
477
478 if (ca_e->ca_list == NULL) {
479 int i;
480 unsigned long key;
481 struct eb_root ca_name_tree = EB_ROOT;
482 struct eb64_node *node, *back;
483 struct {
484 struct eb64_node node;
485 X509_NAME *xname;
486 } *ca_name;
487 STACK_OF(X509_OBJECT) *objs;
488 STACK_OF(X509_NAME) *skn;
489 X509 *x;
490 X509_NAME *xn;
491
492 skn = sk_X509_NAME_new_null();
493 /* take x509 from cafile_tree */
494 objs = X509_STORE_get0_objects(ca_e->ca_store);
495 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
496 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
497 if (!x)
498 continue;
499 xn = X509_get_subject_name(x);
500 if (!xn)
501 continue;
502 /* Check for duplicates. */
503 key = X509_NAME_hash(xn);
504 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
505 node && ca_name == NULL;
506 node = eb64_next(node)) {
507 ca_name = container_of(node, typeof(*ca_name), node);
508 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
509 ca_name = NULL;
510 }
511 /* find a duplicate */
512 if (ca_name)
513 continue;
514 ca_name = calloc(1, sizeof *ca_name);
515 xn = X509_NAME_dup(xn);
516 if (!ca_name ||
517 !xn ||
518 !sk_X509_NAME_push(skn, xn)) {
519 free(ca_name);
520 X509_NAME_free(xn);
521 sk_X509_NAME_pop_free(skn, X509_NAME_free);
522 sk_X509_NAME_free(skn);
523 skn = NULL;
524 break;
525 }
526 ca_name->node.key = key;
527 ca_name->xname = xn;
528 eb64_insert(&ca_name_tree, &ca_name->node);
529 }
530 ca_e->ca_list = skn;
531 /* remove temporary ca_name tree */
532 node = eb64_first(&ca_name_tree);
533 while (node) {
534 ca_name = container_of(node, typeof(*ca_name), node);
535 back = eb64_next(node);
536 eb64_delete(node);
537 free(ca_name);
538 node = back;
539 }
540 }
541 return ca_e->ca_list;
542}
543
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100544/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100545struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100546 unsigned long long int xxh64;
547 unsigned char ciphersuite_len;
548 char ciphersuite[0];
549};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100550struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100551static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200552static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100553
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200554#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
555struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
556#endif
557
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200558#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000559static unsigned int openssl_engines_initialized;
560struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
561struct ssl_engine_list {
562 struct list list;
563 ENGINE *e;
564};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200565#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000566
Remi Gacogne8de54152014-07-15 11:36:40 +0200567#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200568static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200569static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200570static DH *local_dh_1024 = NULL;
571static DH *local_dh_2048 = NULL;
572static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100573static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200574#endif /* OPENSSL_NO_DH */
575
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100576#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200577/* X509V3 Extensions that will be added on generated certificates */
578#define X509V3_EXT_SIZE 5
579static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
580 "basicConstraints",
581 "nsComment",
582 "subjectKeyIdentifier",
583 "authorityKeyIdentifier",
584 "keyUsage",
585};
586static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
587 "CA:FALSE",
588 "\"OpenSSL Generated Certificate\"",
589 "hash",
590 "keyid,issuer:always",
591 "nonRepudiation,digitalSignature,keyEncipherment"
592};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200593/* LRU cache to store generated certificate */
594static struct lru64_head *ssl_ctx_lru_tree = NULL;
595static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200596static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100597__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200598
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200599#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
600
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100601static struct ssl_bind_kw ssl_bind_kws[];
602
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200603#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500604/* The order here matters for picking a default context,
605 * keep the most common keytype at the bottom of the list
606 */
607const char *SSL_SOCK_KEYTYPE_NAMES[] = {
608 "dsa",
609 "ecdsa",
610 "rsa"
611};
612#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100613#else
614#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500615#endif
616
William Lallemandc3cd35f2017-11-28 11:04:43 +0100617static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100618static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
619
620#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
621
622#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
623 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
624
625#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
626 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200627
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100628/*
629 * This function gives the detail of the SSL error. It is used only
630 * if the debug mode and the verbose mode are activated. It dump all
631 * the SSL error until the stack was empty.
632 */
633static forceinline void ssl_sock_dump_errors(struct connection *conn)
634{
635 unsigned long ret;
636
637 if (unlikely(global.mode & MODE_DEBUG)) {
638 while(1) {
639 ret = ERR_get_error();
640 if (ret == 0)
641 return;
642 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200643 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100644 ERR_func_error_string(ret), ERR_reason_error_string(ret));
645 }
646 }
647}
648
yanbzhube2774d2015-12-10 15:07:30 -0500649
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200650#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000651static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
652{
653 int err_code = ERR_ABORT;
654 ENGINE *engine;
655 struct ssl_engine_list *el;
656
657 /* grab the structural reference to the engine */
658 engine = ENGINE_by_id(engine_id);
659 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100660 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000661 goto fail_get;
662 }
663
664 if (!ENGINE_init(engine)) {
665 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100666 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000667 goto fail_init;
668 }
669
670 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100671 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000672 goto fail_set_method;
673 }
674
675 el = calloc(1, sizeof(*el));
676 el->e = engine;
677 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100678 nb_engines++;
679 if (global_ssl.async)
680 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000681 return 0;
682
683fail_set_method:
684 /* release the functional reference from ENGINE_init() */
685 ENGINE_finish(engine);
686
687fail_init:
688 /* release the structural reference from ENGINE_by_id() */
689 ENGINE_free(engine);
690
691fail_get:
692 return err_code;
693}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200694#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000695
Willy Tarreau5db847a2019-05-09 14:13:35 +0200696#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200697/*
698 * openssl async fd handler
699 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200700void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000701{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200702 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000703
Emeric Brun3854e012017-05-17 20:42:48 +0200704 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000705 * to poll this fd until it is requested
706 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000707 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000708 fd_cant_recv(fd);
709
710 /* crypto engine is available, let's notify the associated
711 * connection that it can pursue its processing.
712 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200713 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000714}
715
Emeric Brun3854e012017-05-17 20:42:48 +0200716/*
717 * openssl async delayed SSL_free handler
718 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200719void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000720{
721 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200722 OSSL_ASYNC_FD all_fd[32];
723 size_t num_all_fds = 0;
724 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000725
Emeric Brun3854e012017-05-17 20:42:48 +0200726 /* We suppose that the async job for a same SSL *
727 * are serialized. So if we are awake it is
728 * because the running job has just finished
729 * and we can remove all async fds safely
730 */
731 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
732 if (num_all_fds > 32) {
733 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
734 return;
735 }
736
737 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
738 for (i=0 ; i < num_all_fds ; i++)
739 fd_remove(all_fd[i]);
740
741 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000742 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100743 _HA_ATOMIC_SUB(&sslconns, 1);
744 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000745}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000746/*
Emeric Brun3854e012017-05-17 20:42:48 +0200747 * function used to manage a returned SSL_ERROR_WANT_ASYNC
748 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000749 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200750static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000751{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100752 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200753 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200754 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000755 size_t num_add_fds = 0;
756 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200757 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000758
759 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
760 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200761 if (num_add_fds > 32 || num_del_fds > 32) {
762 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 +0000763 return;
764 }
765
Emeric Brun3854e012017-05-17 20:42:48 +0200766 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000767
Emeric Brun3854e012017-05-17 20:42:48 +0200768 /* We remove unused fds from the fdtab */
769 for (i=0 ; i < num_del_fds ; i++)
770 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000771
Emeric Brun3854e012017-05-17 20:42:48 +0200772 /* We add new fds to the fdtab */
773 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200774 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000775 }
776
Emeric Brun3854e012017-05-17 20:42:48 +0200777 num_add_fds = 0;
778 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
779 if (num_add_fds > 32) {
780 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
781 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000782 }
Emeric Brun3854e012017-05-17 20:42:48 +0200783
784 /* We activate the polling for all known async fds */
785 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000786 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200787 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000788 /* To ensure that the fd cache won't be used
789 * We'll prefer to catch a real RD event
790 * because handling an EAGAIN on this fd will
791 * result in a context switch and also
792 * some engines uses a fd in blocking mode.
793 */
794 fd_cant_recv(add_fd[i]);
795 }
Emeric Brun3854e012017-05-17 20:42:48 +0200796
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000797}
798#endif
799
William Lallemand104a7a62019-10-14 14:14:59 +0200800#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200801/*
802 * This function returns the number of seconds elapsed
803 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
804 * date presented un ASN1_GENERALIZEDTIME.
805 *
806 * In parsing error case, it returns -1.
807 */
808static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
809{
810 long epoch;
811 char *p, *end;
812 const unsigned short month_offset[12] = {
813 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
814 };
815 int year, month;
816
817 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
818
819 p = (char *)d->data;
820 end = p + d->length;
821
822 if (end - p < 4) return -1;
823 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
824 p += 4;
825 if (end - p < 2) return -1;
826 month = 10 * (p[0] - '0') + p[1] - '0';
827 if (month < 1 || month > 12) return -1;
828 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
829 We consider leap years and the current month (<marsh or not) */
830 epoch = ( ((year - 1970) * 365)
831 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
832 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
833 + month_offset[month-1]
834 ) * 24 * 60 * 60;
835 p += 2;
836 if (end - p < 2) return -1;
837 /* Add the number of seconds of completed days of current month */
838 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
839 p += 2;
840 if (end - p < 2) return -1;
841 /* Add the completed hours of the current day */
842 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
843 p += 2;
844 if (end - p < 2) return -1;
845 /* Add the completed minutes of the current hour */
846 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
847 p += 2;
848 if (p == end) return -1;
849 /* Test if there is available seconds */
850 if (p[0] < '0' || p[0] > '9')
851 goto nosec;
852 if (end - p < 2) return -1;
853 /* Add the seconds of the current minute */
854 epoch += 10 * (p[0] - '0') + p[1] - '0';
855 p += 2;
856 if (p == end) return -1;
857 /* Ignore seconds float part if present */
858 if (p[0] == '.') {
859 do {
860 if (++p == end) return -1;
861 } while (p[0] >= '0' && p[0] <= '9');
862 }
863
864nosec:
865 if (p[0] == 'Z') {
866 if (end - p != 1) return -1;
867 return epoch;
868 }
869 else if (p[0] == '+') {
870 if (end - p != 5) return -1;
871 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700872 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 +0200873 }
874 else if (p[0] == '-') {
875 if (end - p != 5) return -1;
876 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700877 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 +0200878 }
879
880 return -1;
881}
882
William Lallemand104a7a62019-10-14 14:14:59 +0200883/*
884 * struct alignment works here such that the key.key is the same as key_data
885 * Do not change the placement of key_data
886 */
887struct certificate_ocsp {
888 struct ebmb_node key;
889 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
890 struct buffer response;
891 long expire;
892};
893
894struct ocsp_cbk_arg {
895 int is_single;
896 int single_kt;
897 union {
898 struct certificate_ocsp *s_ocsp;
899 /*
900 * m_ocsp will have multiple entries dependent on key type
901 * Entry 0 - DSA
902 * Entry 1 - ECDSA
903 * Entry 2 - RSA
904 */
905 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
906 };
907};
908
Emeric Brun1d3865b2014-06-20 15:37:32 +0200909static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200910
911/* This function starts to check if the OCSP response (in DER format) contained
912 * in chunk 'ocsp_response' is valid (else exits on error).
913 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
914 * contained in the OCSP Response and exits on error if no match.
915 * If it's a valid OCSP Response:
916 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
917 * pointed by 'ocsp'.
918 * If 'ocsp' is NULL, the function looks up into the OCSP response's
919 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
920 * from the response) and exits on error if not found. Finally, If an OCSP response is
921 * already present in the container, it will be overwritten.
922 *
923 * Note: OCSP response containing more than one OCSP Single response is not
924 * considered valid.
925 *
926 * Returns 0 on success, 1 in error case.
927 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200928static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
929 struct certificate_ocsp *ocsp,
930 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200931{
932 OCSP_RESPONSE *resp;
933 OCSP_BASICRESP *bs = NULL;
934 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200935 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200936 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200937 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200938 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200939 int reason;
940 int ret = 1;
941
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200942 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
943 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200944 if (!resp) {
945 memprintf(err, "Unable to parse OCSP response");
946 goto out;
947 }
948
949 rc = OCSP_response_status(resp);
950 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
951 memprintf(err, "OCSP response status not successful");
952 goto out;
953 }
954
955 bs = OCSP_response_get1_basic(resp);
956 if (!bs) {
957 memprintf(err, "Failed to get basic response from OCSP Response");
958 goto out;
959 }
960
961 count_sr = OCSP_resp_count(bs);
962 if (count_sr > 1) {
963 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
964 goto out;
965 }
966
967 sr = OCSP_resp_get0(bs, 0);
968 if (!sr) {
969 memprintf(err, "Failed to get OCSP single response");
970 goto out;
971 }
972
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200973 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
974
Emeric Brun4147b2e2014-06-16 18:36:30 +0200975 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200976 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200977 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200978 goto out;
979 }
980
Emeric Brun13a6b482014-06-20 15:44:34 +0200981 if (!nextupd) {
982 memprintf(err, "OCSP single response: missing nextupdate");
983 goto out;
984 }
985
Emeric Brunc8b27b62014-06-19 14:16:17 +0200986 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200987 if (!rc) {
988 memprintf(err, "OCSP single response: no longer valid.");
989 goto out;
990 }
991
992 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200993 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200994 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
995 goto out;
996 }
997 }
998
999 if (!ocsp) {
1000 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
1001 unsigned char *p;
1002
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001003 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001004 if (!rc) {
1005 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
1006 goto out;
1007 }
1008
1009 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
1010 memprintf(err, "OCSP single response: Certificate ID too long");
1011 goto out;
1012 }
1013
1014 p = key;
1015 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001016 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001017 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1018 if (!ocsp) {
1019 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1020 goto out;
1021 }
1022 }
1023
1024 /* According to comments on "chunk_dup", the
1025 previous chunk buffer will be freed */
1026 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1027 memprintf(err, "OCSP response: Memory allocation error");
1028 goto out;
1029 }
1030
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001031 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1032
Emeric Brun4147b2e2014-06-16 18:36:30 +02001033 ret = 0;
1034out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001035 ERR_clear_error();
1036
Emeric Brun4147b2e2014-06-16 18:36:30 +02001037 if (bs)
1038 OCSP_BASICRESP_free(bs);
1039
1040 if (resp)
1041 OCSP_RESPONSE_free(resp);
1042
1043 return ret;
1044}
1045/*
1046 * External function use to update the OCSP response in the OCSP response's
1047 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1048 * to update in DER format.
1049 *
1050 * Returns 0 on success, 1 in error case.
1051 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001052int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001053{
1054 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1055}
1056
William Lallemand4a660132019-10-14 14:51:41 +02001057#endif
1058
1059#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001060/*
1061 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001062 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001063 *
1064 * Returns 0 on success, 1 in error case.
1065 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001066static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001067{
1068 int fd = -1;
1069 int r = 0;
1070 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001071 struct buffer *ocsp_response;
1072 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001073
William Lallemand3b5f3602019-10-16 18:05:05 +02001074 if (buf) {
1075 int i, j;
1076 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001077
William Lallemand3b5f3602019-10-16 18:05:05 +02001078 /* remove \r and \n from the payload */
1079 for (i = 0, j = 0; buf[i]; i++) {
1080 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001081 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001082 buf[j++] = buf[i];
1083 }
1084 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001085
William Lallemand3b5f3602019-10-16 18:05:05 +02001086 ret = base64dec(buf, j, trash.area, trash.size);
1087 if (ret < 0) {
1088 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001089 goto end;
1090 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001091 trash.data = ret;
1092 src = &trash;
1093 } else {
1094 fd = open(ocsp_path, O_RDONLY);
1095 if (fd == -1) {
1096 memprintf(err, "Error opening OCSP response file");
1097 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001098 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001099
1100 trash.data = 0;
1101 while (trash.data < trash.size) {
1102 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1103 if (r < 0) {
1104 if (errno == EINTR)
1105 continue;
1106
1107 memprintf(err, "Error reading OCSP response from file");
1108 goto end;
1109 }
1110 else if (r == 0) {
1111 break;
1112 }
1113 trash.data += r;
1114 }
1115 close(fd);
1116 fd = -1;
1117 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001118 }
1119
William Lallemand3b5f3602019-10-16 18:05:05 +02001120 ocsp_response = calloc(1, sizeof(*ocsp_response));
1121 if (!chunk_dup(ocsp_response, src)) {
1122 free(ocsp_response);
1123 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001124 goto end;
1125 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001126 /* no error, fill ckch with new context, old context must be free */
1127 if (ckch->ocsp_response) {
1128 free(ckch->ocsp_response->area);
1129 ckch->ocsp_response->area = NULL;
1130 free(ckch->ocsp_response);
1131 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001132 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001133 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001134end:
1135 if (fd != -1)
1136 close(fd);
1137
1138 return ret;
1139}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001140#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001141
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001142#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1143static 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)
1144{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001145 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001146 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001147 struct connection *conn;
1148 int head;
1149 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001150 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001151
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001152 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001153 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001154 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1155
1156 keys = ref->tlskeys;
1157 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001158
1159 if (enc) {
1160 memcpy(key_name, keys[head].name, 16);
1161
1162 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001163 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001164
Emeric Brun9e754772019-01-10 17:51:55 +01001165 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001166
Emeric Brun9e754772019-01-10 17:51:55 +01001167 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1168 goto end;
1169
Willy Tarreau9356dac2019-05-10 09:22:53 +02001170 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001171 ret = 1;
1172 }
1173 else if (ref->key_size_bits == 256 ) {
1174
1175 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1176 goto end;
1177
Willy Tarreau9356dac2019-05-10 09:22:53 +02001178 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001179 ret = 1;
1180 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001181 } else {
1182 for (i = 0; i < TLS_TICKETS_NO; i++) {
1183 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1184 goto found;
1185 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001186 ret = 0;
1187 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001188
Christopher Faulet16f45c82018-02-16 11:23:49 +01001189 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001190 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001191 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 +01001192 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1193 goto end;
1194 /* 2 for key renewal, 1 if current key is still valid */
1195 ret = i ? 2 : 1;
1196 }
1197 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001198 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 +01001199 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1200 goto end;
1201 /* 2 for key renewal, 1 if current key is still valid */
1202 ret = i ? 2 : 1;
1203 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001204 }
Emeric Brun9e754772019-01-10 17:51:55 +01001205
Christopher Faulet16f45c82018-02-16 11:23:49 +01001206 end:
1207 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1208 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001209}
1210
1211struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1212{
1213 struct tls_keys_ref *ref;
1214
1215 list_for_each_entry(ref, &tlskeys_reference, list)
1216 if (ref->filename && strcmp(filename, ref->filename) == 0)
1217 return ref;
1218 return NULL;
1219}
1220
1221struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1222{
1223 struct tls_keys_ref *ref;
1224
1225 list_for_each_entry(ref, &tlskeys_reference, list)
1226 if (ref->unique_id == unique_id)
1227 return ref;
1228 return NULL;
1229}
1230
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001231/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001232 * match existing ones, this function returns -1
1233 * else it returns 0 on success.
1234 */
1235int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001236 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001237{
Emeric Brun9e754772019-01-10 17:51:55 +01001238 if (ref->key_size_bits == 128) {
1239 if (tlskey->data != sizeof(struct tls_sess_key_128))
1240 return -1;
1241 }
1242 else if (ref->key_size_bits == 256) {
1243 if (tlskey->data != sizeof(struct tls_sess_key_256))
1244 return -1;
1245 }
1246 else
1247 return -1;
1248
Christopher Faulet16f45c82018-02-16 11:23:49 +01001249 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001250 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1251 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001252 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1253 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001254
1255 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001256}
1257
Willy Tarreau83061a82018-07-13 11:56:34 +02001258int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001259{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001260 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1261
1262 if(!ref) {
1263 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1264 return 1;
1265 }
Emeric Brun9e754772019-01-10 17:51:55 +01001266 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1267 memprintf(err, "Invalid key size");
1268 return 1;
1269 }
1270
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001271 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001272}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001273
1274/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001275 * automatic ids. It's called just after the basic checks. It returns
1276 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001277 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001278static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001279{
1280 int i = 0;
1281 struct tls_keys_ref *ref, *ref2, *ref3;
1282 struct list tkr = LIST_HEAD_INIT(tkr);
1283
1284 list_for_each_entry(ref, &tlskeys_reference, list) {
1285 if (ref->unique_id == -1) {
1286 /* Look for the first free id. */
1287 while (1) {
1288 list_for_each_entry(ref2, &tlskeys_reference, list) {
1289 if (ref2->unique_id == i) {
1290 i++;
1291 break;
1292 }
1293 }
1294 if (&ref2->list == &tlskeys_reference)
1295 break;
1296 }
1297
1298 /* Uses the unique id and increment it for the next entry. */
1299 ref->unique_id = i;
1300 i++;
1301 }
1302 }
1303
1304 /* This sort the reference list by id. */
1305 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1306 LIST_DEL(&ref->list);
1307 list_for_each_entry(ref3, &tkr, list) {
1308 if (ref->unique_id < ref3->unique_id) {
1309 LIST_ADDQ(&ref3->list, &ref->list);
1310 break;
1311 }
1312 }
1313 if (&ref3->list == &tkr)
1314 LIST_ADDQ(&tkr, &ref->list);
1315 }
1316
1317 /* swap root */
1318 LIST_ADD(&tkr, &tlskeys_reference);
1319 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001320 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001321}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001322#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1323
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001324#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001325int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1326{
1327 switch (evp_keytype) {
1328 case EVP_PKEY_RSA:
1329 return 2;
1330 case EVP_PKEY_DSA:
1331 return 0;
1332 case EVP_PKEY_EC:
1333 return 1;
1334 }
1335
1336 return -1;
1337}
1338
Emeric Brun4147b2e2014-06-16 18:36:30 +02001339/*
1340 * Callback used to set OCSP status extension content in server hello.
1341 */
1342int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1343{
yanbzhube2774d2015-12-10 15:07:30 -05001344 struct certificate_ocsp *ocsp;
1345 struct ocsp_cbk_arg *ocsp_arg;
1346 char *ssl_buf;
1347 EVP_PKEY *ssl_pkey;
1348 int key_type;
1349 int index;
1350
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001351 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001352
1353 ssl_pkey = SSL_get_privatekey(ssl);
1354 if (!ssl_pkey)
1355 return SSL_TLSEXT_ERR_NOACK;
1356
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001357 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001358
1359 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1360 ocsp = ocsp_arg->s_ocsp;
1361 else {
1362 /* For multiple certs per context, we have to find the correct OCSP response based on
1363 * the certificate type
1364 */
1365 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1366
1367 if (index < 0)
1368 return SSL_TLSEXT_ERR_NOACK;
1369
1370 ocsp = ocsp_arg->m_ocsp[index];
1371
1372 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001373
1374 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 !ocsp->response.area ||
1376 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001377 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001378 return SSL_TLSEXT_ERR_NOACK;
1379
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001380 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001381 if (!ssl_buf)
1382 return SSL_TLSEXT_ERR_NOACK;
1383
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001384 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1385 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001386
1387 return SSL_TLSEXT_ERR_OK;
1388}
1389
William Lallemand4a660132019-10-14 14:51:41 +02001390#endif
1391
1392#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001393/*
1394 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001395 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1396 * status extension, the issuer's certificate is mandatory. It should be
1397 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001398 *
William Lallemand246c0242019-10-11 08:59:13 +02001399 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1400 * OCSP response. If file is empty or content is not a valid OCSP response,
1401 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1402 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001403 *
1404 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001405 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001406 */
William Lallemand4a660132019-10-14 14:51:41 +02001407#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001408static 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 +02001409{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001410 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001411 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001412 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001413 struct certificate_ocsp *ocsp = NULL, *iocsp;
1414 char *warn = NULL;
1415 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001416 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001417
Emeric Brun4147b2e2014-06-16 18:36:30 +02001418
William Lallemand246c0242019-10-11 08:59:13 +02001419 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001420 if (!x)
1421 goto out;
1422
William Lallemand246c0242019-10-11 08:59:13 +02001423 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001424 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1425 if (chain) {
1426 /* check if one of the certificate of the chain is the issuer */
1427 for (i = 0; i < sk_X509_num(chain); i++) {
1428 X509 *ti = sk_X509_value(chain, i);
1429 if (X509_check_issued(ti, x) == X509_V_OK) {
1430 issuer = ti;
1431 break;
1432 }
1433 }
1434 }
William Lallemand246c0242019-10-11 08:59:13 +02001435 if (!issuer)
1436 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001437
1438 cid = OCSP_cert_to_id(0, x, issuer);
1439 if (!cid)
1440 goto out;
1441
1442 i = i2d_OCSP_CERTID(cid, NULL);
1443 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1444 goto out;
1445
Vincent Bernat02779b62016-04-03 13:48:43 +02001446 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001447 if (!ocsp)
1448 goto out;
1449
1450 p = ocsp->key_data;
1451 i2d_OCSP_CERTID(cid, &p);
1452
1453 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1454 if (iocsp == ocsp)
1455 ocsp = NULL;
1456
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001457#ifndef SSL_CTX_get_tlsext_status_cb
1458# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1459 *cb = (void (*) (void))ctx->tlsext_status_cb;
1460#endif
1461 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1462
1463 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001464 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001465 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001466
1467 cb_arg->is_single = 1;
1468 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001469
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001470 pkey = X509_get_pubkey(x);
1471 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1472 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001473
1474 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1475 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1476 } else {
1477 /*
1478 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1479 * Update that cb_arg with the new cert's staple
1480 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001481 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001482 struct certificate_ocsp *tmp_ocsp;
1483 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001484 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001485 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001486
1487#ifdef SSL_CTX_get_tlsext_status_arg
1488 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1489#else
1490 cb_arg = ctx->tlsext_status_arg;
1491#endif
yanbzhube2774d2015-12-10 15:07:30 -05001492
1493 /*
1494 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1495 * the order of operations below matter, take care when changing it
1496 */
1497 tmp_ocsp = cb_arg->s_ocsp;
1498 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1499 cb_arg->s_ocsp = NULL;
1500 cb_arg->m_ocsp[index] = tmp_ocsp;
1501 cb_arg->is_single = 0;
1502 cb_arg->single_kt = 0;
1503
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001504 pkey = X509_get_pubkey(x);
1505 key_type = EVP_PKEY_base_id(pkey);
1506 EVP_PKEY_free(pkey);
1507
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001508 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001509 if (index >= 0 && !cb_arg->m_ocsp[index])
1510 cb_arg->m_ocsp[index] = iocsp;
1511
1512 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001513
1514 ret = 0;
1515
1516 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001517 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001518 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001519 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001520 }
1521
1522out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001523 if (cid)
1524 OCSP_CERTID_free(cid);
1525
1526 if (ocsp)
1527 free(ocsp);
1528
1529 if (warn)
1530 free(warn);
1531
Emeric Brun4147b2e2014-06-16 18:36:30 +02001532 return ret;
1533}
William Lallemand4a660132019-10-14 14:51:41 +02001534#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001535static 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 +02001536{
William Lallemand4a660132019-10-14 14:51:41 +02001537 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 +02001538}
1539#endif
1540
William Lallemand4a660132019-10-14 14:51:41 +02001541#endif
1542
1543
Willy Tarreau5db847a2019-05-09 14:13:35 +02001544#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001545
1546#define CT_EXTENSION_TYPE 18
1547
1548static int sctl_ex_index = -1;
1549
1550/*
1551 * Try to parse Signed Certificate Timestamp List structure. This function
1552 * makes only basic test if the data seems like SCTL. No signature validation
1553 * is performed.
1554 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001555static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001556{
1557 int ret = 1;
1558 int len, pos, sct_len;
1559 unsigned char *data;
1560
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001561 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001562 goto out;
1563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001565 len = (data[0] << 8) | data[1];
1566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001568 goto out;
1569
1570 data = data + 2;
1571 pos = 0;
1572 while (pos < len) {
1573 if (len - pos < 2)
1574 goto out;
1575
1576 sct_len = (data[pos] << 8) | data[pos + 1];
1577 if (pos + sct_len + 2 > len)
1578 goto out;
1579
1580 pos += sct_len + 2;
1581 }
1582
1583 ret = 0;
1584
1585out:
1586 return ret;
1587}
1588
William Lallemand0dfae6c2019-10-16 18:06:58 +02001589/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1590 * It fills the ckch->sctl buffer
1591 * return 0 on success or != 0 on failure */
1592static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001593{
1594 int fd = -1;
1595 int r = 0;
1596 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001597 struct buffer tmp;
1598 struct buffer *src;
1599 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001600
William Lallemand0dfae6c2019-10-16 18:06:58 +02001601 if (buf) {
1602 tmp.area = buf;
1603 tmp.data = strlen(buf);
1604 tmp.size = tmp.data + 1;
1605 src = &tmp;
1606 } else {
1607 fd = open(sctl_path, O_RDONLY);
1608 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001609 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001610
1611 trash.data = 0;
1612 while (trash.data < trash.size) {
1613 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1614 if (r < 0) {
1615 if (errno == EINTR)
1616 continue;
1617 goto end;
1618 }
1619 else if (r == 0) {
1620 break;
1621 }
1622 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001623 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001624 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001625 }
1626
William Lallemand0dfae6c2019-10-16 18:06:58 +02001627 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001628 if (ret)
1629 goto end;
1630
William Lallemand0dfae6c2019-10-16 18:06:58 +02001631 sctl = calloc(1, sizeof(*sctl));
1632 if (!chunk_dup(sctl, src)) {
1633 free(sctl);
1634 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001635 goto end;
1636 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001637 /* no error, fill ckch with new context, old context must be free */
1638 if (ckch->sctl) {
1639 free(ckch->sctl->area);
1640 ckch->sctl->area = NULL;
1641 free(ckch->sctl);
1642 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001643 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001644 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001645end:
1646 if (fd != -1)
1647 close(fd);
1648
1649 return ret;
1650}
1651
1652int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1653{
Willy Tarreau83061a82018-07-13 11:56:34 +02001654 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001655
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001656 *out = (unsigned char *) sctl->area;
1657 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001658
1659 return 1;
1660}
1661
1662int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1663{
1664 return 1;
1665}
1666
William Lallemanda17f4112019-10-10 15:16:44 +02001667static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001668{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001669 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001670
William Lallemanda17f4112019-10-10 15:16:44 +02001671 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 +01001672 goto out;
1673
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001674 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1675
1676 ret = 0;
1677
1678out:
1679 return ret;
1680}
1681
1682#endif
1683
Emeric Brune1f38db2012-09-03 20:36:47 +02001684void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1685{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001686 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001687 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001688 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001689 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001690
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001691#ifndef SSL_OP_NO_RENEGOTIATION
1692 /* Please note that BoringSSL defines this macro to zero so don't
1693 * change this to #if and do not assign a default value to this macro!
1694 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001695 if (where & SSL_CB_HANDSHAKE_START) {
1696 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001697 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 +02001698 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001699 conn->err_code = CO_ER_SSL_RENEG;
1700 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001701 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001702#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001703
1704 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001705 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001706 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001707 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001708 consider that the buffering was activated,
1709 so we rise the output buffer size from 4k
1710 to 16k */
1711 write_bio = SSL_get_wbio(ssl);
1712 if (write_bio != SSL_get_rbio(ssl)) {
1713 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001714 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001715 }
1716 }
1717 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001718}
1719
Emeric Brune64aef12012-09-21 13:15:06 +02001720/* Callback is called for each certificate of the chain during a verify
1721 ok is set to 1 if preverify detect no error on current certificate.
1722 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001723int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001724{
1725 SSL *ssl;
1726 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001727 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001728 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001729
1730 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001731 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001732
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001733 ctx = conn->xprt_ctx;
1734
1735 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001736
Emeric Brun81c00f02012-09-21 14:31:21 +02001737 if (ok) /* no errors */
1738 return ok;
1739
1740 depth = X509_STORE_CTX_get_error_depth(x_store);
1741 err = X509_STORE_CTX_get_error(x_store);
1742
1743 /* check if CA error needs to be ignored */
1744 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001745 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1746 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1747 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001748 }
1749
Willy Tarreau731248f2020-02-04 14:02:02 +01001750 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001751 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001752 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001753 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001754 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001755
Willy Tarreau20879a02012-12-03 16:32:10 +01001756 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001757 return 0;
1758 }
1759
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001760 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1761 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001762
Emeric Brun81c00f02012-09-21 14:31:21 +02001763 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001764 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001765 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001766 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001767 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001768 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001769
Willy Tarreau20879a02012-12-03 16:32:10 +01001770 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001771 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001772}
1773
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001774static inline
1775void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001776 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001777{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001778 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001779 unsigned char *msg;
1780 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001781 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001782
1783 /* This function is called for "from client" and "to server"
1784 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001785 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001786 */
1787
1788 /* "write_p" is set to 0 is the bytes are received messages,
1789 * otherwise it is set to 1.
1790 */
1791 if (write_p != 0)
1792 return;
1793
1794 /* content_type contains the type of message received or sent
1795 * according with the SSL/TLS protocol spec. This message is
1796 * encoded with one byte. The value 256 (two bytes) is used
1797 * for designing the SSL/TLS record layer. According with the
1798 * rfc6101, the expected message (other than 256) are:
1799 * - change_cipher_spec(20)
1800 * - alert(21)
1801 * - handshake(22)
1802 * - application_data(23)
1803 * - (255)
1804 * We are interessed by the handshake and specially the client
1805 * hello.
1806 */
1807 if (content_type != 22)
1808 return;
1809
1810 /* The message length is at least 4 bytes, containing the
1811 * message type and the message length.
1812 */
1813 if (len < 4)
1814 return;
1815
1816 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001817 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001818 * - hello_request(0)
1819 * - client_hello(1)
1820 * - server_hello(2)
1821 * - certificate(11)
1822 * - server_key_exchange (12)
1823 * - certificate_request(13)
1824 * - server_hello_done(14)
1825 * We are interested by the client hello.
1826 */
1827 msg = (unsigned char *)buf;
1828 if (msg[0] != 1)
1829 return;
1830
1831 /* Next three bytes are the length of the message. The total length
1832 * must be this decoded length + 4. If the length given as argument
1833 * is not the same, we abort the protocol dissector.
1834 */
1835 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1836 if (len < rec_len + 4)
1837 return;
1838 msg += 4;
1839 end = msg + rec_len;
1840 if (end < msg)
1841 return;
1842
1843 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1844 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001845 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1846 */
1847 msg += 1 + 1 + 4 + 28;
1848 if (msg > end)
1849 return;
1850
1851 /* Next, is session id:
1852 * if present, we have to jump by length + 1 for the size information
1853 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001854 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001855 if (msg[0] > 0)
1856 msg += msg[0];
1857 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001858 if (msg > end)
1859 return;
1860
1861 /* Next two bytes are the ciphersuite length. */
1862 if (msg + 2 > end)
1863 return;
1864 rec_len = (msg[0] << 8) + msg[1];
1865 msg += 2;
1866 if (msg + rec_len > end || msg + rec_len < msg)
1867 return;
1868
Willy Tarreaubafbe012017-11-24 17:34:44 +01001869 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001870 if (!capture)
1871 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001872 /* Compute the xxh64 of the ciphersuite. */
1873 capture->xxh64 = XXH64(msg, rec_len, 0);
1874
1875 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001876 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1877 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001878 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001879
1880 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001881}
1882
Emeric Brun29f037d2014-04-25 19:05:36 +02001883/* Callback is called for ssl protocol analyse */
1884void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1885{
Emeric Brun29f037d2014-04-25 19:05:36 +02001886#ifdef TLS1_RT_HEARTBEAT
1887 /* test heartbeat received (write_p is set to 0
1888 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001889 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001890 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001891 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001892 const unsigned char *p = buf;
1893 unsigned int payload;
1894
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001895 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001896
1897 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1898 if (*p != TLS1_HB_REQUEST)
1899 return;
1900
Willy Tarreauaeed6722014-04-25 23:59:58 +02001901 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001902 goto kill_it;
1903
1904 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001905 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001906 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001907 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001908 /* We have a clear heartbleed attack (CVE-2014-0160), the
1909 * advertised payload is larger than the advertised packet
1910 * length, so we have garbage in the buffer between the
1911 * payload and the end of the buffer (p+len). We can't know
1912 * if the SSL stack is patched, and we don't know if we can
1913 * safely wipe out the area between p+3+len and payload.
1914 * So instead, we prevent the response from being sent by
1915 * setting the max_send_fragment to 0 and we report an SSL
1916 * error, which will kill this connection. It will be reported
1917 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001918 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1919 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001920 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001921 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1922 return;
1923 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001924#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001925 if (global_ssl.capture_cipherlist > 0)
1926 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001927}
1928
Bernard Spil13c53f82018-02-15 13:34:58 +01001929#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001930static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1931 const unsigned char *in, unsigned int inlen,
1932 void *arg)
1933{
1934 struct server *srv = arg;
1935
1936 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1937 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1938 return SSL_TLSEXT_ERR_OK;
1939 return SSL_TLSEXT_ERR_NOACK;
1940}
1941#endif
1942
1943#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001944/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001945 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001946 */
1947static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1948 unsigned int *len, void *arg)
1949{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001950 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001951
1952 *data = (const unsigned char *)conf->npn_str;
1953 *len = conf->npn_len;
1954 return SSL_TLSEXT_ERR_OK;
1955}
1956#endif
1957
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001958#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001959/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001960 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001961 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001962static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1963 unsigned char *outlen,
1964 const unsigned char *server,
1965 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001966{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001967 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001968
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001969 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1970 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1971 return SSL_TLSEXT_ERR_NOACK;
1972 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001973 return SSL_TLSEXT_ERR_OK;
1974}
1975#endif
1976
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001977#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001978#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001979
Christopher Faulet30548802015-06-11 13:39:32 +02001980/* Create a X509 certificate with the specified servername and serial. This
1981 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001982static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001983ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001984{
Christopher Faulet7969a332015-10-09 11:15:03 +02001985 X509 *cacert = bind_conf->ca_sign_cert;
1986 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001987 SSL_CTX *ssl_ctx = NULL;
1988 X509 *newcrt = NULL;
1989 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001990 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001991 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001992 X509_NAME *name;
1993 const EVP_MD *digest;
1994 X509V3_CTX ctx;
1995 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001996 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001997
Christopher Faulet48a83322017-07-28 16:56:09 +02001998 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001999#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002000 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
2001#else
2002 tmp_ssl = SSL_new(bind_conf->default_ctx);
2003 if (tmp_ssl)
2004 pkey = SSL_get_privatekey(tmp_ssl);
2005#endif
2006 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002007 goto mkcert_error;
2008
2009 /* Create the certificate */
2010 if (!(newcrt = X509_new()))
2011 goto mkcert_error;
2012
2013 /* Set version number for the certificate (X509v3) and the serial
2014 * number */
2015 if (X509_set_version(newcrt, 2L) != 1)
2016 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002017 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002018
2019 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002020 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2021 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002022 goto mkcert_error;
2023
2024 /* set public key in the certificate */
2025 if (X509_set_pubkey(newcrt, pkey) != 1)
2026 goto mkcert_error;
2027
2028 /* Set issuer name from the CA */
2029 if (!(name = X509_get_subject_name(cacert)))
2030 goto mkcert_error;
2031 if (X509_set_issuer_name(newcrt, name) != 1)
2032 goto mkcert_error;
2033
2034 /* Set the subject name using the same, but the CN */
2035 name = X509_NAME_dup(name);
2036 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2037 (const unsigned char *)servername,
2038 -1, -1, 0) != 1) {
2039 X509_NAME_free(name);
2040 goto mkcert_error;
2041 }
2042 if (X509_set_subject_name(newcrt, name) != 1) {
2043 X509_NAME_free(name);
2044 goto mkcert_error;
2045 }
2046 X509_NAME_free(name);
2047
2048 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002049 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002050 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2051 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2052 X509_EXTENSION *ext;
2053
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002054 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002055 goto mkcert_error;
2056 if (!X509_add_ext(newcrt, ext, -1)) {
2057 X509_EXTENSION_free(ext);
2058 goto mkcert_error;
2059 }
2060 X509_EXTENSION_free(ext);
2061 }
2062
2063 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002064
2065 key_type = EVP_PKEY_base_id(capkey);
2066
2067 if (key_type == EVP_PKEY_DSA)
2068 digest = EVP_sha1();
2069 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002070 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002071 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002072 digest = EVP_sha256();
2073 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002074#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002075 int nid;
2076
2077 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2078 goto mkcert_error;
2079 if (!(digest = EVP_get_digestbynid(nid)))
2080 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002081#else
2082 goto mkcert_error;
2083#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002084 }
2085
Christopher Faulet31af49d2015-06-09 17:29:50 +02002086 if (!(X509_sign(newcrt, capkey, digest)))
2087 goto mkcert_error;
2088
2089 /* Create and set the new SSL_CTX */
2090 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2091 goto mkcert_error;
2092 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2093 goto mkcert_error;
2094 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2095 goto mkcert_error;
2096 if (!SSL_CTX_check_private_key(ssl_ctx))
2097 goto mkcert_error;
2098
2099 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002100
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002101#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002102 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002103#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002104#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2105 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002106 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002107 EC_KEY *ecc;
2108 int nid;
2109
2110 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2111 goto end;
2112 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2113 goto end;
2114 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2115 EC_KEY_free(ecc);
2116 }
2117#endif
2118 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002119 return ssl_ctx;
2120
2121 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002122 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002123 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002124 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2125 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002126 return NULL;
2127}
2128
Christopher Faulet7969a332015-10-09 11:15:03 +02002129SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002130ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002131{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002132 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002133 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002134
Olivier Houchard66ab4982019-02-26 18:37:15 +01002135 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002136}
2137
Christopher Faulet30548802015-06-11 13:39:32 +02002138/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002139 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002140SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002141ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002142{
2143 struct lru64 *lru = NULL;
2144
2145 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002146 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002147 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002148 if (lru && lru->domain) {
2149 if (ssl)
2150 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002151 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002152 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002153 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002154 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002155 }
2156 return NULL;
2157}
2158
Emeric Brun821bb9b2017-06-15 16:37:39 +02002159/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2160 * function is not thread-safe, it should only be used to check if a certificate
2161 * exists in the lru cache (with no warranty it will not be removed by another
2162 * thread). It is kept for backward compatibility. */
2163SSL_CTX *
2164ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2165{
2166 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2167}
2168
Christopher Fauletd2cab922015-07-28 16:03:47 +02002169/* Set a certificate int the LRU cache used to store generated
2170 * certificate. Return 0 on success, otherwise -1 */
2171int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002172ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002173{
2174 struct lru64 *lru = NULL;
2175
2176 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002177 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002178 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002179 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002180 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002181 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002182 }
Christopher Faulet30548802015-06-11 13:39:32 +02002183 if (lru->domain && lru->data)
2184 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002185 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002186 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002187 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002188 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002189 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002190}
2191
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002192/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002193unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002194ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002195{
2196 return XXH32(data, len, ssl_ctx_lru_seed);
2197}
2198
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002199/* Generate a cert and immediately assign it to the SSL session so that the cert's
2200 * refcount is maintained regardless of the cert's presence in the LRU cache.
2201 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002202static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002203ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002204{
2205 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002206 SSL_CTX *ssl_ctx = NULL;
2207 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002208 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002209
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002210 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002211 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002212 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002213 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002214 if (lru && lru->domain)
2215 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002216 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002217 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002218 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002219 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002220 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002221 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002222 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002223 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002224 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002225 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002226 SSL_set_SSL_CTX(ssl, ssl_ctx);
2227 /* No LRU cache, this CTX will be released as soon as the session dies */
2228 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002229 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002230 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002231 return 0;
2232}
2233static int
2234ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2235{
2236 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002237 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002238
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002239 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002240 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002241 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002242 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002243 }
2244 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002245}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002246#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002247
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002248#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002249typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2250
2251static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002252{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002253#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002254 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002255 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2256#endif
2257}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002258static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2259 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002260 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2261}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002262static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002263#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002264 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002265 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2266#endif
2267}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002268static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002269#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002270 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002271 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2272#endif
2273}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002274/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002275static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2276/* Unusable in this context. */
2277static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2278static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2279static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2280static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2281static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002282#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002283typedef enum { SET_MIN, SET_MAX } set_context_func;
2284
2285static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2286 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002287 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2288}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002289static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2290 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2291 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2292}
2293static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2294 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002295 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2296}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002297static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2298 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2299 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2300}
2301static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2302 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002303 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2304}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002305static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2306 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2307 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2308}
2309static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2310 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002311 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2312}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002313static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2314 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2315 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2316}
2317static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002318#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002319 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002320 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2321#endif
2322}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002323static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2324#if SSL_OP_NO_TLSv1_3
2325 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2326 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002327#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002328}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002329#endif
2330static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2331static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002332
2333static struct {
2334 int option;
2335 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002336 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2337 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002338 const char *name;
2339} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002340 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2341 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2342 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2343 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2344 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2345 {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 +02002346};
2347
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002348static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2349{
2350 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2351 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2352 SSL_set_SSL_CTX(ssl, ctx);
2353}
2354
Willy Tarreau5db847a2019-05-09 14:13:35 +02002355#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356
2357static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2358{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002359 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002360 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002361
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002362 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2363 return SSL_TLSEXT_ERR_OK;
2364 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002365}
2366
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002367#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2369{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002370 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002371#else
2372static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2373{
2374#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002375 struct connection *conn;
2376 struct bind_conf *s;
2377 const uint8_t *extension_data;
2378 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002379 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002380
2381 char *wildp = NULL;
2382 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002383 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002384 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002385 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002386 int i;
2387
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002388 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002389 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002390
Olivier Houchard9679ac92017-10-27 14:58:08 +02002391 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002392 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002393#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002394 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2395 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002396#else
2397 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2398#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002399 /*
2400 * The server_name extension was given too much extensibility when it
2401 * was written, so parsing the normal case is a bit complex.
2402 */
2403 size_t len;
2404 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002405 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002406 /* Extract the length of the supplied list of names. */
2407 len = (*extension_data++) << 8;
2408 len |= *extension_data++;
2409 if (len + 2 != extension_len)
2410 goto abort;
2411 /*
2412 * The list in practice only has a single element, so we only consider
2413 * the first one.
2414 */
2415 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2416 goto abort;
2417 extension_len = len - 1;
2418 /* Now we can finally pull out the byte array with the actual hostname. */
2419 if (extension_len <= 2)
2420 goto abort;
2421 len = (*extension_data++) << 8;
2422 len |= *extension_data++;
2423 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2424 || memchr(extension_data, 0, len) != NULL)
2425 goto abort;
2426 servername = extension_data;
2427 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002428 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002429#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2430 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002431 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002432 }
2433#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002434 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002435 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002436 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002437 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002438 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002439 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002440 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002441 goto abort;
2442 }
2443
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002444 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002445#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002446 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002447#else
2448 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2449#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002450 uint8_t sign;
2451 size_t len;
2452 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002453 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002454 len = (*extension_data++) << 8;
2455 len |= *extension_data++;
2456 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002457 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 if (len % 2 != 0)
2459 goto abort;
2460 for (; len > 0; len -= 2) {
2461 extension_data++; /* hash */
2462 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002463 switch (sign) {
2464 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002465 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002466 break;
2467 case TLSEXT_signature_ecdsa:
2468 has_ecdsa_sig = 1;
2469 break;
2470 default:
2471 continue;
2472 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002473 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002474 break;
2475 }
2476 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002477 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002478 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002479 }
2480 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002481 const SSL_CIPHER *cipher;
2482 size_t len;
2483 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002484 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002485#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002486 len = ctx->cipher_suites_len;
2487 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002488#else
2489 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2490#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002491 if (len % 2 != 0)
2492 goto abort;
2493 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002494#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002495 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002496 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002497#else
2498 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2499#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002500 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002501 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002502 break;
2503 }
2504 }
2505 }
2506
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002507 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002508 trash.area[i] = tolower(servername[i]);
2509 if (!wildp && (trash.area[i] == '.'))
2510 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002511 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002513
William Lallemand150bfa82019-09-19 17:12:49 +02002514 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002515
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002516 for (i = 0; i < 2; i++) {
2517 if (i == 0) /* lookup in full qualified names */
2518 node = ebst_lookup(&s->sni_ctx, trash.area);
2519 else if (i == 1 && wildp) /* lookup in wildcards names */
2520 node = ebst_lookup(&s->sni_w_ctx, wildp);
2521 else
2522 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002523 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002524 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002525 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002526 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002527 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002528 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002529 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002530 break;
2531 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002532 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002533 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002534 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002535 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002536 if (!node_anonymous)
2537 node_anonymous = n;
2538 break;
2539 }
2540 }
2541 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002542 /* select by key_signature priority order */
2543 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2544 : ((has_rsa_sig && node_rsa) ? node_rsa
2545 : (node_anonymous ? node_anonymous
2546 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2547 : node_rsa /* no rsa signature case (far far away) */
2548 )));
2549 if (node) {
2550 /* switch ctx */
2551 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2552 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002553 if (conf) {
2554 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2555 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2556 if (conf->early_data)
2557 allow_early = 1;
2558 }
William Lallemand02010472019-10-18 11:02:19 +02002559 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002560 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002561 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002562 }
William Lallemand150bfa82019-09-19 17:12:49 +02002563
William Lallemand02010472019-10-18 11:02:19 +02002564 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002565#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002566 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002567 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002568 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002569 }
2570#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002571 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002572 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002573 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002574 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002575 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002576 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002577allow_early:
2578#ifdef OPENSSL_IS_BORINGSSL
2579 if (allow_early)
2580 SSL_set_early_data_enabled(ssl, 1);
2581#else
2582 if (!allow_early)
2583 SSL_set_max_early_data(ssl, 0);
2584#endif
2585 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002586 abort:
2587 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2588 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002589#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002590 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002591#else
2592 *al = SSL_AD_UNRECOGNIZED_NAME;
2593 return 0;
2594#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002595}
2596
2597#else /* OPENSSL_IS_BORINGSSL */
2598
Emeric Brunfc0421f2012-09-07 17:30:07 +02002599/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2600 * warning when no match is found, which implies the default (first) cert
2601 * will keep being used.
2602 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002603static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002604{
2605 const char *servername;
2606 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002607 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002608 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002609 int i;
2610 (void)al; /* shut gcc stupid warning */
2611
2612 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002613 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002614#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002615 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2616 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002617#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002618 if (s->strict_sni)
2619 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002620 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002621 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002622 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002623 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002624 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002626 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002627 if (!servername[i])
2628 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 trash.area[i] = tolower(servername[i]);
2630 if (!wildp && (trash.area[i] == '.'))
2631 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002632 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002634
William Lallemand150bfa82019-09-19 17:12:49 +02002635 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002636 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002637 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002638 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2639 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002640 if (!container_of(n, struct sni_ctx, name)->neg) {
2641 node = n;
2642 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002643 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002644 }
2645 if (!node && wildp) {
2646 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002647 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2648 /* lookup a not neg filter */
2649 if (!container_of(n, struct sni_ctx, name)->neg) {
2650 node = n;
2651 break;
2652 }
2653 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002654 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002655 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002656#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002657 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2658 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002659 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002660 return SSL_TLSEXT_ERR_OK;
2661 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002662#endif
William Lallemand21724f02019-11-04 17:56:13 +01002663 if (s->strict_sni) {
2664 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002665 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002666 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002667 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002668 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002669 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002670 }
2671
2672 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002673 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002674 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002675 return SSL_TLSEXT_ERR_OK;
2676}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002677#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002678#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2679
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002680#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002681
2682static DH * ssl_get_dh_1024(void)
2683{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002684 static unsigned char dh1024_p[]={
2685 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2686 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2687 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2688 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2689 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2690 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2691 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2692 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2693 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2694 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2695 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2696 };
2697 static unsigned char dh1024_g[]={
2698 0x02,
2699 };
2700
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002701 BIGNUM *p;
2702 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002703 DH *dh = DH_new();
2704 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002705 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2706 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002707
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002708 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002709 DH_free(dh);
2710 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002711 } else {
2712 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002713 }
2714 }
2715 return dh;
2716}
2717
2718static DH *ssl_get_dh_2048(void)
2719{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002720 static unsigned char dh2048_p[]={
2721 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2722 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2723 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2724 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2725 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2726 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2727 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2728 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2729 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2730 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2731 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2732 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2733 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2734 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2735 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2736 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2737 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2738 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2739 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2740 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2741 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2742 0xB7,0x1F,0x77,0xF3,
2743 };
2744 static unsigned char dh2048_g[]={
2745 0x02,
2746 };
2747
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002748 BIGNUM *p;
2749 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002750 DH *dh = DH_new();
2751 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002752 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2753 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002754
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002755 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002756 DH_free(dh);
2757 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002758 } else {
2759 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002760 }
2761 }
2762 return dh;
2763}
2764
2765static DH *ssl_get_dh_4096(void)
2766{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002767 static unsigned char dh4096_p[]={
2768 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2769 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2770 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2771 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2772 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2773 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2774 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2775 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2776 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2777 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2778 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2779 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2780 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2781 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2782 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2783 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2784 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2785 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2786 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2787 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2788 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2789 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2790 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2791 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2792 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2793 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2794 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2795 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2796 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2797 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2798 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2799 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2800 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2801 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2802 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2803 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2804 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2805 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2806 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2807 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2808 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2809 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2810 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002811 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002812 static unsigned char dh4096_g[]={
2813 0x02,
2814 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002815
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002816 BIGNUM *p;
2817 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002818 DH *dh = DH_new();
2819 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002820 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2821 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002822
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002823 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002824 DH_free(dh);
2825 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002826 } else {
2827 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002828 }
2829 }
2830 return dh;
2831}
2832
2833/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002834 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002835static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2836{
2837 DH *dh = NULL;
2838 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002839 int type;
2840
2841 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002842
2843 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2844 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2845 */
2846 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2847 keylen = EVP_PKEY_bits(pkey);
2848 }
2849
Willy Tarreauef934602016-12-22 23:12:01 +01002850 if (keylen > global_ssl.default_dh_param) {
2851 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002852 }
2853
Remi Gacogned3a341a2015-05-29 16:26:17 +02002854 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002855 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002856 }
2857 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002858 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002859 }
2860 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002861 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002862 }
2863
2864 return dh;
2865}
2866
Remi Gacogne47783ef2015-05-29 15:53:22 +02002867static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002868{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002869 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002870 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002871
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002872 if (in == NULL)
2873 goto end;
2874
Remi Gacogne47783ef2015-05-29 15:53:22 +02002875 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002876 goto end;
2877
Remi Gacogne47783ef2015-05-29 15:53:22 +02002878 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2879
2880end:
2881 if (in)
2882 BIO_free(in);
2883
Emeric Brune1b4ed42018-08-16 15:14:12 +02002884 ERR_clear_error();
2885
Remi Gacogne47783ef2015-05-29 15:53:22 +02002886 return dh;
2887}
2888
2889int ssl_sock_load_global_dh_param_from_file(const char *filename)
2890{
2891 global_dh = ssl_sock_get_dh_from_file(filename);
2892
2893 if (global_dh) {
2894 return 0;
2895 }
2896
2897 return -1;
2898}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002899#endif
2900
William Lallemand82b21bb2020-04-10 10:26:27 +02002901/* release ssl bind conf */
2902void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
2903{
2904 if (conf) {
2905#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2906 free(conf->npn_str);
2907 conf->npn_str = NULL;
2908#endif
2909#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2910 free(conf->alpn_str);
2911 conf->alpn_str = NULL;
2912#endif
2913 free(conf->ca_file);
2914 conf->ca_file = NULL;
2915 free(conf->ca_verify_file);
2916 conf->ca_verify_file = NULL;
2917 free(conf->crl_file);
2918 conf->crl_file = NULL;
2919 free(conf->ciphers);
2920 conf->ciphers = NULL;
2921#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
2922 free(conf->ciphersuites);
2923 conf->ciphersuites = NULL;
2924#endif
2925 free(conf->curves);
2926 conf->curves = NULL;
2927 free(conf->ecdhe);
2928 conf->ecdhe = NULL;
2929 }
2930}
2931
William Lallemandd9d5d1b2020-04-09 16:31:05 +02002932/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
2933/* The caller must use the lock of the bind_conf if used with inserted SNIs */
2934static void ckch_inst_free(struct ckch_inst *inst)
2935{
2936 struct sni_ctx *sni, *sni_s;
2937
2938 if (inst == NULL)
2939 return;
2940
2941 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
2942 SSL_CTX_free(sni->ctx);
2943 LIST_DEL(&sni->by_ckch_inst);
2944 ebmb_delete(&sni->name);
2945 free(sni);
2946 }
2947 LIST_DEL(&inst->by_ckchs);
2948 LIST_DEL(&inst->by_crtlist_entry);
2949 free(inst);
2950}
2951
William Lallemand9117de92019-10-04 00:29:42 +02002952/* Alloc and init a ckch_inst */
2953static struct ckch_inst *ckch_inst_new()
2954{
2955 struct ckch_inst *ckch_inst;
2956
2957 ckch_inst = calloc(1, sizeof *ckch_inst);
William Lallemand9cef2e22020-04-09 16:25:10 +02002958 if (!ckch_inst)
2959 return NULL;
2960
2961 LIST_INIT(&ckch_inst->sni_ctx);
2962 LIST_INIT(&ckch_inst->by_ckchs);
2963 LIST_INIT(&ckch_inst->by_crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02002964
2965 return ckch_inst;
2966}
2967
William Lallemand82b21bb2020-04-10 10:26:27 +02002968/* free sni filters */
2969static void crtlist_free_filters(char **args)
2970{
2971 int i;
2972
2973 if (!args)
2974 return;
2975
2976 for (i = 0; args[i]; i++)
2977 free(args[i]);
2978
2979 free(args);
2980}
2981
2982/* Alloc and duplicate a char ** array */
2983static char **crtlist_dup_filters(char **args, int fcount)
2984{
2985 char **dst;
2986 int i;
2987
2988 if (fcount == 0)
2989 return NULL;
2990
2991 dst = calloc(fcount + 1, sizeof(*dst));
2992 if (!dst)
2993 return NULL;
2994
2995 for (i = 0; i < fcount; i++) {
2996 dst[i] = strdup(args[i]);
2997 if (!dst[i])
2998 goto error;
2999 }
3000 return dst;
3001
3002error:
3003 crtlist_free_filters(dst);
3004 return NULL;
3005}
3006
William Lallemande718dfb2020-04-10 11:09:25 +02003007/*
3008 * Detach and free a crtlist_entry.
3009 * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst
3010 */
3011static void crtlist_entry_free(struct crtlist_entry *entry)
3012{
3013 struct ckch_inst *inst, *inst_s;
3014
3015 if (entry == NULL)
3016 return;
3017
3018 ebpt_delete(&entry->node);
3019 LIST_DEL(&entry->by_crtlist);
3020 LIST_DEL(&entry->by_ckch_store);
3021 crtlist_free_filters(entry->filters);
3022 ssl_sock_free_ssl_conf(entry->ssl_conf);
3023 free(entry->ssl_conf);
3024 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
3025 ckch_inst_free(inst);
3026 }
3027 free(entry);
3028}
3029
3030/*
3031 * Allocate and initialize a crtlist_entry
3032 */
3033static struct crtlist_entry *crtlist_entry_new()
3034{
3035 struct crtlist_entry *entry;
3036
3037 entry = calloc(1, sizeof(*entry));
3038 if (entry == NULL)
3039 return NULL;
3040
3041 LIST_INIT(&entry->ckch_inst);
3042
3043 /* initialize the nodes so we can LIST_DEL in any cases */
3044 LIST_INIT(&entry->by_crtlist);
3045 LIST_INIT(&entry->by_ckch_store);
3046
3047 return entry;
3048}
3049
William Lallemand82b21bb2020-04-10 10:26:27 +02003050/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
3051static void crtlist_free(struct crtlist *crtlist)
3052{
3053 struct crtlist_entry *entry, *s_entry;
3054
3055 if (crtlist == NULL)
3056 return;
3057
3058 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
3059 crtlist_entry_free(entry);
3060 }
3061 ebmb_delete(&crtlist->node);
3062 free(crtlist);
3063}
3064
3065/* Alloc and initialize a struct crtlist
3066 * <filename> is the key of the ebmb_node
3067 * <unique> initialize the list of entries to be unique (1) or not (0)
3068 */
3069static struct crtlist *crtlist_new(const char *filename, int unique)
3070{
3071 struct crtlist *newlist;
3072
3073 newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1);
3074 if (newlist == NULL)
3075 return NULL;
3076
3077 memcpy(newlist->node.key, filename, strlen(filename) + 1);
3078 if (unique)
3079 newlist->entries = EB_ROOT_UNIQUE;
3080 else
3081 newlist->entries = EB_ROOT;
3082
3083 LIST_INIT(&newlist->ord_entries);
3084
3085 return newlist;
3086}
William Lallemand9117de92019-10-04 00:29:42 +02003087
3088/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02003089static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02003090 struct bind_conf *s, struct ssl_bind_conf *conf,
3091 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003092{
3093 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003094 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003095
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003096 if (*name == '!') {
3097 neg = 1;
3098 name++;
3099 }
3100 if (*name == '*') {
3101 wild = 1;
3102 name++;
3103 }
3104 /* !* filter is a nop */
3105 if (neg && wild)
3106 return order;
3107 if (*name) {
3108 int j, len;
3109 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003110 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003111 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003112 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02003113 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003114 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003115
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003116 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02003117 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02003118 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003119 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02003120 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003121 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003122 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003123 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003124 sc->order = order++;
3125 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02003126 sc->wild = wild;
3127 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01003128 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02003129 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003130 }
3131 return order;
3132}
3133
William Lallemand6af03992019-07-23 15:00:54 +02003134/*
William Lallemand1d29c742019-10-04 00:53:29 +02003135 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
3136 * This function can't return an error.
3137 *
3138 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
3139 */
3140static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
3141{
3142
3143 struct sni_ctx *sc0, *sc0b, *sc1;
3144 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01003145 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02003146
3147 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
3148
3149 /* ignore if sc0 was already inserted in a tree */
3150 if (sc0->name.node.leaf_p)
3151 continue;
3152
3153 /* Check for duplicates. */
3154 if (sc0->wild)
3155 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
3156 else
3157 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
3158
3159 for (; node; node = ebmb_next_dup(node)) {
3160 sc1 = ebmb_entry(node, struct sni_ctx, name);
3161 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
3162 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
3163 /* it's a duplicate, we should remove and free it */
3164 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02003165 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02003166 free(sc0);
3167 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02003168 break;
William Lallemand1d29c742019-10-04 00:53:29 +02003169 }
3170 }
3171
3172 /* if duplicate, ignore the insertion */
3173 if (!sc0)
3174 continue;
3175
3176 if (sc0->wild)
3177 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3178 else
3179 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003180
3181 /* replace the default_ctx if required with the first ctx */
3182 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02003183 SSL_CTX_free(bind_conf->default_ctx);
3184 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01003185 bind_conf->default_ctx = sc0->ctx;
3186 def = 1;
3187 }
William Lallemand1d29c742019-10-04 00:53:29 +02003188 }
3189}
3190
3191/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003192 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003193 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003194struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003195
William Lallemand2954c472020-03-06 21:54:13 +01003196/* tree of crtlist (crt-list/directory) */
3197static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003198
Emeric Brun7a883362019-10-17 13:27:40 +02003199/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003200 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003201 * DH parameter is loaded into the SSL_CTX and if there is no
3202 * DH parameter available in ckchs nor in global, the default
3203 * DH parameters are applied on the SSL_CTX.
3204 * Returns a bitfield containing the flags:
3205 * ERR_FATAL in any fatal error case
3206 * ERR_ALERT if a reason of the error is availabine in err
3207 * ERR_WARN if a warning is available into err
3208 * The value 0 means there is no error nor warning and
3209 * the operation succeed.
3210 */
William Lallemandfa892222019-07-23 16:06:08 +02003211#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003212static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3213 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003214{
Emeric Brun7a883362019-10-17 13:27:40 +02003215 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003216 DH *dh = NULL;
3217
William Lallemanda8c73742019-07-31 18:31:34 +02003218 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003219 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003220 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3221 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3222 err && *err ? *err : "", path);
3223#if defined(SSL_CTX_set_dh_auto)
3224 SSL_CTX_set_dh_auto(ctx, 1);
3225 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3226 err && *err ? *err : "");
3227#else
3228 memprintf(err, "%s, DH ciphers won't be available.\n",
3229 err && *err ? *err : "");
3230#endif
3231 ret |= ERR_WARN;
3232 goto end;
3233 }
William Lallemandfa892222019-07-23 16:06:08 +02003234
3235 if (ssl_dh_ptr_index >= 0) {
3236 /* store a pointer to the DH params to avoid complaining about
3237 ssl-default-dh-param not being set for this SSL_CTX */
3238 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3239 }
3240 }
3241 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003242 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3243 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3244 err && *err ? *err : "", path);
3245#if defined(SSL_CTX_set_dh_auto)
3246 SSL_CTX_set_dh_auto(ctx, 1);
3247 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3248 err && *err ? *err : "");
3249#else
3250 memprintf(err, "%s, DH ciphers won't be available.\n",
3251 err && *err ? *err : "");
3252#endif
3253 ret |= ERR_WARN;
3254 goto end;
3255 }
William Lallemandfa892222019-07-23 16:06:08 +02003256 }
3257 else {
3258 /* Clear openssl global errors stack */
3259 ERR_clear_error();
3260
3261 if (global_ssl.default_dh_param <= 1024) {
3262 /* we are limited to DH parameter of 1024 bits anyway */
3263 if (local_dh_1024 == NULL)
3264 local_dh_1024 = ssl_get_dh_1024();
3265
Emeric Brun7a883362019-10-17 13:27:40 +02003266 if (local_dh_1024 == NULL) {
3267 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3268 err && *err ? *err : "", path);
3269 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003270 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003271 }
William Lallemandfa892222019-07-23 16:06:08 +02003272
Emeric Bruna9363eb2019-10-17 14:53:03 +02003273 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3274 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3275 err && *err ? *err : "", path);
3276#if defined(SSL_CTX_set_dh_auto)
3277 SSL_CTX_set_dh_auto(ctx, 1);
3278 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3279 err && *err ? *err : "");
3280#else
3281 memprintf(err, "%s, DH ciphers won't be available.\n",
3282 err && *err ? *err : "");
3283#endif
3284 ret |= ERR_WARN;
3285 goto end;
3286 }
William Lallemandfa892222019-07-23 16:06:08 +02003287 }
3288 else {
3289 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3290 }
William Lallemandfa892222019-07-23 16:06:08 +02003291 }
3292
3293end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003294 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003295 return ret;
3296}
3297#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003298
yanbzhu488a4d22015-12-01 15:16:07 -05003299/* Frees the contents of a cert_key_and_chain
3300 */
3301static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3302{
yanbzhu488a4d22015-12-01 15:16:07 -05003303 if (!ckch)
3304 return;
3305
3306 /* Free the certificate and set pointer to NULL */
3307 if (ckch->cert)
3308 X509_free(ckch->cert);
3309 ckch->cert = NULL;
3310
3311 /* Free the key and set pointer to NULL */
3312 if (ckch->key)
3313 EVP_PKEY_free(ckch->key);
3314 ckch->key = NULL;
3315
3316 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003317 if (ckch->chain)
3318 sk_X509_pop_free(ckch->chain, X509_free);
3319 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003320
William Lallemand455af502019-10-17 18:04:45 +02003321 if (ckch->dh)
3322 DH_free(ckch->dh);
3323 ckch->dh = NULL;
3324
3325 if (ckch->sctl) {
3326 free(ckch->sctl->area);
3327 ckch->sctl->area = NULL;
3328 free(ckch->sctl);
3329 ckch->sctl = NULL;
3330 }
3331
3332 if (ckch->ocsp_response) {
3333 free(ckch->ocsp_response->area);
3334 ckch->ocsp_response->area = NULL;
3335 free(ckch->ocsp_response);
3336 ckch->ocsp_response = NULL;
3337 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003338
3339 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003340 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003341 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003342}
3343
William Lallemand8d0f8932019-10-17 18:03:58 +02003344/*
3345 *
3346 * This function copy a cert_key_and_chain in memory
3347 *
3348 * It's used to try to apply changes on a ckch before committing them, because
3349 * most of the time it's not possible to revert those changes
3350 *
3351 * Return a the dst or NULL
3352 */
3353static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3354 struct cert_key_and_chain *dst)
3355{
3356 if (src->cert) {
3357 dst->cert = src->cert;
3358 X509_up_ref(src->cert);
3359 }
3360
3361 if (src->key) {
3362 dst->key = src->key;
3363 EVP_PKEY_up_ref(src->key);
3364 }
3365
3366 if (src->chain) {
3367 dst->chain = X509_chain_up_ref(src->chain);
3368 }
3369
3370 if (src->dh) {
3371 DH_up_ref(src->dh);
3372 dst->dh = src->dh;
3373 }
3374
3375 if (src->sctl) {
3376 struct buffer *sctl;
3377
3378 sctl = calloc(1, sizeof(*sctl));
3379 if (!chunk_dup(sctl, src->sctl)) {
3380 free(sctl);
3381 sctl = NULL;
3382 goto error;
3383 }
3384 dst->sctl = sctl;
3385 }
3386
3387 if (src->ocsp_response) {
3388 struct buffer *ocsp_response;
3389
3390 ocsp_response = calloc(1, sizeof(*ocsp_response));
3391 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3392 free(ocsp_response);
3393 ocsp_response = NULL;
3394 goto error;
3395 }
3396 dst->ocsp_response = ocsp_response;
3397 }
3398
3399 if (src->ocsp_issuer) {
3400 X509_up_ref(src->ocsp_issuer);
3401 dst->ocsp_issuer = src->ocsp_issuer;
3402 }
3403
3404 return dst;
3405
3406error:
3407
3408 /* free everything */
3409 ssl_sock_free_cert_key_and_chain_contents(dst);
3410
3411 return NULL;
3412}
3413
3414
yanbzhu488a4d22015-12-01 15:16:07 -05003415/* checks if a key and cert exists in the ckch
3416 */
William Lallemand1633e392019-09-30 12:58:13 +02003417#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003418static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3419{
3420 return (ckch->cert != NULL && ckch->key != NULL);
3421}
William Lallemand1633e392019-09-30 12:58:13 +02003422#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003423
William Lallemandf9568fc2019-10-16 18:27:58 +02003424/*
3425 * return 0 on success or != 0 on failure
3426 */
3427static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3428{
3429 int ret = 1;
3430 BIO *in = NULL;
3431 X509 *issuer;
3432
3433 if (buf) {
3434 /* reading from a buffer */
3435 in = BIO_new_mem_buf(buf, -1);
3436 if (in == NULL) {
3437 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3438 goto end;
3439 }
3440
3441 } else {
3442 /* reading from a file */
3443 in = BIO_new(BIO_s_file());
3444 if (in == NULL)
3445 goto end;
3446
3447 if (BIO_read_filename(in, path) <= 0)
3448 goto end;
3449 }
3450
3451 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3452 if (!issuer) {
3453 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003454 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003455 goto end;
3456 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003457 /* no error, fill ckch with new context, old context must be free */
3458 if (ckch->ocsp_issuer)
3459 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003460 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003461 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003462
3463end:
3464
3465 ERR_clear_error();
3466 if (in)
3467 BIO_free(in);
3468
3469 return ret;
3470}
3471
William Lallemand96a9c972019-10-17 11:56:17 +02003472
3473/*
3474 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003475 * The PEM must contain at least a Certificate,
3476 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003477 *
William Lallemand96a9c972019-10-17 11:56:17 +02003478 * If it failed you should not attempt to use the ckch but free it.
3479 *
3480 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003481 */
William Lallemand96a9c972019-10-17 11:56:17 +02003482static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
yanbzhu488a4d22015-12-01 15:16:07 -05003483{
William Lallemandf11365b2019-09-19 14:25:58 +02003484 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003485 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003486 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003487 X509 *cert = NULL;
3488 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003489 DH *dh = NULL;
3490 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003491
3492 if (buf) {
3493 /* reading from a buffer */
3494 in = BIO_new_mem_buf(buf, -1);
3495 if (in == NULL) {
3496 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3497 goto end;
3498 }
yanbzhu488a4d22015-12-01 15:16:07 -05003499
William Lallemand96a9c972019-10-17 11:56:17 +02003500 } else {
3501 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003502 in = BIO_new(BIO_s_file());
William Lallemand7fd01b32020-04-07 14:16:32 +02003503 if (in == NULL) {
3504 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
William Lallemandf11365b2019-09-19 14:25:58 +02003505 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003506 }
yanbzhu488a4d22015-12-01 15:16:07 -05003507
William Lallemand7fd01b32020-04-07 14:16:32 +02003508 if (BIO_read_filename(in, path) <= 0) {
3509 memprintf(err, "%scannot open the file '%s'.\n",
3510 err && *err ? *err : "", path);
William Lallemandf11365b2019-09-19 14:25:58 +02003511 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003512 }
William Lallemandf11365b2019-09-19 14:25:58 +02003513 }
yanbzhu488a4d22015-12-01 15:16:07 -05003514
yanbzhu488a4d22015-12-01 15:16:07 -05003515 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003516 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003517 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003518
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003519#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003520 /* Seek back to beginning of file */
3521 if (BIO_reset(in) == -1) {
3522 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3523 err && *err ? *err : "", path);
3524 goto end;
3525 }
3526
William Lallemand96a9c972019-10-17 11:56:17 +02003527 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3528 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003529#endif
William Lallemandfa892222019-07-23 16:06:08 +02003530
Willy Tarreaubb137a82016-04-06 19:02:38 +02003531 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003532 if (BIO_reset(in) == -1) {
3533 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3534 err && *err ? *err : "", path);
3535 goto end;
3536 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003537
3538 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003539 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3540 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003541 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003542 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003543 goto end;
3544 }
3545
William Lallemand96a9c972019-10-17 11:56:17 +02003546 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003547 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3548 if (chain == NULL)
3549 chain = sk_X509_new_null();
3550 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003551 X509_free(ca);
3552 goto end;
3553 }
3554 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003555
yanbzhu488a4d22015-12-01 15:16:07 -05003556 ret = ERR_get_error();
3557 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3558 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003559 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003560 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003561 }
3562
William Lallemand75b15f72020-01-23 10:56:05 +01003563 /* once it loaded the PEM, it should remove everything else in the ckch */
3564 if (ckch->ocsp_response) {
3565 free(ckch->ocsp_response->area);
3566 ckch->ocsp_response->area = NULL;
3567 free(ckch->ocsp_response);
3568 ckch->ocsp_response = NULL;
3569 }
3570
3571 if (ckch->sctl) {
3572 free(ckch->sctl->area);
3573 ckch->sctl->area = NULL;
3574 free(ckch->sctl);
3575 ckch->sctl = NULL;
3576 }
3577
3578 if (ckch->ocsp_issuer) {
3579 X509_free(ckch->ocsp_issuer);
3580 ckch->ocsp_issuer = NULL;
3581 }
3582
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003583 /* no error, fill ckch with new context, old context will be free at end: */
3584 SWAP(ckch->key, key);
3585 SWAP(ckch->dh, dh);
3586 SWAP(ckch->cert, cert);
3587 SWAP(ckch->chain, chain);
3588
William Lallemand246c0242019-10-11 08:59:13 +02003589 ret = 0;
3590
William Lallemand96a9c972019-10-17 11:56:17 +02003591end:
William Lallemand246c0242019-10-11 08:59:13 +02003592
3593 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003594 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003595 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003596 if (key)
3597 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003598 if (dh)
3599 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003600 if (cert)
3601 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003602 if (chain)
3603 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003604
William Lallemand96a9c972019-10-17 11:56:17 +02003605 return ret;
3606}
3607
3608/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003609 * Try to load a private key file from a <path> or a buffer <buf>
3610 *
3611 * If it failed you should not attempt to use the ckch but free it.
3612 *
3613 * Return 0 on success or != 0 on failure
3614 */
3615static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3616{
3617 BIO *in = NULL;
3618 int ret = 1;
3619 EVP_PKEY *key = NULL;
3620
3621 if (buf) {
3622 /* reading from a buffer */
3623 in = BIO_new_mem_buf(buf, -1);
3624 if (in == NULL) {
3625 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3626 goto end;
3627 }
3628
3629 } else {
3630 /* reading from a file */
3631 in = BIO_new(BIO_s_file());
3632 if (in == NULL)
3633 goto end;
3634
3635 if (BIO_read_filename(in, path) <= 0)
3636 goto end;
3637 }
3638
3639 /* Read Private Key */
3640 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3641 if (key == NULL) {
3642 memprintf(err, "%sunable to load private key from file '%s'.\n",
3643 err && *err ? *err : "", path);
3644 goto end;
3645 }
3646
3647 ret = 0;
3648
3649 SWAP(ckch->key, key);
3650
3651end:
3652
3653 ERR_clear_error();
3654 if (in)
3655 BIO_free(in);
3656 if (key)
3657 EVP_PKEY_free(key);
3658
3659 return ret;
3660}
3661
3662/*
William Lallemand96a9c972019-10-17 11:56:17 +02003663 * Try to load in a ckch every files related to a ckch.
3664 * (PEM, sctl, ocsp, issuer etc.)
3665 *
3666 * This function is only used to load files during the configuration parsing,
3667 * it is not used with the CLI.
3668 *
3669 * This allows us to carry the contents of the file without having to read the
3670 * file multiple times. The caller must call
3671 * ssl_sock_free_cert_key_and_chain_contents.
3672 *
3673 * returns:
3674 * 0 on Success
3675 * 1 on SSL Failure
3676 */
3677static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3678{
3679 int ret = 1;
3680
3681 /* try to load the PEM */
3682 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3683 goto end;
3684 }
3685
William Lallemand4c5adbf2020-02-24 14:23:22 +01003686 /* try to load an external private key if it wasn't in the PEM */
3687 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3688 char fp[MAXPATHLEN+1];
3689 struct stat st;
3690
3691 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3692 if (stat(fp, &st) == 0) {
3693 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3694 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3695 err && *err ? *err : "", fp);
3696 goto end;
3697 }
3698 }
3699 }
3700
3701 if (ckch->key == NULL) {
3702 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3703 goto end;
3704 }
3705
3706 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3707 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3708 err && *err ? *err : "", path);
3709 goto end;
3710 }
3711
William Lallemanda17f4112019-10-10 15:16:44 +02003712#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3713 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003714 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003715 char fp[MAXPATHLEN+1];
3716 struct stat st;
3717
3718 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3719 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003720 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003721 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003722 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003723 ret = 1;
3724 goto end;
3725 }
3726 }
3727 }
3728#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003729
William Lallemand246c0242019-10-11 08:59:13 +02003730 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003731 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003732 char fp[MAXPATHLEN+1];
3733 struct stat st;
3734
3735 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3736 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003737 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003738 ret = 1;
3739 goto end;
3740 }
3741 }
3742 }
3743
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003744#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003745 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003746 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003747 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003748 struct stat st;
3749 char fp[MAXPATHLEN+1];
3750
3751 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3752 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003753 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003754 ret = 1;
3755 goto end;
3756 }
3757
3758 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003759 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003760 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003761 ret = 1;
3762 goto end;
3763 }
William Lallemand246c0242019-10-11 08:59:13 +02003764 }
3765 }
3766 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003767#endif
William Lallemand246c0242019-10-11 08:59:13 +02003768
yanbzhu488a4d22015-12-01 15:16:07 -05003769 ret = 0;
3770
3771end:
3772
3773 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003774
3775 /* Something went wrong in one of the reads */
3776 if (ret != 0)
3777 ssl_sock_free_cert_key_and_chain_contents(ckch);
3778
3779 return ret;
3780}
3781
3782/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003783 * Returns a bitfield containing the flags:
3784 * ERR_FATAL in any fatal error case
3785 * ERR_ALERT if the reason of the error is available in err
3786 * ERR_WARN if a warning is available into err
3787 * The value 0 means there is no error nor warning and
3788 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003789 */
3790static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3791{
Emeric Bruna96b5822019-10-17 13:25:14 +02003792 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003793 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003794
yanbzhu488a4d22015-12-01 15:16:07 -05003795 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3796 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3797 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003798 errcode |= ERR_ALERT | ERR_FATAL;
3799 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003800 }
3801
3802 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3803 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3804 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003805 errcode |= ERR_ALERT | ERR_FATAL;
3806 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003807 }
3808
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003809 if (ckch->chain) {
3810 find_chain = ckch->chain;
3811 } else {
3812 /* Find Certificate Chain in global */
3813 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003814 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003815 if (issuer)
3816 find_chain = issuer->chain;
3817 }
William Lallemand85888572020-02-27 14:48:35 +01003818
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003819 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003820 if (find_chain) {
3821 int i;
3822 X509 *ca;
3823 for (i = 0; i < sk_X509_num(find_chain); i++) {
3824 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003825 /* skip self issued (Root CA) */
3826 if (!X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca)))
3827 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003828 /*
3829 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3830 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3831 */
3832 X509_up_ref(ca);
3833 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3834 X509_free(ca);
3835 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3836 err && *err ? *err : "", path);
3837 errcode |= ERR_ALERT | ERR_FATAL;
3838 goto end;
3839 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003840 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003841 }
yanbzhu488a4d22015-12-01 15:16:07 -05003842
William Lallemandfa892222019-07-23 16:06:08 +02003843#ifndef OPENSSL_NO_DH
3844 /* store a NULL pointer to indicate we have not yet loaded
3845 a custom DH param file */
3846 if (ssl_dh_ptr_index >= 0) {
3847 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3848 }
3849
Emeric Brun7a883362019-10-17 13:27:40 +02003850 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3851 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003852 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3853 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003854 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003855 }
3856#endif
3857
William Lallemanda17f4112019-10-10 15:16:44 +02003858#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3859 if (sctl_ex_index >= 0 && ckch->sctl) {
3860 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3861 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003862 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003863 errcode |= ERR_ALERT | ERR_FATAL;
3864 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003865 }
3866 }
3867#endif
3868
William Lallemand4a660132019-10-14 14:51:41 +02003869#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003870 /* Load OCSP Info into context */
3871 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003872 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003873 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",
3874 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003875 errcode |= ERR_ALERT | ERR_FATAL;
3876 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003877 }
3878 }
William Lallemand246c0242019-10-11 08:59:13 +02003879#endif
3880
Emeric Bruna96b5822019-10-17 13:25:14 +02003881 end:
3882 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003883}
3884
William Lallemandc4ecddf2019-07-31 16:50:08 +02003885#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003886
William Lallemand28a8fce2019-10-04 17:36:55 +02003887static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003888{
3889 struct sni_keytype *s_kt = NULL;
3890 struct ebmb_node *node;
3891 int i;
3892
3893 for (i = 0; i < trash.size; i++) {
3894 if (!str[i])
3895 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003896 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003897 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003898 trash.area[i] = 0;
3899 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003900 if (!node) {
3901 /* CN not found in tree */
3902 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3903 /* Using memcpy here instead of strncpy.
3904 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3905 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3906 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003907 if (!s_kt)
3908 return -1;
3909
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003910 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003911 s_kt->keytypes = 0;
3912 ebst_insert(sni_keytypes, &s_kt->name);
3913 } else {
3914 /* CN found in tree */
3915 s_kt = container_of(node, struct sni_keytype, name);
3916 }
3917
3918 /* Mark that this CN has the keytype of key_index via keytypes mask */
3919 s_kt->keytypes |= 1<<key_index;
3920
William Lallemand28a8fce2019-10-04 17:36:55 +02003921 return 0;
3922
yanbzhu08ce6ab2015-12-02 13:01:29 -05003923}
3924
William Lallemandc4ecddf2019-07-31 16:50:08 +02003925#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003926/*
William Lallemandba1c33f2020-04-08 17:55:45 +02003927 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
William Lallemand8c1cdde2019-10-18 10:58:14 +02003928 */
William Lallemandba1c33f2020-04-08 17:55:45 +02003929static void ckch_store_free(struct ckch_store *store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003930{
William Lallemandba1c33f2020-04-08 17:55:45 +02003931 struct ckch_inst *inst, *inst_s;
3932
3933 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003934 return;
3935
William Lallemandba1c33f2020-04-08 17:55:45 +02003936#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L
3937 if (store->multi) {
William Lallemand8c1cdde2019-10-18 10:58:14 +02003938 int n;
3939
William Lallemandba1c33f2020-04-08 17:55:45 +02003940 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3941 ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003942 } else
3943#endif
3944 {
William Lallemandba1c33f2020-04-08 17:55:45 +02003945 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003946 }
3947
William Lallemandba1c33f2020-04-08 17:55:45 +02003948 free(store->ckch);
3949 store->ckch = NULL;
3950
3951 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
3952 ckch_inst_free(inst);
3953 }
3954 ebmb_delete(&store->node);
3955 free(store);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003956}
3957
William Lallemand8a874e42020-04-09 10:32:53 +02003958/*
3959 * create and initialize a ckch_store
3960 * <path> is the key name
3961 * <nmemb> is the number of store->ckch objects to allocate
3962 *
3963 * Return a ckch_store or NULL upon failure.
3964 */
3965static struct ckch_store *ckch_store_new(const char *filename, int nmemb)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003966{
William Lallemand8a874e42020-04-09 10:32:53 +02003967 struct ckch_store *store;
William Lallemand8c1cdde2019-10-18 10:58:14 +02003968 int pathlen;
3969
William Lallemand8a874e42020-04-09 10:32:53 +02003970 pathlen = strlen(filename);
3971 store = calloc(1, sizeof(*store) + pathlen + 1);
3972 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003973 return NULL;
William Lallemand8a874e42020-04-09 10:32:53 +02003974
3975 if (nmemb > 1)
3976 store->multi = 1;
3977 else
3978 store->multi = 0;
3979
3980 memcpy(store->path, filename, pathlen + 1);
3981
3982 LIST_INIT(&store->ckch_inst);
3983 LIST_INIT(&store->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003984
William Lallemand8a874e42020-04-09 10:32:53 +02003985 store->ckch = calloc(nmemb, sizeof(*store->ckch));
3986 if (!store->ckch)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003987 goto error;
3988
William Lallemand8a874e42020-04-09 10:32:53 +02003989 return store;
3990error:
3991 ckch_store_free(store);
3992 return NULL;
3993}
3994
3995/* allocate and duplicate a ckch_store
3996 * Return a new ckch_store or NULL */
3997static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3998{
3999 struct ckch_store *dst;
4000
4001 dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1);
4002
William Lallemand8c1cdde2019-10-18 10:58:14 +02004003#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4004 if (src->multi) {
4005 int n;
4006
4007 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4008 if (&src->ckch[n]) {
4009 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
4010 goto error;
4011 }
4012 }
4013 } else
4014#endif
4015 {
4016 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
4017 goto error;
4018 }
4019
4020 return dst;
4021
4022error:
William Lallemandba1c33f2020-04-08 17:55:45 +02004023 ckch_store_free(dst);
William Lallemand8c1cdde2019-10-18 10:58:14 +02004024
4025 return NULL;
4026}
William Lallemandc4ecddf2019-07-31 16:50:08 +02004027
William Lallemand36b84632019-07-18 19:28:17 +02004028/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004029 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02004030 */
William Lallemande3af8fb2019-10-08 11:36:53 +02004031static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02004032{
4033 struct ebmb_node *eb;
4034
William Lallemande3af8fb2019-10-08 11:36:53 +02004035 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02004036 if (!eb)
4037 return NULL;
4038
William Lallemande3af8fb2019-10-08 11:36:53 +02004039 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02004040}
4041
4042/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004043 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02004044 */
William Lallemande3af8fb2019-10-08 11:36:53 +02004045static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02004046{
William Lallemande3af8fb2019-10-08 11:36:53 +02004047 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02004048
William Lallemand8a874e42020-04-09 10:32:53 +02004049 ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1);
William Lallemande3af8fb2019-10-08 11:36:53 +02004050 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02004051 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
4052 goto end;
4053 }
William Lallemand36b84632019-07-18 19:28:17 +02004054 if (!multi) {
4055
William Lallemand96a9c972019-10-17 11:56:17 +02004056 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004057 goto end;
4058
William Lallemande3af8fb2019-10-08 11:36:53 +02004059 /* insert into the ckchs tree */
4060 memcpy(ckchs->path, path, strlen(path) + 1);
4061 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004062 } else {
4063 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02004064#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4065 char fp[MAXPATHLEN+1] = {0};
4066 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02004067
4068 /* Load all possible certs and keys */
4069 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4070 struct stat buf;
4071 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
4072 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02004073 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004074 goto end;
4075 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02004076 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02004077 }
4078 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02004079#endif
William Lallemand36b84632019-07-18 19:28:17 +02004080
4081 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02004082 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02004083 goto end;
4084 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004085 /* insert into the ckchs tree */
4086 memcpy(ckchs->path, path, strlen(path) + 1);
4087 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004088 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004089 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004090
William Lallemand36b84632019-07-18 19:28:17 +02004091end:
William Lallemandba1c33f2020-04-08 17:55:45 +02004092 ckch_store_free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02004093
4094 return NULL;
4095}
4096
William Lallemandc4ecddf2019-07-31 16:50:08 +02004097#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4098
William Lallemand36b84632019-07-18 19:28:17 +02004099/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004100 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02004101 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05004102 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
4103 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08004104 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05004105 *
Emeric Brun054563d2019-10-17 13:16:58 +02004106 * Returns a bitfield containing the flags:
4107 * ERR_FATAL in any fatal error case
4108 * ERR_ALERT if the reason of the error is available in err
4109 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02004110 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05004111 */
Emeric Brun054563d2019-10-17 13:16:58 +02004112static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4113 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4114 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004115{
William Lallemand36b84632019-07-18 19:28:17 +02004116 int i = 0, n = 0;
4117 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02004118 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004119 struct ebmb_node *node;
4120 struct ebmb_node *next;
4121 /* Array of SSL_CTX pointers corresponding to each possible combo
4122 * of keytypes
4123 */
4124 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02004125 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004126 X509_NAME *xname = NULL;
4127 char *str = NULL;
4128#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4129 STACK_OF(GENERAL_NAME) *names = NULL;
4130#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02004131 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004132
Emeric Brun054563d2019-10-17 13:16:58 +02004133 *ckchi = NULL;
4134
William Lallemande3af8fb2019-10-08 11:36:53 +02004135 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02004136 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4137 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004138 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004139 }
4140
4141 ckch_inst = ckch_inst_new();
4142 if (!ckch_inst) {
4143 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4144 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004145 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004146 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004147 }
4148
William Lallemande3af8fb2019-10-08 11:36:53 +02004149 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004150
yanbzhu08ce6ab2015-12-02 13:01:29 -05004151 /* Process each ckch and update keytypes for each CN/SAN
4152 * for example, if CN/SAN www.a.com is associated with
4153 * certs with keytype 0 and 2, then at the end of the loop,
4154 * www.a.com will have:
4155 * keyindex = 0 | 1 | 4 = 5
4156 */
4157 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004158 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004159
4160 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4161 continue;
4162
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004163 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004164 for (i = 0; i < fcount; i++) {
4165 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4166 if (ret < 0) {
4167 memprintf(err, "%sunable to allocate SSL context.\n",
4168 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004169 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004170 goto end;
4171 }
4172 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004173 } else {
4174 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4175 * so the line that contains logic is marked via comments
4176 */
4177 xname = X509_get_subject_name(certs_and_keys[n].cert);
4178 i = -1;
4179 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4180 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004181 ASN1_STRING *value;
4182 value = X509_NAME_ENTRY_get_data(entry);
4183 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004184 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004185 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004186
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004187 OPENSSL_free(str);
4188 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004189 if (ret < 0) {
4190 memprintf(err, "%sunable to allocate SSL context.\n",
4191 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004192 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004193 goto end;
4194 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004195 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004196 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004197
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004198 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004199#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004200 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4201 if (names) {
4202 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4203 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004204
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004205 if (name->type == GEN_DNS) {
4206 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4207 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004208 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004209
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004210 OPENSSL_free(str);
4211 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004212 if (ret < 0) {
4213 memprintf(err, "%sunable to allocate SSL context.\n",
4214 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004215 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004216 goto end;
4217 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004218 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004219 }
4220 }
4221 }
4222 }
4223#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4224 }
4225
4226 /* If no files found, return error */
4227 if (eb_is_empty(&sni_keytypes_map)) {
4228 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4229 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004230 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004231 goto end;
4232 }
4233
4234 /* We now have a map of CN/SAN to keytypes that are loaded in
4235 * Iterate through the map to create the SSL_CTX's (if needed)
4236 * and add each CTX to the SNI tree
4237 *
4238 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004239 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004240 * combination is denoted by the key in the map. Each key
4241 * has a value between 1 and 2^n - 1. Conveniently, the array
4242 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4243 * entry in the array to correspond to the unique combo (key)
4244 * associated with i. This unique key combo (i) will be associated
4245 * with combos[i-1]
4246 */
4247
4248 node = ebmb_first(&sni_keytypes_map);
4249 while (node) {
4250 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004251 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004252 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004253
4254 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4255 i = container_of(node, struct sni_keytype, name)->keytypes;
4256 cur_ctx = key_combos[i-1].ctx;
4257
4258 if (cur_ctx == NULL) {
4259 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004260 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004261 if (cur_ctx == NULL) {
4262 memprintf(err, "%sunable to allocate SSL context.\n",
4263 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004264 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004265 goto end;
4266 }
4267
yanbzhube2774d2015-12-10 15:07:30 -05004268 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004269 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4270 if (i & (1<<n)) {
4271 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004272 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004273 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4274 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004275 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004276 }
4277 }
4278
yanbzhu08ce6ab2015-12-02 13:01:29 -05004279 /* Update key_combos */
4280 key_combos[i-1].ctx = cur_ctx;
4281 }
4282
4283 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004284
William Lallemand1d29c742019-10-04 00:53:29 +02004285 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 +02004286 kinfo, str, key_combos[i-1].order);
4287 if (key_combos[i-1].order < 0) {
4288 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004289 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004290 goto end;
4291 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004292 node = ebmb_next(node);
4293 }
4294
4295
4296 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4297 if (!bind_conf->default_ctx) {
4298 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4299 if (key_combos[i].ctx) {
4300 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004301 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004302 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004303 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004304 break;
4305 }
4306 }
4307 }
4308
William Lallemand614ca0d2019-10-07 13:52:11 +02004309 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004310 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004311 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004312
yanbzhu08ce6ab2015-12-02 13:01:29 -05004313end:
4314
4315 if (names)
4316 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4317
yanbzhu08ce6ab2015-12-02 13:01:29 -05004318 node = ebmb_first(&sni_keytypes_map);
4319 while (node) {
4320 next = ebmb_next(node);
4321 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004322 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004323 node = next;
4324 }
4325
William Lallemand02e19a52020-04-08 16:11:26 +02004326 /* we need to free the ctx since we incremented the refcount where it's used */
4327 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4328 if (key_combos[i].ctx)
4329 SSL_CTX_free(key_combos[i].ctx);
4330 }
4331
Emeric Brun054563d2019-10-17 13:16:58 +02004332 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004333 if (ckch_inst->is_default) {
4334 SSL_CTX_free(bind_conf->default_ctx);
4335 bind_conf->default_ctx = NULL;
4336 }
4337
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004338 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004339 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004340 }
4341
Emeric Brun054563d2019-10-17 13:16:58 +02004342 *ckchi = ckch_inst;
4343 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004344}
4345#else
4346/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004347static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4348 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4349 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004350{
4351 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4352 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004353 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004354}
4355
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004356#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004357
William Lallemand614ca0d2019-10-07 13:52:11 +02004358/*
4359 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004360 *
4361 * Returns a bitfield containing the flags:
4362 * ERR_FATAL in any fatal error case
4363 * ERR_ALERT if the reason of the error is available in err
4364 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004365 */
Emeric Brun054563d2019-10-17 13:16:58 +02004366static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4367 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004368{
William Lallemandc9402072019-05-15 15:33:54 +02004369 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004370 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004371 int order = 0;
4372 X509_NAME *xname;
4373 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004374 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004375 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004376#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4377 STACK_OF(GENERAL_NAME) *names;
4378#endif
William Lallemand36b84632019-07-18 19:28:17 +02004379 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004380 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004381 int errcode = 0;
4382
4383 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004384
William Lallemande3af8fb2019-10-08 11:36:53 +02004385 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004386 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004387
William Lallemande3af8fb2019-10-08 11:36:53 +02004388 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004389
William Lallemandc9402072019-05-15 15:33:54 +02004390 ctx = SSL_CTX_new(SSLv23_server_method());
4391 if (!ctx) {
4392 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4393 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004394 errcode |= ERR_ALERT | ERR_FATAL;
4395 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004396 }
4397
Emeric Bruna96b5822019-10-17 13:25:14 +02004398 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4399 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004400 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004401
4402 ckch_inst = ckch_inst_new();
4403 if (!ckch_inst) {
4404 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4405 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004406 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004407 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004408 }
4409
William Lallemand36b84632019-07-18 19:28:17 +02004410 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004411 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004412 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004413 switch(EVP_PKEY_base_id(pkey)) {
4414 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004415 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004416 break;
4417 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004418 kinfo.sig = TLSEXT_signature_ecdsa;
4419 break;
4420 case EVP_PKEY_DSA:
4421 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004422 break;
4423 }
4424 EVP_PKEY_free(pkey);
4425 }
4426
Emeric Brun50bcecc2013-04-22 13:05:23 +02004427 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004428 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004429 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 +02004430 if (order < 0) {
4431 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004432 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004433 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004434 }
4435 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004436 }
4437 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004438#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004439 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004440 if (names) {
4441 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4442 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4443 if (name->type == GEN_DNS) {
4444 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004445 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004446 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004447 if (order < 0) {
4448 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004449 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004450 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004451 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004452 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004453 }
4454 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004455 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004456 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004457#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004458 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004459 i = -1;
4460 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4461 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004462 ASN1_STRING *value;
4463
4464 value = X509_NAME_ENTRY_get_data(entry);
4465 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004466 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004467 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004468 if (order < 0) {
4469 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004470 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004471 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004472 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004473 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004474 }
4475 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004476 /* we must not free the SSL_CTX anymore below, since it's already in
4477 * the tree, so it will be discovered and cleaned in time.
4478 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004479
Emeric Brunfc0421f2012-09-07 17:30:07 +02004480#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004481 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004482 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4483 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004484 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004485 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004486 }
4487#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004488 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004489 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004490 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004491 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004492 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004493 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004494
William Lallemand9117de92019-10-04 00:29:42 +02004495 /* everything succeed, the ckch instance can be used */
4496 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004497 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004498 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004499
William Lallemand02e19a52020-04-08 16:11:26 +02004500 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4501
Emeric Brun054563d2019-10-17 13:16:58 +02004502 *ckchi = ckch_inst;
4503 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004504
4505error:
4506 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004507 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004508 if (ckch_inst->is_default)
4509 SSL_CTX_free(ctx);
4510
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004511 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004512 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004513 }
William Lallemandd9199372019-10-04 15:37:05 +02004514 SSL_CTX_free(ctx);
4515
Emeric Brun054563d2019-10-17 13:16:58 +02004516 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004517}
4518
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004519/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004520static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4521 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004522 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004523{
Emeric Brun054563d2019-10-17 13:16:58 +02004524 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004525
4526 /* we found the ckchs in the tree, we can use it directly */
4527 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004528 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 +02004529 else
William Lallemand24bde432020-03-09 16:48:43 +01004530 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 +02004531
Emeric Brun054563d2019-10-17 13:16:58 +02004532 if (errcode & ERR_CODE)
4533 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004534
William Lallemand24bde432020-03-09 16:48:43 +01004535 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004536
4537 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004538 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004539 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004540}
4541
William Lallemand6be66ec2020-03-06 22:26:32 +01004542
William Lallemand4c68bba2020-03-30 18:45:10 +02004543
4544
4545/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4546 * done once. Zero is returned if the operation fails. No error is returned
4547 * if the random is said as not implemented, because we expect that openssl
4548 * will use another method once needed.
4549 */
4550static int ssl_initialize_random()
4551{
4552 unsigned char random;
4553 static int random_initialized = 0;
4554
4555 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4556 random_initialized = 1;
4557
4558 return random_initialized;
4559}
4560
William Lallemandec2d4932020-04-09 13:44:21 +02004561
William Lallemand6be66ec2020-03-06 22:26:32 +01004562/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4563 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4564 *
4565 * This function tries to open and store certificate files.
4566 */
4567static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004568{
William Lallemand6be66ec2020-03-06 22:26:32 +01004569 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004570 struct dirent **de_list;
4571 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004572 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004573 char *end;
4574 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004575 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004576 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004577#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004578 int is_bundle;
4579 int j;
4580#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004581
William Lallemandec2d4932020-04-09 13:44:21 +02004582 dir = crtlist_new(path, 1);
William Lallemand6be66ec2020-03-06 22:26:32 +01004583 if (dir == NULL) {
4584 memprintf(err, "not enough memory");
4585 return ERR_ALERT | ERR_FATAL;
4586 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004587
William Lallemand2d232c22020-03-06 22:12:35 +01004588 n = scandir(path, &de_list, 0, alphasort);
4589 if (n < 0) {
4590 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4591 err && *err ? *err : "", path, strerror(errno));
4592 cfgerr |= ERR_ALERT | ERR_FATAL;
4593 }
4594 else {
4595 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004596 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004597 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004598
William Lallemand2d232c22020-03-06 22:12:35 +01004599 end = strrchr(de->d_name, '.');
4600 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4601 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004602
William Lallemanda64593c2020-03-17 20:11:41 +01004603 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4604 if (stat(fp, &buf) != 0) {
4605 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4606 err && *err ? *err : "", fp, strerror(errno));
4607 cfgerr |= ERR_ALERT | ERR_FATAL;
4608 goto ignore_entry;
4609 }
4610 if (!S_ISREG(buf.st_mode))
4611 goto ignore_entry;
4612
William Lallemande718dfb2020-04-10 11:09:25 +02004613 entry = crtlist_entry_new();
William Lallemand6be66ec2020-03-06 22:26:32 +01004614 if (entry == NULL) {
4615 memprintf(err, "not enough memory '%s'", fp);
4616 cfgerr |= ERR_ALERT | ERR_FATAL;
4617 goto ignore_entry;
4618 }
4619
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004620#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004621 is_bundle = 0;
4622 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004623
William Lallemand2d232c22020-03-06 22:12:35 +01004624 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4625 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4626 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4627 is_bundle = 1;
4628 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004629 }
William Lallemand2d232c22020-03-06 22:12:35 +01004630 }
yanbzhu63ea8462015-12-09 13:35:14 -05004631
William Lallemand2d232c22020-03-06 22:12:35 +01004632 if (is_bundle) {
4633 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004634
William Lallemand2d232c22020-03-06 22:12:35 +01004635 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004636
William Lallemand2d232c22020-03-06 22:12:35 +01004637 /* increment i and free de until we get to a non-bundle cert
4638 * Note here that we look at de_list[i + 1] before freeing de
4639 * this is important since ignore_entry will free de. This also
4640 * guarantees that de->d_name continues to hold the same prefix.
4641 */
4642 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4643 free(de);
4644 i++;
4645 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004646 }
William Lallemand2d232c22020-03-06 22:12:35 +01004647
4648 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004649 ckchs = ckchs_lookup(fp);
4650 if (ckchs == NULL)
4651 ckchs = ckchs_load_cert_file(fp, 1, err);
4652 if (ckchs == NULL) {
4653 free(de);
4654 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004655 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004656 goto end;
4657 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004658 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004659 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004660 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004661 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004662 ebpt_insert(&dir->entries, &entry->node);
4663
William Lallemand2d232c22020-03-06 22:12:35 +01004664 /* Successfully processed the bundle */
4665 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004666 }
William Lallemand2d232c22020-03-06 22:12:35 +01004667 }
yanbzhu63ea8462015-12-09 13:35:14 -05004668
4669#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004670 ckchs = ckchs_lookup(fp);
4671 if (ckchs == NULL)
4672 ckchs = ckchs_load_cert_file(fp, 0, err);
4673 if (ckchs == NULL) {
4674 free(de);
4675 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004676 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004677 goto end;
4678 }
4679 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004680 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004681 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004682 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004683 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004684
yanbzhu08ce6ab2015-12-02 13:01:29 -05004685ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004686 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004687 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004688end:
William Lallemand2d232c22020-03-06 22:12:35 +01004689 free(de_list);
4690 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004691
4692 if (cfgerr & ERR_CODE) {
4693 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004694 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004695 } else {
4696 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004697 }
William Lallemand2d232c22020-03-06 22:12:35 +01004698 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004699
William Lallemand2d232c22020-03-06 22:12:35 +01004700}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004701
William Lallemand97b08102020-04-01 10:14:00 +02004702/*
4703 * Read a single crt-list line. /!\ alter the <line> string.
4704 * Fill <crt_path> and <crtlist_entry>
4705 * <crtlist_entry> must be alloc and free by the caller
4706 * <crtlist_entry->ssl_conf> is alloc by the function
4707 * <crtlist_entry->filters> is alloc by the function
4708 * <crt_path> is a ptr in <line>
4709 * Return an error code
4710 */
4711static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4712{
4713 int cfgerr = 0;
4714 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4715 char *end;
4716 char *args[MAX_CRT_ARGS + 1];
4717 struct ssl_bind_conf *ssl_conf = NULL;
4718
4719 if (!line || !crt_path || !entry)
4720 return ERR_ALERT | ERR_FATAL;
4721
4722 end = line + strlen(line);
4723 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4724 /* Check if we reached the limit and the last char is not \n.
4725 * Watch out for the last line without the terminating '\n'!
4726 */
4727 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4728 linenum, file, CRT_LINESIZE-1);
4729 cfgerr |= ERR_ALERT | ERR_FATAL;
4730 goto error;
4731 }
4732 arg = 0;
4733 newarg = 1;
4734 while (*line) {
4735 if (isspace((unsigned char)*line)) {
4736 newarg = 1;
4737 *line = 0;
4738 } else if (*line == '[') {
4739 if (ssl_b) {
4740 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4741 cfgerr |= ERR_ALERT | ERR_FATAL;
4742 goto error;
4743 }
4744 if (!arg) {
4745 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4746 cfgerr |= ERR_ALERT | ERR_FATAL;
4747 goto error;
4748 }
4749 ssl_b = arg;
4750 newarg = 1;
4751 *line = 0;
4752 } else if (*line == ']') {
4753 if (ssl_e) {
4754 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4755 cfgerr |= ERR_ALERT | ERR_FATAL;
4756 goto error;
4757 }
4758 if (!ssl_b) {
4759 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4760 cfgerr |= ERR_ALERT | ERR_FATAL;
4761 goto error;
4762 }
4763 ssl_e = arg;
4764 newarg = 1;
4765 *line = 0;
4766 } else if (newarg) {
4767 if (arg == MAX_CRT_ARGS) {
4768 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4769 cfgerr |= ERR_ALERT | ERR_FATAL;
4770 goto error;
4771 }
4772 newarg = 0;
4773 args[arg++] = line;
4774 }
4775 line++;
4776 }
4777 args[arg++] = line;
4778
4779 /* empty line */
4780 if (!*args[0]) {
4781 cfgerr |= ERR_NONE;
4782 goto error;
4783 }
4784
4785 *crt_path = args[0];
4786
4787 ssl_conf = calloc(1, sizeof *ssl_conf);
4788 if (!ssl_conf) {
4789 memprintf(err, "not enough memory!");
4790 cfgerr |= ERR_ALERT | ERR_FATAL;
4791 goto error;
4792 }
4793 cur_arg = ssl_b ? ssl_b : 1;
4794 while (cur_arg < ssl_e) {
4795 newarg = 0;
4796 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4797 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4798 newarg = 1;
4799 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4800 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4801 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4802 args[cur_arg], linenum, file);
4803 cfgerr |= ERR_ALERT | ERR_FATAL;
4804 goto error;
4805 }
4806 cur_arg += 1 + ssl_bind_kws[i].skip;
4807 break;
4808 }
4809 }
4810 if (!cfgerr && !newarg) {
4811 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4812 args[cur_arg], linenum, file);
4813 cfgerr |= ERR_ALERT | ERR_FATAL;
4814 goto error;
4815 }
4816 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004817 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004818 entry->ssl_conf = ssl_conf;
4819 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4820 entry->fcount = arg - cur_arg - 1;
4821
4822 return cfgerr;
4823
4824error:
4825 crtlist_free_filters(entry->filters);
4826 entry->filters = NULL;
4827 ssl_sock_free_ssl_conf(entry->ssl_conf);
4828 free(entry->ssl_conf);
4829 entry->ssl_conf = NULL;
4830 return cfgerr;
4831}
4832
William Lallemand2954c472020-03-06 21:54:13 +01004833/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4834 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4835 *
4836 * This function tries to open and store certificate files.
4837 */
4838static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4839{
4840 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004841 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004842 char thisline[CRT_LINESIZE];
4843 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004844 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004845 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004846 int linenum = 0;
4847 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004848
Willy Tarreauad1731d2013-04-02 17:35:58 +02004849 if ((f = fopen(file, "r")) == NULL) {
4850 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004851 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004852 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004853
William Lallemandec2d4932020-04-09 13:44:21 +02004854 newlist = crtlist_new(file, 0);
William Lallemand2954c472020-03-06 21:54:13 +01004855 if (newlist == NULL) {
4856 memprintf(err, "Not enough memory!");
4857 cfgerr |= ERR_ALERT | ERR_FATAL;
4858 goto error;
4859 }
William Lallemand2954c472020-03-06 21:54:13 +01004860
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004861 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004862 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004863 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004864 char *crt_path;
William Lallemand2954c472020-03-06 21:54:13 +01004865 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004866
4867 linenum++;
4868 end = line + strlen(line);
4869 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4870 /* Check if we reached the limit and the last char is not \n.
4871 * Watch out for the last line without the terminating '\n'!
4872 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004873 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4874 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004875 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004876 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004877 }
William Lallemand97b08102020-04-01 10:14:00 +02004878
4879 if (*line == '#' || *line == '\n' || *line == '\r')
4880 continue;
4881
William Lallemande718dfb2020-04-10 11:09:25 +02004882 entry = crtlist_entry_new();
William Lallemand97b08102020-04-01 10:14:00 +02004883 if (entry == NULL) {
4884 memprintf(err, "Not enough memory!");
4885 cfgerr |= ERR_ALERT | ERR_FATAL;
4886 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004887 }
William Lallemand97b08102020-04-01 10:14:00 +02004888
4889 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4890 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004891 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004892 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004893
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004894 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004895 if (!crt_path || !*crt_path) {
William Lallemande718dfb2020-04-10 11:09:25 +02004896 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004897 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004898 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004899 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004900
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004901 if (*crt_path != '/' && global_ssl.crt_base) {
4902 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4903 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4904 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004905 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004906 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004907 }
4908 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4909 crt_path = path;
4910 }
4911
William Lallemand2954c472020-03-06 21:54:13 +01004912 /* Look for a ckch_store or create one */
4913 ckchs = ckchs_lookup(crt_path);
4914 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004915 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004916 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004917 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004918 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004919 }
William Lallemand909086e2020-03-17 16:53:27 +01004920 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004921 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004922
William Lallemand97b08102020-04-01 10:14:00 +02004923 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004924 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004925
William Lallemand2954c472020-03-06 21:54:13 +01004926 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004927 entry->crtlist = newlist;
William Lallemand2954c472020-03-06 21:54:13 +01004928 ebpt_insert(&newlist->entries, &entry->node);
4929 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004930 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004931
4932 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004933 }
William Lallemand2954c472020-03-06 21:54:13 +01004934 if (cfgerr & ERR_CODE)
4935 goto error;
4936
William Lallemandc69f02d2020-04-06 19:07:03 +02004937 newlist->linecount = linenum;
4938
William Lallemand2954c472020-03-06 21:54:13 +01004939 fclose(f);
4940 *crtlist = newlist;
4941
4942 return cfgerr;
4943error:
William Lallemande718dfb2020-04-10 11:09:25 +02004944 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004945
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004946 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004947 crtlist_free(newlist);
4948 return cfgerr;
4949}
4950
4951/* Load a crt-list file, this is done in 2 parts:
4952 * - store the content of the file in a crtlist structure with crtlist_entry structures
4953 * - generate the instances by iterating on entries in the crtlist struct
4954 *
4955 * Nothing is locked there, this function is used in the configuration parser.
4956 *
4957 * Returns a set of ERR_* flags possibly with an error in <err>.
4958 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004959int 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 +01004960{
4961 struct crtlist *crtlist = NULL;
4962 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004963 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004964 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004965 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004966 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004967
William Lallemand79d31ec2020-03-25 15:10:49 +01004968 bind_conf_node = malloc(sizeof(*bind_conf_node));
4969 if (!bind_conf_node) {
4970 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4971 cfgerr |= ERR_FATAL | ERR_ALERT;
4972 goto error;
4973 }
4974 bind_conf_node->next = NULL;
4975 bind_conf_node->bind_conf = bind_conf;
4976
William Lallemand41ca9302020-04-08 13:15:18 +02004977 /* strip trailing slashes, including first one */
4978 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4979 *end = 0;
4980
William Lallemand2954c472020-03-06 21:54:13 +01004981 /* look for an existing crtlist or create one */
4982 eb = ebst_lookup(&crtlists_tree, file);
4983 if (eb) {
4984 crtlist = ebmb_entry(eb, struct crtlist, node);
4985 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004986 /* load a crt-list OR a directory */
4987 if (dir)
4988 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4989 else
4990 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4991
William Lallemand2954c472020-03-06 21:54:13 +01004992 if (!(cfgerr & ERR_CODE))
4993 ebst_insert(&crtlists_tree, &crtlist->node);
4994 }
4995
4996 if (cfgerr & ERR_CODE) {
4997 cfgerr |= ERR_FATAL | ERR_ALERT;
4998 goto error;
4999 }
5000
5001 /* generates ckch instance from the crtlist_entry */
5002 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5003 struct ckch_store *store;
5004 struct ckch_inst *ckch_inst = NULL;
5005
5006 store = entry->node.key;
5007 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
5008 if (cfgerr & ERR_CODE) {
5009 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
5010 goto error;
5011 }
William Lallemand49398312020-03-30 17:01:33 +02005012 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02005013 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01005014 }
William Lallemand2954c472020-03-06 21:54:13 +01005015
William Lallemand79d31ec2020-03-25 15:10:49 +01005016 /* add the bind_conf to the list */
5017 bind_conf_node->next = crtlist->bind_conf;
5018 crtlist->bind_conf = bind_conf_node;
5019
William Lallemand2954c472020-03-06 21:54:13 +01005020 return cfgerr;
5021error:
5022 {
William Lallemand49398312020-03-30 17:01:33 +02005023 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01005024 struct ckch_inst *inst, *s_inst;
5025
William Lallemand49398312020-03-30 17:01:33 +02005026 lastentry = entry; /* which entry we tried to generate last */
5027 if (lastentry) {
5028 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5029 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
5030 break;
5031
5032 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01005033
William Lallemand49398312020-03-30 17:01:33 +02005034 /* this was not generated for this bind_conf, skip */
5035 if (inst->bind_conf != bind_conf)
5036 continue;
5037
William Lallemandd9d5d1b2020-04-09 16:31:05 +02005038 /* free the sni_ctx and instance */
5039 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02005040 }
William Lallemand2954c472020-03-06 21:54:13 +01005041 }
William Lallemand2954c472020-03-06 21:54:13 +01005042 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005043 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005044 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005045 return cfgerr;
5046}
5047
William Lallemand06b22a82020-03-16 14:45:55 +01005048/* Returns a set of ERR_* flags possibly with an error in <err>. */
5049int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5050{
5051 struct stat buf;
5052 char fp[MAXPATHLEN+1];
5053 int cfgerr = 0;
5054 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005055 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005056
5057 if ((ckchs = ckchs_lookup(path))) {
5058 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005059 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005060 }
5061 if (stat(path, &buf) == 0) {
5062 if (S_ISDIR(buf.st_mode) == 0) {
5063 ckchs = ckchs_load_cert_file(path, 0, err);
5064 if (!ckchs)
5065 return ERR_ALERT | ERR_FATAL;
5066
William Lallemand24bde432020-03-09 16:48:43 +01005067 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005068 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005069 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005070 }
5071 } else {
5072 /* stat failed, could be a bundle */
5073 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5074 /* try to load a bundle if it is permitted */
5075 ckchs = ckchs_load_cert_file(path, 1, err);
5076 if (!ckchs)
5077 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005078 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005079 } else {
5080 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5081 err && *err ? *err : "", fp, strerror(errno));
5082 cfgerr |= ERR_ALERT | ERR_FATAL;
5083 }
5084 }
5085
5086 return cfgerr;
5087}
5088
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005089/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005090static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005091ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005092{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005093 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005094 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005095 SSL_OP_ALL | /* all known workarounds for bugs */
5096 SSL_OP_NO_SSLv2 |
5097 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005098 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005099 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005100 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005101 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005102 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005103 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005104 SSL_MODE_ENABLE_PARTIAL_WRITE |
5105 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005106 SSL_MODE_RELEASE_BUFFERS |
5107 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005108 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005109 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005110 int flags = MC_SSL_O_ALL;
5111 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005112
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005113 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005114 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005115
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005116 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005117 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5118 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5119 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005120 else
5121 flags = conf_ssl_methods->flags;
5122
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005123 min = conf_ssl_methods->min;
5124 max = conf_ssl_methods->max;
5125 /* start with TLSv10 to remove SSLv3 per default */
5126 if (!min && (!max || max >= CONF_TLSV10))
5127 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005128 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005129 if (min)
5130 flags |= (methodVersions[min].flag - 1);
5131 if (max)
5132 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005133 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005134 min = max = CONF_TLSV_NONE;
5135 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005136 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005137 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005138 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005139 if (min) {
5140 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005141 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5142 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5143 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5144 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005145 hole = 0;
5146 }
5147 max = i;
5148 }
5149 else {
5150 min = max = i;
5151 }
5152 }
5153 else {
5154 if (min)
5155 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005156 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005157 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005158 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5159 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005160 cfgerr += 1;
5161 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005162 /* save real min/max in bind_conf */
5163 conf_ssl_methods->min = min;
5164 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005165
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005166#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005167 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005168 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005169 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005170 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005171 else
5172 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5173 if (flags & methodVersions[i].flag)
5174 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005175#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005176 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005177 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5178 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005179#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005180
5181 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5182 options |= SSL_OP_NO_TICKET;
5183 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5184 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005185
5186#ifdef SSL_OP_NO_RENEGOTIATION
5187 options |= SSL_OP_NO_RENEGOTIATION;
5188#endif
5189
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005190 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005191
Willy Tarreau5db847a2019-05-09 14:13:35 +02005192#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005193 if (global_ssl.async)
5194 mode |= SSL_MODE_ASYNC;
5195#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005196 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005197 if (global_ssl.life_time)
5198 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005199
5200#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5201#ifdef OPENSSL_IS_BORINGSSL
5202 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5203 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005204#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005205 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005206 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005207 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5208 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005209#else
5210 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005211#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005212 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005213#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005214 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005215}
5216
William Lallemand4f45bb92017-10-30 20:08:51 +01005217
5218static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5219{
5220 if (first == block) {
5221 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5222 if (first->len > 0)
5223 sh_ssl_sess_tree_delete(sh_ssl_sess);
5224 }
5225}
5226
5227/* return first block from sh_ssl_sess */
5228static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5229{
5230 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5231
5232}
5233
5234/* store a session into the cache
5235 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5236 * data: asn1 encoded session
5237 * data_len: asn1 encoded session length
5238 * Returns 1 id session was stored (else 0)
5239 */
5240static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5241{
5242 struct shared_block *first;
5243 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5244
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005245 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005246 if (!first) {
5247 /* Could not retrieve enough free blocks to store that session */
5248 return 0;
5249 }
5250
5251 /* STORE the key in the first elem */
5252 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5253 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5254 first->len = sizeof(struct sh_ssl_sess_hdr);
5255
5256 /* it returns the already existing node
5257 or current node if none, never returns null */
5258 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5259 if (oldsh_ssl_sess != sh_ssl_sess) {
5260 /* NOTE: Row couldn't be in use because we lock read & write function */
5261 /* release the reserved row */
5262 shctx_row_dec_hot(ssl_shctx, first);
5263 /* replace the previous session already in the tree */
5264 sh_ssl_sess = oldsh_ssl_sess;
5265 /* ignore the previous session data, only use the header */
5266 first = sh_ssl_sess_first_block(sh_ssl_sess);
5267 shctx_row_inc_hot(ssl_shctx, first);
5268 first->len = sizeof(struct sh_ssl_sess_hdr);
5269 }
5270
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005271 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005272 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005273 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005274 }
5275
5276 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005277
5278 return 1;
5279}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005280
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005281/* SSL callback used when a new session is created while connecting to a server */
5282static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5283{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005284 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005285 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005286
Willy Tarreau07d94e42018-09-20 10:57:52 +02005287 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005288
Olivier Houcharde6060c52017-11-16 17:42:52 +01005289 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5290 int len;
5291 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005292
Olivier Houcharde6060c52017-11-16 17:42:52 +01005293 len = i2d_SSL_SESSION(sess, NULL);
5294 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5295 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5296 } else {
5297 free(s->ssl_ctx.reused_sess[tid].ptr);
5298 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5299 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5300 }
5301 if (s->ssl_ctx.reused_sess[tid].ptr) {
5302 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5303 &ptr);
5304 }
5305 } else {
5306 free(s->ssl_ctx.reused_sess[tid].ptr);
5307 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5308 }
5309
5310 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005311}
5312
Olivier Houcharde6060c52017-11-16 17:42:52 +01005313
William Lallemanded0b5ad2017-10-30 19:36:36 +01005314/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005315int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005316{
5317 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5318 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5319 unsigned char *p;
5320 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005321 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005322 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005323
5324 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005325 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005326 * note: SSL_SESSION_set1_id is using
5327 * a memcpy so we need to use a different pointer
5328 * than sid_data or sid_ctx_data to avoid valgrind
5329 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005330 */
5331
5332 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005333
5334 /* copy value in an other buffer */
5335 memcpy(encid, sid_data, sid_length);
5336
5337 /* pad with 0 */
5338 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5339 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5340
5341 /* force length to zero to avoid ASN1 encoding */
5342 SSL_SESSION_set1_id(sess, encid, 0);
5343
5344 /* force length to zero to avoid ASN1 encoding */
5345 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005346
5347 /* check if buffer is large enough for the ASN1 encoded session */
5348 data_len = i2d_SSL_SESSION(sess, NULL);
5349 if (data_len > SHSESS_MAX_DATA_LEN)
5350 goto err;
5351
5352 p = encsess;
5353
5354 /* process ASN1 session encoding before the lock */
5355 i2d_SSL_SESSION(sess, &p);
5356
William Lallemanded0b5ad2017-10-30 19:36:36 +01005357
William Lallemanda3c77cf2017-10-30 23:44:40 +01005358 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005359 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005360 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005361 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005362err:
5363 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005364 SSL_SESSION_set1_id(sess, encid, sid_length);
5365 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005366
5367 return 0; /* do not increment session reference count */
5368}
5369
5370/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005371SSL_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 +01005372{
William Lallemand4f45bb92017-10-30 20:08:51 +01005373 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005374 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5375 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005376 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005377 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005378
5379 global.shctx_lookups++;
5380
5381 /* allow the session to be freed automatically by openssl */
5382 *do_copy = 0;
5383
5384 /* tree key is zeros padded sessionid */
5385 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5386 memcpy(tmpkey, key, key_len);
5387 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5388 key = tmpkey;
5389 }
5390
5391 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005392 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005393
5394 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005395 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5396 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005397 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005398 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005399 global.shctx_misses++;
5400 return NULL;
5401 }
5402
William Lallemand4f45bb92017-10-30 20:08:51 +01005403 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5404 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005405
William Lallemand4f45bb92017-10-30 20:08:51 +01005406 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 +01005407
William Lallemanda3c77cf2017-10-30 23:44:40 +01005408 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005409
5410 /* decode ASN1 session */
5411 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005412 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005413 /* Reset session id and session id contenxt */
5414 if (sess) {
5415 SSL_SESSION_set1_id(sess, key, key_len);
5416 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5417 }
5418
5419 return sess;
5420}
5421
William Lallemand4f45bb92017-10-30 20:08:51 +01005422
William Lallemanded0b5ad2017-10-30 19:36:36 +01005423/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005424void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005425{
William Lallemand4f45bb92017-10-30 20:08:51 +01005426 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005427 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5428 unsigned int sid_length;
5429 const unsigned char *sid_data;
5430 (void)ctx;
5431
5432 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5433 /* tree key is zeros padded sessionid */
5434 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5435 memcpy(tmpkey, sid_data, sid_length);
5436 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5437 sid_data = tmpkey;
5438 }
5439
William Lallemanda3c77cf2017-10-30 23:44:40 +01005440 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005441
5442 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005443 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5444 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005445 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005446 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005447 }
5448
5449 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005450 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005451}
5452
5453/* Set session cache mode to server and disable openssl internal cache.
5454 * Set shared cache callbacks on an ssl context.
5455 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005456void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005457{
5458 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5459
5460 if (!ssl_shctx) {
5461 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5462 return;
5463 }
5464
5465 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5466 SSL_SESS_CACHE_NO_INTERNAL |
5467 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5468
5469 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005470 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5471 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5472 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005473}
5474
William Lallemand8b453912019-11-21 15:48:10 +01005475/*
5476 * This function applies the SSL configuration on a SSL_CTX
5477 * It returns an error code and fills the <err> buffer
5478 */
5479int 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 +01005480{
5481 struct proxy *curproxy = bind_conf->frontend;
5482 int cfgerr = 0;
5483 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005484 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005485 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005486#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005487 const char *conf_ciphersuites;
5488#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005489 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005490
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005491 if (ssl_conf) {
5492 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5493 int i, min, max;
5494 int flags = MC_SSL_O_ALL;
5495
5496 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005497 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5498 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005499 if (min)
5500 flags |= (methodVersions[min].flag - 1);
5501 if (max)
5502 flags |= ~((methodVersions[max].flag << 1) - 1);
5503 min = max = CONF_TLSV_NONE;
5504 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5505 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5506 if (min)
5507 max = i;
5508 else
5509 min = max = i;
5510 }
5511 /* save real min/max */
5512 conf_ssl_methods->min = min;
5513 conf_ssl_methods->max = max;
5514 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005515 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5516 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005517 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005518 }
5519 }
5520
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005521 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005522 case SSL_SOCK_VERIFY_NONE:
5523 verify = SSL_VERIFY_NONE;
5524 break;
5525 case SSL_SOCK_VERIFY_OPTIONAL:
5526 verify = SSL_VERIFY_PEER;
5527 break;
5528 case SSL_SOCK_VERIFY_REQUIRED:
5529 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5530 break;
5531 }
5532 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5533 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005534 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 +01005535 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 +01005536 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 +01005537 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005538 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005539 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005540 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005541 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005542 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005543 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005544 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5545 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5546 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5547 cfgerr |= ERR_ALERT | ERR_FATAL;
5548 }
5549 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005550 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005551 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 +02005552 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005553 }
Emeric Brun850efd52014-01-29 12:24:34 +01005554 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005555 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5556 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005557 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005558 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005559#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005560 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005561 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5562
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005563 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005564 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5565 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005566 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005567 }
Emeric Brun561e5742012-10-02 15:20:55 +02005568 else {
5569 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5570 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005571 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005572#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005573 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005574 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005575#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005576 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005577 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005578 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5579 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005580 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005581 }
5582 }
5583#endif
5584
William Lallemand4f45bb92017-10-30 20:08:51 +01005585 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005586 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5587 if (conf_ciphers &&
5588 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005589 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5590 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005591 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005592 }
5593
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005594#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005595 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5596 if (conf_ciphersuites &&
5597 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005598 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5599 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005600 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005601 }
5602#endif
5603
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005604#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005605 /* If tune.ssl.default-dh-param has not been set,
5606 neither has ssl-default-dh-file and no static DH
5607 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005608 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005609 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005610 (ssl_dh_ptr_index == -1 ||
5611 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005612 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5613 const SSL_CIPHER * cipher = NULL;
5614 char cipher_description[128];
5615 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5616 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5617 which is not ephemeral DH. */
5618 const char dhe_description[] = " Kx=DH ";
5619 const char dhe_export_description[] = " Kx=DH(";
5620 int idx = 0;
5621 int dhe_found = 0;
5622 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005623
Remi Gacogne23d5d372014-10-10 17:04:26 +02005624 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005625
Remi Gacogne23d5d372014-10-10 17:04:26 +02005626 if (ssl) {
5627 ciphers = SSL_get_ciphers(ssl);
5628
5629 if (ciphers) {
5630 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5631 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5632 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5633 if (strstr(cipher_description, dhe_description) != NULL ||
5634 strstr(cipher_description, dhe_export_description) != NULL) {
5635 dhe_found = 1;
5636 break;
5637 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005638 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005639 }
5640 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005641 SSL_free(ssl);
5642 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005643 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005644
Lukas Tribus90132722014-08-18 00:56:33 +02005645 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005646 memprintf(err, "%sSetting tune.ssl.default-dh-param to 1024 by default, if your workload permits it you should set it to at least 2048. Please set a value >= 1024 to make this warning disappear.\n",
5647 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005648 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005649 }
5650
Willy Tarreauef934602016-12-22 23:12:01 +01005651 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005652 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005653
Willy Tarreauef934602016-12-22 23:12:01 +01005654 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005655 if (local_dh_1024 == NULL) {
5656 local_dh_1024 = ssl_get_dh_1024();
5657 }
Willy Tarreauef934602016-12-22 23:12:01 +01005658 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005659 if (local_dh_2048 == NULL) {
5660 local_dh_2048 = ssl_get_dh_2048();
5661 }
Willy Tarreauef934602016-12-22 23:12:01 +01005662 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005663 if (local_dh_4096 == NULL) {
5664 local_dh_4096 = ssl_get_dh_4096();
5665 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005666 }
5667 }
5668 }
5669#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005670
Emeric Brunfc0421f2012-09-07 17:30:07 +02005671 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005672#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005673 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005674#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005675
Bernard Spil13c53f82018-02-15 13:34:58 +01005676#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005677 ssl_conf_cur = NULL;
5678 if (ssl_conf && ssl_conf->npn_str)
5679 ssl_conf_cur = ssl_conf;
5680 else if (bind_conf->ssl_conf.npn_str)
5681 ssl_conf_cur = &bind_conf->ssl_conf;
5682 if (ssl_conf_cur)
5683 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005684#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005685#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005686 ssl_conf_cur = NULL;
5687 if (ssl_conf && ssl_conf->alpn_str)
5688 ssl_conf_cur = ssl_conf;
5689 else if (bind_conf->ssl_conf.alpn_str)
5690 ssl_conf_cur = &bind_conf->ssl_conf;
5691 if (ssl_conf_cur)
5692 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005693#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005694#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005695 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5696 if (conf_curves) {
5697 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005698 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5699 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005700 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005701 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005702 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005703 }
5704#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005705#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005706 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005707 int i;
5708 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005709#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005710 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005711 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5712 NULL);
5713
5714 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005715 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005716 return cfgerr;
5717 }
5718#else
5719 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5720 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5721 ECDHE_DEFAULT_CURVE);
5722#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005723
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005724 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005725 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005726 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5727 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005728 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005729 }
5730 else {
5731 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5732 EC_KEY_free(ecdh);
5733 }
5734 }
5735#endif
5736
Emeric Brunfc0421f2012-09-07 17:30:07 +02005737 return cfgerr;
5738}
5739
Evan Broderbe554312013-06-27 00:05:25 -07005740static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5741{
5742 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5743 size_t prefixlen, suffixlen;
5744
5745 /* Trivial case */
5746 if (strcmp(pattern, hostname) == 0)
5747 return 1;
5748
Evan Broderbe554312013-06-27 00:05:25 -07005749 /* The rest of this logic is based on RFC 6125, section 6.4.3
5750 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5751
Emeric Bruna848dae2013-10-08 11:27:28 +02005752 pattern_wildcard = NULL;
5753 pattern_left_label_end = pattern;
5754 while (*pattern_left_label_end != '.') {
5755 switch (*pattern_left_label_end) {
5756 case 0:
5757 /* End of label not found */
5758 return 0;
5759 case '*':
5760 /* If there is more than one wildcards */
5761 if (pattern_wildcard)
5762 return 0;
5763 pattern_wildcard = pattern_left_label_end;
5764 break;
5765 }
5766 pattern_left_label_end++;
5767 }
5768
5769 /* If it's not trivial and there is no wildcard, it can't
5770 * match */
5771 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005772 return 0;
5773
5774 /* Make sure all labels match except the leftmost */
5775 hostname_left_label_end = strchr(hostname, '.');
5776 if (!hostname_left_label_end
5777 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5778 return 0;
5779
5780 /* Make sure the leftmost label of the hostname is long enough
5781 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005782 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005783 return 0;
5784
5785 /* Finally compare the string on either side of the
5786 * wildcard */
5787 prefixlen = pattern_wildcard - pattern;
5788 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005789 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5790 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005791 return 0;
5792
5793 return 1;
5794}
5795
5796static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5797{
5798 SSL *ssl;
5799 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005800 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005801 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005802 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005803
5804 int depth;
5805 X509 *cert;
5806 STACK_OF(GENERAL_NAME) *alt_names;
5807 int i;
5808 X509_NAME *cert_subject;
5809 char *str;
5810
5811 if (ok == 0)
5812 return ok;
5813
5814 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005815 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005816 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005817
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005818 /* We're checking if the provided hostnames match the desired one. The
5819 * desired hostname comes from the SNI we presented if any, or if not
5820 * provided then it may have been explicitly stated using a "verifyhost"
5821 * directive. If neither is set, we don't care about the name so the
5822 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005823 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005824 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005825 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005826 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005827 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005828 if (!servername)
5829 return ok;
5830 }
Evan Broderbe554312013-06-27 00:05:25 -07005831
5832 /* We only need to verify the CN on the actual server cert,
5833 * not the indirect CAs */
5834 depth = X509_STORE_CTX_get_error_depth(ctx);
5835 if (depth != 0)
5836 return ok;
5837
5838 /* At this point, the cert is *not* OK unless we can find a
5839 * hostname match */
5840 ok = 0;
5841
5842 cert = X509_STORE_CTX_get_current_cert(ctx);
5843 /* It seems like this might happen if verify peer isn't set */
5844 if (!cert)
5845 return ok;
5846
5847 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5848 if (alt_names) {
5849 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5850 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5851 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005852#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005853 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5854#else
Evan Broderbe554312013-06-27 00:05:25 -07005855 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005856#endif
Evan Broderbe554312013-06-27 00:05:25 -07005857 ok = ssl_sock_srv_hostcheck(str, servername);
5858 OPENSSL_free(str);
5859 }
5860 }
5861 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005862 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005863 }
5864
5865 cert_subject = X509_get_subject_name(cert);
5866 i = -1;
5867 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5868 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005869 ASN1_STRING *value;
5870 value = X509_NAME_ENTRY_get_data(entry);
5871 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005872 ok = ssl_sock_srv_hostcheck(str, servername);
5873 OPENSSL_free(str);
5874 }
5875 }
5876
Willy Tarreau71d058c2017-07-26 20:09:56 +02005877 /* report the mismatch and indicate if SNI was used or not */
5878 if (!ok && !conn->err_code)
5879 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005880 return ok;
5881}
5882
Emeric Brun94324a42012-10-11 14:00:19 +02005883/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005884int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005885{
Willy Tarreau03209342016-12-22 17:08:28 +01005886 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005887 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005888 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005889 SSL_OP_ALL | /* all known workarounds for bugs */
5890 SSL_OP_NO_SSLv2 |
5891 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005892 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005893 SSL_MODE_ENABLE_PARTIAL_WRITE |
5894 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005895 SSL_MODE_RELEASE_BUFFERS |
5896 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005897 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005898 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005899 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005900 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005901 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005902
Thierry Fournier383085f2013-01-24 14:15:43 +01005903 /* Make sure openssl opens /dev/urandom before the chroot */
5904 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005905 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005906 cfgerr++;
5907 }
5908
Willy Tarreaufce03112015-01-15 21:32:40 +01005909 /* Automatic memory computations need to know we use SSL there */
5910 global.ssl_used_backend = 1;
5911
5912 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005913 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005914 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005915 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5916 curproxy->id, srv->id,
5917 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005918 cfgerr++;
5919 return cfgerr;
5920 }
5921 }
Emeric Brun94324a42012-10-11 14:00:19 +02005922 if (srv->use_ssl)
5923 srv->xprt = &ssl_sock;
5924 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005925 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005926
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005927 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005928 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005929 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5930 proxy_type_str(curproxy), curproxy->id,
5931 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005932 cfgerr++;
5933 return cfgerr;
5934 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005935
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005936 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005937 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5938 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5939 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005940 else
5941 flags = conf_ssl_methods->flags;
5942
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005943 /* Real min and max should be determinate with configuration and openssl's capabilities */
5944 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005945 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005946 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005947 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005948
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005949 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005950 min = max = CONF_TLSV_NONE;
5951 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005952 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005953 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005954 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005955 if (min) {
5956 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005957 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5958 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5959 proxy_type_str(curproxy), curproxy->id, srv->id,
5960 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005961 hole = 0;
5962 }
5963 max = i;
5964 }
5965 else {
5966 min = max = i;
5967 }
5968 }
5969 else {
5970 if (min)
5971 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005972 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005973 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005974 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5975 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005976 cfgerr += 1;
5977 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005978
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005979#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005980 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005981 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005982 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005983 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005984 else
5985 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5986 if (flags & methodVersions[i].flag)
5987 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005988#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005989 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005990 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5991 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005992#endif
5993
5994 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5995 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005996 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005997
Willy Tarreau5db847a2019-05-09 14:13:35 +02005998#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005999 if (global_ssl.async)
6000 mode |= SSL_MODE_ASYNC;
6001#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01006002 SSL_CTX_set_mode(ctx, mode);
6003 srv->ssl_ctx.ctx = ctx;
6004
Emeric Bruna7aa3092012-10-26 12:58:00 +02006005 if (srv->ssl_ctx.client_crt) {
6006 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 +01006007 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
6008 proxy_type_str(curproxy), curproxy->id,
6009 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006010 cfgerr++;
6011 }
6012 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 +01006013 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
6014 proxy_type_str(curproxy), curproxy->id,
6015 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006016 cfgerr++;
6017 }
6018 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006019 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
6020 proxy_type_str(curproxy), curproxy->id,
6021 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006022 cfgerr++;
6023 }
6024 }
Emeric Brun94324a42012-10-11 14:00:19 +02006025
Emeric Brun850efd52014-01-29 12:24:34 +01006026 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
6027 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01006028 switch (srv->ssl_ctx.verify) {
6029 case SSL_SOCK_VERIFY_NONE:
6030 verify = SSL_VERIFY_NONE;
6031 break;
6032 case SSL_SOCK_VERIFY_REQUIRED:
6033 verify = SSL_VERIFY_PEER;
6034 break;
6035 }
Evan Broderbe554312013-06-27 00:05:25 -07006036 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01006037 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02006038 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01006039 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02006040 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02006041 /* set CAfile to verify */
6042 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6043 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006044 curproxy->id, srv->id,
6045 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006046 cfgerr++;
6047 }
6048 }
Emeric Brun850efd52014-01-29 12:24:34 +01006049 else {
6050 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006051 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",
6052 curproxy->id, srv->id,
6053 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006054 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006055 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6056 curproxy->id, srv->id,
6057 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006058 cfgerr++;
6059 }
Emeric Brunef42d922012-10-11 16:11:36 +02006060#ifdef X509_V_FLAG_CRL_CHECK
6061 if (srv->ssl_ctx.crl_file) {
6062 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6063
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006064 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006065 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6066 curproxy->id, srv->id,
6067 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006068 cfgerr++;
6069 }
6070 else {
6071 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6072 }
6073 }
6074#endif
6075 }
6076
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006077 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6078 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6079 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006080 if (srv->ssl_ctx.ciphers &&
6081 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006082 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6083 curproxy->id, srv->id,
6084 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006085 cfgerr++;
6086 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006087
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006088#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006089 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006090 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006091 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6092 curproxy->id, srv->id,
6093 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6094 cfgerr++;
6095 }
6096#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006097#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6098 if (srv->ssl_ctx.npn_str)
6099 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6100#endif
6101#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6102 if (srv->ssl_ctx.alpn_str)
6103 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6104#endif
6105
Emeric Brun94324a42012-10-11 14:00:19 +02006106
6107 return cfgerr;
6108}
6109
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006110/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006111 * be NULL, in which case nothing is done. Returns the number of errors
6112 * encountered.
6113 */
Willy Tarreau03209342016-12-22 17:08:28 +01006114int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006115{
6116 struct ebmb_node *node;
6117 struct sni_ctx *sni;
6118 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006119 int errcode = 0;
6120 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006121
Willy Tarreaufce03112015-01-15 21:32:40 +01006122 /* Automatic memory computations need to know we use SSL there */
6123 global.ssl_used_frontend = 1;
6124
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006125 /* Make sure openssl opens /dev/urandom before the chroot */
6126 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006127 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006128 err++;
6129 }
6130 /* Create initial_ctx used to start the ssl connection before do switchctx */
6131 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006132 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006133 /* It should not be necessary to call this function, but it's
6134 necessary first to check and move all initialisation related
6135 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006136 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006137 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006138 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006139 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006140
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006141 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006142 while (node) {
6143 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006144 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6145 /* only initialize the CTX on its first occurrence and
6146 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006147 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006148 node = ebmb_next(node);
6149 }
6150
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006151 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006152 while (node) {
6153 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006154 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006155 /* only initialize the CTX on its first occurrence and
6156 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006157 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6158 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006159 node = ebmb_next(node);
6160 }
William Lallemand8b453912019-11-21 15:48:10 +01006161
6162 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006163 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006164 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006165 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006166 err++;
6167 }
6168
6169 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006170 return err;
6171}
6172
Willy Tarreau55d37912016-12-21 23:38:39 +01006173/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6174 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6175 * alerts are directly emitted since the rest of the stack does it below.
6176 */
6177int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6178{
6179 struct proxy *px = bind_conf->frontend;
6180 int alloc_ctx;
6181 int err;
6182
6183 if (!bind_conf->is_ssl) {
6184 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006185 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6186 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006187 }
6188 return 0;
6189 }
6190 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006191 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006192 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6193 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006194 }
6195 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006196 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6197 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006198 return -1;
6199 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006200 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006201 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006202 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006203 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006204 sizeof(*sh_ssl_sess_tree),
6205 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006206 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006207 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6208 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");
6209 else
6210 ha_alert("Unable to allocate SSL session cache.\n");
6211 return -1;
6212 }
6213 /* free block callback */
6214 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6215 /* init the root tree within the extra space */
6216 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6217 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006218 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006219 err = 0;
6220 /* initialize all certificate contexts */
6221 err += ssl_sock_prepare_all_ctx(bind_conf);
6222
6223 /* initialize CA variables if the certificates generation is enabled */
6224 err += ssl_sock_load_ca(bind_conf);
6225
6226 return -err;
6227}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006228
6229/* release ssl context allocated for servers. */
6230void ssl_sock_free_srv_ctx(struct server *srv)
6231{
Olivier Houchardc7566002018-11-20 23:33:50 +01006232#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6233 if (srv->ssl_ctx.alpn_str)
6234 free(srv->ssl_ctx.alpn_str);
6235#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006236#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006237 if (srv->ssl_ctx.npn_str)
6238 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006239#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006240 if (srv->ssl_ctx.ctx)
6241 SSL_CTX_free(srv->ssl_ctx.ctx);
6242}
6243
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006244/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006245 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6246 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006247void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006248{
6249 struct ebmb_node *node, *back;
6250 struct sni_ctx *sni;
6251
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006252 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006253 while (node) {
6254 sni = ebmb_entry(node, struct sni_ctx, name);
6255 back = ebmb_next(node);
6256 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006257 SSL_CTX_free(sni->ctx);
6258 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006259 ssl_sock_free_ssl_conf(sni->conf);
6260 free(sni->conf);
6261 sni->conf = NULL;
6262 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006263 free(sni);
6264 node = back;
6265 }
6266
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006267 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006268 while (node) {
6269 sni = ebmb_entry(node, struct sni_ctx, name);
6270 back = ebmb_next(node);
6271 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006272 SSL_CTX_free(sni->ctx);
6273 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006274 ssl_sock_free_ssl_conf(sni->conf);
6275 free(sni->conf);
6276 sni->conf = NULL;
6277 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006278 free(sni);
6279 node = back;
6280 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006281 SSL_CTX_free(bind_conf->initial_ctx);
6282 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006283 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006284 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006285 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006286}
6287
Willy Tarreau795cdab2016-12-22 17:30:54 +01006288/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6289void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6290{
6291 ssl_sock_free_ca(bind_conf);
6292 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006293 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006294 free(bind_conf->ca_sign_file);
6295 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006296 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006297 free(bind_conf->keys_ref->filename);
6298 free(bind_conf->keys_ref->tlskeys);
6299 LIST_DEL(&bind_conf->keys_ref->list);
6300 free(bind_conf->keys_ref);
6301 }
6302 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006303 bind_conf->ca_sign_pass = NULL;
6304 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006305}
6306
Christopher Faulet31af49d2015-06-09 17:29:50 +02006307/* Load CA cert file and private key used to generate certificates */
6308int
Willy Tarreau03209342016-12-22 17:08:28 +01006309ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006310{
Willy Tarreau03209342016-12-22 17:08:28 +01006311 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006312 FILE *fp;
6313 X509 *cacert = NULL;
6314 EVP_PKEY *capkey = NULL;
6315 int err = 0;
6316
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006317 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006318 return err;
6319
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006320#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006321 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006322 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006323 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006324 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006325 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006326#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006327
Christopher Faulet31af49d2015-06-09 17:29:50 +02006328 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006329 ha_alert("Proxy '%s': cannot enable certificate generation, "
6330 "no CA certificate File configured at [%s:%d].\n",
6331 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006332 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006333 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006334
6335 /* read in the CA certificate */
6336 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006337 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6338 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006339 goto load_error;
6340 }
6341 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006342 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6343 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006344 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006345 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006346 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006347 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006348 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6349 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006350 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006351 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006352
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006353 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006354 bind_conf->ca_sign_cert = cacert;
6355 bind_conf->ca_sign_pkey = capkey;
6356 return err;
6357
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006358 read_error:
6359 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006360 if (capkey) EVP_PKEY_free(capkey);
6361 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006362 load_error:
6363 bind_conf->generate_certs = 0;
6364 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006365 return err;
6366}
6367
6368/* Release CA cert and private key used to generate certificated */
6369void
6370ssl_sock_free_ca(struct bind_conf *bind_conf)
6371{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006372 if (bind_conf->ca_sign_pkey)
6373 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6374 if (bind_conf->ca_sign_cert)
6375 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006376 bind_conf->ca_sign_pkey = NULL;
6377 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006378}
6379
Emeric Brun46591952012-05-18 15:47:34 +02006380/*
6381 * This function is called if SSL * context is not yet allocated. The function
6382 * is designed to be called before any other data-layer operation and sets the
6383 * handshake flag on the connection. It is safe to call it multiple times.
6384 * It returns 0 on success and -1 in error case.
6385 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006386static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006387{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006388 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006389 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006390 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006391 return 0;
6392
Willy Tarreau3c728722014-01-23 13:50:42 +01006393 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006394 return 0;
6395
Olivier Houchard66ab4982019-02-26 18:37:15 +01006396 ctx = pool_alloc(ssl_sock_ctx_pool);
6397 if (!ctx) {
6398 conn->err_code = CO_ER_SSL_NO_MEM;
6399 return -1;
6400 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006401 ctx->wait_event.tasklet = tasklet_new();
6402 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006403 conn->err_code = CO_ER_SSL_NO_MEM;
6404 pool_free(ssl_sock_ctx_pool, ctx);
6405 return -1;
6406 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006407 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6408 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006409 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006410 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006411 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006412 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006413 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006414 ctx->xprt_st = 0;
6415 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006416
6417 /* Only work with sockets for now, this should be adapted when we'll
6418 * add QUIC support.
6419 */
6420 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006421 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006422 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6423 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006424 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006425
Willy Tarreau20879a02012-12-03 16:32:10 +01006426 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6427 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006428 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006429 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006430
Emeric Brun46591952012-05-18 15:47:34 +02006431 /* If it is in client mode initiate SSL session
6432 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006433 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006434 int may_retry = 1;
6435
6436 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006437 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006438 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6439 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006440 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006441 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006442 goto retry_connect;
6443 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006444 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006445 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006446 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006447 ctx->bio = BIO_new(ha_meth);
6448 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006449 SSL_free(ctx->ssl);
6450 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006451 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006452 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006453 goto retry_connect;
6454 }
Emeric Brun55476152014-11-12 17:35:37 +01006455 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006456 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006457 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006458 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006459 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006460
Evan Broderbe554312013-06-27 00:05:25 -07006461 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006462 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6463 SSL_free(ctx->ssl);
6464 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006465 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006466 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006467 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006468 goto retry_connect;
6469 }
Emeric Brun55476152014-11-12 17:35:37 +01006470 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006471 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006472 }
6473
Olivier Houchard66ab4982019-02-26 18:37:15 +01006474 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006475 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6476 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6477 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 +01006478 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006479 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006480 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6481 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006482 } else if (sess) {
6483 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006484 }
6485 }
Evan Broderbe554312013-06-27 00:05:25 -07006486
Emeric Brun46591952012-05-18 15:47:34 +02006487 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006488 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006489
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006490 _HA_ATOMIC_ADD(&sslconns, 1);
6491 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006492 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006493 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006494 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006495 return 0;
6496 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006497 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006498 int may_retry = 1;
6499
6500 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006501 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006502 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6503 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006504 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006505 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006506 goto retry_accept;
6507 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006508 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006509 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006510 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006511 ctx->bio = BIO_new(ha_meth);
6512 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006513 SSL_free(ctx->ssl);
6514 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006515 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006516 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006517 goto retry_accept;
6518 }
Emeric Brun55476152014-11-12 17:35:37 +01006519 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006520 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006521 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006522 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006523 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006524
Emeric Brune1f38db2012-09-03 20:36:47 +02006525 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006526 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6527 SSL_free(ctx->ssl);
6528 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006529 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006530 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006531 goto retry_accept;
6532 }
Emeric Brun55476152014-11-12 17:35:37 +01006533 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006534 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006535 }
6536
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006537#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6538 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6539 b_alloc(&ctx->early_buf);
6540 SSL_set_max_early_data(ctx->ssl,
6541 /* Only allow early data if we managed to allocate
6542 * a buffer.
6543 */
6544 (!b_is_null(&ctx->early_buf)) ?
6545 global.tune.bufsize - global.tune.maxrewrite : 0);
6546 }
6547#endif
6548
Olivier Houchard66ab4982019-02-26 18:37:15 +01006549 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006550
Emeric Brun46591952012-05-18 15:47:34 +02006551 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006552 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006553#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006554 conn->flags |= CO_FL_EARLY_SSL_HS;
6555#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006556
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006557 _HA_ATOMIC_ADD(&sslconns, 1);
6558 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006559 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006560 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006561 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006562 return 0;
6563 }
6564 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006565 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006566err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006567 if (ctx && ctx->wait_event.tasklet)
6568 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006569 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006570 return -1;
6571}
6572
6573
6574/* This is the callback which is used when an SSL handshake is pending. It
6575 * updates the FD status if it wants some polling before being called again.
6576 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6577 * otherwise it returns non-zero and removes itself from the connection's
6578 * flags (the bit is provided in <flag> by the caller).
6579 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006580static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006581{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006582 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006583 int ret;
6584
Willy Tarreau3c728722014-01-23 13:50:42 +01006585 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006586 return 0;
6587
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006588 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006589 goto out_error;
6590
Willy Tarreau5db847a2019-05-09 14:13:35 +02006591#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006592 /*
6593 * Check if we have early data. If we do, we have to read them
6594 * before SSL_do_handshake() is called, And there's no way to
6595 * detect early data, except to try to read them
6596 */
6597 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006598 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006599
Olivier Houchard54907bb2019-12-19 15:02:39 +01006600 while (1) {
6601 ret = SSL_read_early_data(ctx->ssl,
6602 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6603 &read_data);
6604 if (ret == SSL_READ_EARLY_DATA_ERROR)
6605 goto check_error;
6606 if (read_data > 0) {
6607 conn->flags |= CO_FL_EARLY_DATA;
6608 b_add(&ctx->early_buf, read_data);
6609 }
6610 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6611 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6612 if (!b_data(&ctx->early_buf))
6613 b_free(&ctx->early_buf);
6614 break;
6615 }
6616 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006617 }
6618#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006619 /* If we use SSL_do_handshake to process a reneg initiated by
6620 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6621 * Usually SSL_write and SSL_read are used and process implicitly
6622 * the reneg handshake.
6623 * Here we use SSL_peek as a workaround for reneg.
6624 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006625 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006626 char c;
6627
Olivier Houchard66ab4982019-02-26 18:37:15 +01006628 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006629 if (ret <= 0) {
6630 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006631 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006632
Emeric Brun674b7432012-11-08 19:21:55 +01006633 if (ret == SSL_ERROR_WANT_WRITE) {
6634 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006635 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006636 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006637 return 0;
6638 }
6639 else if (ret == SSL_ERROR_WANT_READ) {
6640 /* handshake may have been completed but we have
6641 * no more data to read.
6642 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006643 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006644 ret = 1;
6645 goto reneg_ok;
6646 }
6647 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006648 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006649 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006650 return 0;
6651 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006652#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006653 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006654 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006655 return 0;
6656 }
6657#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006658 else if (ret == SSL_ERROR_SYSCALL) {
6659 /* if errno is null, then connection was successfully established */
6660 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6661 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006662 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006663#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6664 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006665 conn->err_code = CO_ER_SSL_HANDSHAKE;
6666#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006667 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006668#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006669 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006670 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006671 empty_handshake = state == TLS_ST_BEFORE;
6672#else
Lukas Tribus49799162019-07-08 14:29:15 +02006673 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6674 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006675#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006676 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006677 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006678 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006679 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6680 else
6681 conn->err_code = CO_ER_SSL_EMPTY;
6682 }
6683 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006684 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006685 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6686 else
6687 conn->err_code = CO_ER_SSL_ABORT;
6688 }
6689 }
6690 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006691 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006692 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006693 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006694 conn->err_code = CO_ER_SSL_HANDSHAKE;
6695 }
Lukas Tribus49799162019-07-08 14:29:15 +02006696#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006697 }
Emeric Brun674b7432012-11-08 19:21:55 +01006698 goto out_error;
6699 }
6700 else {
6701 /* Fail on all other handshake errors */
6702 /* Note: OpenSSL may leave unread bytes in the socket's
6703 * buffer, causing an RST to be emitted upon close() on
6704 * TCP sockets. We first try to drain possibly pending
6705 * data to avoid this as much as possible.
6706 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006707 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006708 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006709 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006710 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006711 goto out_error;
6712 }
6713 }
6714 /* read some data: consider handshake completed */
6715 goto reneg_ok;
6716 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006717 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006718check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006719 if (ret != 1) {
6720 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006721 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006722
6723 if (ret == SSL_ERROR_WANT_WRITE) {
6724 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006725 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006726 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006727 return 0;
6728 }
6729 else if (ret == SSL_ERROR_WANT_READ) {
6730 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006731 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006732 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6733 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006734 return 0;
6735 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006736#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006737 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006738 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006739 return 0;
6740 }
6741#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006742 else if (ret == SSL_ERROR_SYSCALL) {
6743 /* if errno is null, then connection was successfully established */
6744 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6745 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006746 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006747#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6748 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006749 conn->err_code = CO_ER_SSL_HANDSHAKE;
6750#else
6751 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006752#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006753 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006754 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006755 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006756#else
Lukas Tribus49799162019-07-08 14:29:15 +02006757 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6758 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006759#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006760 if (empty_handshake) {
6761 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006762 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006763 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6764 else
6765 conn->err_code = CO_ER_SSL_EMPTY;
6766 }
6767 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006768 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006769 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6770 else
6771 conn->err_code = CO_ER_SSL_ABORT;
6772 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006773 }
6774 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006775 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006776 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6777 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006778 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006779 }
Lukas Tribus49799162019-07-08 14:29:15 +02006780#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006781 }
Willy Tarreau89230192012-09-28 20:22:13 +02006782 goto out_error;
6783 }
Emeric Brun46591952012-05-18 15:47:34 +02006784 else {
6785 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006786 /* Note: OpenSSL may leave unread bytes in the socket's
6787 * buffer, causing an RST to be emitted upon close() on
6788 * TCP sockets. We first try to drain possibly pending
6789 * data to avoid this as much as possible.
6790 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006791 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006792 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006793 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006794 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006795 goto out_error;
6796 }
6797 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006798#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006799 else {
6800 /*
6801 * If the server refused the early data, we have to send a
6802 * 425 to the client, as we no longer have the data to sent
6803 * them again.
6804 */
6805 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006806 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006807 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6808 goto out_error;
6809 }
6810 }
6811 }
6812#endif
6813
Emeric Brun46591952012-05-18 15:47:34 +02006814
Emeric Brun674b7432012-11-08 19:21:55 +01006815reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006816
Willy Tarreau5db847a2019-05-09 14:13:35 +02006817#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006818 /* ASYNC engine API doesn't support moving read/write
6819 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006820 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006821 */
6822 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006823 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006824#endif
Emeric Brun46591952012-05-18 15:47:34 +02006825 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006826 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006827 if (objt_server(conn->target)) {
6828 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6829 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6830 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006831 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006832 else {
6833 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6834 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6835 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6836 }
Emeric Brun46591952012-05-18 15:47:34 +02006837 }
6838
6839 /* The connection is now established at both layers, it's time to leave */
6840 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6841 return 1;
6842
6843 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006844 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006845 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006846 ERR_clear_error();
6847
Emeric Brun9fa89732012-10-04 17:09:56 +02006848 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006849 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6850 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6851 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006852 }
6853
Emeric Brun46591952012-05-18 15:47:34 +02006854 /* Fail on all other handshake errors */
6855 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006856 if (!conn->err_code)
6857 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006858 return 0;
6859}
6860
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006861/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6862 * event subscriber <es> is not allowed to change from a previous call as long
6863 * as at least one event is still subscribed. The <event_type> must only be a
6864 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6865 * unless the transport layer was already released.
6866 */
6867static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006868{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006869 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006870
Olivier Houchard0ff28652019-06-24 18:57:39 +02006871 if (!ctx)
6872 return -1;
6873
Willy Tarreau113d52b2020-01-10 09:20:26 +01006874 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006875 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006876
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006877 ctx->subs = es;
6878 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006879
6880 /* we may have to subscribe to lower layers for new events */
6881 event_type &= ~ctx->wait_event.events;
6882 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6883 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006884 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006885}
6886
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006887/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6888 * The <es> pointer is not allowed to differ from the one passed to the
6889 * subscribe() call. It always returns zero.
6890 */
6891static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006892{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006893 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006894
Willy Tarreau113d52b2020-01-10 09:20:26 +01006895 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006896 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006897
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006898 es->events &= ~event_type;
6899 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006900 ctx->subs = NULL;
6901
6902 /* If we subscribed, and we're not doing the handshake,
6903 * then we subscribed because the upper layer asked for it,
6904 * as the upper layer is no longer interested, we can
6905 * unsubscribe too.
6906 */
6907 event_type &= ctx->wait_event.events;
6908 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6909 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006910
6911 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006912}
6913
Olivier Houchard2e055482019-05-27 19:50:12 +02006914/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6915 * Returns 0 on success, and non-zero on failure.
6916 */
6917static 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)
6918{
6919 struct ssl_sock_ctx *ctx = xprt_ctx;
6920
6921 if (oldxprt_ops != NULL)
6922 *oldxprt_ops = ctx->xprt;
6923 if (oldxprt_ctx != NULL)
6924 *oldxprt_ctx = ctx->xprt_ctx;
6925 ctx->xprt = toadd_ops;
6926 ctx->xprt_ctx = toadd_ctx;
6927 return 0;
6928}
6929
Olivier Houchard5149b592019-05-23 17:47:36 +02006930/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6931 * return 0, otherwise just call the remove_xprt method from the underlying
6932 * XPRT.
6933 */
6934static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6935{
6936 struct ssl_sock_ctx *ctx = xprt_ctx;
6937
6938 if (ctx->xprt_ctx == toremove_ctx) {
6939 ctx->xprt_ctx = newctx;
6940 ctx->xprt = newops;
6941 return 0;
6942 }
6943 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6944}
6945
Olivier Houchardea8dd942019-05-20 14:02:16 +02006946static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6947{
6948 struct ssl_sock_ctx *ctx = context;
6949
6950 /* First if we're doing an handshake, try that */
6951 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6952 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6953 /* If we had an error, or the handshake is done and I/O is available,
6954 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006955 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006956 * we can't be sure conn_fd_handler() will be called again.
6957 */
6958 if ((ctx->conn->flags & CO_FL_ERROR) ||
6959 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6960 int ret = 0;
6961 int woke = 0;
6962
6963 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006964 if (ctx->subs) {
6965 tasklet_wakeup(ctx->subs->tasklet);
6966 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006967 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006968 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006969 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006970
Olivier Houchardea8dd942019-05-20 14:02:16 +02006971 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006972 * upper layers know. If we have no mux, create it,
6973 * and once we have a mux, call its wake method if we didn't
6974 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006975 */
6976 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006977 if (!ctx->conn->mux)
6978 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006979 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6980 ctx->conn->mux->wake(ctx->conn);
6981 return NULL;
6982 }
6983 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006984#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6985 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006986 else if (b_data(&ctx->early_buf) && ctx->subs &&
6987 ctx->subs->events & SUB_RETRY_RECV) {
6988 tasklet_wakeup(ctx->subs->tasklet);
6989 ctx->subs->events &= ~SUB_RETRY_RECV;
6990 if (!ctx->subs->events)
6991 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006992 }
6993#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006994 return NULL;
6995}
6996
Emeric Brun46591952012-05-18 15:47:34 +02006997/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006998 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006999 * buffer wraps, in which case a second call may be performed. The connection's
7000 * flags are updated with whatever special event is detected (error, read0,
7001 * empty). The caller is responsible for taking care of those events and
7002 * avoiding the call if inappropriate. The function does not call the
7003 * connection's polling update function, so the caller is responsible for this.
7004 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007005static 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 +02007006{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007007 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02007008 ssize_t ret;
7009 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02007010
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007011 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007012 goto out_error;
7013
Olivier Houchard54907bb2019-12-19 15:02:39 +01007014#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
7015 if (b_data(&ctx->early_buf)) {
7016 try = b_contig_space(buf);
7017 if (try > b_data(&ctx->early_buf))
7018 try = b_data(&ctx->early_buf);
7019 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
7020 b_add(buf, try);
7021 b_del(&ctx->early_buf, try);
7022 if (b_data(&ctx->early_buf) == 0)
7023 b_free(&ctx->early_buf);
7024 return try;
7025 }
7026#endif
7027
Willy Tarreau911db9b2020-01-23 16:27:54 +01007028 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007029 /* a handshake was requested */
7030 return 0;
7031
Emeric Brun46591952012-05-18 15:47:34 +02007032 /* read the largest possible block. For this, we perform only one call
7033 * to recv() unless the buffer wraps and we exactly fill the first hunk,
7034 * in which case we accept to do it once again. A new attempt is made on
7035 * EINTR too.
7036 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01007037 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007038
Willy Tarreau591d4452018-06-15 17:21:00 +02007039 try = b_contig_space(buf);
7040 if (!try)
7041 break;
7042
Willy Tarreauabf08d92014-01-14 11:31:27 +01007043 if (try > count)
7044 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007045
Olivier Houchard66ab4982019-02-26 18:37:15 +01007046 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007047
Emeric Brune1f38db2012-09-03 20:36:47 +02007048 if (conn->flags & CO_FL_ERROR) {
7049 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007050 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007051 }
Emeric Brun46591952012-05-18 15:47:34 +02007052 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007053 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007054 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007055 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007056 }
Emeric Brun46591952012-05-18 15:47:34 +02007057 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007058 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007059 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007060 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007061 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007062 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007063#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007064 /* Async mode can be re-enabled, because we're leaving data state.*/
7065 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007066 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007067#endif
Emeric Brun46591952012-05-18 15:47:34 +02007068 break;
7069 }
7070 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007071 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007072 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7073 SUB_RETRY_RECV,
7074 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007075 /* handshake is running, and it may need to re-enable read */
7076 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007077#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007078 /* Async mode can be re-enabled, because we're leaving data state.*/
7079 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007080 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007081#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007082 break;
7083 }
Emeric Brun46591952012-05-18 15:47:34 +02007084 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007085 } else if (ret == SSL_ERROR_ZERO_RETURN)
7086 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007087 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7088 * stack before shutting down the connection for
7089 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007090 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7091 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007092 /* otherwise it's a real error */
7093 goto out_error;
7094 }
7095 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007096 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007097 return done;
7098
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007099 clear_ssl_error:
7100 /* Clear openssl global errors stack */
7101 ssl_sock_dump_errors(conn);
7102 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007103 read0:
7104 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007105 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007106
Emeric Brun46591952012-05-18 15:47:34 +02007107 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007108 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007109 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007110 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007111 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007112 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007113}
7114
7115
Willy Tarreau787db9a2018-06-14 18:31:46 +02007116/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7117 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7118 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007119 * Only one call to send() is performed, unless the buffer wraps, in which case
7120 * a second call may be performed. The connection's flags are updated with
7121 * whatever special event is detected (error, empty). The caller is responsible
7122 * for taking care of those events and avoiding the call if inappropriate. The
7123 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007124 * is responsible for this. The buffer's output is not adjusted, it's up to the
7125 * caller to take care of this. It's up to the caller to update the buffer's
7126 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007127 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007128static 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 +02007129{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007130 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007131 ssize_t ret;
7132 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007133
7134 done = 0;
7135
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007136 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007137 goto out_error;
7138
Willy Tarreau911db9b2020-01-23 16:27:54 +01007139 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007140 /* a handshake was requested */
7141 return 0;
7142
7143 /* send the largest possible block. For this we perform only one call
7144 * to send() unless the buffer wraps and we exactly fill the first hunk,
7145 * in which case we accept to do it once again.
7146 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007147 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007148#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007149 size_t written_data;
7150#endif
7151
Willy Tarreau787db9a2018-06-14 18:31:46 +02007152 try = b_contig_data(buf, done);
7153 if (try > count)
7154 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007155
Willy Tarreau7bed9452014-02-02 02:00:24 +01007156 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007157 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007158 global_ssl.max_record && try > global_ssl.max_record) {
7159 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007160 }
7161 else {
7162 /* we need to keep the information about the fact that
7163 * we're not limiting the upcoming send(), because if it
7164 * fails, we'll have to retry with at least as many data.
7165 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007166 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007167 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007168
Willy Tarreau5db847a2019-05-09 14:13:35 +02007169#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007170 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007171 unsigned int max_early;
7172
Olivier Houchard522eea72017-11-03 16:27:47 +01007173 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007174 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007175 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007176 if (SSL_get0_session(ctx->ssl))
7177 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007178 else
7179 max_early = 0;
7180 }
7181
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007182 if (try + ctx->sent_early_data > max_early) {
7183 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007184 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007185 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007186 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007187 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007188 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007189 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007190 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007191 if (ret == 1) {
7192 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007193 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007194 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007195 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007196 /* Initiate the handshake, now */
7197 tasklet_wakeup(ctx->wait_event.tasklet);
7198 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007199
Olivier Houchardc2aae742017-09-22 18:26:28 +02007200 }
7201
7202 } else
7203#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007204 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007205
Emeric Brune1f38db2012-09-03 20:36:47 +02007206 if (conn->flags & CO_FL_ERROR) {
7207 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007208 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007209 }
Emeric Brun46591952012-05-18 15:47:34 +02007210 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007211 /* A send succeeded, so we can consider ourself connected */
7212 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007213 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007214 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007215 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007216 }
7217 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007218 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007219
Emeric Brun46591952012-05-18 15:47:34 +02007220 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007221 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007222 /* handshake is running, and it may need to re-enable write */
7223 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007224 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007225#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007226 /* Async mode can be re-enabled, because we're leaving data state.*/
7227 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007228 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007229#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007230 break;
7231 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007232
Emeric Brun46591952012-05-18 15:47:34 +02007233 break;
7234 }
7235 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007236 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007237 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007238 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7239 SUB_RETRY_RECV,
7240 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007241#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007242 /* Async mode can be re-enabled, because we're leaving data state.*/
7243 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007244 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007245#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007246 break;
7247 }
Emeric Brun46591952012-05-18 15:47:34 +02007248 goto out_error;
7249 }
7250 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007251 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007252 return done;
7253
7254 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007255 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007256 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007257 ERR_clear_error();
7258
Emeric Brun46591952012-05-18 15:47:34 +02007259 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007260 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007261}
7262
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007263static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007264
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007265 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007266
Olivier Houchardea8dd942019-05-20 14:02:16 +02007267
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007268 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007269 if (ctx->wait_event.events != 0)
7270 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7271 ctx->wait_event.events,
7272 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007273 if (ctx->subs) {
7274 ctx->subs->events = 0;
7275 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007276 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007277
Olivier Houchard692c1d02019-05-23 18:41:47 +02007278 if (ctx->xprt->close)
7279 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007280#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007281 if (global_ssl.async) {
7282 OSSL_ASYNC_FD all_fd[32], afd;
7283 size_t num_all_fds = 0;
7284 int i;
7285
Olivier Houchard66ab4982019-02-26 18:37:15 +01007286 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007287 if (num_all_fds > 32) {
7288 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7289 return;
7290 }
7291
Olivier Houchard66ab4982019-02-26 18:37:15 +01007292 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007293
7294 /* If an async job is pending, we must try to
7295 to catch the end using polling before calling
7296 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007297 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007298 for (i=0 ; i < num_all_fds ; i++) {
7299 /* switch on an handler designed to
7300 * handle the SSL_free
7301 */
7302 afd = all_fd[i];
7303 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007304 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007305 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007306 /* To ensure that the fd cache won't be used
7307 * and we'll catch a real RD event.
7308 */
7309 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007310 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007311 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007312 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007313 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007314 return;
7315 }
Emeric Brun3854e012017-05-17 20:42:48 +02007316 /* Else we can remove the fds from the fdtab
7317 * and call SSL_free.
7318 * note: we do a fd_remove and not a delete
7319 * because the fd is owned by the engine.
7320 * the engine is responsible to close
7321 */
7322 for (i=0 ; i < num_all_fds ; i++)
7323 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007324 }
7325#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007326 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007327 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007328 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007329 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007330 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007331 }
Emeric Brun46591952012-05-18 15:47:34 +02007332}
7333
7334/* This function tries to perform a clean shutdown on an SSL connection, and in
7335 * any case, flags the connection as reusable if no handshake was in progress.
7336 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007337static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007338{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007339 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007340
Willy Tarreau911db9b2020-01-23 16:27:54 +01007341 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007342 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007343 if (!clean)
7344 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007345 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007346 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007347 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007348 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007349 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007350 ERR_clear_error();
7351 }
Emeric Brun46591952012-05-18 15:47:34 +02007352}
7353
William Lallemandd4f946c2019-12-05 10:26:40 +01007354/* fill a buffer with the algorithm and size of a public key */
7355static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007356{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007357 int bits = 0;
7358 int sig = TLSEXT_signature_anonymous;
7359 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007360 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007361
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007362 pkey = X509_get_pubkey(crt);
7363 if (pkey) {
7364 bits = EVP_PKEY_bits(pkey);
7365 switch(EVP_PKEY_base_id(pkey)) {
7366 case EVP_PKEY_RSA:
7367 sig = TLSEXT_signature_rsa;
7368 break;
7369 case EVP_PKEY_EC:
7370 sig = TLSEXT_signature_ecdsa;
7371 break;
7372 case EVP_PKEY_DSA:
7373 sig = TLSEXT_signature_dsa;
7374 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007375 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007376 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007377 }
7378
7379 switch(sig) {
7380 case TLSEXT_signature_rsa:
7381 len = chunk_printf(out, "RSA%d", bits);
7382 break;
7383 case TLSEXT_signature_ecdsa:
7384 len = chunk_printf(out, "EC%d", bits);
7385 break;
7386 case TLSEXT_signature_dsa:
7387 len = chunk_printf(out, "DSA%d", bits);
7388 break;
7389 default:
7390 return 0;
7391 }
7392 if (len < 0)
7393 return 0;
7394 return 1;
7395}
7396
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007397/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007398int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7399{
7400 struct ssl_sock_ctx *ctx;
7401 X509 *crt;
7402
7403 if (!ssl_sock_is_ssl(conn))
7404 return 0;
7405
7406 ctx = conn->xprt_ctx;
7407
7408 crt = SSL_get_certificate(ctx->ssl);
7409 if (!crt)
7410 return 0;
7411
7412 return cert_get_pkey_algo(crt, out);
7413}
7414
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007415/* used for ppv2 cert signature (can be used for logging) */
7416const char *ssl_sock_get_cert_sig(struct connection *conn)
7417{
Christopher Faulet82004142019-09-10 10:12:03 +02007418 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007419
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007420 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7421 X509 *crt;
7422
7423 if (!ssl_sock_is_ssl(conn))
7424 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007425 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007426 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007427 if (!crt)
7428 return NULL;
7429 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7430 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7431}
7432
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007433/* used for ppv2 authority */
7434const char *ssl_sock_get_sni(struct connection *conn)
7435{
7436#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007437 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007438
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007439 if (!ssl_sock_is_ssl(conn))
7440 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007441 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007442 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007443#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007444 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007445#endif
7446}
7447
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007448/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007449const char *ssl_sock_get_cipher_name(struct connection *conn)
7450{
Christopher Faulet82004142019-09-10 10:12:03 +02007451 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007452
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007453 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007454 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007455 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007456 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007457}
7458
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007459/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007460const char *ssl_sock_get_proto_version(struct connection *conn)
7461{
Christopher Faulet82004142019-09-10 10:12:03 +02007462 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007463
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007464 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007465 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007466 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007467 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007468}
7469
Willy Tarreau8d598402012-10-22 17:58:39 +02007470/* Extract a serial from a cert, and copy it to a chunk.
7471 * Returns 1 if serial is found and copied, 0 if no serial found and
7472 * -1 if output is not large enough.
7473 */
7474static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007475ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007476{
7477 ASN1_INTEGER *serial;
7478
7479 serial = X509_get_serialNumber(crt);
7480 if (!serial)
7481 return 0;
7482
7483 if (out->size < serial->length)
7484 return -1;
7485
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007486 memcpy(out->area, serial->data, serial->length);
7487 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007488 return 1;
7489}
7490
Emeric Brun43e79582014-10-29 19:03:26 +01007491/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007492 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7493 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007494 */
7495static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007496ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007497{
7498 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007499 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007500
7501 len =i2d_X509(crt, NULL);
7502 if (len <= 0)
7503 return 1;
7504
7505 if (out->size < len)
7506 return -1;
7507
7508 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007509 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007510 return 1;
7511}
7512
Emeric Brunce5ad802012-10-22 14:11:22 +02007513
Willy Tarreau83061a82018-07-13 11:56:34 +02007514/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007515 * Returns 1 if serial is found and copied, 0 if no valid time found
7516 * and -1 if output is not large enough.
7517 */
7518static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007519ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007520{
7521 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7522 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7523
7524 if (gentm->length < 12)
7525 return 0;
7526 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7527 return 0;
7528 if (out->size < gentm->length-2)
7529 return -1;
7530
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007531 memcpy(out->area, gentm->data+2, gentm->length-2);
7532 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007533 return 1;
7534 }
7535 else if (tm->type == V_ASN1_UTCTIME) {
7536 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7537
7538 if (utctm->length < 10)
7539 return 0;
7540 if (utctm->data[0] >= 0x35)
7541 return 0;
7542 if (out->size < utctm->length)
7543 return -1;
7544
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007545 memcpy(out->area, utctm->data, utctm->length);
7546 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007547 return 1;
7548 }
7549
7550 return 0;
7551}
7552
Emeric Brun87855892012-10-17 17:39:35 +02007553/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7554 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7555 */
7556static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007557ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7558 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007559{
7560 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007561 ASN1_OBJECT *obj;
7562 ASN1_STRING *data;
7563 const unsigned char *data_ptr;
7564 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007565 int i, j, n;
7566 int cur = 0;
7567 const char *s;
7568 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007569 int name_count;
7570
7571 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007572
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007573 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007574 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007575 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007576 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007577 else
7578 j = i;
7579
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007580 ne = X509_NAME_get_entry(a, j);
7581 obj = X509_NAME_ENTRY_get_object(ne);
7582 data = X509_NAME_ENTRY_get_data(ne);
7583 data_ptr = ASN1_STRING_get0_data(data);
7584 data_len = ASN1_STRING_length(data);
7585 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007586 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007587 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007588 s = tmp;
7589 }
7590
7591 if (chunk_strcasecmp(entry, s) != 0)
7592 continue;
7593
7594 if (pos < 0)
7595 cur--;
7596 else
7597 cur++;
7598
7599 if (cur != pos)
7600 continue;
7601
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007602 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007603 return -1;
7604
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007605 memcpy(out->area, data_ptr, data_len);
7606 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007607 return 1;
7608 }
7609
7610 return 0;
7611
William Lallemandd4f946c2019-12-05 10:26:40 +01007612}
7613
7614/*
7615 * Extract and format the DNS SAN extensions and copy result into a chuink
7616 * Return 0;
7617 */
7618#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7619static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7620{
7621 int i;
7622 char *str;
7623 STACK_OF(GENERAL_NAME) *names = NULL;
7624
7625 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7626 if (names) {
7627 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7628 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7629 if (i > 0)
7630 chunk_appendf(out, ", ");
7631 if (name->type == GEN_DNS) {
7632 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7633 chunk_appendf(out, "DNS:%s", str);
7634 OPENSSL_free(str);
7635 }
7636 }
7637 }
7638 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7639 }
7640 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007641}
William Lallemandd4f946c2019-12-05 10:26:40 +01007642#endif
Emeric Brun87855892012-10-17 17:39:35 +02007643
Elliot Otchet71f82972020-01-15 08:12:14 -05007644/*
7645 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7646 * Currently supports rfc2253 for returning LDAP V3 DNs.
7647 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7648 */
7649static int
7650ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7651{
7652 BIO *bio = NULL;
7653 int ret = 0;
7654 int data_len = 0;
7655
7656 if (chunk_strcmp(format, "rfc2253") == 0) {
7657 bio = BIO_new(BIO_s_mem());
7658 if (bio == NULL)
7659 goto out;
7660
7661 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7662 goto out;
7663
7664 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7665 goto out;
7666
7667 out->data = data_len;
7668
7669 ret = 1;
7670 }
7671out:
7672 if (bio)
7673 BIO_free(bio);
7674 return ret;
7675}
7676
Emeric Brun87855892012-10-17 17:39:35 +02007677/* Extract and format full DN from a X509_NAME and copy result into a chunk
7678 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7679 */
7680static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007681ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007682{
7683 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007684 ASN1_OBJECT *obj;
7685 ASN1_STRING *data;
7686 const unsigned char *data_ptr;
7687 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007688 int i, n, ln;
7689 int l = 0;
7690 const char *s;
7691 char *p;
7692 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007693 int name_count;
7694
7695
7696 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007697
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007698 out->data = 0;
7699 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007700 for (i = 0; i < name_count; i++) {
7701 ne = X509_NAME_get_entry(a, i);
7702 obj = X509_NAME_ENTRY_get_object(ne);
7703 data = X509_NAME_ENTRY_get_data(ne);
7704 data_ptr = ASN1_STRING_get0_data(data);
7705 data_len = ASN1_STRING_length(data);
7706 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007707 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007708 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007709 s = tmp;
7710 }
7711 ln = strlen(s);
7712
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007713 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007714 if (l > out->size)
7715 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007716 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007717
7718 *(p++)='/';
7719 memcpy(p, s, ln);
7720 p += ln;
7721 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007722 memcpy(p, data_ptr, data_len);
7723 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007724 }
7725
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007726 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007727 return 0;
7728
7729 return 1;
7730}
7731
Olivier Houchardab28a322018-12-21 19:45:40 +01007732void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7733{
7734#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007735 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007736
Olivier Houcharde488ea82019-06-28 14:10:33 +02007737 if (!ssl_sock_is_ssl(conn))
7738 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007739 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007740 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007741#endif
7742}
7743
Willy Tarreau119a4082016-12-22 21:58:38 +01007744/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7745 * to disable SNI.
7746 */
Willy Tarreau63076412015-07-10 11:33:32 +02007747void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7748{
7749#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007750 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007751
Willy Tarreau119a4082016-12-22 21:58:38 +01007752 char *prev_name;
7753
Willy Tarreau63076412015-07-10 11:33:32 +02007754 if (!ssl_sock_is_ssl(conn))
7755 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007756 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007757
Willy Tarreau119a4082016-12-22 21:58:38 +01007758 /* if the SNI changes, we must destroy the reusable context so that a
7759 * new connection will present a new SNI. As an optimization we could
7760 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7761 * server.
7762 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007763 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007764 if ((!prev_name && hostname) ||
7765 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007766 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007767
Olivier Houchard66ab4982019-02-26 18:37:15 +01007768 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007769#endif
7770}
7771
Emeric Brun0abf8362014-06-24 18:26:41 +02007772/* Extract peer certificate's common name into the chunk dest
7773 * Returns
7774 * the len of the extracted common name
7775 * or 0 if no CN found in DN
7776 * or -1 on error case (i.e. no peer certificate)
7777 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007778int ssl_sock_get_remote_common_name(struct connection *conn,
7779 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007780{
Christopher Faulet82004142019-09-10 10:12:03 +02007781 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007782 X509 *crt = NULL;
7783 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007784 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007785 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007786 .area = (char *)&find_cn,
7787 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007788 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007789 int result = -1;
David Safb76832014-05-08 23:42:08 -04007790
7791 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007792 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007793 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007794
7795 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007796 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007797 if (!crt)
7798 goto out;
7799
7800 name = X509_get_subject_name(crt);
7801 if (!name)
7802 goto out;
David Safb76832014-05-08 23:42:08 -04007803
Emeric Brun0abf8362014-06-24 18:26:41 +02007804 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7805out:
David Safb76832014-05-08 23:42:08 -04007806 if (crt)
7807 X509_free(crt);
7808
7809 return result;
7810}
7811
Dave McCowan328fb582014-07-30 10:39:13 -04007812/* returns 1 if client passed a certificate for this session, 0 if not */
7813int ssl_sock_get_cert_used_sess(struct connection *conn)
7814{
Christopher Faulet82004142019-09-10 10:12:03 +02007815 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007816 X509 *crt = NULL;
7817
7818 if (!ssl_sock_is_ssl(conn))
7819 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007820 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007821
7822 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007823 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007824 if (!crt)
7825 return 0;
7826
7827 X509_free(crt);
7828 return 1;
7829}
7830
7831/* returns 1 if client passed a certificate for this connection, 0 if not */
7832int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007833{
Christopher Faulet82004142019-09-10 10:12:03 +02007834 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007835
David Safb76832014-05-08 23:42:08 -04007836 if (!ssl_sock_is_ssl(conn))
7837 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007838 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007839 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007840}
7841
7842/* returns result from SSL verify */
7843unsigned int ssl_sock_get_verify_result(struct connection *conn)
7844{
Christopher Faulet82004142019-09-10 10:12:03 +02007845 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007846
David Safb76832014-05-08 23:42:08 -04007847 if (!ssl_sock_is_ssl(conn))
7848 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007849 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007850 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007851}
7852
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007853/* Returns the application layer protocol name in <str> and <len> when known.
7854 * Zero is returned if the protocol name was not found, otherwise non-zero is
7855 * returned. The string is allocated in the SSL context and doesn't have to be
7856 * freed by the caller. NPN is also checked if available since older versions
7857 * of openssl (1.0.1) which are more common in field only support this one.
7858 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007859static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007860{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007861#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7862 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007863 struct ssl_sock_ctx *ctx = xprt_ctx;
7864 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007865 return 0;
7866
7867 *str = NULL;
7868
7869#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007870 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007871 if (*str)
7872 return 1;
7873#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007874#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007875 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007876 if (*str)
7877 return 1;
7878#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007879#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007880 return 0;
7881}
7882
Willy Tarreau7875d092012-09-10 08:20:03 +02007883/***** Below are some sample fetching functions for ACL/patterns *****/
7884
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007885static int
7886smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7887{
7888 struct connection *conn;
7889
7890 conn = objt_conn(smp->sess->origin);
7891 if (!conn || conn->xprt != &ssl_sock)
7892 return 0;
7893
7894 smp->flags = 0;
7895 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007896#ifdef OPENSSL_IS_BORINGSSL
7897 {
7898 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7899 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7900 SSL_early_data_accepted(ctx->ssl));
7901 }
7902#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007903 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007904 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007905#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007906 return 1;
7907}
7908
Emeric Brune64aef12012-09-21 13:15:06 +02007909/* boolean, returns true if client cert was present */
7910static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007911smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007912{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007913 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007914 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007915
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007916 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007917 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007918 return 0;
7919
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007920 ctx = conn->xprt_ctx;
7921
Willy Tarreau911db9b2020-01-23 16:27:54 +01007922 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007923 smp->flags |= SMP_F_MAY_CHANGE;
7924 return 0;
7925 }
7926
7927 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007928 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007929 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007930
7931 return 1;
7932}
7933
Emeric Brun43e79582014-10-29 19:03:26 +01007934/* binary, returns a certificate in a binary chunk (der/raw).
7935 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7936 * should be use.
7937 */
7938static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007939smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007940{
7941 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7942 X509 *crt = NULL;
7943 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007944 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007945 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007946 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007947
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007948 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007949 if (!conn || conn->xprt != &ssl_sock)
7950 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007951 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007952
Willy Tarreau911db9b2020-01-23 16:27:54 +01007953 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007954 smp->flags |= SMP_F_MAY_CHANGE;
7955 return 0;
7956 }
7957
7958 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007959 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007960 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007961 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007962
7963 if (!crt)
7964 goto out;
7965
7966 smp_trash = get_trash_chunk();
7967 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7968 goto out;
7969
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007970 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007971 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007972 ret = 1;
7973out:
7974 /* SSL_get_peer_certificate, it increase X509 * ref count */
7975 if (cert_peer && crt)
7976 X509_free(crt);
7977 return ret;
7978}
7979
Emeric Brunba841a12014-04-30 17:05:08 +02007980/* binary, returns serial of certificate in a binary chunk.
7981 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7982 * should be use.
7983 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007984static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007985smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007986{
Emeric Brunba841a12014-04-30 17:05:08 +02007987 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007988 X509 *crt = NULL;
7989 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007990 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007991 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007992 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007993
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007994 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007995 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007996 return 0;
7997
Olivier Houchard66ab4982019-02-26 18:37:15 +01007998 ctx = conn->xprt_ctx;
7999
Willy Tarreau911db9b2020-01-23 16:27:54 +01008000 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02008001 smp->flags |= SMP_F_MAY_CHANGE;
8002 return 0;
8003 }
8004
Emeric Brunba841a12014-04-30 17:05:08 +02008005 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008006 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008007 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008008 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008009
Willy Tarreau8d598402012-10-22 17:58:39 +02008010 if (!crt)
8011 goto out;
8012
Willy Tarreau47ca5452012-12-23 20:22:19 +01008013 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02008014 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
8015 goto out;
8016
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008017 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008018 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02008019 ret = 1;
8020out:
Emeric Brunba841a12014-04-30 17:05:08 +02008021 /* SSL_get_peer_certificate, it increase X509 * ref count */
8022 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02008023 X509_free(crt);
8024 return ret;
8025}
Emeric Brune64aef12012-09-21 13:15:06 +02008026
Emeric Brunba841a12014-04-30 17:05:08 +02008027/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
8028 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8029 * should be use.
8030 */
James Votha051b4a2013-05-14 20:37:59 +02008031static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008032smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02008033{
Emeric Brunba841a12014-04-30 17:05:08 +02008034 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02008035 X509 *crt = NULL;
8036 const EVP_MD *digest;
8037 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01008038 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008039 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008040 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008041 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02008042
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008043 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008044 if (!conn || conn->xprt != &ssl_sock)
8045 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008046 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008047
Willy Tarreau911db9b2020-01-23 16:27:54 +01008048 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008049 smp->flags |= SMP_F_MAY_CHANGE;
8050 return 0;
8051 }
8052
Emeric Brunba841a12014-04-30 17:05:08 +02008053 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008054 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008055 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008056 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008057 if (!crt)
8058 goto out;
8059
8060 smp_trash = get_trash_chunk();
8061 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008062 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8063 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008064 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008065 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008066 ret = 1;
8067out:
Emeric Brunba841a12014-04-30 17:05:08 +02008068 /* SSL_get_peer_certificate, it increase X509 * ref count */
8069 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008070 X509_free(crt);
8071 return ret;
8072}
8073
Emeric Brunba841a12014-04-30 17:05:08 +02008074/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8075 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8076 * should be use.
8077 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008078static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008079smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008080{
Emeric Brunba841a12014-04-30 17:05:08 +02008081 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008082 X509 *crt = NULL;
8083 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008084 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008085 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008086 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008087
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008088 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008089 if (!conn || conn->xprt != &ssl_sock)
8090 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008091 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008092
Willy Tarreau911db9b2020-01-23 16:27:54 +01008093 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008094 smp->flags |= SMP_F_MAY_CHANGE;
8095 return 0;
8096 }
8097
Emeric Brunba841a12014-04-30 17:05:08 +02008098 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008099 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008100 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008101 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008102 if (!crt)
8103 goto out;
8104
Willy Tarreau47ca5452012-12-23 20:22:19 +01008105 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008106 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008107 goto out;
8108
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008109 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008110 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008111 ret = 1;
8112out:
Emeric Brunba841a12014-04-30 17:05:08 +02008113 /* SSL_get_peer_certificate, it increase X509 * ref count */
8114 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008115 X509_free(crt);
8116 return ret;
8117}
8118
Emeric Brunba841a12014-04-30 17:05:08 +02008119/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8120 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8121 * should be use.
8122 */
Emeric Brun87855892012-10-17 17:39:35 +02008123static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008124smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008125{
Emeric Brunba841a12014-04-30 17:05:08 +02008126 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008127 X509 *crt = NULL;
8128 X509_NAME *name;
8129 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008130 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008131 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008132 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008133
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008134 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008135 if (!conn || conn->xprt != &ssl_sock)
8136 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008137 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008138
Willy Tarreau911db9b2020-01-23 16:27:54 +01008139 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008140 smp->flags |= SMP_F_MAY_CHANGE;
8141 return 0;
8142 }
8143
Emeric Brunba841a12014-04-30 17:05:08 +02008144 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008145 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008146 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008147 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008148 if (!crt)
8149 goto out;
8150
8151 name = X509_get_issuer_name(crt);
8152 if (!name)
8153 goto out;
8154
Willy Tarreau47ca5452012-12-23 20:22:19 +01008155 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008156 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008157 int pos = 1;
8158
8159 if (args[1].type == ARGT_SINT)
8160 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008161
8162 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8163 goto out;
8164 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008165 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8166 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8167 goto out;
8168 }
Emeric Brun87855892012-10-17 17:39:35 +02008169 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8170 goto out;
8171
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008172 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008173 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008174 ret = 1;
8175out:
Emeric Brunba841a12014-04-30 17:05:08 +02008176 /* SSL_get_peer_certificate, it increase X509 * ref count */
8177 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008178 X509_free(crt);
8179 return ret;
8180}
8181
Emeric Brunba841a12014-04-30 17:05:08 +02008182/* string, returns notbefore date in ASN1_UTCTIME format.
8183 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8184 * should be use.
8185 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008186static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008187smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008188{
Emeric Brunba841a12014-04-30 17:05:08 +02008189 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008190 X509 *crt = NULL;
8191 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008192 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008193 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008194 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008195
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008196 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008197 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008198 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008199 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008200
Willy Tarreau911db9b2020-01-23 16:27:54 +01008201 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008202 smp->flags |= SMP_F_MAY_CHANGE;
8203 return 0;
8204 }
8205
Emeric Brunba841a12014-04-30 17:05:08 +02008206 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008207 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008208 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008209 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008210 if (!crt)
8211 goto out;
8212
Willy Tarreau47ca5452012-12-23 20:22:19 +01008213 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008214 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008215 goto out;
8216
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008217 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008218 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008219 ret = 1;
8220out:
Emeric Brunba841a12014-04-30 17:05:08 +02008221 /* SSL_get_peer_certificate, it increase X509 * ref count */
8222 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008223 X509_free(crt);
8224 return ret;
8225}
8226
Emeric Brunba841a12014-04-30 17:05:08 +02008227/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8228 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8229 * should be use.
8230 */
Emeric Brun87855892012-10-17 17:39:35 +02008231static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008232smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008233{
Emeric Brunba841a12014-04-30 17:05:08 +02008234 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008235 X509 *crt = NULL;
8236 X509_NAME *name;
8237 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008238 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008239 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008240 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008241
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008242 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008243 if (!conn || conn->xprt != &ssl_sock)
8244 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008245 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008246
Willy Tarreau911db9b2020-01-23 16:27:54 +01008247 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008248 smp->flags |= SMP_F_MAY_CHANGE;
8249 return 0;
8250 }
8251
Emeric Brunba841a12014-04-30 17:05:08 +02008252 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008253 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008254 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008255 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008256 if (!crt)
8257 goto out;
8258
8259 name = X509_get_subject_name(crt);
8260 if (!name)
8261 goto out;
8262
Willy Tarreau47ca5452012-12-23 20:22:19 +01008263 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008264 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008265 int pos = 1;
8266
8267 if (args[1].type == ARGT_SINT)
8268 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008269
8270 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8271 goto out;
8272 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008273 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8274 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8275 goto out;
8276 }
Emeric Brun87855892012-10-17 17:39:35 +02008277 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8278 goto out;
8279
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008280 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008281 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008282 ret = 1;
8283out:
Emeric Brunba841a12014-04-30 17:05:08 +02008284 /* SSL_get_peer_certificate, it increase X509 * ref count */
8285 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008286 X509_free(crt);
8287 return ret;
8288}
Emeric Brun9143d372012-12-20 15:44:16 +01008289
8290/* integer, returns true if current session use a client certificate */
8291static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008292smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008293{
8294 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008295 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008296 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008297
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008298 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008299 if (!conn || conn->xprt != &ssl_sock)
8300 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008301 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008302
Willy Tarreau911db9b2020-01-23 16:27:54 +01008303 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008304 smp->flags |= SMP_F_MAY_CHANGE;
8305 return 0;
8306 }
8307
8308 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008309 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008310 if (crt) {
8311 X509_free(crt);
8312 }
8313
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008314 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008315 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008316 return 1;
8317}
8318
Emeric Brunba841a12014-04-30 17:05:08 +02008319/* integer, returns the certificate version
8320 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8321 * should be use.
8322 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008323static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008324smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008325{
Emeric Brunba841a12014-04-30 17:05:08 +02008326 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008327 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008328 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008329 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008330
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008331 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008332 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008333 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008334 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008335
Willy Tarreau911db9b2020-01-23 16:27:54 +01008336 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008337 smp->flags |= SMP_F_MAY_CHANGE;
8338 return 0;
8339 }
8340
Emeric Brunba841a12014-04-30 17:05:08 +02008341 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008342 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008343 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008344 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008345 if (!crt)
8346 return 0;
8347
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008348 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008349 /* SSL_get_peer_certificate increase X509 * ref count */
8350 if (cert_peer)
8351 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008352 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008353
8354 return 1;
8355}
8356
Emeric Brunba841a12014-04-30 17:05:08 +02008357/* string, returns the certificate's signature algorithm.
8358 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8359 * should be use.
8360 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008361static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008362smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008363{
Emeric Brunba841a12014-04-30 17:05:08 +02008364 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008365 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008366 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008367 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008368 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008369 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008370
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008371 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008372 if (!conn || conn->xprt != &ssl_sock)
8373 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008374 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008375
Willy Tarreau911db9b2020-01-23 16:27:54 +01008376 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008377 smp->flags |= SMP_F_MAY_CHANGE;
8378 return 0;
8379 }
8380
Emeric Brunba841a12014-04-30 17:05:08 +02008381 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008382 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008383 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008384 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008385 if (!crt)
8386 return 0;
8387
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008388 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8389 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008390
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008391 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8392 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008393 /* SSL_get_peer_certificate increase X509 * ref count */
8394 if (cert_peer)
8395 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008396 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008397 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008398
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008399 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008400 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008401 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008402 /* SSL_get_peer_certificate increase X509 * ref count */
8403 if (cert_peer)
8404 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008405
8406 return 1;
8407}
8408
Emeric Brunba841a12014-04-30 17:05:08 +02008409/* string, returns the certificate's key algorithm.
8410 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8411 * should be use.
8412 */
Emeric Brun521a0112012-10-22 12:22:55 +02008413static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008414smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008415{
Emeric Brunba841a12014-04-30 17:05:08 +02008416 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008417 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008418 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008419 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008420 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008421 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008422
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008423 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008424 if (!conn || conn->xprt != &ssl_sock)
8425 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008426 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008427
Willy Tarreau911db9b2020-01-23 16:27:54 +01008428 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008429 smp->flags |= SMP_F_MAY_CHANGE;
8430 return 0;
8431 }
8432
Emeric Brunba841a12014-04-30 17:05:08 +02008433 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008434 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008435 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008436 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008437 if (!crt)
8438 return 0;
8439
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008440 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8441 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008442
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008443 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8444 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008445 /* SSL_get_peer_certificate increase X509 * ref count */
8446 if (cert_peer)
8447 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008448 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008449 }
Emeric Brun521a0112012-10-22 12:22:55 +02008450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008451 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008452 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008453 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008454 if (cert_peer)
8455 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008456
8457 return 1;
8458}
8459
Emeric Brun645ae792014-04-30 14:21:06 +02008460/* boolean, returns true if front conn. transport layer is SSL.
8461 * This function is also usable on backend conn if the fetch keyword 5th
8462 * char is 'b'.
8463 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008464static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008465smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008466{
Emeric Bruneb8def92018-02-19 15:59:48 +01008467 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8468 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008469
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008470 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008471 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008472 return 1;
8473}
8474
Emeric Brun2525b6b2012-10-18 15:59:43 +02008475/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008476static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008477smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008478{
8479#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008480 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008481 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008482
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008483 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008484 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008485 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008486 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008487 return 1;
8488#else
8489 return 0;
8490#endif
8491}
8492
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008493/* boolean, returns true if client session has been resumed.
8494 * This function is also usable on backend conn if the fetch keyword 5th
8495 * char is 'b'.
8496 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008497static int
8498smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8499{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008500 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8501 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008502 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008503
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008504
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008505 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008506 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008507 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008508 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008509 return 1;
8510}
8511
Emeric Brun645ae792014-04-30 14:21:06 +02008512/* string, returns the used cipher if front conn. transport layer is SSL.
8513 * This function is also usable on backend conn if the fetch keyword 5th
8514 * char is 'b'.
8515 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008516static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008517smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008518{
Emeric Bruneb8def92018-02-19 15:59:48 +01008519 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8520 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008521 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008522
Willy Tarreaube508f12016-03-10 11:47:01 +01008523 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008524 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008525 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008526 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008527
Olivier Houchard66ab4982019-02-26 18:37:15 +01008528 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008529 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008530 return 0;
8531
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008532 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008533 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008534 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008535
8536 return 1;
8537}
8538
Emeric Brun645ae792014-04-30 14:21:06 +02008539/* integer, returns the algoritm's keysize if front conn. transport layer
8540 * is SSL.
8541 * This function is also usable on backend conn if the fetch keyword 5th
8542 * char is 'b'.
8543 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008544static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008545smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008546{
Emeric Bruneb8def92018-02-19 15:59:48 +01008547 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8548 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008549 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008550 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008551
Emeric Brun589fcad2012-10-16 14:13:26 +02008552 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008553 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008554 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008555 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008556
Olivier Houchard66ab4982019-02-26 18:37:15 +01008557 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008558 return 0;
8559
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008560 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008561 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008562
8563 return 1;
8564}
8565
Emeric Brun645ae792014-04-30 14:21:06 +02008566/* integer, returns the used keysize if front conn. transport layer is SSL.
8567 * This function is also usable on backend conn if the fetch keyword 5th
8568 * char is 'b'.
8569 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008570static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008571smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008572{
Emeric Bruneb8def92018-02-19 15:59:48 +01008573 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8574 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008575 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008576
Emeric Brun589fcad2012-10-16 14:13:26 +02008577 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008578 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8579 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008580 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008581
Olivier Houchard66ab4982019-02-26 18:37:15 +01008582 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008583 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008584 return 0;
8585
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008586 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008587
8588 return 1;
8589}
8590
Bernard Spil13c53f82018-02-15 13:34:58 +01008591#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008592static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008593smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008594{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008595 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008596 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008597 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008598
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008599 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008600 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008601
Olivier Houchard6b77f492018-11-22 18:18:29 +01008602 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8603 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008604 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8605 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008606 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008607
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008608 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008609 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008610 (const unsigned char **)&smp->data.u.str.area,
8611 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008612
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008613 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008614 return 0;
8615
Willy Tarreau105599c2020-02-25 08:59:23 +01008616 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008617 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008618}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008619#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008620
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008621#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008622static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008623smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008624{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008625 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008626 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008627 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008628
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008629 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008630 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008631
Olivier Houchard6b77f492018-11-22 18:18:29 +01008632 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8633 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8634
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008635 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008636 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008637 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008638
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008639 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008640 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008641 (const unsigned char **)&smp->data.u.str.area,
8642 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008643
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008644 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008645 return 0;
8646
Willy Tarreau105599c2020-02-25 08:59:23 +01008647 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008648 return 1;
8649}
8650#endif
8651
Emeric Brun645ae792014-04-30 14:21:06 +02008652/* string, returns the used protocol if front conn. transport layer is SSL.
8653 * This function is also usable on backend conn if the fetch keyword 5th
8654 * char is 'b'.
8655 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008656static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008657smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008658{
Emeric Bruneb8def92018-02-19 15:59:48 +01008659 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8660 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008661 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008662
Emeric Brun589fcad2012-10-16 14:13:26 +02008663 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008664 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8665 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008666 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008667
Olivier Houchard66ab4982019-02-26 18:37:15 +01008668 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008669 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008670 return 0;
8671
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008672 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008673 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008674 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008675
8676 return 1;
8677}
8678
Willy Tarreau87b09662015-04-03 00:22:06 +02008679/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008680 * This function is also usable on backend conn if the fetch keyword 5th
8681 * char is 'b'.
8682 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008683#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008684static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008685smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008686{
Emeric Bruneb8def92018-02-19 15:59:48 +01008687 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8688 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008689 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008690 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008691 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008693 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008694 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008695
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008696 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8697 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008698 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008699
Olivier Houchard66ab4982019-02-26 18:37:15 +01008700 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008701 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008702 return 0;
8703
Willy Tarreau105599c2020-02-25 08:59:23 +01008704 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008705 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008706 return 0;
8707
Willy Tarreau105599c2020-02-25 08:59:23 +01008708 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008709 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008710}
Patrick Hemmer41966772018-04-28 19:15:48 -04008711#endif
8712
Emeric Brunfe68f682012-10-16 14:59:28 +02008713
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008714#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008715static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008716smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8717{
8718 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8719 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8720 struct buffer *data;
8721 struct ssl_sock_ctx *ctx;
8722
8723 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8724 return 0;
8725 ctx = conn->xprt_ctx;
8726
8727 data = get_trash_chunk();
8728 if (kw[7] == 'c')
8729 data->data = SSL_get_client_random(ctx->ssl,
8730 (unsigned char *) data->area,
8731 data->size);
8732 else
8733 data->data = SSL_get_server_random(ctx->ssl,
8734 (unsigned char *) data->area,
8735 data->size);
8736 if (!data->data)
8737 return 0;
8738
8739 smp->flags = 0;
8740 smp->data.type = SMP_T_BIN;
8741 smp->data.u.str = *data;
8742
8743 return 1;
8744}
8745
8746static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008747smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8748{
8749 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8750 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8751 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008752 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008753 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008754
8755 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8756 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008757 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008758
Olivier Houchard66ab4982019-02-26 18:37:15 +01008759 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008760 if (!ssl_sess)
8761 return 0;
8762
8763 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008764 data->data = SSL_SESSION_get_master_key(ssl_sess,
8765 (unsigned char *) data->area,
8766 data->size);
8767 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008768 return 0;
8769
8770 smp->flags = 0;
8771 smp->data.type = SMP_T_BIN;
8772 smp->data.u.str = *data;
8773
8774 return 1;
8775}
8776#endif
8777
Patrick Hemmer41966772018-04-28 19:15:48 -04008778#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008779static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008780smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008781{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008782 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008783 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008784
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008785 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008786 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008787
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008788 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008789 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8790 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008791 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008792
Olivier Houchard66ab4982019-02-26 18:37:15 +01008793 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008794 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008795 return 0;
8796
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008797 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008798 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008799}
Patrick Hemmer41966772018-04-28 19:15:48 -04008800#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008801
David Sc1ad52e2014-04-08 18:48:47 -04008802static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008803smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8804{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008805 struct connection *conn;
8806 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008807 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008808
8809 conn = objt_conn(smp->sess->origin);
8810 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8811 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008812 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008813
Olivier Houchard66ab4982019-02-26 18:37:15 +01008814 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008815 if (!capture)
8816 return 0;
8817
8818 smp->flags = SMP_F_CONST;
8819 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008820 smp->data.u.str.area = capture->ciphersuite;
8821 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008822 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008823}
8824
8825static int
8826smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8827{
Willy Tarreau83061a82018-07-13 11:56:34 +02008828 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008829
8830 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8831 return 0;
8832
8833 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008834 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008835 smp->data.type = SMP_T_BIN;
8836 smp->data.u.str = *data;
8837 return 1;
8838}
8839
8840static int
8841smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8842{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008843 struct connection *conn;
8844 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008845 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008846
8847 conn = objt_conn(smp->sess->origin);
8848 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8849 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008850 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008851
Olivier Houchard66ab4982019-02-26 18:37:15 +01008852 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008853 if (!capture)
8854 return 0;
8855
8856 smp->data.type = SMP_T_SINT;
8857 smp->data.u.sint = capture->xxh64;
8858 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008859}
8860
8861static int
8862smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8863{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008864#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008865 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008866 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008867
8868 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8869 return 0;
8870
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008871 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008872 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008873 const char *str;
8874 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008875 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008876 uint16_t id = (bin[0] << 8) | bin[1];
8877#if defined(OPENSSL_IS_BORINGSSL)
8878 cipher = SSL_get_cipher_by_value(id);
8879#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008880 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008881 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8882 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008883#endif
8884 str = SSL_CIPHER_get_name(cipher);
8885 if (!str || strcmp(str, "(NONE)") == 0)
8886 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008887 else
8888 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8889 }
8890 smp->data.type = SMP_T_STR;
8891 smp->data.u.str = *data;
8892 return 1;
8893#else
8894 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8895#endif
8896}
8897
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008898#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008899static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008900smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008901{
Emeric Bruneb8def92018-02-19 15:59:48 +01008902 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8903 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008904 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008905 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008906 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008907
8908 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008909 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8910 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008911 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008912
Willy Tarreau911db9b2020-01-23 16:27:54 +01008913 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008914 smp->flags |= SMP_F_MAY_CHANGE;
8915 return 0;
8916 }
8917
8918 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008919 if (!SSL_session_reused(ctx->ssl))
8920 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008921 finished_trash->area,
8922 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008923 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008924 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008925 finished_trash->area,
8926 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008927
8928 if (!finished_len)
8929 return 0;
8930
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008931 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008932 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008933 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008934
8935 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008936}
Patrick Hemmer41966772018-04-28 19:15:48 -04008937#endif
David Sc1ad52e2014-04-08 18:48:47 -04008938
Emeric Brun2525b6b2012-10-18 15:59:43 +02008939/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008940static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008941smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008942{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008943 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008944 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008945
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008946 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008947 if (!conn || conn->xprt != &ssl_sock)
8948 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008949 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008950
Willy Tarreau911db9b2020-01-23 16:27:54 +01008951 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008952 smp->flags = SMP_F_MAY_CHANGE;
8953 return 0;
8954 }
8955
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008956 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008957 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008958 smp->flags = 0;
8959
8960 return 1;
8961}
8962
Emeric Brun2525b6b2012-10-18 15:59:43 +02008963/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008964static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008965smp_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 +02008966{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008967 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008968 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008969
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008970 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008971 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008972 return 0;
8973
Willy Tarreau911db9b2020-01-23 16:27:54 +01008974 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008975 smp->flags = SMP_F_MAY_CHANGE;
8976 return 0;
8977 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008978 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008979
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008980 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008981 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008982 smp->flags = 0;
8983
8984 return 1;
8985}
8986
Emeric Brun2525b6b2012-10-18 15:59:43 +02008987/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008989smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008990{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008991 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008992 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008993
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008994 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008995 if (!conn || conn->xprt != &ssl_sock)
8996 return 0;
8997
Willy Tarreau911db9b2020-01-23 16:27:54 +01008998 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008999 smp->flags = SMP_F_MAY_CHANGE;
9000 return 0;
9001 }
9002
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009003 ctx = conn->xprt_ctx;
9004
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009005 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009006 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009007 smp->flags = 0;
9008
9009 return 1;
9010}
9011
Emeric Brun2525b6b2012-10-18 15:59:43 +02009012/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009013static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009014smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009015{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009016 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009017 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009018
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009019 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009020 if (!conn || conn->xprt != &ssl_sock)
9021 return 0;
9022
Willy Tarreau911db9b2020-01-23 16:27:54 +01009023 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009024 smp->flags = SMP_F_MAY_CHANGE;
9025 return 0;
9026 }
9027
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009028 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009029 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009030 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009031
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009032 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009033 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009034 smp->flags = 0;
9035
9036 return 1;
9037}
9038
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009039/* for ca-file and ca-verify-file */
9040static 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 +02009041{
9042 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009043 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009044 return ERR_ALERT | ERR_FATAL;
9045 }
9046
Willy Tarreauef934602016-12-22 23:12:01 +01009047 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009048 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009049 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009050 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009051
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009052 if (!ssl_store_load_locations_file(*ca_file_p)) {
9053 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009054 return ERR_ALERT | ERR_FATAL;
9055 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009056 return 0;
9057}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009058
9059/* parse the "ca-file" bind keyword */
9060static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9061{
9062 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9063}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009064static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9065{
9066 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9067}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009068
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009069/* parse the "ca-verify-file" bind keyword */
9070static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9071{
9072 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9073}
9074static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9075{
9076 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9077}
9078
Christopher Faulet31af49d2015-06-09 17:29:50 +02009079/* parse the "ca-sign-file" bind keyword */
9080static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9081{
9082 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009083 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009084 return ERR_ALERT | ERR_FATAL;
9085 }
9086
Willy Tarreauef934602016-12-22 23:12:01 +01009087 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9088 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009089 else
9090 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9091
9092 return 0;
9093}
9094
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009095/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009096static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9097{
9098 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009099 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009100 return ERR_ALERT | ERR_FATAL;
9101 }
9102 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9103 return 0;
9104}
9105
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009106/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009107static 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 +02009108{
9109 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009110 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009111 return ERR_ALERT | ERR_FATAL;
9112 }
9113
Emeric Brun76d88952012-10-05 15:47:31 +02009114 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009115 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009116 return 0;
9117}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009118static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9119{
9120 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9121}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009122
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009123#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009124/* parse the "ciphersuites" bind keyword */
9125static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9126{
9127 if (!*args[cur_arg + 1]) {
9128 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9129 return ERR_ALERT | ERR_FATAL;
9130 }
9131
9132 free(conf->ciphersuites);
9133 conf->ciphersuites = strdup(args[cur_arg + 1]);
9134 return 0;
9135}
9136static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9137{
9138 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9139}
9140#endif
9141
Willy Tarreaubbc91962019-10-16 16:42:19 +02009142/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009143static 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 +02009144{
Willy Tarreau38011032013-08-13 16:59:39 +02009145 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009146
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009147 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009148 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009149 return ERR_ALERT | ERR_FATAL;
9150 }
9151
Willy Tarreauef934602016-12-22 23:12:01 +01009152 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9153 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009154 memprintf(err, "'%s' : path too long", args[cur_arg]);
9155 return ERR_ALERT | ERR_FATAL;
9156 }
Willy Tarreauef934602016-12-22 23:12:01 +01009157 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009158 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009159 }
9160
Willy Tarreaubbc91962019-10-16 16:42:19 +02009161 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009162}
9163
Willy Tarreaubbc91962019-10-16 16:42:19 +02009164/* 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 +01009165static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9166{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009167 int err_code;
9168
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009169 if (!*args[cur_arg + 1]) {
9170 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9171 return ERR_ALERT | ERR_FATAL;
9172 }
9173
William Lallemand6be66ec2020-03-06 22:26:32 +01009174 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009175 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009176 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009177
Willy Tarreaubbc91962019-10-16 16:42:19 +02009178 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009179}
9180
Emeric Brunfb510ea2012-10-05 12:00:26 +02009181/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009182static 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 +02009183{
Emeric Brun051cdab2012-10-02 19:25:50 +02009184#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009185 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009186 return ERR_ALERT | ERR_FATAL;
9187#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009188 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009189 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009190 return ERR_ALERT | ERR_FATAL;
9191 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009192
Willy Tarreauef934602016-12-22 23:12:01 +01009193 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9194 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009195 else
9196 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009197
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009198 if (!ssl_store_load_locations_file(conf->crl_file)) {
9199 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9200 return ERR_ALERT | ERR_FATAL;
9201 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009202 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009203#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009204}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009205static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9206{
9207 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9208}
Emeric Brun2b58d042012-09-20 17:10:03 +02009209
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009210/* parse the "curves" bind keyword keyword */
9211static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9212{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009213#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009214 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009215 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009216 return ERR_ALERT | ERR_FATAL;
9217 }
9218 conf->curves = strdup(args[cur_arg + 1]);
9219 return 0;
9220#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009221 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009222 return ERR_ALERT | ERR_FATAL;
9223#endif
9224}
9225static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9226{
9227 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9228}
9229
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009230/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009231static 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 +02009232{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009233#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009234 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009235 return ERR_ALERT | ERR_FATAL;
9236#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009237 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 +02009238 return ERR_ALERT | ERR_FATAL;
9239#else
9240 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009241 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009242 return ERR_ALERT | ERR_FATAL;
9243 }
9244
9245 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009246
9247 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009248#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009249}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009250static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9251{
9252 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9253}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009254
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009255/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009256static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9257{
9258 int code;
9259 char *p = args[cur_arg + 1];
9260 unsigned long long *ignerr = &conf->crt_ignerr;
9261
9262 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009263 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009264 return ERR_ALERT | ERR_FATAL;
9265 }
9266
9267 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9268 ignerr = &conf->ca_ignerr;
9269
9270 if (strcmp(p, "all") == 0) {
9271 *ignerr = ~0ULL;
9272 return 0;
9273 }
9274
9275 while (p) {
9276 code = atoi(p);
9277 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009278 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9279 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009280 return ERR_ALERT | ERR_FATAL;
9281 }
9282 *ignerr |= 1ULL << code;
9283 p = strchr(p, ',');
9284 if (p)
9285 p++;
9286 }
9287
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009288 return 0;
9289}
9290
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009291/* parse tls_method_options "no-xxx" and "force-xxx" */
9292static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009293{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009294 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009295 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009296 p = strchr(arg, '-');
9297 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009298 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009299 p++;
9300 if (!strcmp(p, "sslv3"))
9301 v = CONF_SSLV3;
9302 else if (!strcmp(p, "tlsv10"))
9303 v = CONF_TLSV10;
9304 else if (!strcmp(p, "tlsv11"))
9305 v = CONF_TLSV11;
9306 else if (!strcmp(p, "tlsv12"))
9307 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009308 else if (!strcmp(p, "tlsv13"))
9309 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009310 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009311 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009312 if (!strncmp(arg, "no-", 3))
9313 methods->flags |= methodVersions[v].flag;
9314 else if (!strncmp(arg, "force-", 6))
9315 methods->min = methods->max = v;
9316 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009317 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009318 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009319 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009320 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009321 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009322}
9323
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009324static 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 +02009325{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009326 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009327}
9328
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009329static 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 +02009330{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009331 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9332}
9333
9334/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9335static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9336{
9337 uint16_t i, v = 0;
9338 char *argv = args[cur_arg + 1];
9339 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009340 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009341 return ERR_ALERT | ERR_FATAL;
9342 }
9343 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9344 if (!strcmp(argv, methodVersions[i].name))
9345 v = i;
9346 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009347 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009348 return ERR_ALERT | ERR_FATAL;
9349 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009350 if (!strcmp("ssl-min-ver", args[cur_arg]))
9351 methods->min = v;
9352 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9353 methods->max = v;
9354 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009355 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009356 return ERR_ALERT | ERR_FATAL;
9357 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009358 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009359}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009360
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009361static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9362{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009363#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009364 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 +02009365#endif
9366 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9367}
9368
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009369static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9370{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009371 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009372}
9373
9374static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9375{
9376 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9377}
9378
Emeric Brun2d0c4822012-10-02 13:45:20 +02009379/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009380static 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 +02009381{
Emeric Brun89675492012-10-05 13:48:26 +02009382 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009383 return 0;
9384}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009385
Olivier Houchardc2aae742017-09-22 18:26:28 +02009386/* parse the "allow-0rtt" bind keyword */
9387static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9388{
9389 conf->early_data = 1;
9390 return 0;
9391}
9392
9393static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9394{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009395 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009396 return 0;
9397}
9398
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009399/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009400static 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 +02009401{
Bernard Spil13c53f82018-02-15 13:34:58 +01009402#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009403 char *p1, *p2;
9404
9405 if (!*args[cur_arg + 1]) {
9406 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9407 return ERR_ALERT | ERR_FATAL;
9408 }
9409
9410 free(conf->npn_str);
9411
Willy Tarreau3724da12016-02-12 17:11:12 +01009412 /* the NPN string is built as a suite of (<len> <name>)*,
9413 * so we reuse each comma to store the next <len> and need
9414 * one more for the end of the string.
9415 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009416 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009417 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009418 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9419
9420 /* replace commas with the name length */
9421 p1 = conf->npn_str;
9422 p2 = p1 + 1;
9423 while (1) {
9424 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9425 if (!p2)
9426 p2 = p1 + 1 + strlen(p1 + 1);
9427
9428 if (p2 - (p1 + 1) > 255) {
9429 *p2 = '\0';
9430 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9431 return ERR_ALERT | ERR_FATAL;
9432 }
9433
9434 *p1 = p2 - (p1 + 1);
9435 p1 = p2;
9436
9437 if (!*p2)
9438 break;
9439
9440 *(p2++) = '\0';
9441 }
9442 return 0;
9443#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009444 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009445 return ERR_ALERT | ERR_FATAL;
9446#endif
9447}
9448
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009449static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9450{
9451 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9452}
9453
Willy Tarreauab861d32013-04-02 02:30:41 +02009454/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009455static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Willy Tarreauab861d32013-04-02 02:30:41 +02009456{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009457#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009458 char *p1, *p2;
9459
9460 if (!*args[cur_arg + 1]) {
9461 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9462 return ERR_ALERT | ERR_FATAL;
9463 }
9464
9465 free(conf->alpn_str);
9466
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009467 /* the ALPN string is built as a suite of (<len> <name>)*,
9468 * so we reuse each comma to store the next <len> and need
9469 * one more for the end of the string.
9470 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009471 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009472 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009473 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9474
9475 /* replace commas with the name length */
9476 p1 = conf->alpn_str;
9477 p2 = p1 + 1;
9478 while (1) {
9479 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9480 if (!p2)
9481 p2 = p1 + 1 + strlen(p1 + 1);
9482
9483 if (p2 - (p1 + 1) > 255) {
9484 *p2 = '\0';
9485 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9486 return ERR_ALERT | ERR_FATAL;
9487 }
9488
9489 *p1 = p2 - (p1 + 1);
9490 p1 = p2;
9491
9492 if (!*p2)
9493 break;
9494
9495 *(p2++) = '\0';
9496 }
9497 return 0;
9498#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009499 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009500 return ERR_ALERT | ERR_FATAL;
9501#endif
9502}
9503
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009504static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9505{
9506 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9507}
9508
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009509/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009510static 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 +02009511{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009512 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009513 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009514
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009515 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9516 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009517#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009518 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9519 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9520#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009521 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009522 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9523 if (!conf->ssl_conf.ssl_methods.min)
9524 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9525 if (!conf->ssl_conf.ssl_methods.max)
9526 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009527
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009528 return 0;
9529}
9530
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009531/* parse the "prefer-client-ciphers" bind keyword */
9532static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9533{
9534 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9535 return 0;
9536}
9537
Christopher Faulet31af49d2015-06-09 17:29:50 +02009538/* parse the "generate-certificates" bind keyword */
9539static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9540{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009541#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009542 conf->generate_certs = 1;
9543#else
9544 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9545 err && *err ? *err : "");
9546#endif
9547 return 0;
9548}
9549
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009550/* parse the "strict-sni" bind keyword */
9551static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9552{
9553 conf->strict_sni = 1;
9554 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009555}
9556
9557/* parse the "tls-ticket-keys" bind keyword */
9558static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9559{
9560#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009561 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009562 int i = 0;
9563 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009564 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009565
9566 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009567 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009568 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009569 }
9570
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009571 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009572 if (keys_ref) {
9573 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009574 conf->keys_ref = keys_ref;
9575 return 0;
9576 }
9577
Christopher Faulete566f3d2019-10-21 09:55:49 +02009578 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009579 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009580 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009581 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009582 }
9583
Emeric Brun9e754772019-01-10 17:51:55 +01009584 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009585 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009586 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009587 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009588 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009589
9590 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009591 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009592 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009593 }
9594
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009595 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009596 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009597 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009598 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009599 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009600
Emeric Brun9e754772019-01-10 17:51:55 +01009601 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009602 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9603 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009604 int dec_size;
9605
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009606 /* Strip newline characters from the end */
9607 if(thisline[len - 1] == '\n')
9608 thisline[--len] = 0;
9609
9610 if(thisline[len - 1] == '\r')
9611 thisline[--len] = 0;
9612
Emeric Brun9e754772019-01-10 17:51:55 +01009613 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9614 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009615 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009616 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009617 }
Emeric Brun9e754772019-01-10 17:51:55 +01009618 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9619 keys_ref->key_size_bits = 128;
9620 }
9621 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9622 keys_ref->key_size_bits = 256;
9623 }
9624 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9625 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9626 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009627 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009628 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009629 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009630 i++;
9631 }
9632
9633 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009634 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 +02009635 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009636 }
9637
9638 fclose(f);
9639
9640 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009641 i -= 2;
9642 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009643 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009644 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009645 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009646 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009647
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009648 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9649
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009650 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009651
9652 fail:
9653 if (f)
9654 fclose(f);
9655 if (keys_ref) {
9656 free(keys_ref->filename);
9657 free(keys_ref->tlskeys);
9658 free(keys_ref);
9659 }
9660 return ERR_ALERT | ERR_FATAL;
9661
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009662#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009663 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009664 return ERR_ALERT | ERR_FATAL;
9665#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009666}
9667
Emeric Brund94b3fe2012-09-20 18:23:56 +02009668/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009669static 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 +02009670{
9671 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009672 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009673 return ERR_ALERT | ERR_FATAL;
9674 }
9675
9676 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009677 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009678 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009679 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009680 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009681 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009682 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009683 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9684 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009685 return ERR_ALERT | ERR_FATAL;
9686 }
9687
9688 return 0;
9689}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009690static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9691{
9692 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9693}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009694
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009695/* parse the "no-ca-names" bind keyword */
9696static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9697{
9698 conf->no_ca_names = 1;
9699 return 0;
9700}
9701static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9702{
9703 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9704}
9705
Willy Tarreau92faadf2012-10-10 23:04:25 +02009706/************** "server" keywords ****************/
9707
Olivier Houchardc7566002018-11-20 23:33:50 +01009708/* parse the "npn" bind keyword */
9709static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9710{
9711#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9712 char *p1, *p2;
9713
9714 if (!*args[*cur_arg + 1]) {
9715 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9716 return ERR_ALERT | ERR_FATAL;
9717 }
9718
9719 free(newsrv->ssl_ctx.npn_str);
9720
9721 /* the NPN string is built as a suite of (<len> <name>)*,
9722 * so we reuse each comma to store the next <len> and need
9723 * one more for the end of the string.
9724 */
9725 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9726 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9727 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9728 newsrv->ssl_ctx.npn_len);
9729
9730 /* replace commas with the name length */
9731 p1 = newsrv->ssl_ctx.npn_str;
9732 p2 = p1 + 1;
9733 while (1) {
9734 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9735 newsrv->ssl_ctx.npn_len - (p1 + 1));
9736 if (!p2)
9737 p2 = p1 + 1 + strlen(p1 + 1);
9738
9739 if (p2 - (p1 + 1) > 255) {
9740 *p2 = '\0';
9741 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9742 return ERR_ALERT | ERR_FATAL;
9743 }
9744
9745 *p1 = p2 - (p1 + 1);
9746 p1 = p2;
9747
9748 if (!*p2)
9749 break;
9750
9751 *(p2++) = '\0';
9752 }
9753 return 0;
9754#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009755 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009756 return ERR_ALERT | ERR_FATAL;
9757#endif
9758}
9759
Olivier Houchard92150142018-12-21 19:47:01 +01009760/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009761static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9762{
9763#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9764 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009765 char **alpn_str;
9766 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009767
Olivier Houchard92150142018-12-21 19:47:01 +01009768 if (*args[*cur_arg] == 'c') {
9769 alpn_str = &newsrv->check.alpn_str;
9770 alpn_len = &newsrv->check.alpn_len;
9771 } else {
9772 alpn_str = &newsrv->ssl_ctx.alpn_str;
9773 alpn_len = &newsrv->ssl_ctx.alpn_len;
9774
9775 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009776 if (!*args[*cur_arg + 1]) {
9777 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9778 return ERR_ALERT | ERR_FATAL;
9779 }
9780
Olivier Houchard92150142018-12-21 19:47:01 +01009781 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009782
9783 /* the ALPN string is built as a suite of (<len> <name>)*,
9784 * so we reuse each comma to store the next <len> and need
9785 * one more for the end of the string.
9786 */
Olivier Houchard92150142018-12-21 19:47:01 +01009787 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9788 *alpn_str = calloc(1, *alpn_len + 1);
9789 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009790
9791 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009792 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009793 p2 = p1 + 1;
9794 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009795 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009796 if (!p2)
9797 p2 = p1 + 1 + strlen(p1 + 1);
9798
9799 if (p2 - (p1 + 1) > 255) {
9800 *p2 = '\0';
9801 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9802 return ERR_ALERT | ERR_FATAL;
9803 }
9804
9805 *p1 = p2 - (p1 + 1);
9806 p1 = p2;
9807
9808 if (!*p2)
9809 break;
9810
9811 *(p2++) = '\0';
9812 }
9813 return 0;
9814#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009815 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009816 return ERR_ALERT | ERR_FATAL;
9817#endif
9818}
9819
Emeric Brunef42d922012-10-11 16:11:36 +02009820/* parse the "ca-file" server keyword */
9821static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9822{
9823 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009824 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009825 return ERR_ALERT | ERR_FATAL;
9826 }
9827
Willy Tarreauef934602016-12-22 23:12:01 +01009828 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9829 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009830 else
9831 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9832
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009833 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9834 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9835 return ERR_ALERT | ERR_FATAL;
9836 }
Emeric Brunef42d922012-10-11 16:11:36 +02009837 return 0;
9838}
9839
Olivier Houchard9130a962017-10-17 17:33:43 +02009840/* parse the "check-sni" server keyword */
9841static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9842{
9843 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009844 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009845 return ERR_ALERT | ERR_FATAL;
9846 }
9847
9848 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9849 if (!newsrv->check.sni) {
9850 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9851 return ERR_ALERT | ERR_FATAL;
9852 }
9853 return 0;
9854
9855}
9856
Willy Tarreau92faadf2012-10-10 23:04:25 +02009857/* parse the "check-ssl" server keyword */
9858static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9859{
9860 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009861 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9862 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009863#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009864 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9865 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9866#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009867 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009868 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9869 if (!newsrv->ssl_ctx.methods.min)
9870 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9871 if (!newsrv->ssl_ctx.methods.max)
9872 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9873
Willy Tarreau92faadf2012-10-10 23:04:25 +02009874 return 0;
9875}
9876
9877/* parse the "ciphers" server keyword */
9878static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9879{
9880 if (!*args[*cur_arg + 1]) {
9881 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9882 return ERR_ALERT | ERR_FATAL;
9883 }
9884
9885 free(newsrv->ssl_ctx.ciphers);
9886 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9887 return 0;
9888}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009889
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009890#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009891/* parse the "ciphersuites" server keyword */
9892static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9893{
9894 if (!*args[*cur_arg + 1]) {
9895 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9896 return ERR_ALERT | ERR_FATAL;
9897 }
9898
9899 free(newsrv->ssl_ctx.ciphersuites);
9900 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9901 return 0;
9902}
9903#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009904
Emeric Brunef42d922012-10-11 16:11:36 +02009905/* parse the "crl-file" server keyword */
9906static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9907{
9908#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009909 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009910 return ERR_ALERT | ERR_FATAL;
9911#else
9912 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009913 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009914 return ERR_ALERT | ERR_FATAL;
9915 }
9916
Willy Tarreauef934602016-12-22 23:12:01 +01009917 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9918 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009919 else
9920 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9921
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009922 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9923 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9924 return ERR_ALERT | ERR_FATAL;
9925 }
Emeric Brunef42d922012-10-11 16:11:36 +02009926 return 0;
9927#endif
9928}
9929
Emeric Bruna7aa3092012-10-26 12:58:00 +02009930/* parse the "crt" server keyword */
9931static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9932{
9933 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009934 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009935 return ERR_ALERT | ERR_FATAL;
9936 }
9937
Willy Tarreauef934602016-12-22 23:12:01 +01009938 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009939 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009940 else
9941 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9942
9943 return 0;
9944}
Emeric Brunef42d922012-10-11 16:11:36 +02009945
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009946/* parse the "no-check-ssl" server keyword */
9947static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9948{
9949 newsrv->check.use_ssl = 0;
9950 free(newsrv->ssl_ctx.ciphers);
9951 newsrv->ssl_ctx.ciphers = NULL;
9952 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9953 return 0;
9954}
9955
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009956/* parse the "no-send-proxy-v2-ssl" server keyword */
9957static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9958{
9959 newsrv->pp_opts &= ~SRV_PP_V2;
9960 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9961 return 0;
9962}
9963
9964/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9965static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9966{
9967 newsrv->pp_opts &= ~SRV_PP_V2;
9968 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9969 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9970 return 0;
9971}
9972
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009973/* parse the "no-ssl" server keyword */
9974static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9975{
9976 newsrv->use_ssl = 0;
9977 free(newsrv->ssl_ctx.ciphers);
9978 newsrv->ssl_ctx.ciphers = NULL;
9979 return 0;
9980}
9981
Olivier Houchard522eea72017-11-03 16:27:47 +01009982/* parse the "allow-0rtt" server keyword */
9983static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9984{
9985 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9986 return 0;
9987}
9988
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009989/* parse the "no-ssl-reuse" server keyword */
9990static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9991{
9992 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9993 return 0;
9994}
9995
Emeric Brunf9c5c472012-10-11 15:28:34 +02009996/* parse the "no-tls-tickets" server keyword */
9997static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9998{
9999 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
10000 return 0;
10001}
David Safb76832014-05-08 23:42:08 -040010002/* parse the "send-proxy-v2-ssl" server keyword */
10003static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10004{
10005 newsrv->pp_opts |= SRV_PP_V2;
10006 newsrv->pp_opts |= SRV_PP_V2_SSL;
10007 return 0;
10008}
10009
10010/* parse the "send-proxy-v2-ssl-cn" server keyword */
10011static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10012{
10013 newsrv->pp_opts |= SRV_PP_V2;
10014 newsrv->pp_opts |= SRV_PP_V2_SSL;
10015 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
10016 return 0;
10017}
Emeric Brunf9c5c472012-10-11 15:28:34 +020010018
Willy Tarreau732eac42015-07-09 11:40:25 +020010019/* parse the "sni" server keyword */
10020static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10021{
10022#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
10023 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
10024 return ERR_ALERT | ERR_FATAL;
10025#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010026 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +020010027
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010028 arg = args[*cur_arg + 1];
10029 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +020010030 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
10031 return ERR_ALERT | ERR_FATAL;
10032 }
10033
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010034 free(newsrv->sni_expr);
10035 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010036
Willy Tarreau732eac42015-07-09 11:40:25 +020010037 return 0;
10038#endif
10039}
10040
Willy Tarreau92faadf2012-10-10 23:04:25 +020010041/* parse the "ssl" server keyword */
10042static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10043{
10044 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010045 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10046 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010047#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010048 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10049 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10050#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +020010051 return 0;
10052}
10053
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010054/* parse the "ssl-reuse" server keyword */
10055static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10056{
10057 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10058 return 0;
10059}
10060
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010061/* parse the "tls-tickets" server keyword */
10062static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10063{
10064 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10065 return 0;
10066}
10067
Emeric Brunef42d922012-10-11 16:11:36 +020010068/* parse the "verify" server keyword */
10069static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10070{
10071 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010072 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010073 return ERR_ALERT | ERR_FATAL;
10074 }
10075
10076 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010077 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010078 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010079 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010080 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010081 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10082 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010083 return ERR_ALERT | ERR_FATAL;
10084 }
10085
Evan Broderbe554312013-06-27 00:05:25 -070010086 return 0;
10087}
10088
10089/* parse the "verifyhost" server keyword */
10090static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10091{
10092 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010093 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010094 return ERR_ALERT | ERR_FATAL;
10095 }
10096
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010097 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010098 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10099
Emeric Brunef42d922012-10-11 16:11:36 +020010100 return 0;
10101}
10102
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010103/* parse the "ssl-default-bind-options" keyword in global section */
10104static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10105 struct proxy *defpx, const char *file, int line,
10106 char **err) {
10107 int i = 1;
10108
10109 if (*(args[i]) == 0) {
10110 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10111 return -1;
10112 }
10113 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010114 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010115 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010116 else if (!strcmp(args[i], "prefer-client-ciphers"))
10117 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010118 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10119 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10120 i++;
10121 else {
10122 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10123 return -1;
10124 }
10125 }
10126 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010127 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10128 return -1;
10129 }
10130 i++;
10131 }
10132 return 0;
10133}
10134
10135/* parse the "ssl-default-server-options" keyword in global section */
10136static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10137 struct proxy *defpx, const char *file, int line,
10138 char **err) {
10139 int i = 1;
10140
10141 if (*(args[i]) == 0) {
10142 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10143 return -1;
10144 }
10145 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010146 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010147 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010148 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10149 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10150 i++;
10151 else {
10152 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10153 return -1;
10154 }
10155 }
10156 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010157 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10158 return -1;
10159 }
10160 i++;
10161 }
10162 return 0;
10163}
10164
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010165/* parse the "ca-base" / "crt-base" keywords in global section.
10166 * Returns <0 on alert, >0 on warning, 0 on success.
10167 */
10168static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10169 struct proxy *defpx, const char *file, int line,
10170 char **err)
10171{
10172 char **target;
10173
Willy Tarreauef934602016-12-22 23:12:01 +010010174 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010175
10176 if (too_many_args(1, args, err, NULL))
10177 return -1;
10178
10179 if (*target) {
10180 memprintf(err, "'%s' already specified.", args[0]);
10181 return -1;
10182 }
10183
10184 if (*(args[1]) == 0) {
10185 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10186 return -1;
10187 }
10188 *target = strdup(args[1]);
10189 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010190}
10191
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010192/* "issuers-chain-path" load chain certificate in global */
10193static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10194{
10195 X509 *ca;
10196 X509_NAME *name = NULL;
10197 ASN1_OCTET_STRING *skid = NULL;
10198 STACK_OF(X509) *chain = NULL;
10199 struct issuer_chain *issuer;
10200 struct eb64_node *node;
10201 char *path;
10202 u64 key;
10203 int ret = 0;
10204
10205 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10206 if (chain == NULL) {
10207 chain = sk_X509_new_null();
10208 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10209 name = X509_get_subject_name(ca);
10210 }
10211 if (!sk_X509_push(chain, ca)) {
10212 X509_free(ca);
10213 goto end;
10214 }
10215 }
10216 if (!chain) {
10217 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10218 goto end;
10219 }
10220 if (!skid) {
10221 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10222 goto end;
10223 }
10224 if (!name) {
10225 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10226 goto end;
10227 }
10228 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010229 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010230 issuer = container_of(node, typeof(*issuer), node);
10231 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10232 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10233 goto end;
10234 }
10235 }
10236 issuer = calloc(1, sizeof *issuer);
10237 path = strdup(fp);
10238 if (!issuer || !path) {
10239 free(issuer);
10240 free(path);
10241 goto end;
10242 }
10243 issuer->node.key = key;
10244 issuer->path = path;
10245 issuer->chain = chain;
10246 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010247 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010248 ret = 1;
10249 end:
10250 if (skid)
10251 ASN1_OCTET_STRING_free(skid);
10252 if (chain)
10253 sk_X509_pop_free(chain, X509_free);
10254 return ret;
10255}
10256
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010257static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010258{
10259 AUTHORITY_KEYID *akid;
10260 struct issuer_chain *issuer = NULL;
10261
10262 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10263 if (akid) {
10264 struct eb64_node *node;
10265 u64 hk;
10266 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10267 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10268 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10269 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10270 issuer = ti;
10271 break;
10272 }
10273 }
10274 AUTHORITY_KEYID_free(akid);
10275 }
10276 return issuer;
10277}
10278
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010279static void ssl_free_global_issuers(void)
10280{
10281 struct eb64_node *node, *back;
10282 struct issuer_chain *issuer;
10283
William Lallemande0f3fd52020-02-25 14:53:06 +010010284 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010285 while (node) {
10286 issuer = container_of(node, typeof(*issuer), node);
10287 back = eb64_next(node);
10288 eb64_delete(node);
10289 free(issuer->path);
10290 sk_X509_pop_free(issuer->chain, X509_free);
10291 free(issuer);
10292 node = back;
10293 }
10294}
10295
10296static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10297 struct proxy *defpx, const char *file, int line,
10298 char **err)
10299{
10300 char *path;
10301 struct dirent **de_list;
10302 int i, n;
10303 struct stat buf;
10304 char *end;
10305 char fp[MAXPATHLEN+1];
10306
10307 if (too_many_args(1, args, err, NULL))
10308 return -1;
10309
10310 path = args[1];
10311 if (*path == 0 || stat(path, &buf)) {
10312 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10313 err && *err ? *err : "", args[0]);
10314 return -1;
10315 }
10316 if (S_ISDIR(buf.st_mode) == 0) {
10317 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10318 err && *err ? *err : "", args[0], path);
10319 return -1;
10320 }
10321
10322 /* strip trailing slashes, including first one */
10323 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10324 *end = 0;
10325 /* path already parsed? */
10326 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10327 return 0;
10328 /* overwrite old issuers_chain_path */
10329 free(global_ssl.issuers_chain_path);
10330 global_ssl.issuers_chain_path = strdup(path);
10331 ssl_free_global_issuers();
10332
10333 n = scandir(path, &de_list, 0, alphasort);
10334 if (n < 0) {
10335 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10336 err && *err ? *err : "", args[0], path, strerror(errno));
10337 return -1;
10338 }
10339 for (i = 0; i < n; i++) {
10340 struct dirent *de = de_list[i];
10341 BIO *in = NULL;
10342 char *warn = NULL;
10343
10344 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10345 free(de);
10346 if (stat(fp, &buf) != 0) {
10347 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10348 goto next;
10349 }
10350 if (!S_ISREG(buf.st_mode))
10351 goto next;
10352
10353 in = BIO_new(BIO_s_file());
10354 if (in == NULL)
10355 goto next;
10356 if (BIO_read_filename(in, fp) <= 0)
10357 goto next;
10358 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10359 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010360 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010361 free(warn);
10362 warn = NULL;
10363 }
10364 next:
10365 if (in)
10366 BIO_free(in);
10367 }
10368 free(de_list);
10369
10370 return 0;
10371}
10372
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010373/* parse the "ssl-mode-async" keyword in global section.
10374 * Returns <0 on alert, >0 on warning, 0 on success.
10375 */
10376static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10377 struct proxy *defpx, const char *file, int line,
10378 char **err)
10379{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010380#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010381 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010382 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010383 return 0;
10384#else
10385 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10386 return -1;
10387#endif
10388}
10389
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010390#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010391static int ssl_check_async_engine_count(void) {
10392 int err_code = 0;
10393
Emeric Brun3854e012017-05-17 20:42:48 +020010394 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010395 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010396 err_code = ERR_ABORT;
10397 }
10398 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010399}
10400
Grant Zhang872f9c22017-01-21 01:10:18 +000010401/* parse the "ssl-engine" keyword in global section.
10402 * Returns <0 on alert, >0 on warning, 0 on success.
10403 */
10404static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10405 struct proxy *defpx, const char *file, int line,
10406 char **err)
10407{
10408 char *algo;
10409 int ret = -1;
10410
10411 if (*(args[1]) == 0) {
10412 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10413 return ret;
10414 }
10415
10416 if (*(args[2]) == 0) {
10417 /* if no list of algorithms is given, it defaults to ALL */
10418 algo = strdup("ALL");
10419 goto add_engine;
10420 }
10421
10422 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10423 if (strcmp(args[2], "algo") != 0) {
10424 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10425 return ret;
10426 }
10427
10428 if (*(args[3]) == 0) {
10429 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10430 return ret;
10431 }
10432 algo = strdup(args[3]);
10433
10434add_engine:
10435 if (ssl_init_single_engine(args[1], algo)==0) {
10436 openssl_engines_initialized++;
10437 ret = 0;
10438 }
10439 free(algo);
10440 return ret;
10441}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010442#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010443
Willy Tarreauf22e9682016-12-21 23:23:19 +010010444/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10445 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10446 */
10447static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10448 struct proxy *defpx, const char *file, int line,
10449 char **err)
10450{
10451 char **target;
10452
Willy Tarreauef934602016-12-22 23:12:01 +010010453 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010454
10455 if (too_many_args(1, args, err, NULL))
10456 return -1;
10457
10458 if (*(args[1]) == 0) {
10459 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10460 return -1;
10461 }
10462
10463 free(*target);
10464 *target = strdup(args[1]);
10465 return 0;
10466}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010467
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010468#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010469/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10470 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10471 */
10472static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10473 struct proxy *defpx, const char *file, int line,
10474 char **err)
10475{
10476 char **target;
10477
10478 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10479
10480 if (too_many_args(1, args, err, NULL))
10481 return -1;
10482
10483 if (*(args[1]) == 0) {
10484 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10485 return -1;
10486 }
10487
10488 free(*target);
10489 *target = strdup(args[1]);
10490 return 0;
10491}
10492#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010493
Willy Tarreau9ceda382016-12-21 23:13:03 +010010494/* parse various global tune.ssl settings consisting in positive integers.
10495 * Returns <0 on alert, >0 on warning, 0 on success.
10496 */
10497static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10498 struct proxy *defpx, const char *file, int line,
10499 char **err)
10500{
10501 int *target;
10502
10503 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10504 target = &global.tune.sslcachesize;
10505 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010506 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010507 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010508 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010509 else if (strcmp(args[0], "maxsslconn") == 0)
10510 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010511 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10512 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010513 else {
10514 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10515 return -1;
10516 }
10517
10518 if (too_many_args(1, args, err, NULL))
10519 return -1;
10520
10521 if (*(args[1]) == 0) {
10522 memprintf(err, "'%s' expects an integer argument.", args[0]);
10523 return -1;
10524 }
10525
10526 *target = atoi(args[1]);
10527 if (*target < 0) {
10528 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10529 return -1;
10530 }
10531 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010532}
10533
10534static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10535 struct proxy *defpx, const char *file, int line,
10536 char **err)
10537{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010538 int ret;
10539
10540 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10541 if (ret != 0)
10542 return ret;
10543
Willy Tarreaubafbe012017-11-24 17:34:44 +010010544 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010545 memprintf(err, "'%s' is already configured.", args[0]);
10546 return -1;
10547 }
10548
Willy Tarreaubafbe012017-11-24 17:34:44 +010010549 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10550 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010551 memprintf(err, "Out of memory error.");
10552 return -1;
10553 }
10554 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010555}
10556
10557/* parse "ssl.force-private-cache".
10558 * Returns <0 on alert, >0 on warning, 0 on success.
10559 */
10560static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10561 struct proxy *defpx, const char *file, int line,
10562 char **err)
10563{
10564 if (too_many_args(0, args, err, NULL))
10565 return -1;
10566
Willy Tarreauef934602016-12-22 23:12:01 +010010567 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010568 return 0;
10569}
10570
10571/* parse "ssl.lifetime".
10572 * Returns <0 on alert, >0 on warning, 0 on success.
10573 */
10574static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10575 struct proxy *defpx, const char *file, int line,
10576 char **err)
10577{
10578 const char *res;
10579
10580 if (too_many_args(1, args, err, NULL))
10581 return -1;
10582
10583 if (*(args[1]) == 0) {
10584 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10585 return -1;
10586 }
10587
Willy Tarreauef934602016-12-22 23:12:01 +010010588 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010589 if (res == PARSE_TIME_OVER) {
10590 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10591 args[1], args[0]);
10592 return -1;
10593 }
10594 else if (res == PARSE_TIME_UNDER) {
10595 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10596 args[1], args[0]);
10597 return -1;
10598 }
10599 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010600 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10601 return -1;
10602 }
10603 return 0;
10604}
10605
10606#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010607/* parse "ssl-dh-param-file".
10608 * Returns <0 on alert, >0 on warning, 0 on success.
10609 */
10610static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10611 struct proxy *defpx, const char *file, int line,
10612 char **err)
10613{
10614 if (too_many_args(1, args, err, NULL))
10615 return -1;
10616
10617 if (*(args[1]) == 0) {
10618 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10619 return -1;
10620 }
10621
10622 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10623 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10624 return -1;
10625 }
10626 return 0;
10627}
10628
Willy Tarreau9ceda382016-12-21 23:13:03 +010010629/* parse "ssl.default-dh-param".
10630 * Returns <0 on alert, >0 on warning, 0 on success.
10631 */
10632static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10633 struct proxy *defpx, const char *file, int line,
10634 char **err)
10635{
10636 if (too_many_args(1, args, err, NULL))
10637 return -1;
10638
10639 if (*(args[1]) == 0) {
10640 memprintf(err, "'%s' expects an integer argument.", args[0]);
10641 return -1;
10642 }
10643
Willy Tarreauef934602016-12-22 23:12:01 +010010644 global_ssl.default_dh_param = atoi(args[1]);
10645 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010646 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10647 return -1;
10648 }
10649 return 0;
10650}
10651#endif
10652
William Lallemand3af48e72020-02-03 17:15:52 +010010653
10654/*
10655 * parse "ssl-load-extra-files".
10656 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10657 */
10658static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10659 struct proxy *defpx, const char *file, int line,
10660 char **err)
10661{
10662 int i;
10663 int gf = SSL_GF_NONE;
10664
10665 if (*(args[1]) == 0)
10666 goto err_arg;
10667
10668 for (i = 1; *args[i]; i++) {
10669
10670 if (!strcmp("bundle", args[i])) {
10671 gf |= SSL_GF_BUNDLE;
10672
10673 } else if (!strcmp("sctl", args[i])) {
10674 gf |= SSL_GF_SCTL;
10675
10676 } else if (!strcmp("ocsp", args[i])){
10677 gf |= SSL_GF_OCSP;
10678
10679 } else if (!strcmp("issuer", args[i])){
10680 gf |= SSL_GF_OCSP_ISSUER;
10681
William Lallemand4c5adbf2020-02-24 14:23:22 +010010682 } else if (!strcmp("key", args[i])) {
10683 gf |= SSL_GF_KEY;
10684
William Lallemand3af48e72020-02-03 17:15:52 +010010685 } else if (!strcmp("none", args[i])) {
10686 if (gf != SSL_GF_NONE)
10687 goto err_alone;
10688 gf = SSL_GF_NONE;
10689 i++;
10690 break;
10691
10692 } else if (!strcmp("all", args[i])) {
10693 if (gf != SSL_GF_NONE)
10694 goto err_alone;
10695 gf = SSL_GF_ALL;
10696 i++;
10697 break;
10698 } else {
10699 goto err_arg;
10700 }
10701 }
10702 /* break from loop but there are still arguments */
10703 if (*args[i])
10704 goto err_alone;
10705
10706 global_ssl.extra_files = gf;
10707
10708 return 0;
10709
10710err_alone:
10711 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10712 return -1;
10713
10714err_arg:
10715 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10716 return -1;
10717}
10718
Willy Tarreau9ceda382016-12-21 23:13:03 +010010719
William Lallemand32af2032016-10-29 18:09:35 +020010720/* This function is used with TLS ticket keys management. It permits to browse
10721 * each reference. The variable <getnext> must contain the current node,
10722 * <end> point to the root node.
10723 */
10724#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10725static inline
10726struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10727{
10728 struct tls_keys_ref *ref = getnext;
10729
10730 while (1) {
10731
10732 /* Get next list entry. */
10733 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10734
10735 /* If the entry is the last of the list, return NULL. */
10736 if (&ref->list == end)
10737 return NULL;
10738
10739 return ref;
10740 }
10741}
10742
10743static inline
10744struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10745{
10746 int id;
10747 char *error;
10748
10749 /* If the reference starts by a '#', this is numeric id. */
10750 if (reference[0] == '#') {
10751 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10752 id = strtol(reference + 1, &error, 10);
10753 if (*error != '\0')
10754 return NULL;
10755
10756 /* Perform the unique id lookup. */
10757 return tlskeys_ref_lookupid(id);
10758 }
10759
10760 /* Perform the string lookup. */
10761 return tlskeys_ref_lookup(reference);
10762}
10763#endif
10764
10765
10766#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10767
10768static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10769
10770static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10771 return cli_io_handler_tlskeys_files(appctx);
10772}
10773
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010774/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10775 * (next index to be dumped), and cli.p0 (next key reference).
10776 */
William Lallemand32af2032016-10-29 18:09:35 +020010777static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10778
10779 struct stream_interface *si = appctx->owner;
10780
10781 switch (appctx->st2) {
10782 case STAT_ST_INIT:
10783 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010784 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010785 * later and restart at the state "STAT_ST_INIT".
10786 */
10787 chunk_reset(&trash);
10788
10789 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10790 chunk_appendf(&trash, "# id secret\n");
10791 else
10792 chunk_appendf(&trash, "# id (file)\n");
10793
Willy Tarreau06d80a92017-10-19 14:32:15 +020010794 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010795 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010796 return 0;
10797 }
10798
William Lallemand32af2032016-10-29 18:09:35 +020010799 /* Now, we start the browsing of the references lists.
10800 * Note that the following call to LIST_ELEM return bad pointer. The only
10801 * available field of this pointer is <list>. It is used with the function
10802 * tlskeys_list_get_next() for retruning the first available entry
10803 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010804 if (appctx->ctx.cli.p0 == NULL) {
10805 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10806 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010807 }
10808
10809 appctx->st2 = STAT_ST_LIST;
10810 /* fall through */
10811
10812 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010813 while (appctx->ctx.cli.p0) {
10814 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010815
10816 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010817 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010818 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010819
10820 if (appctx->ctx.cli.i1 == 0)
10821 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10822
William Lallemand32af2032016-10-29 18:09:35 +020010823 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010824 int head;
10825
10826 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10827 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010828 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010829 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010830
10831 chunk_reset(t2);
10832 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010833 if (ref->key_size_bits == 128) {
10834 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10835 sizeof(struct tls_sess_key_128),
10836 t2->area, t2->size);
10837 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10838 t2->area);
10839 }
10840 else if (ref->key_size_bits == 256) {
10841 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10842 sizeof(struct tls_sess_key_256),
10843 t2->area, t2->size);
10844 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10845 t2->area);
10846 }
10847 else {
10848 /* This case should never happen */
10849 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10850 }
William Lallemand32af2032016-10-29 18:09:35 +020010851
Willy Tarreau06d80a92017-10-19 14:32:15 +020010852 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010853 /* let's try again later from this stream. We add ourselves into
10854 * this stream's users so that it can remove us upon termination.
10855 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010856 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010857 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010858 return 0;
10859 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010860 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010861 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010862 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010863 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010864 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010865 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010866 /* let's try again later from this stream. We add ourselves into
10867 * this stream's users so that it can remove us upon termination.
10868 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010869 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010870 return 0;
10871 }
10872
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010873 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010874 break;
10875
10876 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010877 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010878 }
10879
10880 appctx->st2 = STAT_ST_FIN;
10881 /* fall through */
10882
10883 default:
10884 appctx->st2 = STAT_ST_FIN;
10885 return 1;
10886 }
10887 return 0;
10888}
10889
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010890/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010891static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010892{
William Lallemand32af2032016-10-29 18:09:35 +020010893 /* no parameter, shows only file list */
10894 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010895 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010896 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010897 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010898 }
10899
10900 if (args[2][0] == '*') {
10901 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010902 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010903 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010904 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010905 if (!appctx->ctx.cli.p0)
10906 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010907 }
William Lallemand32af2032016-10-29 18:09:35 +020010908 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010909 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010910}
10911
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010912static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010913{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010914 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010915 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010916
William Lallemand32af2032016-10-29 18:09:35 +020010917 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010918 if (!*args[3] || !*args[4])
10919 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 +020010920
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010921 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010922 if (!ref)
10923 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010924
Willy Tarreau1c913e42018-08-22 05:26:57 +020010925 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010926 if (ret < 0)
10927 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010928
Willy Tarreau1c913e42018-08-22 05:26:57 +020010929 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010930 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10931 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010932
Willy Tarreau9d008692019-08-09 11:21:01 +020010933 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010934}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010935#endif
William Lallemand32af2032016-10-29 18:09:35 +020010936
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010937/*
10938 * Take an ssl_bind_conf structure and append the configuration line used to
10939 * create it in the buffer
10940 */
10941static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10942{
10943 int space = 0;
10944
10945 if (conf == NULL)
10946 return;
10947
10948 chunk_appendf(buf, " [");
10949#ifdef OPENSSL_NPN_NEGOTIATED
10950 if (conf->npn_str) {
10951 int len = conf->npn_len;
10952 char *ptr = conf->npn_str;
10953 int comma = 0;
10954
10955 if (space) chunk_appendf(buf, " ");
10956 chunk_appendf(buf, "npn ");
10957 while (len) {
10958 unsigned short size;
10959
10960 size = *ptr;
10961 ptr++;
10962 if (comma)
10963 chunk_memcat(buf, ",", 1);
10964 chunk_memcat(buf, ptr, size);
10965 ptr += size;
10966 len -= size + 1;
10967 comma = 1;
10968 }
10969 chunk_memcat(buf, "", 1); /* finish with a \0 */
10970 space++;
10971 }
10972#endif
10973#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10974 if (conf->alpn_str) {
10975 int len = conf->alpn_len;
10976 char *ptr = conf->alpn_str;
10977 int comma = 0;
10978
10979 if (space) chunk_appendf(buf, " ");
10980 chunk_appendf(buf, "alpn ");
10981 while (len) {
10982 unsigned short size;
10983
10984 size = *ptr;
10985 ptr++;
10986 if (comma)
10987 chunk_memcat(buf, ",", 1);
10988 chunk_memcat(buf, ptr, size);
10989 ptr += size;
10990 len -= size + 1;
10991 comma = 1;
10992 }
10993 chunk_memcat(buf, "", 1); /* finish with a \0 */
10994 space++;
10995 }
10996#endif
10997 /* verify */
10998 {
10999 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
11000 if (space) chunk_appendf(buf, " ");
11001 chunk_appendf(buf, "verify none");
11002 space++;
11003 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
11004 if (space) chunk_appendf(buf, " ");
11005 chunk_appendf(buf, "verify optional");
11006 space++;
11007 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
11008 if (space) chunk_appendf(buf, " ");
11009 chunk_appendf(buf, "verify required");
11010 space++;
11011 }
11012 }
11013
11014 if (conf->no_ca_names) {
11015 if (space) chunk_appendf(buf, " ");
11016 chunk_appendf(buf, "no-ca-names");
11017 space++;
11018 }
11019
11020 if (conf->early_data) {
11021 if (space) chunk_appendf(buf, " ");
11022 chunk_appendf(buf, "allow-0rtt");
11023 space++;
11024 }
11025 if (conf->ca_file) {
11026 if (space) chunk_appendf(buf, " ");
11027 chunk_appendf(buf, "ca-file %s", conf->ca_file);
11028 space++;
11029 }
11030 if (conf->crl_file) {
11031 if (space) chunk_appendf(buf, " ");
11032 chunk_appendf(buf, "crl-file %s", conf->crl_file);
11033 space++;
11034 }
11035 if (conf->ciphers) {
11036 if (space) chunk_appendf(buf, " ");
11037 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11038 space++;
11039 }
11040#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11041 if (conf->ciphersuites) {
11042 if (space) chunk_appendf(buf, " ");
11043 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11044 space++;
11045 }
11046#endif
11047 if (conf->curves) {
11048 if (space) chunk_appendf(buf, " ");
11049 chunk_appendf(buf, "curves %s", conf->curves);
11050 space++;
11051 }
11052 if (conf->ecdhe) {
11053 if (space) chunk_appendf(buf, " ");
11054 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11055 space++;
11056 }
11057
11058 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11059 /* XXX: this part need to be revamp so we don't dump the default settings */
11060 if (conf->ssl_methods.min) {
11061 if (space) chunk_appendf(buf, " ");
11062 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11063 space++;
11064 }
11065
11066 if (conf->ssl_methods.max) {
11067 if (space) chunk_appendf(buf, " ");
11068 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11069 space++;
11070 }
11071
William Lallemand58a52222020-04-02 18:11:47 +020011072 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011073
11074 return;
11075}
11076
11077/* dump a list of filters */
11078static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11079{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011080 int i;
11081
11082 if (!entry->fcount)
11083 return;
11084
11085 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011086 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011087 }
11088 return;
11089}
11090
11091/* CLI IO handler for '(show|dump) ssl crt-list' */
11092static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11093{
11094 struct buffer *trash = alloc_trash_chunk();
11095 struct stream_interface *si = appctx->owner;
11096 struct ebmb_node *lnode;
11097
11098 if (trash == NULL)
11099 return 1;
11100
11101 /* dump the list of crt-lists */
11102 lnode = appctx->ctx.cli.p1;
11103 if (lnode == NULL)
11104 lnode = ebmb_first(&crtlists_tree);
11105 while (lnode) {
11106 chunk_appendf(trash, "%s\n", lnode->key);
11107 if (ci_putchk(si_ic(si), trash) == -1) {
11108 si_rx_room_blk(si);
11109 goto yield;
11110 }
11111 lnode = ebmb_next(lnode);
11112 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011113 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011114 return 1;
11115yield:
11116 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011117 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011118 return 0;
11119}
11120
11121/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11122static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11123{
11124 struct buffer *trash = alloc_trash_chunk();
11125 struct crtlist *crtlist;
11126 struct stream_interface *si = appctx->owner;
11127 struct crtlist_entry *entry;
11128
11129 if (trash == NULL)
11130 return 1;
11131
11132 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11133
11134 entry = appctx->ctx.cli.p1;
11135 if (entry == NULL) {
11136 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11137 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11138 if (ci_putchk(si_ic(si), trash) == -1) {
11139 si_rx_room_blk(si);
11140 goto yield;
11141 }
11142 }
11143
11144 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11145 struct ckch_store *store;
11146 const char *filename;
11147
11148 store = entry->node.key;
11149 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011150 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011151 if (appctx->ctx.cli.i0 == 's') /* show */
11152 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011153 dump_crtlist_sslconf(trash, entry->ssl_conf);
11154 dump_crtlist_filters(trash, entry);
11155 chunk_appendf(trash, "\n");
11156
11157 if (ci_putchk(si_ic(si), trash) == -1) {
11158 si_rx_room_blk(si);
11159 goto yield;
11160 }
11161 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011162 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011163 return 1;
11164yield:
11165 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011166 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011167 return 0;
11168}
11169
11170/* CLI argument parser for '(show|dump) ssl crt-list' */
11171static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11172{
11173 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011174 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011175 int mode;
11176
11177 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11178 return 1;
11179
11180 appctx->ctx.cli.p0 = NULL;
11181 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011182
William Lallemandc69f02d2020-04-06 19:07:03 +020011183 if (*args[3] && !strcmp(args[3], "-n")) {
11184 mode = 's';
11185 filename = args[4];
11186 } else {
11187 mode = 'd';
11188 filename = args[3];
11189 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011190
William Lallemandc69f02d2020-04-06 19:07:03 +020011191 if (mode == 's' && !*args[4])
11192 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11193
11194 if (filename && *filename) {
11195 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011196 if (lnode == NULL)
11197 return cli_err(appctx, "didn't find the specified filename\n");
11198
11199 appctx->ctx.cli.p0 = lnode;
11200 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11201 }
11202 appctx->ctx.cli.i0 = mode;
11203
11204 return 0;
11205}
William Lallemand44b35322019-10-17 16:28:40 +020011206
William Lallemande67c80b2020-03-25 14:42:37 +010011207/* release function of the "add ssl crt-list' command, free things and unlock
11208 the spinlock */
11209static void cli_release_add_crtlist(struct appctx *appctx)
11210{
11211 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11212
11213 if (appctx->st2 != SETCERT_ST_FIN) {
11214 struct ckch_inst *inst, *inst_s;
11215 /* upon error free the ckch_inst and everything inside */
11216 ebpt_delete(&entry->node);
11217 LIST_DEL(&entry->by_crtlist);
11218 LIST_DEL(&entry->by_ckch_store);
11219
11220 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +020011221 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011222 }
11223 crtlist_free_filters(entry->filters);
11224 ssl_sock_free_ssl_conf(entry->ssl_conf);
11225 free(entry->ssl_conf);
11226 free(entry);
11227 }
11228
11229 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11230}
11231
11232
11233/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11234 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11235 *
11236 * The logic is the same as the "commit ssl cert" command but without the
11237 * freeing of the old structures, because there are none.
11238 */
11239static int cli_io_handler_add_crtlist(struct appctx *appctx)
11240{
11241 struct bind_conf_list *bind_conf_node;
11242 struct stream_interface *si = appctx->owner;
11243 struct crtlist *crtlist = appctx->ctx.cli.p0;
11244 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11245 struct ckch_store *store = entry->node.key;
11246 struct buffer *trash = alloc_trash_chunk();
11247 struct ckch_inst *new_inst;
11248 char *err = NULL;
11249 int i = 0;
11250 int errcode = 0;
11251
11252 if (trash == NULL)
11253 goto error;
11254
11255 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11256 * created.
11257 */
11258 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11259 goto error;
11260
11261 while (1) {
11262 switch (appctx->st2) {
11263 case SETCERT_ST_INIT:
11264 /* This state just print the update message */
11265 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11266 if (ci_putchk(si_ic(si), trash) == -1) {
11267 si_rx_room_blk(si);
11268 goto yield;
11269 }
11270 appctx->st2 = SETCERT_ST_GEN;
11271 /* fallthrough */
11272 case SETCERT_ST_GEN:
11273 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11274 if (bind_conf_node == NULL)
11275 bind_conf_node = crtlist->bind_conf;
11276 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11277 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11278 struct sni_ctx *sni;
11279
11280 /* yield every 10 generations */
11281 if (i > 10) {
11282 appctx->ctx.cli.p2 = bind_conf_node;
11283 goto yield;
11284 }
11285
11286 /* we don't support multi-cert bundles, only simple ones */
11287 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11288 if (errcode & ERR_CODE)
11289 goto error;
11290
11291 /* we need to initialize the SSL_CTX generated */
11292 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11293 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11294 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11295 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11296 if (errcode & ERR_CODE)
11297 goto error;
11298 }
11299 }
11300 /* display one dot for each new instance */
11301 chunk_appendf(trash, ".");
11302 i++;
11303 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11304 }
11305 appctx->st2 = SETCERT_ST_INSERT;
11306 /* fallthrough */
11307 case SETCERT_ST_INSERT:
11308 /* insert SNIs in bind_conf */
11309 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11310 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11311 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11312 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11313 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011314 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011315 appctx->st2 = SETCERT_ST_FIN;
11316 goto end;
11317 }
11318 }
11319
11320end:
11321 chunk_appendf(trash, "\n");
11322 if (errcode & ERR_WARN)
11323 chunk_appendf(trash, "%s", err);
11324 chunk_appendf(trash, "Success!\n");
11325 if (ci_putchk(si_ic(si), trash) == -1)
11326 si_rx_room_blk(si);
11327 free_trash_chunk(trash);
11328 /* success: call the release function and don't come back */
11329 return 1;
11330yield:
11331 /* store the state */
11332 if (ci_putchk(si_ic(si), trash) == -1)
11333 si_rx_room_blk(si);
11334 free_trash_chunk(trash);
11335 si_rx_endp_more(si); /* let's come back later */
11336 return 0; /* should come back */
11337
11338error:
11339 /* spin unlock and free are done in the release function */
11340 if (trash) {
11341 chunk_appendf(trash, "\n%sFailed!\n", err);
11342 if (ci_putchk(si_ic(si), trash) == -1)
11343 si_rx_room_blk(si);
11344 free_trash_chunk(trash);
11345 }
11346 /* error: call the release function and don't come back */
11347 return 1;
11348}
11349
11350
11351/*
11352 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011353 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011354 */
11355static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11356{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011357 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011358 struct ckch_store *store;
11359 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011360 char path[MAXPATHLEN+1];
11361 char *crtlist_path;
11362 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011363 struct ebmb_node *eb;
11364 struct ebpt_node *inserted;
11365 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011366 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011367
11368 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11369 return 1;
11370
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011371 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011372 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11373
11374 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011375
11376 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11377 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11378
11379 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11380 if (!eb) {
11381 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11382 goto error;
11383 }
11384 crtlist = ebmb_entry(eb, struct crtlist, node);
11385
William Lallemande718dfb2020-04-10 11:09:25 +020011386 entry = crtlist_entry_new();
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011387 if (entry == NULL) {
11388 memprintf(&err, "Not enough memory!");
11389 goto error;
11390 }
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011391
11392 if (payload) {
11393 char *lf;
11394
11395 lf = strrchr(payload, '\n');
11396 if (lf) {
11397 memprintf(&err, "only one line of payload is supported!");
11398 goto error;
11399 }
11400 /* cert_path is filled here */
11401 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11402 if (cfgerr & ERR_CODE)
11403 goto error;
11404 } else {
11405 cert_path = args[4];
11406 }
11407
11408 if (!cert_path) {
11409 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11410 cfgerr |= ERR_ALERT | ERR_FATAL;
11411 goto error;
11412 }
11413
11414 if (*cert_path != '/' && global_ssl.crt_base) {
11415 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11416 memprintf(&err, "'%s' : path too long", cert_path);
11417 cfgerr |= ERR_ALERT | ERR_FATAL;
11418 goto error;
11419 }
11420 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11421 cert_path = path;
11422 }
11423
William Lallemande67c80b2020-03-25 14:42:37 +010011424 store = ckchs_lookup(cert_path);
11425 if (store == NULL) {
11426 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11427 goto error;
11428 }
William Lallemand36ccc392020-04-08 10:57:24 +020011429 if (store->multi) {
11430 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);
11431 goto error;
11432 }
William Lallemande67c80b2020-03-25 14:42:37 +010011433 if (store->ckch == NULL || store->ckch->cert == NULL) {
11434 memprintf(&err, "certificate '%s' is empty!", cert_path);
11435 goto error;
11436 }
11437
William Lallemande67c80b2020-03-25 14:42:37 +010011438 /* check if it's possible to insert this new crtlist_entry */
11439 entry->node.key = store;
11440 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11441 if (inserted != &entry->node) {
11442 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011443 goto error;
11444 }
11445
11446 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
William Lallemande67c80b2020-03-25 14:42:37 +010011447 entry->crtlist = crtlist;
11448 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11449
11450 appctx->st2 = SETCERT_ST_INIT;
11451 appctx->ctx.cli.p0 = crtlist;
11452 appctx->ctx.cli.p1 = entry;
11453
11454 /* unlock is done in the release handler */
11455 return 0;
11456
11457error:
William Lallemande718dfb2020-04-10 11:09:25 +020011458 crtlist_entry_free(entry);
William Lallemande67c80b2020-03-25 14:42:37 +010011459 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11460 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11461 return cli_dynerr(appctx, err);
11462}
11463
William Lallemand0a9b9412020-04-06 17:43:05 +020011464/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11465static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11466{
11467 struct ckch_store *store;
11468 char *err = NULL;
11469 char *crtlist_path, *cert_path;
11470 struct ebmb_node *ebmb;
11471 struct ebpt_node *ebpt;
11472 struct crtlist *crtlist;
11473 struct crtlist_entry *entry = NULL;
11474 struct ckch_inst *inst, *inst_s;
11475 int linenum = 0;
11476 char *colons;
11477
11478 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11479 return 1;
11480
11481 if (!*args[3] || !*args[4])
11482 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11483
William Lallemand463b5242020-04-08 10:30:44 +020011484 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11485 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11486
William Lallemand0a9b9412020-04-06 17:43:05 +020011487 crtlist_path = args[3];
11488 cert_path = args[4];
11489
11490 colons = strchr(cert_path, ':');
11491 if (colons) {
11492 char *endptr;
11493
11494 linenum = strtol(colons + 1, &endptr, 10);
11495 if (colons + 1 == endptr || *endptr != '\0') {
11496 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11497 goto error;
11498 }
11499 *colons = '\0';
11500 }
11501 /* look for crtlist */
11502 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11503 if (!ebmb) {
11504 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11505 goto error;
11506 }
11507 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11508
11509 /* look for store */
11510 store = ckchs_lookup(cert_path);
11511 if (store == NULL) {
11512 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11513 goto error;
11514 }
William Lallemand36ccc392020-04-08 10:57:24 +020011515 if (store->multi) {
11516 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);
11517 goto error;
11518 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011519 if (store->ckch == NULL || store->ckch->cert == NULL) {
11520 memprintf(&err, "certificate '%s' is empty!", cert_path);
11521 goto error;
11522 }
11523
11524 ebpt = ebpt_lookup(&crtlist->entries, store);
11525 if (!ebpt) {
11526 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11527 goto error;
11528 }
11529
11530 /* list the line number of entries for errors in err, and select the right ebpt */
11531 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11532 struct crtlist_entry *tmp;
11533
11534 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11535 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11536
11537 /* select the entry we wanted */
11538 if (linenum == 0 || tmp->linenum == linenum) {
11539 if (!entry)
11540 entry = tmp;
11541 }
11542 }
11543
11544 /* we didn't found the specified entry */
11545 if (!entry) {
11546 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);
11547 goto error;
11548 }
11549
11550 /* we didn't specified a line number but there were several entries */
11551 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11552 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11553 goto error;
11554 }
11555
11556 /* upon error free the ckch_inst and everything inside */
11557
11558 ebpt_delete(&entry->node);
11559 LIST_DEL(&entry->by_crtlist);
11560 LIST_DEL(&entry->by_ckch_store);
11561
11562 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11563 struct sni_ctx *sni, *sni_s;
11564
11565 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11566 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11567 ebmb_delete(&sni->name);
11568 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011569 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011570 free(sni);
11571 }
11572 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11573 LIST_DEL(&inst->by_ckchs);
11574 free(inst);
11575 }
11576
11577 crtlist_free_filters(entry->filters);
11578 ssl_sock_free_ssl_conf(entry->ssl_conf);
11579 free(entry->ssl_conf);
11580 free(entry);
11581
11582 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11583 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11584 return cli_dynmsg(appctx, LOG_NOTICE, err);
11585
11586error:
11587 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11588 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11589 return cli_dynerr(appctx, err);
11590}
William Lallemande67c80b2020-03-25 14:42:37 +010011591
William Lallemand44b35322019-10-17 16:28:40 +020011592/* Type of SSL payloads that can be updated over the CLI */
11593
11594enum {
11595 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011596 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011597#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011598 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011599#endif
William Lallemand44b35322019-10-17 16:28:40 +020011600 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011601#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011602 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011603#endif
William Lallemand44b35322019-10-17 16:28:40 +020011604 CERT_TYPE_MAX,
11605};
11606
11607struct {
11608 const char *ext;
11609 int type;
11610 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11611 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011612} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011613 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011614 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011615#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011616 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011617#endif
11618#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011619 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011620#endif
William Lallemand44b35322019-10-17 16:28:40 +020011621 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011622 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011623};
11624
William Lallemand8f840d72019-10-23 10:53:05 +020011625
William Lallemandd4f946c2019-12-05 10:26:40 +010011626/* release function of the `show ssl cert' command */
11627static void cli_release_show_cert(struct appctx *appctx)
11628{
11629 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11630}
11631
11632/* IO handler of "show ssl cert <filename>" */
11633static int cli_io_handler_show_cert(struct appctx *appctx)
11634{
11635 struct buffer *trash = alloc_trash_chunk();
11636 struct ebmb_node *node;
11637 struct stream_interface *si = appctx->owner;
11638 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011639
11640 if (trash == NULL)
11641 return 1;
11642
11643 if (!appctx->ctx.ssl.old_ckchs) {
11644 if (ckchs_transaction.old_ckchs) {
11645 ckchs = ckchs_transaction.old_ckchs;
11646 chunk_appendf(trash, "# transaction\n");
11647 if (!ckchs->multi) {
11648 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011649#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011650 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011651 int n;
11652
William Lallemandd4f946c2019-12-05 10:26:40 +010011653 chunk_appendf(trash, "*%s:", ckchs->path);
11654 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11655 if (ckchs->ckch[n].cert)
11656 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11657 }
11658 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011659#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011660 }
11661 }
11662 }
11663
11664 if (!appctx->ctx.cli.p0) {
11665 chunk_appendf(trash, "# filename\n");
11666 node = ebmb_first(&ckchs_tree);
11667 } else {
11668 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11669 }
11670 while (node) {
11671 ckchs = ebmb_entry(node, struct ckch_store, node);
11672 if (!ckchs->multi) {
11673 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011674#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011675 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011676 int n;
11677
William Lallemandd4f946c2019-12-05 10:26:40 +010011678 chunk_appendf(trash, "%s:", ckchs->path);
11679 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11680 if (ckchs->ckch[n].cert)
11681 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11682 }
11683 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011684#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011685 }
11686
11687 node = ebmb_next(node);
11688 if (ci_putchk(si_ic(si), trash) == -1) {
11689 si_rx_room_blk(si);
11690 goto yield;
11691 }
11692 }
11693
11694 appctx->ctx.cli.p0 = NULL;
11695 free_trash_chunk(trash);
11696 return 1;
11697yield:
11698
11699 free_trash_chunk(trash);
11700 appctx->ctx.cli.p0 = ckchs;
11701 return 0; /* should come back */
11702}
11703
11704/* IO handler of the details "show ssl cert <filename>" */
11705static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11706{
11707 struct stream_interface *si = appctx->owner;
11708 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11709 struct buffer *out = alloc_trash_chunk();
11710 struct buffer *tmp = alloc_trash_chunk();
11711 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011712 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011713 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011714 int write = -1;
11715 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011716 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011717
11718 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011719 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011720
11721 if (!ckchs->multi) {
11722 chunk_appendf(out, "Filename: ");
11723 if (ckchs == ckchs_transaction.new_ckchs)
11724 chunk_appendf(out, "*");
11725 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011726
William Lallemand59c16fc2020-03-19 20:26:02 +010011727 chunk_appendf(out, "Status: ");
11728 if (ckchs->ckch->cert == NULL)
11729 chunk_appendf(out, "Empty\n");
11730 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11731 chunk_appendf(out, "Unused\n");
11732 else
11733 chunk_appendf(out, "Used\n");
11734
William Lallemandea987ed2020-03-19 16:48:33 +010011735 if (ckchs->ckch->cert == NULL)
11736 goto end;
11737
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011738 chain = ckchs->ckch->chain;
11739 if (chain == NULL) {
11740 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011741 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011742 if (issuer) {
11743 chain = issuer->chain;
11744 chunk_appendf(out, "Chain Filename: ");
11745 chunk_appendf(out, "%s\n", issuer->path);
11746 }
11747 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011748 chunk_appendf(out, "Serial: ");
11749 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11750 goto end;
11751 dump_binary(out, tmp->area, tmp->data);
11752 chunk_appendf(out, "\n");
11753
11754 chunk_appendf(out, "notBefore: ");
11755 chunk_reset(tmp);
11756 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11757 goto end;
11758 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11759 goto end;
11760 write = BIO_read(bio, tmp->area, tmp->size-1);
11761 tmp->area[write] = '\0';
11762 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011763 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011764 chunk_appendf(out, "%s\n", tmp->area);
11765
11766 chunk_appendf(out, "notAfter: ");
11767 chunk_reset(tmp);
11768 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11769 goto end;
11770 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11771 goto end;
11772 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11773 goto end;
11774 tmp->area[write] = '\0';
11775 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011776 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011777 chunk_appendf(out, "%s\n", tmp->area);
11778
William Lallemandd4f946c2019-12-05 10:26:40 +010011779#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11780 chunk_appendf(out, "Subject Alternative Name: ");
11781 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11782 goto end;
11783 *(out->area + out->data) = '\0';
11784 chunk_appendf(out, "\n");
11785#endif
11786 chunk_reset(tmp);
11787 chunk_appendf(out, "Algorithm: ");
11788 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11789 goto end;
11790 chunk_appendf(out, "%s\n", tmp->area);
11791
11792 chunk_reset(tmp);
11793 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011794 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011795 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011796 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011797 dump_binary(out, tmp->area, tmp->data);
11798 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011799
William Lallemanda90e5932020-02-25 14:07:58 +010011800 chunk_appendf(out, "Subject: ");
11801 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11802 goto end;
11803 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11804 goto end;
11805 *(tmp->area + tmp->data) = '\0';
11806 chunk_appendf(out, "%s\n", tmp->area);
11807
11808 chunk_appendf(out, "Issuer: ");
11809 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11810 goto end;
11811 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11812 goto end;
11813 *(tmp->area + tmp->data) = '\0';
11814 chunk_appendf(out, "%s\n", tmp->area);
11815
William Lallemand35f4a9d2020-02-25 11:56:32 +010011816 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011817 for (i = 0; i < sk_X509_num(chain); i++) {
11818 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011819
William Lallemandbb7288a2020-02-25 14:04:33 +010011820 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011821 if ((name = X509_get_subject_name(ca)) == NULL)
11822 goto end;
11823 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11824 goto end;
11825 *(tmp->area + tmp->data) = '\0';
11826 chunk_appendf(out, "%s\n", tmp->area);
11827
William Lallemandbb7288a2020-02-25 14:04:33 +010011828 chunk_appendf(out, "Chain Issuer: ");
11829 if ((name = X509_get_issuer_name(ca)) == NULL)
11830 goto end;
11831 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11832 goto end;
11833 *(tmp->area + tmp->data) = '\0';
11834 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011835 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011836 }
11837
William Lallemandea987ed2020-03-19 16:48:33 +010011838end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011839 if (ci_putchk(si_ic(si), out) == -1) {
11840 si_rx_room_blk(si);
11841 goto yield;
11842 }
11843
William Lallemand18eeb8e2020-03-20 14:42:36 +010011844end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011845 if (bio)
11846 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011847 free_trash_chunk(tmp);
11848 free_trash_chunk(out);
11849 return 1;
11850yield:
11851 free_trash_chunk(tmp);
11852 free_trash_chunk(out);
11853 return 0; /* should come back */
11854}
11855
11856/* parsing function for 'show ssl cert [certfile]' */
11857static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11858{
11859 struct ckch_store *ckchs;
11860
11861 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11862 return cli_err(appctx, "Can't allocate memory!\n");
11863
11864 /* The operations on the CKCH architecture are locked so we can
11865 * manipulate ckch_store and ckch_inst */
11866 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11867 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11868
11869 /* check if there is a certificate to lookup */
11870 if (*args[3]) {
11871 if (*args[3] == '*') {
11872 if (!ckchs_transaction.new_ckchs)
11873 goto error;
11874
11875 ckchs = ckchs_transaction.new_ckchs;
11876
11877 if (strcmp(args[3] + 1, ckchs->path))
11878 goto error;
11879
11880 } else {
11881 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11882 goto error;
11883
11884 }
11885
11886 if (ckchs->multi)
11887 goto error;
11888
11889 appctx->ctx.cli.p0 = ckchs;
11890 /* use the IO handler that shows details */
11891 appctx->io_handler = cli_io_handler_show_cert_detail;
11892 }
11893
11894 return 0;
11895
11896error:
11897 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11898 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11899}
11900
William Lallemand430413e2019-10-28 14:30:47 +010011901/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011902static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011903{
11904 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011905
William Lallemand430413e2019-10-28 14:30:47 +010011906 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011907
William Lallemand430413e2019-10-28 14:30:47 +010011908 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011909 /* 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 +010011910 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011911
William Lallemandbeea2a42019-10-30 17:45:33 +010011912 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020011913 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011914 }
11915}
11916
11917
11918/*
11919 * This function tries to create the new ckch_inst and their SNIs
11920 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011921static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011922{
11923 struct stream_interface *si = appctx->owner;
11924 int y = 0;
11925 char *err = NULL;
11926 int errcode = 0;
11927 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11928 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011929 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011930 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011931 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011932
William Lallemand33cc76f2019-10-31 11:43:45 +010011933 if (trash == NULL)
11934 goto error;
11935
William Lallemand8f840d72019-10-23 10:53:05 +020011936 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11937 goto error;
11938
William Lallemand430413e2019-10-28 14:30:47 +010011939 while (1) {
11940 switch (appctx->st2) {
11941 case SETCERT_ST_INIT:
11942 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011943 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011944 if (ci_putchk(si_ic(si), trash) == -1) {
11945 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011946 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011947 }
11948 appctx->st2 = SETCERT_ST_GEN;
11949 /* fallthrough */
11950 case SETCERT_ST_GEN:
11951 /*
11952 * This state generates the ckch instances with their
11953 * sni_ctxs and SSL_CTX.
11954 *
William Lallemand430413e2019-10-28 14:30:47 +010011955 * Since the SSL_CTX generation can be CPU consumer, we
11956 * yield every 10 instances.
11957 */
William Lallemand8f840d72019-10-23 10:53:05 +020011958
William Lallemandbeea2a42019-10-30 17:45:33 +010011959 old_ckchs = appctx->ctx.ssl.old_ckchs;
11960 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011961
William Lallemandbeea2a42019-10-30 17:45:33 +010011962 if (!new_ckchs)
11963 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020011964
William Lallemandbeea2a42019-10-30 17:45:33 +010011965 /* get the next ckchi to regenerate */
11966 ckchi = appctx->ctx.ssl.next_ckchi;
11967 /* we didn't start yet, set it to the first elem */
11968 if (ckchi == NULL)
11969 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011970
William Lallemandbeea2a42019-10-30 17:45:33 +010011971 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
11972 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
11973 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010011974 char **sni_filter = NULL;
11975 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020011976
William Lallemandbeea2a42019-10-30 17:45:33 +010011977 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
11978 if (y >= 10) {
11979 /* save the next ckchi to compute */
11980 appctx->ctx.ssl.next_ckchi = ckchi;
11981 goto yield;
11982 }
William Lallemandcaa16192020-04-08 16:29:15 +020011983
11984 if (ckchi->crtlist_entry) {
11985 sni_filter = ckchi->crtlist_entry->filters;
11986 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010011987 }
William Lallemand38df1c82019-12-04 15:39:35 +010011988
William Lallemandbeea2a42019-10-30 17:45:33 +010011989 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010011990 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 +010011991 else
William Lallemand38df1c82019-12-04 15:39:35 +010011992 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
11993
William Lallemandbeea2a42019-10-30 17:45:33 +010011994 if (errcode & ERR_CODE)
11995 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020011996
William Lallemand21724f02019-11-04 17:56:13 +010011997 /* if the previous ckchi was used as the default */
11998 if (ckchi->is_default)
11999 new_inst->is_default = 1;
12000
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012001 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010012002 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
12003 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012004 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 +010012005 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
12006 if (errcode & ERR_CODE)
12007 goto error;
12008 }
12009 }
12010
12011
William Lallemandbeea2a42019-10-30 17:45:33 +010012012 /* display one dot per new instance */
12013 chunk_appendf(trash, ".");
12014 /* link the new ckch_inst to the duplicate */
12015 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
12016 y++;
12017 }
William Lallemand430413e2019-10-28 14:30:47 +010012018 appctx->st2 = SETCERT_ST_INSERT;
12019 /* fallthrough */
12020 case SETCERT_ST_INSERT:
12021 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012022
William Lallemandbeea2a42019-10-30 17:45:33 +010012023 old_ckchs = appctx->ctx.ssl.old_ckchs;
12024 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012025
William Lallemandbeea2a42019-10-30 17:45:33 +010012026 if (!new_ckchs)
12027 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012028
William Lallemand90afe902020-03-30 19:29:45 +020012029 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12030 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12031 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12032 ebpt_delete(&entry->node);
12033 /* change the ptr and reinsert the node */
12034 entry->node.key = new_ckchs;
12035 ebpt_insert(&entry->crtlist->entries, &entry->node);
12036 }
12037
William Lallemand21724f02019-11-04 17:56:13 +010012038 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012039 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12040 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12041 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12042 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12043 }
William Lallemand8f840d72019-10-23 10:53:05 +020012044
William Lallemandbeea2a42019-10-30 17:45:33 +010012045 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12046 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemandd5e93772020-04-09 17:12:16 +020012047 struct bind_conf *bind_conf = ckchi->bind_conf;
12048
12049 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020012050 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020012051 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010012052 }
William Lallemand8f840d72019-10-23 10:53:05 +020012053
William Lallemandbeea2a42019-10-30 17:45:33 +010012054 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020012055 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010012056 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012057 appctx->st2 = SETCERT_ST_FIN;
12058 /* fallthrough */
12059 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012060 /* we achieved the transaction, we can set everything to NULL */
12061 free(ckchs_transaction.path);
12062 ckchs_transaction.path = NULL;
12063 ckchs_transaction.new_ckchs = NULL;
12064 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012065 goto end;
12066 }
William Lallemand8f840d72019-10-23 10:53:05 +020012067 }
William Lallemand430413e2019-10-28 14:30:47 +010012068end:
William Lallemand8f840d72019-10-23 10:53:05 +020012069
William Lallemanded442432019-11-21 16:41:07 +010012070 chunk_appendf(trash, "\n");
12071 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012072 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012073 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012074 if (ci_putchk(si_ic(si), trash) == -1)
12075 si_rx_room_blk(si);
12076 free_trash_chunk(trash);
12077 /* success: call the release function and don't come back */
12078 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012079yield:
12080 /* store the state */
12081 if (ci_putchk(si_ic(si), trash) == -1)
12082 si_rx_room_blk(si);
12083 free_trash_chunk(trash);
12084 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012085 return 0; /* should come back */
12086
12087error:
12088 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012089 if (trash) {
12090 chunk_appendf(trash, "\n%sFailed!\n", err);
12091 if (ci_putchk(si_ic(si), trash) == -1)
12092 si_rx_room_blk(si);
12093 free_trash_chunk(trash);
12094 }
William Lallemand430413e2019-10-28 14:30:47 +010012095 /* error: call the release function and don't come back */
12096 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012097}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012098
12099/*
12100 * Parsing function of 'commit ssl cert'
12101 */
12102static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12103{
12104 char *err = NULL;
12105
William Lallemand230662a2019-12-03 13:32:54 +010012106 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12107 return 1;
12108
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012109 if (!*args[3])
12110 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12111
12112 /* The operations on the CKCH architecture are locked so we can
12113 * manipulate ckch_store and ckch_inst */
12114 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12115 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12116
12117 if (!ckchs_transaction.path) {
12118 memprintf(&err, "No ongoing transaction! !\n");
12119 goto error;
12120 }
12121
12122 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12123 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12124 goto error;
12125 }
12126
William Lallemand4c5adbf2020-02-24 14:23:22 +010012127#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12128 if (ckchs_transaction.new_ckchs->multi) {
12129 int n;
12130
12131 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12132 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)) {
12133 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12134 goto error;
12135 }
12136 }
12137 } else
12138#endif
12139 {
12140 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12141 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12142 goto error;
12143 }
12144 }
12145
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012146 /* init the appctx structure */
12147 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012148 appctx->ctx.ssl.next_ckchi = NULL;
12149 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12150 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12151
12152 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12153 return 0;
12154
12155error:
12156
12157 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12158 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12159
12160 return cli_dynerr(appctx, err);
12161}
12162
William Lallemand8f840d72019-10-23 10:53:05 +020012163/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012164 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012165 */
William Lallemand150bfa82019-09-19 17:12:49 +020012166static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12167{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012168 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012169 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012170 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012171 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012172 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012173 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012174 char *end;
12175 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012176 struct cert_key_and_chain *ckch;
12177 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012178
William Lallemand230662a2019-12-03 13:32:54 +010012179 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12180 return 1;
12181
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012182 if ((buf = alloc_trash_chunk()) == NULL)
12183 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012184
12185 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012186 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012187
12188 /* The operations on the CKCH architecture are locked so we can
12189 * manipulate ckch_store and ckch_inst */
12190 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12191 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12192
William Lallemand8f840d72019-10-23 10:53:05 +020012193 if (!chunk_strcpy(buf, args[3])) {
12194 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12195 errcode |= ERR_ALERT | ERR_FATAL;
12196 goto end;
12197 }
12198
William Lallemand44b35322019-10-17 16:28:40 +020012199 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012200 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012201 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012202 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12203 *end = '\0';
12204 type = cert_exts[i].type;
12205 break;
12206 }
12207 }
12208
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012209 appctx->ctx.ssl.old_ckchs = NULL;
12210 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012211
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012212 /* if there is an ongoing transaction */
12213 if (ckchs_transaction.path) {
12214 /* 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 +020012215#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012216 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012217 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012218 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012219
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012220 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012221 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012222 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012223 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012224 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12225 bundle = j; /* keep the type of certificate so we insert it at the right place */
12226 *end = '\0'; /* it's a bundle let's end the string*/
12227 break;
12228 }
William Lallemand150bfa82019-09-19 17:12:49 +020012229 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012230 if (bundle < 0) {
12231 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);
12232 errcode |= ERR_ALERT | ERR_FATAL;
12233 goto end;
12234 }
12235 }
12236#endif
12237
12238 /* if there is an ongoing transaction, check if this is the same file */
12239 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12240 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12241 errcode |= ERR_ALERT | ERR_FATAL;
12242 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012243 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012244
12245 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12246
12247 } else {
12248 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12249
12250 /* lookup for the certificate in the tree:
12251 * check if this is used as a bundle AND as a unique certificate */
12252 for (i = 0; i < 2; i++) {
12253
12254 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12255 /* only the bundle name is in the tree and you should
12256 * never update a bundle name, only a filename */
12257 if (bundle < 0 && find_ckchs[i]->multi) {
12258 /* we tried to look for a non-bundle and we found a bundle */
12259 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12260 err ? err : "", args[3], args[3]);
12261 errcode |= ERR_ALERT | ERR_FATAL;
12262 goto end;
12263 }
William Lallemand3246d942019-11-04 14:02:11 +010012264 /* If we want a bundle but this is not a bundle
12265 * example: When you try to update <file>.rsa, but
12266 * <file> is a regular file */
12267 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12268 find_ckchs[i] = NULL;
12269 break;
12270 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012271 }
12272#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12273 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012274 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012275 int j;
12276
12277 /* check if it was used in a bundle by removing the
12278 * .dsa/.rsa/.ecdsa at the end of the filename */
12279 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012280 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012281 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12282 bundle = j; /* keep the type of certificate so we insert it at the right place */
12283 *end = '\0'; /* it's a bundle let's end the string*/
12284 break;
12285 }
12286 }
William Lallemand37031b82019-11-04 13:38:53 +010012287 if (bundle < 0) /* we didn't find a bundle extension */
12288 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012289 }
William Lallemand963b2e72019-10-14 11:38:36 +020012290#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012291 /* bundles are not supported here, so we don't need to lookup again */
12292 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012293#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012294 }
12295
12296 if (find_ckchs[0] && find_ckchs[1]) {
12297 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",
12298 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12299 errcode |= ERR_ALERT | ERR_FATAL;
12300 goto end;
12301 }
12302
12303 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012304 }
12305
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012306 if (!appctx->ctx.ssl.old_ckchs) {
12307 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012308 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012309 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012310 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012311 }
12312
William Lallemand8a7fdf02019-11-04 10:59:32 +010012313 if (!appctx->ctx.ssl.path) {
12314 /* this is a new transaction, set the path of the transaction */
12315 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12316 if (!appctx->ctx.ssl.path) {
12317 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12318 errcode |= ERR_ALERT | ERR_FATAL;
12319 goto end;
12320 }
12321 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012322
12323 old_ckchs = appctx->ctx.ssl.old_ckchs;
12324
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012325 /* duplicate the ckch store */
12326 new_ckchs = ckchs_dup(old_ckchs);
12327 if (!new_ckchs) {
12328 memprintf(&err, "%sCannot allocate memory!\n",
12329 err ? err : "");
12330 errcode |= ERR_ALERT | ERR_FATAL;
12331 goto end;
12332 }
12333
12334 if (!new_ckchs->multi)
12335 ckch = new_ckchs->ckch;
12336 else
12337 ckch = &new_ckchs->ckch[bundle];
12338
12339 /* appply the change on the duplicate */
12340 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12341 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12342 errcode |= ERR_ALERT | ERR_FATAL;
12343 goto end;
12344 }
12345
12346 appctx->ctx.ssl.new_ckchs = new_ckchs;
12347
12348 /* we succeed, we can save the ckchs in the transaction */
12349
12350 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012351 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012352 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12353 ckchs_transaction.path = appctx->ctx.ssl.path;
12354 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12355 } else {
12356 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12357
12358 }
12359
12360 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020012361 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012362
12363 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12364
12365
William Lallemand8f840d72019-10-23 10:53:05 +020012366 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012367
William Lallemand8f840d72019-10-23 10:53:05 +020012368end:
12369 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012370
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012371 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012372
William Lallemandba1c33f2020-04-08 17:55:45 +020012373 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012374 appctx->ctx.ssl.new_ckchs = NULL;
12375
12376 appctx->ctx.ssl.old_ckchs = NULL;
12377
12378 free(appctx->ctx.ssl.path);
12379 appctx->ctx.ssl.path = NULL;
12380
12381 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012382 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012383 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012384
12385 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12386 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012387 }
William Lallemand8f840d72019-10-23 10:53:05 +020012388 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012389}
12390
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012391/* parsing function of 'abort ssl cert' */
12392static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12393{
12394 char *err = NULL;
12395
William Lallemand230662a2019-12-03 13:32:54 +010012396 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12397 return 1;
12398
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012399 if (!*args[3])
12400 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12401
12402 /* The operations on the CKCH architecture are locked so we can
12403 * manipulate ckch_store and ckch_inst */
12404 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12405 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12406
12407 if (!ckchs_transaction.path) {
12408 memprintf(&err, "No ongoing transaction!\n");
12409 goto error;
12410 }
12411
12412 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12413 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12414 goto error;
12415 }
12416
12417 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020012418 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012419 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020012420 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012421 ckchs_transaction.old_ckchs = NULL;
12422 free(ckchs_transaction.path);
12423 ckchs_transaction.path = NULL;
12424
12425 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12426
12427 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12428 return cli_dynmsg(appctx, LOG_NOTICE, err);
12429
12430error:
12431 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12432
12433 return cli_dynerr(appctx, err);
12434}
12435
William Lallemandea987ed2020-03-19 16:48:33 +010012436/* parsing function of 'new ssl cert' */
12437static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12438{
12439 struct ckch_store *store;
12440 char *err = NULL;
12441 char *path;
12442
12443 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12444 return 1;
12445
12446 if (!*args[3])
12447 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12448
12449 path = args[3];
12450
12451 /* The operations on the CKCH architecture are locked so we can
12452 * manipulate ckch_store and ckch_inst */
12453 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12454 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12455
12456 store = ckchs_lookup(path);
12457 if (store != NULL) {
12458 memprintf(&err, "Certificate '%s' already exists!\n", path);
12459 store = NULL; /* we don't want to free it */
12460 goto error;
12461 }
William Lallemand8a874e42020-04-09 10:32:53 +020012462 /* we won't support multi-certificate bundle here */
12463 store = ckch_store_new(path, 1);
William Lallemandea987ed2020-03-19 16:48:33 +010012464 if (!store) {
12465 memprintf(&err, "unable to allocate memory.\n");
12466 goto error;
12467 }
William Lallemandea987ed2020-03-19 16:48:33 +010012468
12469 /* insert into the ckchs tree */
William Lallemandea987ed2020-03-19 16:48:33 +010012470 ebst_insert(&ckchs_tree, &store->node);
12471 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12472
12473 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12474 return cli_dynmsg(appctx, LOG_NOTICE, err);
12475error:
12476 free(store);
12477 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12478 return cli_dynerr(appctx, err);
12479}
12480
William Lallemand419e6342020-04-08 12:05:39 +020012481/* parsing function of 'del ssl cert' */
12482static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12483{
12484 struct ckch_store *store;
12485 char *err = NULL;
12486 char *filename;
12487
12488 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12489 return 1;
12490
12491 if (!*args[3])
12492 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12493
12494 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12495 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12496
12497 filename = args[3];
12498
12499 store = ckchs_lookup(filename);
12500 if (store == NULL) {
12501 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12502 goto error;
12503 }
12504 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12505 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12506 goto error;
12507 }
12508
12509 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020012510 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020012511
12512 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12513
12514 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12515 return cli_dynmsg(appctx, LOG_NOTICE, err);
12516
12517error:
12518 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12519 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12520 return cli_dynerr(appctx, err);
12521}
12522
12523
12524
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012525static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012526{
12527#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12528 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012529 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012530
12531 if (!payload)
12532 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012533
12534 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012535 if (!*payload)
12536 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012537
12538 /* remove \r and \n from the payload */
12539 for (i = 0, j = 0; payload[i]; i++) {
12540 if (payload[i] == '\r' || payload[i] == '\n')
12541 continue;
12542 payload[j++] = payload[i];
12543 }
12544 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012545
Willy Tarreau1c913e42018-08-22 05:26:57 +020012546 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012547 if (ret < 0)
12548 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012549
Willy Tarreau1c913e42018-08-22 05:26:57 +020012550 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012551 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012552 if (err)
12553 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12554 else
12555 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012556 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012557
12558 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012559#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012560 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 +020012561#endif
12562
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012563}
12564
Willy Tarreau86a394e2019-05-09 14:15:32 +020012565#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012566static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12567{
12568 switch (arg->type) {
12569 case ARGT_STR:
12570 smp->data.type = SMP_T_STR;
12571 smp->data.u.str = arg->data.str;
12572 return 1;
12573 case ARGT_VAR:
12574 if (!vars_get_by_desc(&arg->data.var, smp))
12575 return 0;
12576 if (!sample_casts[smp->data.type][SMP_T_STR])
12577 return 0;
12578 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12579 return 0;
12580 return 1;
12581 default:
12582 return 0;
12583 }
12584}
12585
12586static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12587 const char *file, int line, char **err)
12588{
12589 switch(args[0].data.sint) {
12590 case 128:
12591 case 192:
12592 case 256:
12593 break;
12594 default:
12595 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12596 return 0;
12597 }
12598 /* Try to decode a variable. */
12599 vars_check_arg(&args[1], NULL);
12600 vars_check_arg(&args[2], NULL);
12601 vars_check_arg(&args[3], NULL);
12602 return 1;
12603}
12604
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012605/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012606static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12607{
12608 struct sample nonce, key, aead_tag;
12609 struct buffer *smp_trash, *smp_trash_alloc;
12610 EVP_CIPHER_CTX *ctx;
12611 int dec_size, ret;
12612
12613 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12614 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12615 return 0;
12616
12617 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12618 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12619 return 0;
12620
12621 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12622 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12623 return 0;
12624
12625 smp_trash = get_trash_chunk();
12626 smp_trash_alloc = alloc_trash_chunk();
12627 if (!smp_trash_alloc)
12628 return 0;
12629
12630 ctx = EVP_CIPHER_CTX_new();
12631
12632 if (!ctx)
12633 goto err;
12634
12635 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12636 if (dec_size < 0)
12637 goto err;
12638 smp_trash->data = dec_size;
12639
12640 /* Set cipher type and mode */
12641 switch(arg_p[0].data.sint) {
12642 case 128:
12643 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12644 break;
12645 case 192:
12646 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12647 break;
12648 case 256:
12649 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12650 break;
12651 }
12652
12653 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12654
12655 /* Initialise IV */
12656 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12657 goto err;
12658
12659 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12660 if (dec_size < 0)
12661 goto err;
12662 smp_trash->data = dec_size;
12663
12664 /* Initialise key */
12665 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12666 goto err;
12667
12668 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12669 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12670 goto err;
12671
12672 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12673 if (dec_size < 0)
12674 goto err;
12675 smp_trash_alloc->data = dec_size;
12676 dec_size = smp_trash->data;
12677
12678 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12679 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12680
12681 if (ret <= 0)
12682 goto err;
12683
12684 smp->data.u.str.data = dec_size + smp_trash->data;
12685 smp->data.u.str.area = smp_trash->area;
12686 smp->data.type = SMP_T_BIN;
12687 smp->flags &= ~SMP_F_CONST;
12688 free_trash_chunk(smp_trash_alloc);
12689 return 1;
12690
12691err:
12692 free_trash_chunk(smp_trash_alloc);
12693 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012694}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012695# endif
William Lallemand32af2032016-10-29 18:09:35 +020012696
Elliot Otchet71f82972020-01-15 08:12:14 -050012697/* Argument validation functions */
12698
12699/* This function is used to validate the arguments passed to any "x_dn" ssl
12700 * keywords. These keywords support specifying a third parameter that must be
12701 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12702 */
12703int val_dnfmt(struct arg *arg, char **err_msg)
12704{
12705 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12706 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12707 return 0;
12708 }
12709 return 1;
12710}
12711
William Lallemand32af2032016-10-29 18:09:35 +020012712/* register cli keywords */
12713static struct cli_kw_list cli_kws = {{ },{
12714#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12715 { { "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 +020012716 { { "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 +020012717#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012718 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012719 { { "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 +010012720 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12721 { { "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 +010012722 { { "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 +020012723 { { "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 +010012724 { { "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 +010012725 { { "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 +020012726 { { "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 +020012727 { { "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 +020012728 { { NULL }, NULL, NULL, NULL }
12729}};
12730
Willy Tarreau0108d902018-11-25 19:14:37 +010012731INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012732
Willy Tarreau7875d092012-09-10 08:20:03 +020012733/* Note: must not be declared <const> as its list will be overwritten.
12734 * Please take care of keeping this list alphabetically sorted.
12735 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012736static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012737 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012738 { "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 +010012739#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012740 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012741#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012742 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012743#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12744 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12745#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012746 { "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 +020012747 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012748 { "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 +020012749 { "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 +020012750#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012751 { "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 -040012752#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012753#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012754 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12755 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012756 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12757#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012758 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12759 { "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 +010012760 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012761 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012762 { "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 +020012763 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12764 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12765 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12766 { "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 -050012767 { "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 +020012768 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12769 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012770 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012771 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12772 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012773 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012774 { "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 +020012775 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12776 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12777 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12778 { "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 -050012779 { "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 +020012780 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012781 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012782 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012783 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012784 { "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 +010012785 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012786 { "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 +020012787 { "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 +010012788 { "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 +020012789 { "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 +010012790#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012791 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012792#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012793#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012794 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012795#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012796 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012797#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012798 { "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 -040012799#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012800 { "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 +020012801#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012802 { "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 -040012803#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012804#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012805 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12806 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012807 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12808#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012809#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012810 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012811#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012812 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12813 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12814 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12815 { "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 +020012816 { NULL, NULL, 0, 0, 0 },
12817}};
12818
Willy Tarreau0108d902018-11-25 19:14:37 +010012819INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12820
Willy Tarreau7875d092012-09-10 08:20:03 +020012821/* Note: must not be declared <const> as its list will be overwritten.
12822 * Please take care of keeping this list alphabetically sorted.
12823 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012824static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012825 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12826 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012827 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012828}};
12829
Willy Tarreau0108d902018-11-25 19:14:37 +010012830INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12831
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012832/* Note: must not be declared <const> as its list will be overwritten.
12833 * Please take care of keeping this list alphabetically sorted, doing so helps
12834 * all code contributors.
12835 * Optional keywords are also declared with a NULL ->parse() function so that
12836 * the config parser can report an appropriate error when a known keyword was
12837 * not enabled.
12838 */
William Lallemand557823f2020-04-01 17:42:47 +020012839
12840/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12841 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012842static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012843 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012844 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012845 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12846 { "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 +010012847 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012848#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012849 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12850#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012851 { "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 +010012852 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012853 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012854 { "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 +010012855 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012856 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12857 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012858 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12859 { NULL, NULL, 0 },
12860};
12861
Willy Tarreau0108d902018-11-25 19:14:37 +010012862/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12863
Willy Tarreau51fb7652012-09-18 18:24:39 +020012864static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012865 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012866 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012867 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12868 { "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 +020012869 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12870 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12871 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12872 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012873#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012874 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12875#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012876 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012877 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012878 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012879 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12880 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12881 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12882 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12883 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12884 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12885 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012886 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012887 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012888 { "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 +020012889 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12890 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12891 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12892 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012893 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012894 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12895 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012896 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12897 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012898 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12899 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12900 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12901 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12902 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012903 { NULL, NULL, 0 },
12904}};
Emeric Brun46591952012-05-18 15:47:34 +020012905
Willy Tarreau0108d902018-11-25 19:14:37 +010012906INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12907
Willy Tarreau92faadf2012-10-10 23:04:25 +020012908/* Note: must not be declared <const> as its list will be overwritten.
12909 * Please take care of keeping this list alphabetically sorted, doing so helps
12910 * all code contributors.
12911 * Optional keywords are also declared with a NULL ->parse() function so that
12912 * the config parser can report an appropriate error when a known keyword was
12913 * not enabled.
12914 */
12915static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012916 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012917 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012918 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012919 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012920 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012921 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12922 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012923#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012924 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12925#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012926 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12927 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12928 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12929 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12930 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12931 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12932 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12933 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12934 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12935 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12936 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12937 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12938 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12939 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12940 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12941 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12942 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12943 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012944 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012945 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12946 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12947 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12948 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12949 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12950 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12951 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12952 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12953 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12954 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012955 { NULL, NULL, 0, 0 },
12956}};
12957
Willy Tarreau0108d902018-11-25 19:14:37 +010012958INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12959
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012960static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012961 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12962 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012963 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012964 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012965 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12966 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012967#ifndef OPENSSL_NO_DH
12968 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12969#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012970 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012971#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012972 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012973#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010012974 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12975#ifndef OPENSSL_NO_DH
12976 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12977#endif
12978 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12979 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12980 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12981 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012982 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012983 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
12984 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012985#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012986 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
12987 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
12988#endif
William Lallemand3af48e72020-02-03 17:15:52 +010012989 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012990 { 0, NULL, NULL },
12991}};
12992
Willy Tarreau0108d902018-11-25 19:14:37 +010012993INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
12994
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012995/* Note: must not be declared <const> as its list will be overwritten */
12996static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020012997#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012998 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
12999#endif
13000 { NULL, NULL, 0, 0, 0 },
13001}};
13002
13003INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
13004
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020013005/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010013006static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020013007 .snd_buf = ssl_sock_from_buf,
13008 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010013009 .subscribe = ssl_subscribe,
13010 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020013011 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020013012 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020013013 .rcv_pipe = NULL,
13014 .snd_pipe = NULL,
13015 .shutr = NULL,
13016 .shutw = ssl_sock_shutw,
13017 .close = ssl_sock_close,
13018 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010013019 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010013020 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010013021 .prepare_srv = ssl_sock_prepare_srv_ctx,
13022 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013023 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013024 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013025};
13026
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013027enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13028 struct session *sess, struct stream *s, int flags)
13029{
13030 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013031 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013032
13033 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013034 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013035
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013036 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013037 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013038 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013039 s->req.flags |= CF_READ_NULL;
13040 return ACT_RET_YIELD;
13041 }
13042 }
13043 return (ACT_RET_CONT);
13044}
13045
13046static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13047{
13048 rule->action_ptr = ssl_action_wait_for_hs;
13049
13050 return ACT_RET_PRS_OK;
13051}
13052
13053static struct action_kw_list http_req_actions = {ILH, {
13054 { "wait-for-handshake", ssl_parse_wait_for_hs },
13055 { /* END */ }
13056}};
13057
Willy Tarreau0108d902018-11-25 19:14:37 +010013058INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13059
Willy Tarreau5db847a2019-05-09 14:13:35 +020013060#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013061
13062static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13063{
13064 if (ptr) {
13065 chunk_destroy(ptr);
13066 free(ptr);
13067 }
13068}
13069
13070#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013071static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13072{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013073 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013074}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013075
Emeric Brun46591952012-05-18 15:47:34 +020013076__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013077static void __ssl_sock_init(void)
13078{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013079#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013080 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013081 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013082#endif
Emeric Brun46591952012-05-18 15:47:34 +020013083
Willy Tarreauef934602016-12-22 23:12:01 +010013084 if (global_ssl.listen_default_ciphers)
13085 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13086 if (global_ssl.connect_default_ciphers)
13087 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013088#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013089 if (global_ssl.listen_default_ciphersuites)
13090 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13091 if (global_ssl.connect_default_ciphersuites)
13092 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13093#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013094
Willy Tarreau13e14102016-12-22 20:25:26 +010013095 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013096#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013097 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013098#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013099#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013100 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013101 n = sk_SSL_COMP_num(cm);
13102 while (n--) {
13103 (void) sk_SSL_COMP_pop(cm);
13104 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013105#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013106
Willy Tarreau5db847a2019-05-09 14:13:35 +020013107#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013108 ssl_locking_init();
13109#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013110#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013111 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13112#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013113 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013114 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 +020013115#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013116 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013117 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013118#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013119#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13120 hap_register_post_check(tlskeys_finalize_config);
13121#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013122
13123 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13124 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13125
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013126 hap_register_post_deinit(ssl_free_global_issuers);
13127
Willy Tarreau80713382018-11-26 10:19:54 +010013128#ifndef OPENSSL_NO_DH
13129 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13130 hap_register_post_deinit(ssl_free_dh);
13131#endif
13132#ifndef OPENSSL_NO_ENGINE
13133 hap_register_post_deinit(ssl_free_engines);
13134#endif
13135 /* Load SSL string for the verbose & debug mode. */
13136 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013137 ha_meth = BIO_meth_new(0x666, "ha methods");
13138 BIO_meth_set_write(ha_meth, ha_ssl_write);
13139 BIO_meth_set_read(ha_meth, ha_ssl_read);
13140 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13141 BIO_meth_set_create(ha_meth, ha_ssl_new);
13142 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13143 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13144 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013145
13146 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013147}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013148
Willy Tarreau80713382018-11-26 10:19:54 +010013149/* Compute and register the version string */
13150static void ssl_register_build_options()
13151{
13152 char *ptr = NULL;
13153 int i;
13154
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013155 memprintf(&ptr, "Built with OpenSSL version : "
13156#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013157 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013158#else /* OPENSSL_IS_BORINGSSL */
13159 OPENSSL_VERSION_TEXT
13160 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013161 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013162 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013163#endif
13164 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013165#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013166 "no (library version too old)"
13167#elif defined(OPENSSL_NO_TLSEXT)
13168 "no (disabled via OPENSSL_NO_TLSEXT)"
13169#else
13170 "yes"
13171#endif
13172 "", ptr);
13173
13174 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13175#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13176 "yes"
13177#else
13178#ifdef OPENSSL_NO_TLSEXT
13179 "no (because of OPENSSL_NO_TLSEXT)"
13180#else
13181 "no (version might be too old, 0.9.8f min needed)"
13182#endif
13183#endif
13184 "", ptr);
13185
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013186 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13187 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13188 if (methodVersions[i].option)
13189 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013190
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013191 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013192}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013193
Willy Tarreau80713382018-11-26 10:19:54 +010013194INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013195
Emeric Brun46591952012-05-18 15:47:34 +020013196
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013197#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013198void ssl_free_engines(void) {
13199 struct ssl_engine_list *wl, *wlb;
13200 /* free up engine list */
13201 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13202 ENGINE_finish(wl->e);
13203 ENGINE_free(wl->e);
13204 LIST_DEL(&wl->list);
13205 free(wl);
13206 }
13207}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013208#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013209
Remi Gacogned3a23c32015-05-28 16:39:47 +020013210#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013211void ssl_free_dh(void) {
13212 if (local_dh_1024) {
13213 DH_free(local_dh_1024);
13214 local_dh_1024 = NULL;
13215 }
13216 if (local_dh_2048) {
13217 DH_free(local_dh_2048);
13218 local_dh_2048 = NULL;
13219 }
13220 if (local_dh_4096) {
13221 DH_free(local_dh_4096);
13222 local_dh_4096 = NULL;
13223 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013224 if (global_dh) {
13225 DH_free(global_dh);
13226 global_dh = NULL;
13227 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013228}
13229#endif
13230
13231__attribute__((destructor))
13232static void __ssl_sock_deinit(void)
13233{
13234#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013235 if (ssl_ctx_lru_tree) {
13236 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013237 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013238 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013239#endif
13240
Willy Tarreau5db847a2019-05-09 14:13:35 +020013241#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013242 ERR_remove_state(0);
13243 ERR_free_strings();
13244
13245 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013246#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013247
Willy Tarreau5db847a2019-05-09 14:13:35 +020013248#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013249 CRYPTO_cleanup_all_ex_data();
13250#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013251 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013252}
13253
13254
Emeric Brun46591952012-05-18 15:47:34 +020013255/*
13256 * Local variables:
13257 * c-indent-level: 8
13258 * c-basic-offset: 8
13259 * End:
13260 */