blob: a09e437c77d40d6efe10b74f145273703ec00621 [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 Lallemand9117de92019-10-04 00:29:42 +02002901/* Alloc and init a ckch_inst */
2902static struct ckch_inst *ckch_inst_new()
2903{
2904 struct ckch_inst *ckch_inst;
2905
2906 ckch_inst = calloc(1, sizeof *ckch_inst);
William Lallemand9cef2e22020-04-09 16:25:10 +02002907 if (!ckch_inst)
2908 return NULL;
2909
2910 LIST_INIT(&ckch_inst->sni_ctx);
2911 LIST_INIT(&ckch_inst->by_ckchs);
2912 LIST_INIT(&ckch_inst->by_crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02002913
2914 return ckch_inst;
2915}
2916
2917
2918/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002919static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002920 struct bind_conf *s, struct ssl_bind_conf *conf,
2921 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002922{
2923 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002924 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002925
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002926 if (*name == '!') {
2927 neg = 1;
2928 name++;
2929 }
2930 if (*name == '*') {
2931 wild = 1;
2932 name++;
2933 }
2934 /* !* filter is a nop */
2935 if (neg && wild)
2936 return order;
2937 if (*name) {
2938 int j, len;
2939 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002940 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002941 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002942 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002943 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002944 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002945
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002946 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002947 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002948 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002949 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02002950 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002951 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002952 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002953 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002954 sc->order = order++;
2955 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002956 sc->wild = wild;
2957 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01002958 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02002959 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002960 }
2961 return order;
2962}
2963
William Lallemand6af03992019-07-23 15:00:54 +02002964/*
William Lallemand1d29c742019-10-04 00:53:29 +02002965 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2966 * This function can't return an error.
2967 *
2968 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2969 */
2970static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2971{
2972
2973 struct sni_ctx *sc0, *sc0b, *sc1;
2974 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002975 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002976
2977 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2978
2979 /* ignore if sc0 was already inserted in a tree */
2980 if (sc0->name.node.leaf_p)
2981 continue;
2982
2983 /* Check for duplicates. */
2984 if (sc0->wild)
2985 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2986 else
2987 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2988
2989 for (; node; node = ebmb_next_dup(node)) {
2990 sc1 = ebmb_entry(node, struct sni_ctx, name);
2991 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2992 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2993 /* it's a duplicate, we should remove and free it */
2994 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02002995 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02002996 free(sc0);
2997 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002998 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002999 }
3000 }
3001
3002 /* if duplicate, ignore the insertion */
3003 if (!sc0)
3004 continue;
3005
3006 if (sc0->wild)
3007 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3008 else
3009 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003010
3011 /* replace the default_ctx if required with the first ctx */
3012 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02003013 SSL_CTX_free(bind_conf->default_ctx);
3014 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01003015 bind_conf->default_ctx = sc0->ctx;
3016 def = 1;
3017 }
William Lallemand1d29c742019-10-04 00:53:29 +02003018 }
3019}
3020
3021/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003022 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003023 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003024struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003025
William Lallemand2954c472020-03-06 21:54:13 +01003026/* tree of crtlist (crt-list/directory) */
3027static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003028
Emeric Brun7a883362019-10-17 13:27:40 +02003029/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003030 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003031 * DH parameter is loaded into the SSL_CTX and if there is no
3032 * DH parameter available in ckchs nor in global, the default
3033 * DH parameters are applied on the SSL_CTX.
3034 * Returns a bitfield containing the flags:
3035 * ERR_FATAL in any fatal error case
3036 * ERR_ALERT if a reason of the error is availabine in err
3037 * ERR_WARN if a warning is available into err
3038 * The value 0 means there is no error nor warning and
3039 * the operation succeed.
3040 */
William Lallemandfa892222019-07-23 16:06:08 +02003041#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003042static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3043 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003044{
Emeric Brun7a883362019-10-17 13:27:40 +02003045 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003046 DH *dh = NULL;
3047
William Lallemanda8c73742019-07-31 18:31:34 +02003048 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003049 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003050 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3051 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3052 err && *err ? *err : "", path);
3053#if defined(SSL_CTX_set_dh_auto)
3054 SSL_CTX_set_dh_auto(ctx, 1);
3055 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3056 err && *err ? *err : "");
3057#else
3058 memprintf(err, "%s, DH ciphers won't be available.\n",
3059 err && *err ? *err : "");
3060#endif
3061 ret |= ERR_WARN;
3062 goto end;
3063 }
William Lallemandfa892222019-07-23 16:06:08 +02003064
3065 if (ssl_dh_ptr_index >= 0) {
3066 /* store a pointer to the DH params to avoid complaining about
3067 ssl-default-dh-param not being set for this SSL_CTX */
3068 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3069 }
3070 }
3071 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003072 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3073 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3074 err && *err ? *err : "", path);
3075#if defined(SSL_CTX_set_dh_auto)
3076 SSL_CTX_set_dh_auto(ctx, 1);
3077 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3078 err && *err ? *err : "");
3079#else
3080 memprintf(err, "%s, DH ciphers won't be available.\n",
3081 err && *err ? *err : "");
3082#endif
3083 ret |= ERR_WARN;
3084 goto end;
3085 }
William Lallemandfa892222019-07-23 16:06:08 +02003086 }
3087 else {
3088 /* Clear openssl global errors stack */
3089 ERR_clear_error();
3090
3091 if (global_ssl.default_dh_param <= 1024) {
3092 /* we are limited to DH parameter of 1024 bits anyway */
3093 if (local_dh_1024 == NULL)
3094 local_dh_1024 = ssl_get_dh_1024();
3095
Emeric Brun7a883362019-10-17 13:27:40 +02003096 if (local_dh_1024 == NULL) {
3097 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3098 err && *err ? *err : "", path);
3099 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003100 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003101 }
William Lallemandfa892222019-07-23 16:06:08 +02003102
Emeric Bruna9363eb2019-10-17 14:53:03 +02003103 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3104 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3105 err && *err ? *err : "", path);
3106#if defined(SSL_CTX_set_dh_auto)
3107 SSL_CTX_set_dh_auto(ctx, 1);
3108 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3109 err && *err ? *err : "");
3110#else
3111 memprintf(err, "%s, DH ciphers won't be available.\n",
3112 err && *err ? *err : "");
3113#endif
3114 ret |= ERR_WARN;
3115 goto end;
3116 }
William Lallemandfa892222019-07-23 16:06:08 +02003117 }
3118 else {
3119 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3120 }
William Lallemandfa892222019-07-23 16:06:08 +02003121 }
3122
3123end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003124 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003125 return ret;
3126}
3127#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003128
yanbzhu488a4d22015-12-01 15:16:07 -05003129/* Frees the contents of a cert_key_and_chain
3130 */
3131static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3132{
yanbzhu488a4d22015-12-01 15:16:07 -05003133 if (!ckch)
3134 return;
3135
3136 /* Free the certificate and set pointer to NULL */
3137 if (ckch->cert)
3138 X509_free(ckch->cert);
3139 ckch->cert = NULL;
3140
3141 /* Free the key and set pointer to NULL */
3142 if (ckch->key)
3143 EVP_PKEY_free(ckch->key);
3144 ckch->key = NULL;
3145
3146 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003147 if (ckch->chain)
3148 sk_X509_pop_free(ckch->chain, X509_free);
3149 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003150
William Lallemand455af502019-10-17 18:04:45 +02003151 if (ckch->dh)
3152 DH_free(ckch->dh);
3153 ckch->dh = NULL;
3154
3155 if (ckch->sctl) {
3156 free(ckch->sctl->area);
3157 ckch->sctl->area = NULL;
3158 free(ckch->sctl);
3159 ckch->sctl = NULL;
3160 }
3161
3162 if (ckch->ocsp_response) {
3163 free(ckch->ocsp_response->area);
3164 ckch->ocsp_response->area = NULL;
3165 free(ckch->ocsp_response);
3166 ckch->ocsp_response = NULL;
3167 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003168
3169 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003170 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003171 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003172}
3173
William Lallemand8d0f8932019-10-17 18:03:58 +02003174/*
3175 *
3176 * This function copy a cert_key_and_chain in memory
3177 *
3178 * It's used to try to apply changes on a ckch before committing them, because
3179 * most of the time it's not possible to revert those changes
3180 *
3181 * Return a the dst or NULL
3182 */
3183static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3184 struct cert_key_and_chain *dst)
3185{
3186 if (src->cert) {
3187 dst->cert = src->cert;
3188 X509_up_ref(src->cert);
3189 }
3190
3191 if (src->key) {
3192 dst->key = src->key;
3193 EVP_PKEY_up_ref(src->key);
3194 }
3195
3196 if (src->chain) {
3197 dst->chain = X509_chain_up_ref(src->chain);
3198 }
3199
3200 if (src->dh) {
3201 DH_up_ref(src->dh);
3202 dst->dh = src->dh;
3203 }
3204
3205 if (src->sctl) {
3206 struct buffer *sctl;
3207
3208 sctl = calloc(1, sizeof(*sctl));
3209 if (!chunk_dup(sctl, src->sctl)) {
3210 free(sctl);
3211 sctl = NULL;
3212 goto error;
3213 }
3214 dst->sctl = sctl;
3215 }
3216
3217 if (src->ocsp_response) {
3218 struct buffer *ocsp_response;
3219
3220 ocsp_response = calloc(1, sizeof(*ocsp_response));
3221 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3222 free(ocsp_response);
3223 ocsp_response = NULL;
3224 goto error;
3225 }
3226 dst->ocsp_response = ocsp_response;
3227 }
3228
3229 if (src->ocsp_issuer) {
3230 X509_up_ref(src->ocsp_issuer);
3231 dst->ocsp_issuer = src->ocsp_issuer;
3232 }
3233
3234 return dst;
3235
3236error:
3237
3238 /* free everything */
3239 ssl_sock_free_cert_key_and_chain_contents(dst);
3240
3241 return NULL;
3242}
3243
3244
yanbzhu488a4d22015-12-01 15:16:07 -05003245/* checks if a key and cert exists in the ckch
3246 */
William Lallemand1633e392019-09-30 12:58:13 +02003247#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003248static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3249{
3250 return (ckch->cert != NULL && ckch->key != NULL);
3251}
William Lallemand1633e392019-09-30 12:58:13 +02003252#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003253
William Lallemandf9568fc2019-10-16 18:27:58 +02003254/*
3255 * return 0 on success or != 0 on failure
3256 */
3257static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3258{
3259 int ret = 1;
3260 BIO *in = NULL;
3261 X509 *issuer;
3262
3263 if (buf) {
3264 /* reading from a buffer */
3265 in = BIO_new_mem_buf(buf, -1);
3266 if (in == NULL) {
3267 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3268 goto end;
3269 }
3270
3271 } else {
3272 /* reading from a file */
3273 in = BIO_new(BIO_s_file());
3274 if (in == NULL)
3275 goto end;
3276
3277 if (BIO_read_filename(in, path) <= 0)
3278 goto end;
3279 }
3280
3281 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3282 if (!issuer) {
3283 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003284 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003285 goto end;
3286 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003287 /* no error, fill ckch with new context, old context must be free */
3288 if (ckch->ocsp_issuer)
3289 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003290 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003291 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003292
3293end:
3294
3295 ERR_clear_error();
3296 if (in)
3297 BIO_free(in);
3298
3299 return ret;
3300}
3301
William Lallemand96a9c972019-10-17 11:56:17 +02003302
3303/*
3304 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003305 * The PEM must contain at least a Certificate,
3306 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003307 *
William Lallemand96a9c972019-10-17 11:56:17 +02003308 * If it failed you should not attempt to use the ckch but free it.
3309 *
3310 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003311 */
William Lallemand96a9c972019-10-17 11:56:17 +02003312static 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 -05003313{
William Lallemandf11365b2019-09-19 14:25:58 +02003314 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003315 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003316 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003317 X509 *cert = NULL;
3318 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003319 DH *dh = NULL;
3320 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003321
3322 if (buf) {
3323 /* reading from a buffer */
3324 in = BIO_new_mem_buf(buf, -1);
3325 if (in == NULL) {
3326 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3327 goto end;
3328 }
yanbzhu488a4d22015-12-01 15:16:07 -05003329
William Lallemand96a9c972019-10-17 11:56:17 +02003330 } else {
3331 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003332 in = BIO_new(BIO_s_file());
William Lallemand7fd01b32020-04-07 14:16:32 +02003333 if (in == NULL) {
3334 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
William Lallemandf11365b2019-09-19 14:25:58 +02003335 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003336 }
yanbzhu488a4d22015-12-01 15:16:07 -05003337
William Lallemand7fd01b32020-04-07 14:16:32 +02003338 if (BIO_read_filename(in, path) <= 0) {
3339 memprintf(err, "%scannot open the file '%s'.\n",
3340 err && *err ? *err : "", path);
William Lallemandf11365b2019-09-19 14:25:58 +02003341 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003342 }
William Lallemandf11365b2019-09-19 14:25:58 +02003343 }
yanbzhu488a4d22015-12-01 15:16:07 -05003344
yanbzhu488a4d22015-12-01 15:16:07 -05003345 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003346 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003347 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003348
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003349#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003350 /* Seek back to beginning of file */
3351 if (BIO_reset(in) == -1) {
3352 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3353 err && *err ? *err : "", path);
3354 goto end;
3355 }
3356
William Lallemand96a9c972019-10-17 11:56:17 +02003357 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3358 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003359#endif
William Lallemandfa892222019-07-23 16:06:08 +02003360
Willy Tarreaubb137a82016-04-06 19:02:38 +02003361 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003362 if (BIO_reset(in) == -1) {
3363 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3364 err && *err ? *err : "", path);
3365 goto end;
3366 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003367
3368 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003369 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3370 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003371 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003372 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003373 goto end;
3374 }
3375
William Lallemand96a9c972019-10-17 11:56:17 +02003376 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003377 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3378 if (chain == NULL)
3379 chain = sk_X509_new_null();
3380 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003381 X509_free(ca);
3382 goto end;
3383 }
3384 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003385
yanbzhu488a4d22015-12-01 15:16:07 -05003386 ret = ERR_get_error();
3387 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3388 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003389 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003390 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003391 }
3392
William Lallemand75b15f72020-01-23 10:56:05 +01003393 /* once it loaded the PEM, it should remove everything else in the ckch */
3394 if (ckch->ocsp_response) {
3395 free(ckch->ocsp_response->area);
3396 ckch->ocsp_response->area = NULL;
3397 free(ckch->ocsp_response);
3398 ckch->ocsp_response = NULL;
3399 }
3400
3401 if (ckch->sctl) {
3402 free(ckch->sctl->area);
3403 ckch->sctl->area = NULL;
3404 free(ckch->sctl);
3405 ckch->sctl = NULL;
3406 }
3407
3408 if (ckch->ocsp_issuer) {
3409 X509_free(ckch->ocsp_issuer);
3410 ckch->ocsp_issuer = NULL;
3411 }
3412
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003413 /* no error, fill ckch with new context, old context will be free at end: */
3414 SWAP(ckch->key, key);
3415 SWAP(ckch->dh, dh);
3416 SWAP(ckch->cert, cert);
3417 SWAP(ckch->chain, chain);
3418
William Lallemand246c0242019-10-11 08:59:13 +02003419 ret = 0;
3420
William Lallemand96a9c972019-10-17 11:56:17 +02003421end:
William Lallemand246c0242019-10-11 08:59:13 +02003422
3423 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003424 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003425 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003426 if (key)
3427 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003428 if (dh)
3429 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003430 if (cert)
3431 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003432 if (chain)
3433 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003434
William Lallemand96a9c972019-10-17 11:56:17 +02003435 return ret;
3436}
3437
3438/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003439 * Try to load a private key file from a <path> or a buffer <buf>
3440 *
3441 * If it failed you should not attempt to use the ckch but free it.
3442 *
3443 * Return 0 on success or != 0 on failure
3444 */
3445static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3446{
3447 BIO *in = NULL;
3448 int ret = 1;
3449 EVP_PKEY *key = NULL;
3450
3451 if (buf) {
3452 /* reading from a buffer */
3453 in = BIO_new_mem_buf(buf, -1);
3454 if (in == NULL) {
3455 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3456 goto end;
3457 }
3458
3459 } else {
3460 /* reading from a file */
3461 in = BIO_new(BIO_s_file());
3462 if (in == NULL)
3463 goto end;
3464
3465 if (BIO_read_filename(in, path) <= 0)
3466 goto end;
3467 }
3468
3469 /* Read Private Key */
3470 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3471 if (key == NULL) {
3472 memprintf(err, "%sunable to load private key from file '%s'.\n",
3473 err && *err ? *err : "", path);
3474 goto end;
3475 }
3476
3477 ret = 0;
3478
3479 SWAP(ckch->key, key);
3480
3481end:
3482
3483 ERR_clear_error();
3484 if (in)
3485 BIO_free(in);
3486 if (key)
3487 EVP_PKEY_free(key);
3488
3489 return ret;
3490}
3491
3492/*
William Lallemand96a9c972019-10-17 11:56:17 +02003493 * Try to load in a ckch every files related to a ckch.
3494 * (PEM, sctl, ocsp, issuer etc.)
3495 *
3496 * This function is only used to load files during the configuration parsing,
3497 * it is not used with the CLI.
3498 *
3499 * This allows us to carry the contents of the file without having to read the
3500 * file multiple times. The caller must call
3501 * ssl_sock_free_cert_key_and_chain_contents.
3502 *
3503 * returns:
3504 * 0 on Success
3505 * 1 on SSL Failure
3506 */
3507static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3508{
3509 int ret = 1;
3510
3511 /* try to load the PEM */
3512 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3513 goto end;
3514 }
3515
William Lallemand4c5adbf2020-02-24 14:23:22 +01003516 /* try to load an external private key if it wasn't in the PEM */
3517 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3518 char fp[MAXPATHLEN+1];
3519 struct stat st;
3520
3521 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3522 if (stat(fp, &st) == 0) {
3523 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3524 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3525 err && *err ? *err : "", fp);
3526 goto end;
3527 }
3528 }
3529 }
3530
3531 if (ckch->key == NULL) {
3532 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3533 goto end;
3534 }
3535
3536 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3537 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3538 err && *err ? *err : "", path);
3539 goto end;
3540 }
3541
William Lallemanda17f4112019-10-10 15:16:44 +02003542#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3543 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003544 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003545 char fp[MAXPATHLEN+1];
3546 struct stat st;
3547
3548 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3549 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003550 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003551 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003552 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003553 ret = 1;
3554 goto end;
3555 }
3556 }
3557 }
3558#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003559
William Lallemand246c0242019-10-11 08:59:13 +02003560 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003561 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003562 char fp[MAXPATHLEN+1];
3563 struct stat st;
3564
3565 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3566 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003567 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003568 ret = 1;
3569 goto end;
3570 }
3571 }
3572 }
3573
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003574#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003575 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003576 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003577 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003578 struct stat st;
3579 char fp[MAXPATHLEN+1];
3580
3581 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3582 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003583 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003584 ret = 1;
3585 goto end;
3586 }
3587
3588 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003589 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003590 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003591 ret = 1;
3592 goto end;
3593 }
William Lallemand246c0242019-10-11 08:59:13 +02003594 }
3595 }
3596 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003597#endif
William Lallemand246c0242019-10-11 08:59:13 +02003598
yanbzhu488a4d22015-12-01 15:16:07 -05003599 ret = 0;
3600
3601end:
3602
3603 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003604
3605 /* Something went wrong in one of the reads */
3606 if (ret != 0)
3607 ssl_sock_free_cert_key_and_chain_contents(ckch);
3608
3609 return ret;
3610}
3611
3612/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003613 * Returns a bitfield containing the flags:
3614 * ERR_FATAL in any fatal error case
3615 * ERR_ALERT if the reason of the error is available in err
3616 * ERR_WARN if a warning is available into err
3617 * The value 0 means there is no error nor warning and
3618 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003619 */
3620static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3621{
Emeric Bruna96b5822019-10-17 13:25:14 +02003622 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003623 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003624
yanbzhu488a4d22015-12-01 15:16:07 -05003625 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3626 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3627 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003628 errcode |= ERR_ALERT | ERR_FATAL;
3629 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003630 }
3631
3632 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3633 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3634 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003635 errcode |= ERR_ALERT | ERR_FATAL;
3636 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003637 }
3638
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003639 if (ckch->chain) {
3640 find_chain = ckch->chain;
3641 } else {
3642 /* Find Certificate Chain in global */
3643 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003644 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003645 if (issuer)
3646 find_chain = issuer->chain;
3647 }
William Lallemand85888572020-02-27 14:48:35 +01003648
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003649 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003650 if (find_chain) {
3651 int i;
3652 X509 *ca;
3653 for (i = 0; i < sk_X509_num(find_chain); i++) {
3654 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003655 /* skip self issued (Root CA) */
3656 if (!X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca)))
3657 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003658 /*
3659 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3660 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3661 */
3662 X509_up_ref(ca);
3663 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3664 X509_free(ca);
3665 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3666 err && *err ? *err : "", path);
3667 errcode |= ERR_ALERT | ERR_FATAL;
3668 goto end;
3669 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003670 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003671 }
yanbzhu488a4d22015-12-01 15:16:07 -05003672
William Lallemandfa892222019-07-23 16:06:08 +02003673#ifndef OPENSSL_NO_DH
3674 /* store a NULL pointer to indicate we have not yet loaded
3675 a custom DH param file */
3676 if (ssl_dh_ptr_index >= 0) {
3677 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3678 }
3679
Emeric Brun7a883362019-10-17 13:27:40 +02003680 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3681 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003682 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3683 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003684 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003685 }
3686#endif
3687
William Lallemanda17f4112019-10-10 15:16:44 +02003688#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3689 if (sctl_ex_index >= 0 && ckch->sctl) {
3690 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3691 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003692 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003693 errcode |= ERR_ALERT | ERR_FATAL;
3694 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003695 }
3696 }
3697#endif
3698
William Lallemand4a660132019-10-14 14:51:41 +02003699#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003700 /* Load OCSP Info into context */
3701 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003702 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003703 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",
3704 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003705 errcode |= ERR_ALERT | ERR_FATAL;
3706 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003707 }
3708 }
William Lallemand246c0242019-10-11 08:59:13 +02003709#endif
3710
Emeric Bruna96b5822019-10-17 13:25:14 +02003711 end:
3712 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003713}
3714
William Lallemandc4ecddf2019-07-31 16:50:08 +02003715#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003716
William Lallemand28a8fce2019-10-04 17:36:55 +02003717static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003718{
3719 struct sni_keytype *s_kt = NULL;
3720 struct ebmb_node *node;
3721 int i;
3722
3723 for (i = 0; i < trash.size; i++) {
3724 if (!str[i])
3725 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003726 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003727 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003728 trash.area[i] = 0;
3729 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003730 if (!node) {
3731 /* CN not found in tree */
3732 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3733 /* Using memcpy here instead of strncpy.
3734 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3735 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3736 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003737 if (!s_kt)
3738 return -1;
3739
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003740 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003741 s_kt->keytypes = 0;
3742 ebst_insert(sni_keytypes, &s_kt->name);
3743 } else {
3744 /* CN found in tree */
3745 s_kt = container_of(node, struct sni_keytype, name);
3746 }
3747
3748 /* Mark that this CN has the keytype of key_index via keytypes mask */
3749 s_kt->keytypes |= 1<<key_index;
3750
William Lallemand28a8fce2019-10-04 17:36:55 +02003751 return 0;
3752
yanbzhu08ce6ab2015-12-02 13:01:29 -05003753}
3754
William Lallemandc4ecddf2019-07-31 16:50:08 +02003755#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003756/*
3757 * Free a ckch_store and its ckch(s)
3758 * The linked ckch_inst are not free'd
3759 */
3760void ckchs_free(struct ckch_store *ckchs)
3761{
3762 if (!ckchs)
3763 return;
3764
3765#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3766 if (ckchs->multi) {
3767 int n;
3768
William Lallemand8621ac52020-04-08 17:38:27 +02003769 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand8c1cdde2019-10-18 10:58:14 +02003770 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
William Lallemand8621ac52020-04-08 17:38:27 +02003771 }
3772 free(ckchs->ckch);
3773 ckchs->ckch = NULL;
William Lallemand8c1cdde2019-10-18 10:58:14 +02003774 } else
3775#endif
3776 {
3777 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
William Lallemand8621ac52020-04-08 17:38:27 +02003778 free(ckchs->ckch);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003779 ckchs->ckch = NULL;
3780 }
3781
3782 free(ckchs);
3783}
3784
3785/* allocate and duplicate a ckch_store
3786 * Return a new ckch_store or NULL */
3787static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3788{
3789 struct ckch_store *dst;
3790 int pathlen;
3791
3792 pathlen = strlen(src->path);
3793 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3794 if (!dst)
3795 return NULL;
3796 /* copy previous key */
3797 memcpy(dst->path, src->path, pathlen + 1);
3798 dst->multi = src->multi;
3799 LIST_INIT(&dst->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +02003800 LIST_INIT(&dst->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003801
3802 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3803 if (!dst->ckch)
3804 goto error;
3805
3806#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3807 if (src->multi) {
3808 int n;
3809
3810 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3811 if (&src->ckch[n]) {
3812 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3813 goto error;
3814 }
3815 }
3816 } else
3817#endif
3818 {
3819 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3820 goto error;
3821 }
3822
3823 return dst;
3824
3825error:
3826 ckchs_free(dst);
3827
3828 return NULL;
3829}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003830
William Lallemand36b84632019-07-18 19:28:17 +02003831/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003832 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003833 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003834static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003835{
3836 struct ebmb_node *eb;
3837
William Lallemande3af8fb2019-10-08 11:36:53 +02003838 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003839 if (!eb)
3840 return NULL;
3841
William Lallemande3af8fb2019-10-08 11:36:53 +02003842 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003843}
3844
3845/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003846 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003847 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003848static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003849{
William Lallemande3af8fb2019-10-08 11:36:53 +02003850 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003851
William Lallemande3af8fb2019-10-08 11:36:53 +02003852 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3853 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003854 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3855 goto end;
3856 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003857 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003858
William Lallemande3af8fb2019-10-08 11:36:53 +02003859 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003860 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3861 goto end;
3862 }
3863
William Lallemand9117de92019-10-04 00:29:42 +02003864 LIST_INIT(&ckchs->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +02003865 LIST_INIT(&ckchs->crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02003866
William Lallemand36b84632019-07-18 19:28:17 +02003867 if (!multi) {
3868
William Lallemand96a9c972019-10-17 11:56:17 +02003869 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003870 goto end;
3871
William Lallemande3af8fb2019-10-08 11:36:53 +02003872 /* insert into the ckchs tree */
3873 memcpy(ckchs->path, path, strlen(path) + 1);
3874 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003875 } else {
3876 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003877#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3878 char fp[MAXPATHLEN+1] = {0};
3879 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003880
3881 /* Load all possible certs and keys */
3882 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3883 struct stat buf;
3884 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3885 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003886 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003887 goto end;
3888 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003889 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003890 }
3891 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003892#endif
William Lallemand36b84632019-07-18 19:28:17 +02003893
3894 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003895 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003896 goto end;
3897 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003898 /* insert into the ckchs tree */
3899 memcpy(ckchs->path, path, strlen(path) + 1);
3900 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003901 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003902 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003903
William Lallemand36b84632019-07-18 19:28:17 +02003904end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003905 if (ckchs) {
3906 free(ckchs->ckch);
3907 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003908 }
3909
William Lallemande3af8fb2019-10-08 11:36:53 +02003910 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003911
3912 return NULL;
3913}
3914
William Lallemandc4ecddf2019-07-31 16:50:08 +02003915#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3916
William Lallemand36b84632019-07-18 19:28:17 +02003917/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003918 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003919 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003920 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3921 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003922 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003923 *
Emeric Brun054563d2019-10-17 13:16:58 +02003924 * Returns a bitfield containing the flags:
3925 * ERR_FATAL in any fatal error case
3926 * ERR_ALERT if the reason of the error is available in err
3927 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003928 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003929 */
Emeric Brun054563d2019-10-17 13:16:58 +02003930static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3931 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3932 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003933{
William Lallemand36b84632019-07-18 19:28:17 +02003934 int i = 0, n = 0;
3935 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003936 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003937 struct ebmb_node *node;
3938 struct ebmb_node *next;
3939 /* Array of SSL_CTX pointers corresponding to each possible combo
3940 * of keytypes
3941 */
3942 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003943 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003944 X509_NAME *xname = NULL;
3945 char *str = NULL;
3946#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3947 STACK_OF(GENERAL_NAME) *names = NULL;
3948#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003949 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003950
Emeric Brun054563d2019-10-17 13:16:58 +02003951 *ckchi = NULL;
3952
William Lallemande3af8fb2019-10-08 11:36:53 +02003953 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003954 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3955 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003956 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003957 }
3958
3959 ckch_inst = ckch_inst_new();
3960 if (!ckch_inst) {
3961 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3962 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003963 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003964 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003965 }
3966
William Lallemande3af8fb2019-10-08 11:36:53 +02003967 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003968
yanbzhu08ce6ab2015-12-02 13:01:29 -05003969 /* Process each ckch and update keytypes for each CN/SAN
3970 * for example, if CN/SAN www.a.com is associated with
3971 * certs with keytype 0 and 2, then at the end of the loop,
3972 * www.a.com will have:
3973 * keyindex = 0 | 1 | 4 = 5
3974 */
3975 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003976 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003977
3978 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3979 continue;
3980
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003981 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003982 for (i = 0; i < fcount; i++) {
3983 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3984 if (ret < 0) {
3985 memprintf(err, "%sunable to allocate SSL context.\n",
3986 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003987 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003988 goto end;
3989 }
3990 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003991 } else {
3992 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3993 * so the line that contains logic is marked via comments
3994 */
3995 xname = X509_get_subject_name(certs_and_keys[n].cert);
3996 i = -1;
3997 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3998 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003999 ASN1_STRING *value;
4000 value = X509_NAME_ENTRY_get_data(entry);
4001 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004002 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004003 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004004
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004005 OPENSSL_free(str);
4006 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004007 if (ret < 0) {
4008 memprintf(err, "%sunable to allocate SSL context.\n",
4009 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004010 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004011 goto end;
4012 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004013 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004014 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004015
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004016 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004017#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004018 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4019 if (names) {
4020 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4021 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004022
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004023 if (name->type == GEN_DNS) {
4024 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4025 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004026 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004027
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004028 OPENSSL_free(str);
4029 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004030 if (ret < 0) {
4031 memprintf(err, "%sunable to allocate SSL context.\n",
4032 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004033 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004034 goto end;
4035 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004036 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004037 }
4038 }
4039 }
4040 }
4041#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4042 }
4043
4044 /* If no files found, return error */
4045 if (eb_is_empty(&sni_keytypes_map)) {
4046 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4047 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004048 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004049 goto end;
4050 }
4051
4052 /* We now have a map of CN/SAN to keytypes that are loaded in
4053 * Iterate through the map to create the SSL_CTX's (if needed)
4054 * and add each CTX to the SNI tree
4055 *
4056 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004057 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004058 * combination is denoted by the key in the map. Each key
4059 * has a value between 1 and 2^n - 1. Conveniently, the array
4060 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4061 * entry in the array to correspond to the unique combo (key)
4062 * associated with i. This unique key combo (i) will be associated
4063 * with combos[i-1]
4064 */
4065
4066 node = ebmb_first(&sni_keytypes_map);
4067 while (node) {
4068 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004069 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004070 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004071
4072 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4073 i = container_of(node, struct sni_keytype, name)->keytypes;
4074 cur_ctx = key_combos[i-1].ctx;
4075
4076 if (cur_ctx == NULL) {
4077 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004078 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004079 if (cur_ctx == NULL) {
4080 memprintf(err, "%sunable to allocate SSL context.\n",
4081 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004082 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004083 goto end;
4084 }
4085
yanbzhube2774d2015-12-10 15:07:30 -05004086 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004087 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4088 if (i & (1<<n)) {
4089 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004090 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004091 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4092 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004093 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004094 }
4095 }
4096
yanbzhu08ce6ab2015-12-02 13:01:29 -05004097 /* Update key_combos */
4098 key_combos[i-1].ctx = cur_ctx;
4099 }
4100
4101 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004102
William Lallemand1d29c742019-10-04 00:53:29 +02004103 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 +02004104 kinfo, str, key_combos[i-1].order);
4105 if (key_combos[i-1].order < 0) {
4106 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004107 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004108 goto end;
4109 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004110 node = ebmb_next(node);
4111 }
4112
4113
4114 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4115 if (!bind_conf->default_ctx) {
4116 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4117 if (key_combos[i].ctx) {
4118 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004119 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004120 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004121 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004122 break;
4123 }
4124 }
4125 }
4126
William Lallemand614ca0d2019-10-07 13:52:11 +02004127 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004128 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004129 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004130
yanbzhu08ce6ab2015-12-02 13:01:29 -05004131end:
4132
4133 if (names)
4134 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4135
yanbzhu08ce6ab2015-12-02 13:01:29 -05004136 node = ebmb_first(&sni_keytypes_map);
4137 while (node) {
4138 next = ebmb_next(node);
4139 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004140 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004141 node = next;
4142 }
4143
William Lallemand02e19a52020-04-08 16:11:26 +02004144 /* we need to free the ctx since we incremented the refcount where it's used */
4145 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4146 if (key_combos[i].ctx)
4147 SSL_CTX_free(key_combos[i].ctx);
4148 }
4149
Emeric Brun054563d2019-10-17 13:16:58 +02004150 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004151 struct sni_ctx *sc0, *sc0b;
4152
William Lallemand0c6d12f2019-10-04 18:38:51 +02004153
4154 /* free the sni_ctx in case of error */
4155 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4156
4157 ebmb_delete(&sc0->name);
4158 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02004159 SSL_CTX_free(sc0->ctx);
William Lallemand0c6d12f2019-10-04 18:38:51 +02004160 free(sc0);
4161 }
William Lallemand02e19a52020-04-08 16:11:26 +02004162 if (ckch_inst->is_default) {
4163 SSL_CTX_free(bind_conf->default_ctx);
4164 bind_conf->default_ctx = NULL;
4165 }
4166
William Lallemand614ca0d2019-10-07 13:52:11 +02004167 free(ckch_inst);
4168 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004169 }
4170
Emeric Brun054563d2019-10-17 13:16:58 +02004171 *ckchi = ckch_inst;
4172 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004173}
4174#else
4175/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004176static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4177 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4178 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004179{
4180 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4181 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004182 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004183}
4184
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004185#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004186
William Lallemand614ca0d2019-10-07 13:52:11 +02004187/*
4188 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004189 *
4190 * Returns a bitfield containing the flags:
4191 * ERR_FATAL in any fatal error case
4192 * ERR_ALERT if the reason of the error is available in err
4193 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004194 */
Emeric Brun054563d2019-10-17 13:16:58 +02004195static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4196 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004197{
William Lallemandc9402072019-05-15 15:33:54 +02004198 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004199 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004200 int order = 0;
4201 X509_NAME *xname;
4202 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004203 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004204 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004205#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4206 STACK_OF(GENERAL_NAME) *names;
4207#endif
William Lallemand36b84632019-07-18 19:28:17 +02004208 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004209 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004210 int errcode = 0;
4211
4212 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004213
William Lallemande3af8fb2019-10-08 11:36:53 +02004214 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004215 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004216
William Lallemande3af8fb2019-10-08 11:36:53 +02004217 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004218
William Lallemandc9402072019-05-15 15:33:54 +02004219 ctx = SSL_CTX_new(SSLv23_server_method());
4220 if (!ctx) {
4221 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4222 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004223 errcode |= ERR_ALERT | ERR_FATAL;
4224 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004225 }
4226
Emeric Bruna96b5822019-10-17 13:25:14 +02004227 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4228 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004229 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004230
4231 ckch_inst = ckch_inst_new();
4232 if (!ckch_inst) {
4233 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4234 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004235 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004236 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004237 }
4238
William Lallemand36b84632019-07-18 19:28:17 +02004239 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004240 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004241 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004242 switch(EVP_PKEY_base_id(pkey)) {
4243 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004244 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004245 break;
4246 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004247 kinfo.sig = TLSEXT_signature_ecdsa;
4248 break;
4249 case EVP_PKEY_DSA:
4250 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004251 break;
4252 }
4253 EVP_PKEY_free(pkey);
4254 }
4255
Emeric Brun50bcecc2013-04-22 13:05:23 +02004256 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004257 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004258 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 +02004259 if (order < 0) {
4260 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004261 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004262 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004263 }
4264 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004265 }
4266 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004267#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004268 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004269 if (names) {
4270 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4271 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4272 if (name->type == GEN_DNS) {
4273 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004274 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004275 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004276 if (order < 0) {
4277 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004278 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004279 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004280 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004281 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004282 }
4283 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004284 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004285 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004286#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004287 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004288 i = -1;
4289 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4290 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004291 ASN1_STRING *value;
4292
4293 value = X509_NAME_ENTRY_get_data(entry);
4294 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004295 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004296 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004297 if (order < 0) {
4298 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004299 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004300 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004301 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004302 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004303 }
4304 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004305 /* we must not free the SSL_CTX anymore below, since it's already in
4306 * the tree, so it will be discovered and cleaned in time.
4307 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004308
Emeric Brunfc0421f2012-09-07 17:30:07 +02004309#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004310 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004311 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4312 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004313 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004314 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004315 }
4316#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004317 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004318 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004319 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004320 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004321 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004322 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004323
William Lallemand9117de92019-10-04 00:29:42 +02004324 /* everything succeed, the ckch instance can be used */
4325 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004326 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004327 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004328
William Lallemand02e19a52020-04-08 16:11:26 +02004329 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4330
Emeric Brun054563d2019-10-17 13:16:58 +02004331 *ckchi = ckch_inst;
4332 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004333
4334error:
4335 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004336 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004337 struct sni_ctx *sc0, *sc0b;
4338
4339 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4340
4341 ebmb_delete(&sc0->name);
William Lallemand02e19a52020-04-08 16:11:26 +02004342 SSL_CTX_free(sc0->ctx);
William Lallemandd9199372019-10-04 15:37:05 +02004343 LIST_DEL(&sc0->by_ckch_inst);
4344 free(sc0);
4345 }
William Lallemand02e19a52020-04-08 16:11:26 +02004346 if (ckch_inst->is_default)
4347 SSL_CTX_free(ctx);
4348
William Lallemand614ca0d2019-10-07 13:52:11 +02004349 free(ckch_inst);
4350 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004351 }
William Lallemandd9199372019-10-04 15:37:05 +02004352 SSL_CTX_free(ctx);
4353
Emeric Brun054563d2019-10-17 13:16:58 +02004354 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004355}
4356
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004357/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004358static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4359 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004360 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004361{
Emeric Brun054563d2019-10-17 13:16:58 +02004362 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004363
4364 /* we found the ckchs in the tree, we can use it directly */
4365 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004366 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 +02004367 else
William Lallemand24bde432020-03-09 16:48:43 +01004368 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 +02004369
Emeric Brun054563d2019-10-17 13:16:58 +02004370 if (errcode & ERR_CODE)
4371 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004372
William Lallemand24bde432020-03-09 16:48:43 +01004373 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004374
4375 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004376 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004377 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004378}
4379
William Lallemand6be66ec2020-03-06 22:26:32 +01004380
William Lallemand4c68bba2020-03-30 18:45:10 +02004381
4382
4383/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4384 * done once. Zero is returned if the operation fails. No error is returned
4385 * if the random is said as not implemented, because we expect that openssl
4386 * will use another method once needed.
4387 */
4388static int ssl_initialize_random()
4389{
4390 unsigned char random;
4391 static int random_initialized = 0;
4392
4393 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4394 random_initialized = 1;
4395
4396 return random_initialized;
4397}
4398
4399/* release ssl bind conf */
4400void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
4401{
4402 if (conf) {
4403#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4404 free(conf->npn_str);
4405 conf->npn_str = NULL;
4406#endif
4407#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4408 free(conf->alpn_str);
4409 conf->alpn_str = NULL;
4410#endif
4411 free(conf->ca_file);
4412 conf->ca_file = NULL;
4413 free(conf->ca_verify_file);
4414 conf->ca_verify_file = NULL;
4415 free(conf->crl_file);
4416 conf->crl_file = NULL;
4417 free(conf->ciphers);
4418 conf->ciphers = NULL;
4419#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
4420 free(conf->ciphersuites);
4421 conf->ciphersuites = NULL;
4422#endif
4423 free(conf->curves);
4424 conf->curves = NULL;
4425 free(conf->ecdhe);
4426 conf->ecdhe = NULL;
4427 }
4428}
4429/* free sni filters */
4430static void crtlist_free_filters(char **args)
4431{
4432 int i;
4433
4434 if (!args)
4435 return;
4436
4437 for (i = 0; args[i]; i++)
4438 free(args[i]);
4439
4440 free(args);
4441}
4442
4443/* Alloc and duplicate a char ** array */
4444static char **crtlist_dup_filters(char **args, int fcount)
4445{
4446 char **dst;
4447 int i;
4448
4449 if (fcount == 0)
4450 return NULL;
4451
4452 dst = calloc(fcount + 1, sizeof(*dst));
4453 if (!dst)
4454 return NULL;
4455
4456 for (i = 0; i < fcount; i++) {
4457 dst[i] = strdup(args[i]);
4458 if (!dst[i])
4459 goto error;
4460 }
4461 return dst;
4462
4463error:
4464 crtlist_free_filters(dst);
4465 return NULL;
4466}
4467
4468
4469/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
4470static void crtlist_free(struct crtlist *crtlist)
4471{
4472 struct crtlist_entry *entry, *s_entry;
4473
4474 if (crtlist == NULL)
4475 return;
4476
4477 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
4478 ebpt_delete(&entry->node);
4479 LIST_DEL(&entry->by_crtlist);
4480 crtlist_free_filters(entry->filters);
4481 ssl_sock_free_ssl_conf(entry->ssl_conf);
4482 free(entry->ssl_conf);
4483 free(entry);
4484 }
4485 ebmb_delete(&crtlist->node);
4486 free(crtlist);
4487}
4488
William Lallemand6be66ec2020-03-06 22:26:32 +01004489/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4490 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4491 *
4492 * This function tries to open and store certificate files.
4493 */
4494static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004495{
William Lallemand6be66ec2020-03-06 22:26:32 +01004496 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004497 struct dirent **de_list;
4498 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004499 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004500 char *end;
4501 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004502 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004503 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004504#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004505 int is_bundle;
4506 int j;
4507#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004508
William Lallemand6be66ec2020-03-06 22:26:32 +01004509 dir = malloc(sizeof(*dir) + strlen(path) + 1);
4510 if (dir == NULL) {
4511 memprintf(err, "not enough memory");
4512 return ERR_ALERT | ERR_FATAL;
4513 }
4514 memcpy(dir->node.key, path, strlen(path) + 1);
4515 dir->entries = EB_ROOT_UNIQUE; /* it's a directory, files are unique */
William Lallemand79d31ec2020-03-25 15:10:49 +01004516 dir->bind_conf = NULL;
William Lallemandc69f02d2020-04-06 19:07:03 +02004517 dir->linecount = 0;
William Lallemand83918e22020-03-16 17:21:51 +01004518 LIST_INIT(&dir->ord_entries);
William Lallemand6be66ec2020-03-06 22:26:32 +01004519
William Lallemand2d232c22020-03-06 22:12:35 +01004520 n = scandir(path, &de_list, 0, alphasort);
4521 if (n < 0) {
4522 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4523 err && *err ? *err : "", path, strerror(errno));
4524 cfgerr |= ERR_ALERT | ERR_FATAL;
4525 }
4526 else {
4527 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004528 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004529 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004530
William Lallemand2d232c22020-03-06 22:12:35 +01004531 end = strrchr(de->d_name, '.');
4532 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4533 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004534
William Lallemanda64593c2020-03-17 20:11:41 +01004535 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4536 if (stat(fp, &buf) != 0) {
4537 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4538 err && *err ? *err : "", fp, strerror(errno));
4539 cfgerr |= ERR_ALERT | ERR_FATAL;
4540 goto ignore_entry;
4541 }
4542 if (!S_ISREG(buf.st_mode))
4543 goto ignore_entry;
4544
William Lallemand6be66ec2020-03-06 22:26:32 +01004545 entry = malloc(sizeof(*entry));
4546 if (entry == NULL) {
4547 memprintf(err, "not enough memory '%s'", fp);
4548 cfgerr |= ERR_ALERT | ERR_FATAL;
4549 goto ignore_entry;
4550 }
4551
William Lallemand83918e22020-03-16 17:21:51 +01004552 /* directories don't use ssl_conf and filters */
William Lallemandc69f02d2020-04-06 19:07:03 +02004553 entry->linenum = 0;
William Lallemand83918e22020-03-16 17:21:51 +01004554 entry->fcount = 0;
4555 entry->filters = NULL;
4556 entry->ssl_conf = NULL;
William Lallemandc2e3b722020-03-31 14:31:41 +02004557 LIST_INIT(&entry->ckch_inst);
William Lallemand83918e22020-03-16 17:21:51 +01004558
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004559#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004560 is_bundle = 0;
4561 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004562
William Lallemand2d232c22020-03-06 22:12:35 +01004563 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4564 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4565 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4566 is_bundle = 1;
4567 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004568 }
William Lallemand2d232c22020-03-06 22:12:35 +01004569 }
yanbzhu63ea8462015-12-09 13:35:14 -05004570
William Lallemand2d232c22020-03-06 22:12:35 +01004571 if (is_bundle) {
4572 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004573
William Lallemand2d232c22020-03-06 22:12:35 +01004574 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004575
William Lallemand2d232c22020-03-06 22:12:35 +01004576 /* increment i and free de until we get to a non-bundle cert
4577 * Note here that we look at de_list[i + 1] before freeing de
4578 * this is important since ignore_entry will free de. This also
4579 * guarantees that de->d_name continues to hold the same prefix.
4580 */
4581 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4582 free(de);
4583 i++;
4584 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004585 }
William Lallemand2d232c22020-03-06 22:12:35 +01004586
4587 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004588 ckchs = ckchs_lookup(fp);
4589 if (ckchs == NULL)
4590 ckchs = ckchs_load_cert_file(fp, 1, err);
4591 if (ckchs == NULL) {
4592 free(de);
4593 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004594 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004595 goto end;
4596 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004597 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004598 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004599 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004600 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004601 ebpt_insert(&dir->entries, &entry->node);
4602
William Lallemand2d232c22020-03-06 22:12:35 +01004603 /* Successfully processed the bundle */
4604 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004605 }
William Lallemand2d232c22020-03-06 22:12:35 +01004606 }
yanbzhu63ea8462015-12-09 13:35:14 -05004607
4608#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004609 ckchs = ckchs_lookup(fp);
4610 if (ckchs == NULL)
4611 ckchs = ckchs_load_cert_file(fp, 0, err);
4612 if (ckchs == NULL) {
4613 free(de);
4614 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004615 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004616 goto end;
4617 }
4618 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004619 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004620 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004621 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004622 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004623
yanbzhu08ce6ab2015-12-02 13:01:29 -05004624ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004625 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004626 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004627end:
William Lallemand2d232c22020-03-06 22:12:35 +01004628 free(de_list);
4629 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004630
4631 if (cfgerr & ERR_CODE) {
4632 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004633 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004634 } else {
4635 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004636 }
William Lallemand2d232c22020-03-06 22:12:35 +01004637 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004638
William Lallemand2d232c22020-03-06 22:12:35 +01004639}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004640
William Lallemand97b08102020-04-01 10:14:00 +02004641/*
4642 * Read a single crt-list line. /!\ alter the <line> string.
4643 * Fill <crt_path> and <crtlist_entry>
4644 * <crtlist_entry> must be alloc and free by the caller
4645 * <crtlist_entry->ssl_conf> is alloc by the function
4646 * <crtlist_entry->filters> is alloc by the function
4647 * <crt_path> is a ptr in <line>
4648 * Return an error code
4649 */
4650static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4651{
4652 int cfgerr = 0;
4653 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4654 char *end;
4655 char *args[MAX_CRT_ARGS + 1];
4656 struct ssl_bind_conf *ssl_conf = NULL;
4657
4658 if (!line || !crt_path || !entry)
4659 return ERR_ALERT | ERR_FATAL;
4660
4661 end = line + strlen(line);
4662 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4663 /* Check if we reached the limit and the last char is not \n.
4664 * Watch out for the last line without the terminating '\n'!
4665 */
4666 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4667 linenum, file, CRT_LINESIZE-1);
4668 cfgerr |= ERR_ALERT | ERR_FATAL;
4669 goto error;
4670 }
4671 arg = 0;
4672 newarg = 1;
4673 while (*line) {
4674 if (isspace((unsigned char)*line)) {
4675 newarg = 1;
4676 *line = 0;
4677 } else if (*line == '[') {
4678 if (ssl_b) {
4679 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4680 cfgerr |= ERR_ALERT | ERR_FATAL;
4681 goto error;
4682 }
4683 if (!arg) {
4684 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4685 cfgerr |= ERR_ALERT | ERR_FATAL;
4686 goto error;
4687 }
4688 ssl_b = arg;
4689 newarg = 1;
4690 *line = 0;
4691 } else if (*line == ']') {
4692 if (ssl_e) {
4693 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4694 cfgerr |= ERR_ALERT | ERR_FATAL;
4695 goto error;
4696 }
4697 if (!ssl_b) {
4698 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4699 cfgerr |= ERR_ALERT | ERR_FATAL;
4700 goto error;
4701 }
4702 ssl_e = arg;
4703 newarg = 1;
4704 *line = 0;
4705 } else if (newarg) {
4706 if (arg == MAX_CRT_ARGS) {
4707 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4708 cfgerr |= ERR_ALERT | ERR_FATAL;
4709 goto error;
4710 }
4711 newarg = 0;
4712 args[arg++] = line;
4713 }
4714 line++;
4715 }
4716 args[arg++] = line;
4717
4718 /* empty line */
4719 if (!*args[0]) {
4720 cfgerr |= ERR_NONE;
4721 goto error;
4722 }
4723
4724 *crt_path = args[0];
4725
4726 ssl_conf = calloc(1, sizeof *ssl_conf);
4727 if (!ssl_conf) {
4728 memprintf(err, "not enough memory!");
4729 cfgerr |= ERR_ALERT | ERR_FATAL;
4730 goto error;
4731 }
4732 cur_arg = ssl_b ? ssl_b : 1;
4733 while (cur_arg < ssl_e) {
4734 newarg = 0;
4735 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4736 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4737 newarg = 1;
4738 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4739 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4740 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4741 args[cur_arg], linenum, file);
4742 cfgerr |= ERR_ALERT | ERR_FATAL;
4743 goto error;
4744 }
4745 cur_arg += 1 + ssl_bind_kws[i].skip;
4746 break;
4747 }
4748 }
4749 if (!cfgerr && !newarg) {
4750 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4751 args[cur_arg], linenum, file);
4752 cfgerr |= ERR_ALERT | ERR_FATAL;
4753 goto error;
4754 }
4755 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004756 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004757 entry->ssl_conf = ssl_conf;
4758 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4759 entry->fcount = arg - cur_arg - 1;
4760
4761 return cfgerr;
4762
4763error:
4764 crtlist_free_filters(entry->filters);
4765 entry->filters = NULL;
4766 ssl_sock_free_ssl_conf(entry->ssl_conf);
4767 free(entry->ssl_conf);
4768 entry->ssl_conf = NULL;
4769 return cfgerr;
4770}
4771
William Lallemand2954c472020-03-06 21:54:13 +01004772/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4773 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4774 *
4775 * This function tries to open and store certificate files.
4776 */
4777static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4778{
4779 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004780 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004781 char thisline[CRT_LINESIZE];
4782 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004783 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004784 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004785 int linenum = 0;
4786 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004787
Willy Tarreauad1731d2013-04-02 17:35:58 +02004788 if ((f = fopen(file, "r")) == NULL) {
4789 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004790 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004791 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004792
William Lallemand2954c472020-03-06 21:54:13 +01004793 newlist = malloc(sizeof(*newlist) + strlen(file) + 1);
4794 if (newlist == NULL) {
4795 memprintf(err, "Not enough memory!");
4796 cfgerr |= ERR_ALERT | ERR_FATAL;
4797 goto error;
4798 }
4799 memcpy(newlist->node.key, file, strlen(file) + 1);
4800 newlist->entries = EB_ROOT;
William Lallemand79d31ec2020-03-25 15:10:49 +01004801 newlist->bind_conf = NULL;
William Lallemand97b08102020-04-01 10:14:00 +02004802 newlist->node.node.leaf_p = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004803 LIST_INIT(&newlist->ord_entries);
4804
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004805 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004806 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004807 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004808 char *crt_path;
4809 struct ssl_bind_conf *ssl_conf = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004810 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004811
4812 linenum++;
4813 end = line + strlen(line);
4814 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4815 /* Check if we reached the limit and the last char is not \n.
4816 * Watch out for the last line without the terminating '\n'!
4817 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004818 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4819 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004820 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004821 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004822 }
William Lallemand97b08102020-04-01 10:14:00 +02004823
4824 if (*line == '#' || *line == '\n' || *line == '\r')
4825 continue;
4826
4827 entry = malloc(sizeof(*entry));
4828 if (entry == NULL) {
4829 memprintf(err, "Not enough memory!");
4830 cfgerr |= ERR_ALERT | ERR_FATAL;
4831 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004832 }
William Lallemand97b08102020-04-01 10:14:00 +02004833 entry->fcount = 0;
4834 entry->filters = NULL;
4835 entry->ssl_conf = NULL;
4836 entry->node.key = NULL;
4837 LIST_INIT(&entry->by_crtlist);
4838
4839 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4840 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004841 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004842 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004843
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004844 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004845 if (!crt_path || !*crt_path) {
4846 free(entry);
4847 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004848 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004849 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004850
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004851 if (*crt_path != '/' && global_ssl.crt_base) {
4852 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4853 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4854 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004855 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004856 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004857 }
4858 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4859 crt_path = path;
4860 }
4861
William Lallemand2954c472020-03-06 21:54:13 +01004862 /* Look for a ckch_store or create one */
4863 ckchs = ckchs_lookup(crt_path);
4864 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004865 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004866 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004867 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004868 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004869 }
William Lallemand909086e2020-03-17 16:53:27 +01004870 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004871 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004872
William Lallemand97b08102020-04-01 10:14:00 +02004873 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004874 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004875
William Lallemand2954c472020-03-06 21:54:13 +01004876 entry->node.key = ckchs;
4877 entry->ssl_conf = ssl_conf;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004878 entry->crtlist = newlist;
William Lallemandc2e3b722020-03-31 14:31:41 +02004879 LIST_INIT(&entry->ckch_inst);
William Lallemand2954c472020-03-06 21:54:13 +01004880 ebpt_insert(&newlist->entries, &entry->node);
4881 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004882 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004883
4884 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004885 }
William Lallemand2954c472020-03-06 21:54:13 +01004886 if (cfgerr & ERR_CODE)
4887 goto error;
4888
William Lallemandc69f02d2020-04-06 19:07:03 +02004889 newlist->linecount = linenum;
4890
William Lallemand2954c472020-03-06 21:54:13 +01004891 fclose(f);
4892 *crtlist = newlist;
4893
4894 return cfgerr;
4895error:
William Lallemand97b08102020-04-01 10:14:00 +02004896 if (entry) {
4897 crtlist_free_filters(entry->filters);
4898 entry->filters = NULL;
4899 ssl_sock_free_ssl_conf(entry->ssl_conf);
4900 free(entry->ssl_conf);
4901 entry->ssl_conf = NULL;
4902 free(entry);
4903 }
4904
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004905 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004906 crtlist_free(newlist);
4907 return cfgerr;
4908}
4909
4910/* Load a crt-list file, this is done in 2 parts:
4911 * - store the content of the file in a crtlist structure with crtlist_entry structures
4912 * - generate the instances by iterating on entries in the crtlist struct
4913 *
4914 * Nothing is locked there, this function is used in the configuration parser.
4915 *
4916 * Returns a set of ERR_* flags possibly with an error in <err>.
4917 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004918int 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 +01004919{
4920 struct crtlist *crtlist = NULL;
4921 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004922 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004923 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004924 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004925 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004926
William Lallemand79d31ec2020-03-25 15:10:49 +01004927 bind_conf_node = malloc(sizeof(*bind_conf_node));
4928 if (!bind_conf_node) {
4929 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4930 cfgerr |= ERR_FATAL | ERR_ALERT;
4931 goto error;
4932 }
4933 bind_conf_node->next = NULL;
4934 bind_conf_node->bind_conf = bind_conf;
4935
William Lallemand41ca9302020-04-08 13:15:18 +02004936 /* strip trailing slashes, including first one */
4937 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4938 *end = 0;
4939
William Lallemand2954c472020-03-06 21:54:13 +01004940 /* look for an existing crtlist or create one */
4941 eb = ebst_lookup(&crtlists_tree, file);
4942 if (eb) {
4943 crtlist = ebmb_entry(eb, struct crtlist, node);
4944 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004945 /* load a crt-list OR a directory */
4946 if (dir)
4947 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4948 else
4949 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4950
William Lallemand2954c472020-03-06 21:54:13 +01004951 if (!(cfgerr & ERR_CODE))
4952 ebst_insert(&crtlists_tree, &crtlist->node);
4953 }
4954
4955 if (cfgerr & ERR_CODE) {
4956 cfgerr |= ERR_FATAL | ERR_ALERT;
4957 goto error;
4958 }
4959
4960 /* generates ckch instance from the crtlist_entry */
4961 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4962 struct ckch_store *store;
4963 struct ckch_inst *ckch_inst = NULL;
4964
4965 store = entry->node.key;
4966 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
4967 if (cfgerr & ERR_CODE) {
4968 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
4969 goto error;
4970 }
William Lallemand49398312020-03-30 17:01:33 +02004971 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02004972 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01004973 }
William Lallemand2954c472020-03-06 21:54:13 +01004974
William Lallemand79d31ec2020-03-25 15:10:49 +01004975 /* add the bind_conf to the list */
4976 bind_conf_node->next = crtlist->bind_conf;
4977 crtlist->bind_conf = bind_conf_node;
4978
William Lallemand2954c472020-03-06 21:54:13 +01004979 return cfgerr;
4980error:
4981 {
William Lallemand49398312020-03-30 17:01:33 +02004982 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01004983 struct ckch_inst *inst, *s_inst;
4984
William Lallemand49398312020-03-30 17:01:33 +02004985 lastentry = entry; /* which entry we tried to generate last */
4986 if (lastentry) {
4987 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4988 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
4989 break;
4990
4991 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
4992 struct sni_ctx *sni, *s_sni;
William Lallemand2954c472020-03-06 21:54:13 +01004993
William Lallemand49398312020-03-30 17:01:33 +02004994 /* this was not generated for this bind_conf, skip */
4995 if (inst->bind_conf != bind_conf)
4996 continue;
4997
4998 /* free the sni_ctx */
4999 list_for_each_entry_safe(sni, s_sni, &inst->sni_ctx, by_ckch_inst) {
5000 ebmb_delete(&sni->name);
5001 LIST_DEL(&sni->by_ckch_inst);
5002 free(sni);
5003 }
5004 LIST_DEL(&inst->by_crtlist_entry);
5005 free(inst);
5006 }
William Lallemand2954c472020-03-06 21:54:13 +01005007 }
William Lallemand2954c472020-03-06 21:54:13 +01005008 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005009 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005010 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005011 return cfgerr;
5012}
5013
William Lallemand06b22a82020-03-16 14:45:55 +01005014/* Returns a set of ERR_* flags possibly with an error in <err>. */
5015int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5016{
5017 struct stat buf;
5018 char fp[MAXPATHLEN+1];
5019 int cfgerr = 0;
5020 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005021 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005022
5023 if ((ckchs = ckchs_lookup(path))) {
5024 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005025 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005026 }
5027 if (stat(path, &buf) == 0) {
5028 if (S_ISDIR(buf.st_mode) == 0) {
5029 ckchs = ckchs_load_cert_file(path, 0, err);
5030 if (!ckchs)
5031 return ERR_ALERT | ERR_FATAL;
5032
William Lallemand24bde432020-03-09 16:48:43 +01005033 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005034 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005035 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005036 }
5037 } else {
5038 /* stat failed, could be a bundle */
5039 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5040 /* try to load a bundle if it is permitted */
5041 ckchs = ckchs_load_cert_file(path, 1, err);
5042 if (!ckchs)
5043 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005044 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005045 } else {
5046 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5047 err && *err ? *err : "", fp, strerror(errno));
5048 cfgerr |= ERR_ALERT | ERR_FATAL;
5049 }
5050 }
5051
5052 return cfgerr;
5053}
5054
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005055/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005056static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005057ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005058{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005059 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005060 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005061 SSL_OP_ALL | /* all known workarounds for bugs */
5062 SSL_OP_NO_SSLv2 |
5063 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005064 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005065 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005066 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005067 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005068 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005069 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005070 SSL_MODE_ENABLE_PARTIAL_WRITE |
5071 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005072 SSL_MODE_RELEASE_BUFFERS |
5073 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005074 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005075 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005076 int flags = MC_SSL_O_ALL;
5077 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005078
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005079 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005080 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005081
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005082 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005083 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5084 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5085 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005086 else
5087 flags = conf_ssl_methods->flags;
5088
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005089 min = conf_ssl_methods->min;
5090 max = conf_ssl_methods->max;
5091 /* start with TLSv10 to remove SSLv3 per default */
5092 if (!min && (!max || max >= CONF_TLSV10))
5093 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005094 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005095 if (min)
5096 flags |= (methodVersions[min].flag - 1);
5097 if (max)
5098 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005099 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005100 min = max = CONF_TLSV_NONE;
5101 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005102 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005103 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005104 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005105 if (min) {
5106 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005107 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5108 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5109 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5110 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005111 hole = 0;
5112 }
5113 max = i;
5114 }
5115 else {
5116 min = max = i;
5117 }
5118 }
5119 else {
5120 if (min)
5121 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005122 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005123 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005124 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5125 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005126 cfgerr += 1;
5127 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005128 /* save real min/max in bind_conf */
5129 conf_ssl_methods->min = min;
5130 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005131
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005132#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005133 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005134 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005135 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005136 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005137 else
5138 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5139 if (flags & methodVersions[i].flag)
5140 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005141#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005142 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005143 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5144 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005145#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005146
5147 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5148 options |= SSL_OP_NO_TICKET;
5149 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5150 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005151
5152#ifdef SSL_OP_NO_RENEGOTIATION
5153 options |= SSL_OP_NO_RENEGOTIATION;
5154#endif
5155
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005156 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005157
Willy Tarreau5db847a2019-05-09 14:13:35 +02005158#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005159 if (global_ssl.async)
5160 mode |= SSL_MODE_ASYNC;
5161#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005162 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005163 if (global_ssl.life_time)
5164 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005165
5166#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5167#ifdef OPENSSL_IS_BORINGSSL
5168 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5169 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005170#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005171 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005172 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005173 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5174 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005175#else
5176 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005177#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005178 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005179#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005180 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005181}
5182
William Lallemand4f45bb92017-10-30 20:08:51 +01005183
5184static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5185{
5186 if (first == block) {
5187 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5188 if (first->len > 0)
5189 sh_ssl_sess_tree_delete(sh_ssl_sess);
5190 }
5191}
5192
5193/* return first block from sh_ssl_sess */
5194static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5195{
5196 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5197
5198}
5199
5200/* store a session into the cache
5201 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5202 * data: asn1 encoded session
5203 * data_len: asn1 encoded session length
5204 * Returns 1 id session was stored (else 0)
5205 */
5206static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5207{
5208 struct shared_block *first;
5209 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5210
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005211 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005212 if (!first) {
5213 /* Could not retrieve enough free blocks to store that session */
5214 return 0;
5215 }
5216
5217 /* STORE the key in the first elem */
5218 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5219 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5220 first->len = sizeof(struct sh_ssl_sess_hdr);
5221
5222 /* it returns the already existing node
5223 or current node if none, never returns null */
5224 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5225 if (oldsh_ssl_sess != sh_ssl_sess) {
5226 /* NOTE: Row couldn't be in use because we lock read & write function */
5227 /* release the reserved row */
5228 shctx_row_dec_hot(ssl_shctx, first);
5229 /* replace the previous session already in the tree */
5230 sh_ssl_sess = oldsh_ssl_sess;
5231 /* ignore the previous session data, only use the header */
5232 first = sh_ssl_sess_first_block(sh_ssl_sess);
5233 shctx_row_inc_hot(ssl_shctx, first);
5234 first->len = sizeof(struct sh_ssl_sess_hdr);
5235 }
5236
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005237 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005238 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005239 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005240 }
5241
5242 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005243
5244 return 1;
5245}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005246
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005247/* SSL callback used when a new session is created while connecting to a server */
5248static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5249{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005250 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005251 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005252
Willy Tarreau07d94e42018-09-20 10:57:52 +02005253 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005254
Olivier Houcharde6060c52017-11-16 17:42:52 +01005255 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5256 int len;
5257 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005258
Olivier Houcharde6060c52017-11-16 17:42:52 +01005259 len = i2d_SSL_SESSION(sess, NULL);
5260 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5261 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5262 } else {
5263 free(s->ssl_ctx.reused_sess[tid].ptr);
5264 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5265 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5266 }
5267 if (s->ssl_ctx.reused_sess[tid].ptr) {
5268 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5269 &ptr);
5270 }
5271 } else {
5272 free(s->ssl_ctx.reused_sess[tid].ptr);
5273 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5274 }
5275
5276 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005277}
5278
Olivier Houcharde6060c52017-11-16 17:42:52 +01005279
William Lallemanded0b5ad2017-10-30 19:36:36 +01005280/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005281int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005282{
5283 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5284 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5285 unsigned char *p;
5286 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005287 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005288 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005289
5290 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005291 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005292 * note: SSL_SESSION_set1_id is using
5293 * a memcpy so we need to use a different pointer
5294 * than sid_data or sid_ctx_data to avoid valgrind
5295 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005296 */
5297
5298 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005299
5300 /* copy value in an other buffer */
5301 memcpy(encid, sid_data, sid_length);
5302
5303 /* pad with 0 */
5304 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5305 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5306
5307 /* force length to zero to avoid ASN1 encoding */
5308 SSL_SESSION_set1_id(sess, encid, 0);
5309
5310 /* force length to zero to avoid ASN1 encoding */
5311 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005312
5313 /* check if buffer is large enough for the ASN1 encoded session */
5314 data_len = i2d_SSL_SESSION(sess, NULL);
5315 if (data_len > SHSESS_MAX_DATA_LEN)
5316 goto err;
5317
5318 p = encsess;
5319
5320 /* process ASN1 session encoding before the lock */
5321 i2d_SSL_SESSION(sess, &p);
5322
William Lallemanded0b5ad2017-10-30 19:36:36 +01005323
William Lallemanda3c77cf2017-10-30 23:44:40 +01005324 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005325 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005326 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005327 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005328err:
5329 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005330 SSL_SESSION_set1_id(sess, encid, sid_length);
5331 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005332
5333 return 0; /* do not increment session reference count */
5334}
5335
5336/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005337SSL_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 +01005338{
William Lallemand4f45bb92017-10-30 20:08:51 +01005339 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005340 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5341 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005342 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005343 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005344
5345 global.shctx_lookups++;
5346
5347 /* allow the session to be freed automatically by openssl */
5348 *do_copy = 0;
5349
5350 /* tree key is zeros padded sessionid */
5351 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5352 memcpy(tmpkey, key, key_len);
5353 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5354 key = tmpkey;
5355 }
5356
5357 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005358 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005359
5360 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005361 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5362 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005363 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005364 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005365 global.shctx_misses++;
5366 return NULL;
5367 }
5368
William Lallemand4f45bb92017-10-30 20:08:51 +01005369 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5370 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005371
William Lallemand4f45bb92017-10-30 20:08:51 +01005372 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 +01005373
William Lallemanda3c77cf2017-10-30 23:44:40 +01005374 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005375
5376 /* decode ASN1 session */
5377 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005378 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005379 /* Reset session id and session id contenxt */
5380 if (sess) {
5381 SSL_SESSION_set1_id(sess, key, key_len);
5382 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5383 }
5384
5385 return sess;
5386}
5387
William Lallemand4f45bb92017-10-30 20:08:51 +01005388
William Lallemanded0b5ad2017-10-30 19:36:36 +01005389/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005390void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005391{
William Lallemand4f45bb92017-10-30 20:08:51 +01005392 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005393 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5394 unsigned int sid_length;
5395 const unsigned char *sid_data;
5396 (void)ctx;
5397
5398 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5399 /* tree key is zeros padded sessionid */
5400 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5401 memcpy(tmpkey, sid_data, sid_length);
5402 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5403 sid_data = tmpkey;
5404 }
5405
William Lallemanda3c77cf2017-10-30 23:44:40 +01005406 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005407
5408 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005409 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5410 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005411 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005412 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005413 }
5414
5415 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005416 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005417}
5418
5419/* Set session cache mode to server and disable openssl internal cache.
5420 * Set shared cache callbacks on an ssl context.
5421 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005422void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005423{
5424 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5425
5426 if (!ssl_shctx) {
5427 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5428 return;
5429 }
5430
5431 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5432 SSL_SESS_CACHE_NO_INTERNAL |
5433 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5434
5435 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005436 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5437 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5438 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005439}
5440
William Lallemand8b453912019-11-21 15:48:10 +01005441/*
5442 * This function applies the SSL configuration on a SSL_CTX
5443 * It returns an error code and fills the <err> buffer
5444 */
5445int 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 +01005446{
5447 struct proxy *curproxy = bind_conf->frontend;
5448 int cfgerr = 0;
5449 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005450 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005451 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005452#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005453 const char *conf_ciphersuites;
5454#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005455 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005456
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005457 if (ssl_conf) {
5458 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5459 int i, min, max;
5460 int flags = MC_SSL_O_ALL;
5461
5462 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005463 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5464 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005465 if (min)
5466 flags |= (methodVersions[min].flag - 1);
5467 if (max)
5468 flags |= ~((methodVersions[max].flag << 1) - 1);
5469 min = max = CONF_TLSV_NONE;
5470 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5471 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5472 if (min)
5473 max = i;
5474 else
5475 min = max = i;
5476 }
5477 /* save real min/max */
5478 conf_ssl_methods->min = min;
5479 conf_ssl_methods->max = max;
5480 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005481 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5482 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005483 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005484 }
5485 }
5486
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005487 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005488 case SSL_SOCK_VERIFY_NONE:
5489 verify = SSL_VERIFY_NONE;
5490 break;
5491 case SSL_SOCK_VERIFY_OPTIONAL:
5492 verify = SSL_VERIFY_PEER;
5493 break;
5494 case SSL_SOCK_VERIFY_REQUIRED:
5495 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5496 break;
5497 }
5498 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5499 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005500 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 +01005501 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 +01005502 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 +01005503 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005504 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005505 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005506 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005507 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005508 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005509 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005510 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5511 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5512 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5513 cfgerr |= ERR_ALERT | ERR_FATAL;
5514 }
5515 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005516 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005517 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 +02005518 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005519 }
Emeric Brun850efd52014-01-29 12:24:34 +01005520 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005521 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5522 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005523 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005524 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005525#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005526 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005527 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5528
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005529 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005530 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5531 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005532 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005533 }
Emeric Brun561e5742012-10-02 15:20:55 +02005534 else {
5535 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5536 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005537 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005538#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005539 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005540 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005541#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005542 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005543 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005544 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5545 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005546 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005547 }
5548 }
5549#endif
5550
William Lallemand4f45bb92017-10-30 20:08:51 +01005551 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005552 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5553 if (conf_ciphers &&
5554 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005555 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5556 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005557 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005558 }
5559
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005560#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005561 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5562 if (conf_ciphersuites &&
5563 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005564 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5565 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005566 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005567 }
5568#endif
5569
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005570#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005571 /* If tune.ssl.default-dh-param has not been set,
5572 neither has ssl-default-dh-file and no static DH
5573 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005574 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005575 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005576 (ssl_dh_ptr_index == -1 ||
5577 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005578 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5579 const SSL_CIPHER * cipher = NULL;
5580 char cipher_description[128];
5581 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5582 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5583 which is not ephemeral DH. */
5584 const char dhe_description[] = " Kx=DH ";
5585 const char dhe_export_description[] = " Kx=DH(";
5586 int idx = 0;
5587 int dhe_found = 0;
5588 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005589
Remi Gacogne23d5d372014-10-10 17:04:26 +02005590 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005591
Remi Gacogne23d5d372014-10-10 17:04:26 +02005592 if (ssl) {
5593 ciphers = SSL_get_ciphers(ssl);
5594
5595 if (ciphers) {
5596 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5597 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5598 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5599 if (strstr(cipher_description, dhe_description) != NULL ||
5600 strstr(cipher_description, dhe_export_description) != NULL) {
5601 dhe_found = 1;
5602 break;
5603 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005604 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005605 }
5606 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005607 SSL_free(ssl);
5608 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005609 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005610
Lukas Tribus90132722014-08-18 00:56:33 +02005611 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005612 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",
5613 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005614 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005615 }
5616
Willy Tarreauef934602016-12-22 23:12:01 +01005617 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005618 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005619
Willy Tarreauef934602016-12-22 23:12:01 +01005620 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005621 if (local_dh_1024 == NULL) {
5622 local_dh_1024 = ssl_get_dh_1024();
5623 }
Willy Tarreauef934602016-12-22 23:12:01 +01005624 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005625 if (local_dh_2048 == NULL) {
5626 local_dh_2048 = ssl_get_dh_2048();
5627 }
Willy Tarreauef934602016-12-22 23:12:01 +01005628 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005629 if (local_dh_4096 == NULL) {
5630 local_dh_4096 = ssl_get_dh_4096();
5631 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005632 }
5633 }
5634 }
5635#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005636
Emeric Brunfc0421f2012-09-07 17:30:07 +02005637 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005638#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005639 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005640#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005641
Bernard Spil13c53f82018-02-15 13:34:58 +01005642#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005643 ssl_conf_cur = NULL;
5644 if (ssl_conf && ssl_conf->npn_str)
5645 ssl_conf_cur = ssl_conf;
5646 else if (bind_conf->ssl_conf.npn_str)
5647 ssl_conf_cur = &bind_conf->ssl_conf;
5648 if (ssl_conf_cur)
5649 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005650#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005651#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005652 ssl_conf_cur = NULL;
5653 if (ssl_conf && ssl_conf->alpn_str)
5654 ssl_conf_cur = ssl_conf;
5655 else if (bind_conf->ssl_conf.alpn_str)
5656 ssl_conf_cur = &bind_conf->ssl_conf;
5657 if (ssl_conf_cur)
5658 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005659#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005660#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005661 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5662 if (conf_curves) {
5663 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005664 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5665 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005666 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005667 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005668 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005669 }
5670#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005671#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005672 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005673 int i;
5674 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005675#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005676 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005677 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5678 NULL);
5679
5680 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005681 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005682 return cfgerr;
5683 }
5684#else
5685 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5686 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5687 ECDHE_DEFAULT_CURVE);
5688#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005689
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005690 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005691 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005692 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5693 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005694 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005695 }
5696 else {
5697 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5698 EC_KEY_free(ecdh);
5699 }
5700 }
5701#endif
5702
Emeric Brunfc0421f2012-09-07 17:30:07 +02005703 return cfgerr;
5704}
5705
Evan Broderbe554312013-06-27 00:05:25 -07005706static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5707{
5708 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5709 size_t prefixlen, suffixlen;
5710
5711 /* Trivial case */
5712 if (strcmp(pattern, hostname) == 0)
5713 return 1;
5714
Evan Broderbe554312013-06-27 00:05:25 -07005715 /* The rest of this logic is based on RFC 6125, section 6.4.3
5716 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5717
Emeric Bruna848dae2013-10-08 11:27:28 +02005718 pattern_wildcard = NULL;
5719 pattern_left_label_end = pattern;
5720 while (*pattern_left_label_end != '.') {
5721 switch (*pattern_left_label_end) {
5722 case 0:
5723 /* End of label not found */
5724 return 0;
5725 case '*':
5726 /* If there is more than one wildcards */
5727 if (pattern_wildcard)
5728 return 0;
5729 pattern_wildcard = pattern_left_label_end;
5730 break;
5731 }
5732 pattern_left_label_end++;
5733 }
5734
5735 /* If it's not trivial and there is no wildcard, it can't
5736 * match */
5737 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005738 return 0;
5739
5740 /* Make sure all labels match except the leftmost */
5741 hostname_left_label_end = strchr(hostname, '.');
5742 if (!hostname_left_label_end
5743 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5744 return 0;
5745
5746 /* Make sure the leftmost label of the hostname is long enough
5747 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005748 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005749 return 0;
5750
5751 /* Finally compare the string on either side of the
5752 * wildcard */
5753 prefixlen = pattern_wildcard - pattern;
5754 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005755 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5756 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005757 return 0;
5758
5759 return 1;
5760}
5761
5762static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5763{
5764 SSL *ssl;
5765 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005766 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005767 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005768 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005769
5770 int depth;
5771 X509 *cert;
5772 STACK_OF(GENERAL_NAME) *alt_names;
5773 int i;
5774 X509_NAME *cert_subject;
5775 char *str;
5776
5777 if (ok == 0)
5778 return ok;
5779
5780 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005781 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005782 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005783
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005784 /* We're checking if the provided hostnames match the desired one. The
5785 * desired hostname comes from the SNI we presented if any, or if not
5786 * provided then it may have been explicitly stated using a "verifyhost"
5787 * directive. If neither is set, we don't care about the name so the
5788 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005789 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005790 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005791 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005792 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005793 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005794 if (!servername)
5795 return ok;
5796 }
Evan Broderbe554312013-06-27 00:05:25 -07005797
5798 /* We only need to verify the CN on the actual server cert,
5799 * not the indirect CAs */
5800 depth = X509_STORE_CTX_get_error_depth(ctx);
5801 if (depth != 0)
5802 return ok;
5803
5804 /* At this point, the cert is *not* OK unless we can find a
5805 * hostname match */
5806 ok = 0;
5807
5808 cert = X509_STORE_CTX_get_current_cert(ctx);
5809 /* It seems like this might happen if verify peer isn't set */
5810 if (!cert)
5811 return ok;
5812
5813 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5814 if (alt_names) {
5815 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5816 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5817 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005818#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005819 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5820#else
Evan Broderbe554312013-06-27 00:05:25 -07005821 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005822#endif
Evan Broderbe554312013-06-27 00:05:25 -07005823 ok = ssl_sock_srv_hostcheck(str, servername);
5824 OPENSSL_free(str);
5825 }
5826 }
5827 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005828 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005829 }
5830
5831 cert_subject = X509_get_subject_name(cert);
5832 i = -1;
5833 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5834 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005835 ASN1_STRING *value;
5836 value = X509_NAME_ENTRY_get_data(entry);
5837 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005838 ok = ssl_sock_srv_hostcheck(str, servername);
5839 OPENSSL_free(str);
5840 }
5841 }
5842
Willy Tarreau71d058c2017-07-26 20:09:56 +02005843 /* report the mismatch and indicate if SNI was used or not */
5844 if (!ok && !conn->err_code)
5845 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005846 return ok;
5847}
5848
Emeric Brun94324a42012-10-11 14:00:19 +02005849/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005850int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005851{
Willy Tarreau03209342016-12-22 17:08:28 +01005852 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005853 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005854 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005855 SSL_OP_ALL | /* all known workarounds for bugs */
5856 SSL_OP_NO_SSLv2 |
5857 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005858 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005859 SSL_MODE_ENABLE_PARTIAL_WRITE |
5860 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005861 SSL_MODE_RELEASE_BUFFERS |
5862 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005863 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005864 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005865 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005866 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005867 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005868
Thierry Fournier383085f2013-01-24 14:15:43 +01005869 /* Make sure openssl opens /dev/urandom before the chroot */
5870 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005871 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005872 cfgerr++;
5873 }
5874
Willy Tarreaufce03112015-01-15 21:32:40 +01005875 /* Automatic memory computations need to know we use SSL there */
5876 global.ssl_used_backend = 1;
5877
5878 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005879 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005880 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005881 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5882 curproxy->id, srv->id,
5883 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005884 cfgerr++;
5885 return cfgerr;
5886 }
5887 }
Emeric Brun94324a42012-10-11 14:00:19 +02005888 if (srv->use_ssl)
5889 srv->xprt = &ssl_sock;
5890 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005891 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005892
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005893 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005894 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005895 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5896 proxy_type_str(curproxy), curproxy->id,
5897 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005898 cfgerr++;
5899 return cfgerr;
5900 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005901
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005902 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005903 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5904 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5905 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005906 else
5907 flags = conf_ssl_methods->flags;
5908
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005909 /* Real min and max should be determinate with configuration and openssl's capabilities */
5910 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005911 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005912 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005913 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005914
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005915 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005916 min = max = CONF_TLSV_NONE;
5917 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005918 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005919 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005920 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005921 if (min) {
5922 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005923 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5924 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5925 proxy_type_str(curproxy), curproxy->id, srv->id,
5926 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005927 hole = 0;
5928 }
5929 max = i;
5930 }
5931 else {
5932 min = max = i;
5933 }
5934 }
5935 else {
5936 if (min)
5937 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005938 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005939 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005940 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5941 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005942 cfgerr += 1;
5943 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005944
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005945#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005946 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005947 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005948 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005949 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005950 else
5951 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5952 if (flags & methodVersions[i].flag)
5953 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005954#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005955 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005956 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5957 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005958#endif
5959
5960 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5961 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005962 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005963
Willy Tarreau5db847a2019-05-09 14:13:35 +02005964#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005965 if (global_ssl.async)
5966 mode |= SSL_MODE_ASYNC;
5967#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005968 SSL_CTX_set_mode(ctx, mode);
5969 srv->ssl_ctx.ctx = ctx;
5970
Emeric Bruna7aa3092012-10-26 12:58:00 +02005971 if (srv->ssl_ctx.client_crt) {
5972 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 +01005973 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5974 proxy_type_str(curproxy), curproxy->id,
5975 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005976 cfgerr++;
5977 }
5978 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 +01005979 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5980 proxy_type_str(curproxy), curproxy->id,
5981 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005982 cfgerr++;
5983 }
5984 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005985 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5986 proxy_type_str(curproxy), curproxy->id,
5987 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005988 cfgerr++;
5989 }
5990 }
Emeric Brun94324a42012-10-11 14:00:19 +02005991
Emeric Brun850efd52014-01-29 12:24:34 +01005992 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5993 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005994 switch (srv->ssl_ctx.verify) {
5995 case SSL_SOCK_VERIFY_NONE:
5996 verify = SSL_VERIFY_NONE;
5997 break;
5998 case SSL_SOCK_VERIFY_REQUIRED:
5999 verify = SSL_VERIFY_PEER;
6000 break;
6001 }
Evan Broderbe554312013-06-27 00:05:25 -07006002 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01006003 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02006004 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01006005 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02006006 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02006007 /* set CAfile to verify */
6008 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6009 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006010 curproxy->id, srv->id,
6011 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006012 cfgerr++;
6013 }
6014 }
Emeric Brun850efd52014-01-29 12:24:34 +01006015 else {
6016 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006017 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",
6018 curproxy->id, srv->id,
6019 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006020 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006021 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6022 curproxy->id, srv->id,
6023 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006024 cfgerr++;
6025 }
Emeric Brunef42d922012-10-11 16:11:36 +02006026#ifdef X509_V_FLAG_CRL_CHECK
6027 if (srv->ssl_ctx.crl_file) {
6028 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6029
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006030 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006031 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6032 curproxy->id, srv->id,
6033 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006034 cfgerr++;
6035 }
6036 else {
6037 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6038 }
6039 }
6040#endif
6041 }
6042
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006043 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6044 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6045 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006046 if (srv->ssl_ctx.ciphers &&
6047 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006048 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6049 curproxy->id, srv->id,
6050 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006051 cfgerr++;
6052 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006053
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006054#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006055 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006056 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006057 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6058 curproxy->id, srv->id,
6059 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6060 cfgerr++;
6061 }
6062#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006063#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6064 if (srv->ssl_ctx.npn_str)
6065 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6066#endif
6067#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6068 if (srv->ssl_ctx.alpn_str)
6069 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6070#endif
6071
Emeric Brun94324a42012-10-11 14:00:19 +02006072
6073 return cfgerr;
6074}
6075
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006076/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006077 * be NULL, in which case nothing is done. Returns the number of errors
6078 * encountered.
6079 */
Willy Tarreau03209342016-12-22 17:08:28 +01006080int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006081{
6082 struct ebmb_node *node;
6083 struct sni_ctx *sni;
6084 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006085 int errcode = 0;
6086 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006087
Willy Tarreaufce03112015-01-15 21:32:40 +01006088 /* Automatic memory computations need to know we use SSL there */
6089 global.ssl_used_frontend = 1;
6090
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006091 /* Make sure openssl opens /dev/urandom before the chroot */
6092 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006093 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006094 err++;
6095 }
6096 /* Create initial_ctx used to start the ssl connection before do switchctx */
6097 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006098 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006099 /* It should not be necessary to call this function, but it's
6100 necessary first to check and move all initialisation related
6101 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006102 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006103 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006104 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006105 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006106
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006107 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006108 while (node) {
6109 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006110 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6111 /* only initialize the CTX on its first occurrence and
6112 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006113 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006114 node = ebmb_next(node);
6115 }
6116
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006117 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006118 while (node) {
6119 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006120 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006121 /* only initialize the CTX on its first occurrence and
6122 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006123 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6124 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006125 node = ebmb_next(node);
6126 }
William Lallemand8b453912019-11-21 15:48:10 +01006127
6128 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006129 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006130 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006131 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006132 err++;
6133 }
6134
6135 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006136 return err;
6137}
6138
Willy Tarreau55d37912016-12-21 23:38:39 +01006139/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6140 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6141 * alerts are directly emitted since the rest of the stack does it below.
6142 */
6143int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6144{
6145 struct proxy *px = bind_conf->frontend;
6146 int alloc_ctx;
6147 int err;
6148
6149 if (!bind_conf->is_ssl) {
6150 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006151 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6152 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006153 }
6154 return 0;
6155 }
6156 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006157 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006158 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6159 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006160 }
6161 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006162 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6163 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006164 return -1;
6165 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006166 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006167 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006168 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006169 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006170 sizeof(*sh_ssl_sess_tree),
6171 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006172 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006173 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6174 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");
6175 else
6176 ha_alert("Unable to allocate SSL session cache.\n");
6177 return -1;
6178 }
6179 /* free block callback */
6180 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6181 /* init the root tree within the extra space */
6182 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6183 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006184 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006185 err = 0;
6186 /* initialize all certificate contexts */
6187 err += ssl_sock_prepare_all_ctx(bind_conf);
6188
6189 /* initialize CA variables if the certificates generation is enabled */
6190 err += ssl_sock_load_ca(bind_conf);
6191
6192 return -err;
6193}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006194
6195/* release ssl context allocated for servers. */
6196void ssl_sock_free_srv_ctx(struct server *srv)
6197{
Olivier Houchardc7566002018-11-20 23:33:50 +01006198#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6199 if (srv->ssl_ctx.alpn_str)
6200 free(srv->ssl_ctx.alpn_str);
6201#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006202#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006203 if (srv->ssl_ctx.npn_str)
6204 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006205#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006206 if (srv->ssl_ctx.ctx)
6207 SSL_CTX_free(srv->ssl_ctx.ctx);
6208}
6209
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006210/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006211 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6212 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006213void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006214{
6215 struct ebmb_node *node, *back;
6216 struct sni_ctx *sni;
6217
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006218 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006219 while (node) {
6220 sni = ebmb_entry(node, struct sni_ctx, name);
6221 back = ebmb_next(node);
6222 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006223 SSL_CTX_free(sni->ctx);
6224 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006225 ssl_sock_free_ssl_conf(sni->conf);
6226 free(sni->conf);
6227 sni->conf = NULL;
6228 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006229 free(sni);
6230 node = back;
6231 }
6232
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006233 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006234 while (node) {
6235 sni = ebmb_entry(node, struct sni_ctx, name);
6236 back = ebmb_next(node);
6237 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006238 SSL_CTX_free(sni->ctx);
6239 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006240 ssl_sock_free_ssl_conf(sni->conf);
6241 free(sni->conf);
6242 sni->conf = NULL;
6243 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006244 free(sni);
6245 node = back;
6246 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006247 SSL_CTX_free(bind_conf->initial_ctx);
6248 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006249 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006250 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006251 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006252}
6253
Willy Tarreau795cdab2016-12-22 17:30:54 +01006254/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6255void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6256{
6257 ssl_sock_free_ca(bind_conf);
6258 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006259 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006260 free(bind_conf->ca_sign_file);
6261 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006262 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006263 free(bind_conf->keys_ref->filename);
6264 free(bind_conf->keys_ref->tlskeys);
6265 LIST_DEL(&bind_conf->keys_ref->list);
6266 free(bind_conf->keys_ref);
6267 }
6268 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006269 bind_conf->ca_sign_pass = NULL;
6270 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006271}
6272
Christopher Faulet31af49d2015-06-09 17:29:50 +02006273/* Load CA cert file and private key used to generate certificates */
6274int
Willy Tarreau03209342016-12-22 17:08:28 +01006275ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006276{
Willy Tarreau03209342016-12-22 17:08:28 +01006277 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006278 FILE *fp;
6279 X509 *cacert = NULL;
6280 EVP_PKEY *capkey = NULL;
6281 int err = 0;
6282
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006283 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006284 return err;
6285
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006286#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006287 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006288 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006289 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006290 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006291 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006292#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006293
Christopher Faulet31af49d2015-06-09 17:29:50 +02006294 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006295 ha_alert("Proxy '%s': cannot enable certificate generation, "
6296 "no CA certificate File configured at [%s:%d].\n",
6297 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006298 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006299 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006300
6301 /* read in the CA certificate */
6302 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006303 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6304 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006305 goto load_error;
6306 }
6307 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006308 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6309 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006310 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006311 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006312 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006313 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006314 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6315 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006316 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006317 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006318
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006319 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006320 bind_conf->ca_sign_cert = cacert;
6321 bind_conf->ca_sign_pkey = capkey;
6322 return err;
6323
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006324 read_error:
6325 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006326 if (capkey) EVP_PKEY_free(capkey);
6327 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006328 load_error:
6329 bind_conf->generate_certs = 0;
6330 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006331 return err;
6332}
6333
6334/* Release CA cert and private key used to generate certificated */
6335void
6336ssl_sock_free_ca(struct bind_conf *bind_conf)
6337{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006338 if (bind_conf->ca_sign_pkey)
6339 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6340 if (bind_conf->ca_sign_cert)
6341 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006342 bind_conf->ca_sign_pkey = NULL;
6343 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006344}
6345
Emeric Brun46591952012-05-18 15:47:34 +02006346/*
6347 * This function is called if SSL * context is not yet allocated. The function
6348 * is designed to be called before any other data-layer operation and sets the
6349 * handshake flag on the connection. It is safe to call it multiple times.
6350 * It returns 0 on success and -1 in error case.
6351 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006352static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006353{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006354 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006355 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006356 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006357 return 0;
6358
Willy Tarreau3c728722014-01-23 13:50:42 +01006359 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006360 return 0;
6361
Olivier Houchard66ab4982019-02-26 18:37:15 +01006362 ctx = pool_alloc(ssl_sock_ctx_pool);
6363 if (!ctx) {
6364 conn->err_code = CO_ER_SSL_NO_MEM;
6365 return -1;
6366 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006367 ctx->wait_event.tasklet = tasklet_new();
6368 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006369 conn->err_code = CO_ER_SSL_NO_MEM;
6370 pool_free(ssl_sock_ctx_pool, ctx);
6371 return -1;
6372 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006373 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6374 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006375 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006376 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006377 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006378 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006379 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006380 ctx->xprt_st = 0;
6381 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006382
6383 /* Only work with sockets for now, this should be adapted when we'll
6384 * add QUIC support.
6385 */
6386 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006387 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006388 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6389 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006390 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006391
Willy Tarreau20879a02012-12-03 16:32:10 +01006392 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6393 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006394 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006395 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006396
Emeric Brun46591952012-05-18 15:47:34 +02006397 /* If it is in client mode initiate SSL session
6398 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006399 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006400 int may_retry = 1;
6401
6402 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006403 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006404 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6405 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006406 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006407 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006408 goto retry_connect;
6409 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006410 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006411 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006412 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006413 ctx->bio = BIO_new(ha_meth);
6414 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006415 SSL_free(ctx->ssl);
6416 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006417 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006418 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006419 goto retry_connect;
6420 }
Emeric Brun55476152014-11-12 17:35:37 +01006421 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006422 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006423 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006424 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006425 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006426
Evan Broderbe554312013-06-27 00:05:25 -07006427 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006428 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6429 SSL_free(ctx->ssl);
6430 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006431 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006432 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006433 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006434 goto retry_connect;
6435 }
Emeric Brun55476152014-11-12 17:35:37 +01006436 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006437 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006438 }
6439
Olivier Houchard66ab4982019-02-26 18:37:15 +01006440 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006441 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6442 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6443 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 +01006444 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006445 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006446 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6447 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006448 } else if (sess) {
6449 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006450 }
6451 }
Evan Broderbe554312013-06-27 00:05:25 -07006452
Emeric Brun46591952012-05-18 15:47:34 +02006453 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006454 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006455
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006456 _HA_ATOMIC_ADD(&sslconns, 1);
6457 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006458 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006459 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006460 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006461 return 0;
6462 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006463 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006464 int may_retry = 1;
6465
6466 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006467 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006468 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6469 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006470 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006471 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006472 goto retry_accept;
6473 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006474 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006475 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006476 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006477 ctx->bio = BIO_new(ha_meth);
6478 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006479 SSL_free(ctx->ssl);
6480 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006481 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006482 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006483 goto retry_accept;
6484 }
Emeric Brun55476152014-11-12 17:35:37 +01006485 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006486 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006487 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006488 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006489 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006490
Emeric Brune1f38db2012-09-03 20:36:47 +02006491 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006492 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6493 SSL_free(ctx->ssl);
6494 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006495 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006496 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006497 goto retry_accept;
6498 }
Emeric Brun55476152014-11-12 17:35:37 +01006499 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006500 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006501 }
6502
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006503#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6504 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6505 b_alloc(&ctx->early_buf);
6506 SSL_set_max_early_data(ctx->ssl,
6507 /* Only allow early data if we managed to allocate
6508 * a buffer.
6509 */
6510 (!b_is_null(&ctx->early_buf)) ?
6511 global.tune.bufsize - global.tune.maxrewrite : 0);
6512 }
6513#endif
6514
Olivier Houchard66ab4982019-02-26 18:37:15 +01006515 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006516
Emeric Brun46591952012-05-18 15:47:34 +02006517 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006518 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006519#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006520 conn->flags |= CO_FL_EARLY_SSL_HS;
6521#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006522
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006523 _HA_ATOMIC_ADD(&sslconns, 1);
6524 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006525 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006526 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006527 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006528 return 0;
6529 }
6530 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006531 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006532err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006533 if (ctx && ctx->wait_event.tasklet)
6534 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006535 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006536 return -1;
6537}
6538
6539
6540/* This is the callback which is used when an SSL handshake is pending. It
6541 * updates the FD status if it wants some polling before being called again.
6542 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6543 * otherwise it returns non-zero and removes itself from the connection's
6544 * flags (the bit is provided in <flag> by the caller).
6545 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006546static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006547{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006548 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006549 int ret;
6550
Willy Tarreau3c728722014-01-23 13:50:42 +01006551 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006552 return 0;
6553
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006554 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006555 goto out_error;
6556
Willy Tarreau5db847a2019-05-09 14:13:35 +02006557#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006558 /*
6559 * Check if we have early data. If we do, we have to read them
6560 * before SSL_do_handshake() is called, And there's no way to
6561 * detect early data, except to try to read them
6562 */
6563 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006564 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006565
Olivier Houchard54907bb2019-12-19 15:02:39 +01006566 while (1) {
6567 ret = SSL_read_early_data(ctx->ssl,
6568 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6569 &read_data);
6570 if (ret == SSL_READ_EARLY_DATA_ERROR)
6571 goto check_error;
6572 if (read_data > 0) {
6573 conn->flags |= CO_FL_EARLY_DATA;
6574 b_add(&ctx->early_buf, read_data);
6575 }
6576 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6577 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6578 if (!b_data(&ctx->early_buf))
6579 b_free(&ctx->early_buf);
6580 break;
6581 }
6582 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006583 }
6584#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006585 /* If we use SSL_do_handshake to process a reneg initiated by
6586 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6587 * Usually SSL_write and SSL_read are used and process implicitly
6588 * the reneg handshake.
6589 * Here we use SSL_peek as a workaround for reneg.
6590 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006591 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006592 char c;
6593
Olivier Houchard66ab4982019-02-26 18:37:15 +01006594 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006595 if (ret <= 0) {
6596 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006597 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006598
Emeric Brun674b7432012-11-08 19:21:55 +01006599 if (ret == SSL_ERROR_WANT_WRITE) {
6600 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006601 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006602 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006603 return 0;
6604 }
6605 else if (ret == SSL_ERROR_WANT_READ) {
6606 /* handshake may have been completed but we have
6607 * no more data to read.
6608 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006609 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006610 ret = 1;
6611 goto reneg_ok;
6612 }
6613 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006614 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006615 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006616 return 0;
6617 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006618#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006619 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006620 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006621 return 0;
6622 }
6623#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006624 else if (ret == SSL_ERROR_SYSCALL) {
6625 /* if errno is null, then connection was successfully established */
6626 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6627 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006628 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006629#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6630 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006631 conn->err_code = CO_ER_SSL_HANDSHAKE;
6632#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006633 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006634#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006635 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006636 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006637 empty_handshake = state == TLS_ST_BEFORE;
6638#else
Lukas Tribus49799162019-07-08 14:29:15 +02006639 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6640 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006641#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006642 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006643 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006644 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006645 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6646 else
6647 conn->err_code = CO_ER_SSL_EMPTY;
6648 }
6649 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006650 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006651 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6652 else
6653 conn->err_code = CO_ER_SSL_ABORT;
6654 }
6655 }
6656 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006657 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006658 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006659 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006660 conn->err_code = CO_ER_SSL_HANDSHAKE;
6661 }
Lukas Tribus49799162019-07-08 14:29:15 +02006662#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006663 }
Emeric Brun674b7432012-11-08 19:21:55 +01006664 goto out_error;
6665 }
6666 else {
6667 /* Fail on all other handshake errors */
6668 /* Note: OpenSSL may leave unread bytes in the socket's
6669 * buffer, causing an RST to be emitted upon close() on
6670 * TCP sockets. We first try to drain possibly pending
6671 * data to avoid this as much as possible.
6672 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006673 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006674 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006675 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006676 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006677 goto out_error;
6678 }
6679 }
6680 /* read some data: consider handshake completed */
6681 goto reneg_ok;
6682 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006683 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006684check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006685 if (ret != 1) {
6686 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006687 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006688
6689 if (ret == SSL_ERROR_WANT_WRITE) {
6690 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006691 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006692 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006693 return 0;
6694 }
6695 else if (ret == SSL_ERROR_WANT_READ) {
6696 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006697 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006698 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6699 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006700 return 0;
6701 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006702#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006703 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006704 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006705 return 0;
6706 }
6707#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006708 else if (ret == SSL_ERROR_SYSCALL) {
6709 /* if errno is null, then connection was successfully established */
6710 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6711 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006712 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006713#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6714 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006715 conn->err_code = CO_ER_SSL_HANDSHAKE;
6716#else
6717 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006718#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006719 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006720 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006721 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006722#else
Lukas Tribus49799162019-07-08 14:29:15 +02006723 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6724 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006725#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006726 if (empty_handshake) {
6727 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006728 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006729 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6730 else
6731 conn->err_code = CO_ER_SSL_EMPTY;
6732 }
6733 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006734 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006735 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6736 else
6737 conn->err_code = CO_ER_SSL_ABORT;
6738 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006739 }
6740 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006741 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006742 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6743 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006744 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006745 }
Lukas Tribus49799162019-07-08 14:29:15 +02006746#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006747 }
Willy Tarreau89230192012-09-28 20:22:13 +02006748 goto out_error;
6749 }
Emeric Brun46591952012-05-18 15:47:34 +02006750 else {
6751 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006752 /* Note: OpenSSL may leave unread bytes in the socket's
6753 * buffer, causing an RST to be emitted upon close() on
6754 * TCP sockets. We first try to drain possibly pending
6755 * data to avoid this as much as possible.
6756 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006757 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006758 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006759 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006760 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006761 goto out_error;
6762 }
6763 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006764#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006765 else {
6766 /*
6767 * If the server refused the early data, we have to send a
6768 * 425 to the client, as we no longer have the data to sent
6769 * them again.
6770 */
6771 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006772 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006773 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6774 goto out_error;
6775 }
6776 }
6777 }
6778#endif
6779
Emeric Brun46591952012-05-18 15:47:34 +02006780
Emeric Brun674b7432012-11-08 19:21:55 +01006781reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006782
Willy Tarreau5db847a2019-05-09 14:13:35 +02006783#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006784 /* ASYNC engine API doesn't support moving read/write
6785 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006786 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006787 */
6788 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006789 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006790#endif
Emeric Brun46591952012-05-18 15:47:34 +02006791 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006792 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006793 if (objt_server(conn->target)) {
6794 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6795 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6796 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006797 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006798 else {
6799 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6800 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6801 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6802 }
Emeric Brun46591952012-05-18 15:47:34 +02006803 }
6804
6805 /* The connection is now established at both layers, it's time to leave */
6806 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6807 return 1;
6808
6809 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006810 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006811 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006812 ERR_clear_error();
6813
Emeric Brun9fa89732012-10-04 17:09:56 +02006814 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006815 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6816 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6817 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006818 }
6819
Emeric Brun46591952012-05-18 15:47:34 +02006820 /* Fail on all other handshake errors */
6821 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006822 if (!conn->err_code)
6823 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006824 return 0;
6825}
6826
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006827/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6828 * event subscriber <es> is not allowed to change from a previous call as long
6829 * as at least one event is still subscribed. The <event_type> must only be a
6830 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6831 * unless the transport layer was already released.
6832 */
6833static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006834{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006835 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006836
Olivier Houchard0ff28652019-06-24 18:57:39 +02006837 if (!ctx)
6838 return -1;
6839
Willy Tarreau113d52b2020-01-10 09:20:26 +01006840 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006841 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006842
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006843 ctx->subs = es;
6844 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006845
6846 /* we may have to subscribe to lower layers for new events */
6847 event_type &= ~ctx->wait_event.events;
6848 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6849 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006850 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006851}
6852
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006853/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6854 * The <es> pointer is not allowed to differ from the one passed to the
6855 * subscribe() call. It always returns zero.
6856 */
6857static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006858{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006859 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006860
Willy Tarreau113d52b2020-01-10 09:20:26 +01006861 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006862 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006863
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006864 es->events &= ~event_type;
6865 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006866 ctx->subs = NULL;
6867
6868 /* If we subscribed, and we're not doing the handshake,
6869 * then we subscribed because the upper layer asked for it,
6870 * as the upper layer is no longer interested, we can
6871 * unsubscribe too.
6872 */
6873 event_type &= ctx->wait_event.events;
6874 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6875 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006876
6877 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006878}
6879
Olivier Houchard2e055482019-05-27 19:50:12 +02006880/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6881 * Returns 0 on success, and non-zero on failure.
6882 */
6883static 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)
6884{
6885 struct ssl_sock_ctx *ctx = xprt_ctx;
6886
6887 if (oldxprt_ops != NULL)
6888 *oldxprt_ops = ctx->xprt;
6889 if (oldxprt_ctx != NULL)
6890 *oldxprt_ctx = ctx->xprt_ctx;
6891 ctx->xprt = toadd_ops;
6892 ctx->xprt_ctx = toadd_ctx;
6893 return 0;
6894}
6895
Olivier Houchard5149b592019-05-23 17:47:36 +02006896/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6897 * return 0, otherwise just call the remove_xprt method from the underlying
6898 * XPRT.
6899 */
6900static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6901{
6902 struct ssl_sock_ctx *ctx = xprt_ctx;
6903
6904 if (ctx->xprt_ctx == toremove_ctx) {
6905 ctx->xprt_ctx = newctx;
6906 ctx->xprt = newops;
6907 return 0;
6908 }
6909 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6910}
6911
Olivier Houchardea8dd942019-05-20 14:02:16 +02006912static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6913{
6914 struct ssl_sock_ctx *ctx = context;
6915
6916 /* First if we're doing an handshake, try that */
6917 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6918 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6919 /* If we had an error, or the handshake is done and I/O is available,
6920 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006921 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006922 * we can't be sure conn_fd_handler() will be called again.
6923 */
6924 if ((ctx->conn->flags & CO_FL_ERROR) ||
6925 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6926 int ret = 0;
6927 int woke = 0;
6928
6929 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006930 if (ctx->subs) {
6931 tasklet_wakeup(ctx->subs->tasklet);
6932 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006933 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006934 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006935 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006936
Olivier Houchardea8dd942019-05-20 14:02:16 +02006937 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006938 * upper layers know. If we have no mux, create it,
6939 * and once we have a mux, call its wake method if we didn't
6940 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006941 */
6942 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006943 if (!ctx->conn->mux)
6944 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006945 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6946 ctx->conn->mux->wake(ctx->conn);
6947 return NULL;
6948 }
6949 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006950#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6951 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006952 else if (b_data(&ctx->early_buf) && ctx->subs &&
6953 ctx->subs->events & SUB_RETRY_RECV) {
6954 tasklet_wakeup(ctx->subs->tasklet);
6955 ctx->subs->events &= ~SUB_RETRY_RECV;
6956 if (!ctx->subs->events)
6957 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006958 }
6959#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006960 return NULL;
6961}
6962
Emeric Brun46591952012-05-18 15:47:34 +02006963/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006964 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006965 * buffer wraps, in which case a second call may be performed. The connection's
6966 * flags are updated with whatever special event is detected (error, read0,
6967 * empty). The caller is responsible for taking care of those events and
6968 * avoiding the call if inappropriate. The function does not call the
6969 * connection's polling update function, so the caller is responsible for this.
6970 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006971static 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 +02006972{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006973 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006974 ssize_t ret;
6975 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006976
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006977 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006978 goto out_error;
6979
Olivier Houchard54907bb2019-12-19 15:02:39 +01006980#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6981 if (b_data(&ctx->early_buf)) {
6982 try = b_contig_space(buf);
6983 if (try > b_data(&ctx->early_buf))
6984 try = b_data(&ctx->early_buf);
6985 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6986 b_add(buf, try);
6987 b_del(&ctx->early_buf, try);
6988 if (b_data(&ctx->early_buf) == 0)
6989 b_free(&ctx->early_buf);
6990 return try;
6991 }
6992#endif
6993
Willy Tarreau911db9b2020-01-23 16:27:54 +01006994 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006995 /* a handshake was requested */
6996 return 0;
6997
Emeric Brun46591952012-05-18 15:47:34 +02006998 /* read the largest possible block. For this, we perform only one call
6999 * to recv() unless the buffer wraps and we exactly fill the first hunk,
7000 * in which case we accept to do it once again. A new attempt is made on
7001 * EINTR too.
7002 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01007003 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007004
Willy Tarreau591d4452018-06-15 17:21:00 +02007005 try = b_contig_space(buf);
7006 if (!try)
7007 break;
7008
Willy Tarreauabf08d92014-01-14 11:31:27 +01007009 if (try > count)
7010 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007011
Olivier Houchard66ab4982019-02-26 18:37:15 +01007012 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007013
Emeric Brune1f38db2012-09-03 20:36:47 +02007014 if (conn->flags & CO_FL_ERROR) {
7015 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007016 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007017 }
Emeric Brun46591952012-05-18 15:47:34 +02007018 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007019 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007020 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007021 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007022 }
Emeric Brun46591952012-05-18 15:47:34 +02007023 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007024 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007025 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007026 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007027 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007028 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007029#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007030 /* Async mode can be re-enabled, because we're leaving data state.*/
7031 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007032 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007033#endif
Emeric Brun46591952012-05-18 15:47:34 +02007034 break;
7035 }
7036 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007037 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007038 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7039 SUB_RETRY_RECV,
7040 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007041 /* handshake is running, and it may need to re-enable read */
7042 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007043#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007044 /* Async mode can be re-enabled, because we're leaving data state.*/
7045 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007046 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007047#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007048 break;
7049 }
Emeric Brun46591952012-05-18 15:47:34 +02007050 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007051 } else if (ret == SSL_ERROR_ZERO_RETURN)
7052 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007053 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7054 * stack before shutting down the connection for
7055 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007056 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7057 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007058 /* otherwise it's a real error */
7059 goto out_error;
7060 }
7061 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007062 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007063 return done;
7064
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007065 clear_ssl_error:
7066 /* Clear openssl global errors stack */
7067 ssl_sock_dump_errors(conn);
7068 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007069 read0:
7070 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007071 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007072
Emeric Brun46591952012-05-18 15:47:34 +02007073 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007074 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007075 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007076 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007077 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007078 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007079}
7080
7081
Willy Tarreau787db9a2018-06-14 18:31:46 +02007082/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7083 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7084 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007085 * Only one call to send() is performed, unless the buffer wraps, in which case
7086 * a second call may be performed. The connection's flags are updated with
7087 * whatever special event is detected (error, empty). The caller is responsible
7088 * for taking care of those events and avoiding the call if inappropriate. The
7089 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007090 * is responsible for this. The buffer's output is not adjusted, it's up to the
7091 * caller to take care of this. It's up to the caller to update the buffer's
7092 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007093 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007094static 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 +02007095{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007096 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007097 ssize_t ret;
7098 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007099
7100 done = 0;
7101
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007102 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007103 goto out_error;
7104
Willy Tarreau911db9b2020-01-23 16:27:54 +01007105 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007106 /* a handshake was requested */
7107 return 0;
7108
7109 /* send the largest possible block. For this we perform only one call
7110 * to send() unless the buffer wraps and we exactly fill the first hunk,
7111 * in which case we accept to do it once again.
7112 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007113 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007114#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007115 size_t written_data;
7116#endif
7117
Willy Tarreau787db9a2018-06-14 18:31:46 +02007118 try = b_contig_data(buf, done);
7119 if (try > count)
7120 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007121
Willy Tarreau7bed9452014-02-02 02:00:24 +01007122 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007123 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007124 global_ssl.max_record && try > global_ssl.max_record) {
7125 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007126 }
7127 else {
7128 /* we need to keep the information about the fact that
7129 * we're not limiting the upcoming send(), because if it
7130 * fails, we'll have to retry with at least as many data.
7131 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007132 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007133 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007134
Willy Tarreau5db847a2019-05-09 14:13:35 +02007135#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007136 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007137 unsigned int max_early;
7138
Olivier Houchard522eea72017-11-03 16:27:47 +01007139 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007140 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007141 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007142 if (SSL_get0_session(ctx->ssl))
7143 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007144 else
7145 max_early = 0;
7146 }
7147
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007148 if (try + ctx->sent_early_data > max_early) {
7149 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007150 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007151 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007152 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007153 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007154 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007155 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007156 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007157 if (ret == 1) {
7158 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007159 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007160 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007161 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007162 /* Initiate the handshake, now */
7163 tasklet_wakeup(ctx->wait_event.tasklet);
7164 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007165
Olivier Houchardc2aae742017-09-22 18:26:28 +02007166 }
7167
7168 } else
7169#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007170 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007171
Emeric Brune1f38db2012-09-03 20:36:47 +02007172 if (conn->flags & CO_FL_ERROR) {
7173 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007174 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007175 }
Emeric Brun46591952012-05-18 15:47:34 +02007176 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007177 /* A send succeeded, so we can consider ourself connected */
7178 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007179 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007180 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007181 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007182 }
7183 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007184 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007185
Emeric Brun46591952012-05-18 15:47:34 +02007186 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007187 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007188 /* handshake is running, and it may need to re-enable write */
7189 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007190 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007191#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007192 /* Async mode can be re-enabled, because we're leaving data state.*/
7193 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007194 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007195#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007196 break;
7197 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007198
Emeric Brun46591952012-05-18 15:47:34 +02007199 break;
7200 }
7201 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007202 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007203 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007204 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7205 SUB_RETRY_RECV,
7206 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007207#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007208 /* Async mode can be re-enabled, because we're leaving data state.*/
7209 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007210 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007211#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007212 break;
7213 }
Emeric Brun46591952012-05-18 15:47:34 +02007214 goto out_error;
7215 }
7216 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007217 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007218 return done;
7219
7220 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007221 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007222 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007223 ERR_clear_error();
7224
Emeric Brun46591952012-05-18 15:47:34 +02007225 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007226 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007227}
7228
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007229static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007230
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007231 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007232
Olivier Houchardea8dd942019-05-20 14:02:16 +02007233
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007234 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007235 if (ctx->wait_event.events != 0)
7236 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7237 ctx->wait_event.events,
7238 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007239 if (ctx->subs) {
7240 ctx->subs->events = 0;
7241 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007242 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007243
Olivier Houchard692c1d02019-05-23 18:41:47 +02007244 if (ctx->xprt->close)
7245 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007246#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007247 if (global_ssl.async) {
7248 OSSL_ASYNC_FD all_fd[32], afd;
7249 size_t num_all_fds = 0;
7250 int i;
7251
Olivier Houchard66ab4982019-02-26 18:37:15 +01007252 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007253 if (num_all_fds > 32) {
7254 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7255 return;
7256 }
7257
Olivier Houchard66ab4982019-02-26 18:37:15 +01007258 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007259
7260 /* If an async job is pending, we must try to
7261 to catch the end using polling before calling
7262 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007263 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007264 for (i=0 ; i < num_all_fds ; i++) {
7265 /* switch on an handler designed to
7266 * handle the SSL_free
7267 */
7268 afd = all_fd[i];
7269 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007270 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007271 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007272 /* To ensure that the fd cache won't be used
7273 * and we'll catch a real RD event.
7274 */
7275 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007276 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007277 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007278 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007279 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007280 return;
7281 }
Emeric Brun3854e012017-05-17 20:42:48 +02007282 /* Else we can remove the fds from the fdtab
7283 * and call SSL_free.
7284 * note: we do a fd_remove and not a delete
7285 * because the fd is owned by the engine.
7286 * the engine is responsible to close
7287 */
7288 for (i=0 ; i < num_all_fds ; i++)
7289 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007290 }
7291#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007292 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007293 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007294 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007295 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007296 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007297 }
Emeric Brun46591952012-05-18 15:47:34 +02007298}
7299
7300/* This function tries to perform a clean shutdown on an SSL connection, and in
7301 * any case, flags the connection as reusable if no handshake was in progress.
7302 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007303static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007304{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007305 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007306
Willy Tarreau911db9b2020-01-23 16:27:54 +01007307 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007308 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007309 if (!clean)
7310 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007311 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007312 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007313 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007314 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007315 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007316 ERR_clear_error();
7317 }
Emeric Brun46591952012-05-18 15:47:34 +02007318}
7319
William Lallemandd4f946c2019-12-05 10:26:40 +01007320/* fill a buffer with the algorithm and size of a public key */
7321static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007322{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007323 int bits = 0;
7324 int sig = TLSEXT_signature_anonymous;
7325 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007326 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007327
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007328 pkey = X509_get_pubkey(crt);
7329 if (pkey) {
7330 bits = EVP_PKEY_bits(pkey);
7331 switch(EVP_PKEY_base_id(pkey)) {
7332 case EVP_PKEY_RSA:
7333 sig = TLSEXT_signature_rsa;
7334 break;
7335 case EVP_PKEY_EC:
7336 sig = TLSEXT_signature_ecdsa;
7337 break;
7338 case EVP_PKEY_DSA:
7339 sig = TLSEXT_signature_dsa;
7340 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007341 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007342 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007343 }
7344
7345 switch(sig) {
7346 case TLSEXT_signature_rsa:
7347 len = chunk_printf(out, "RSA%d", bits);
7348 break;
7349 case TLSEXT_signature_ecdsa:
7350 len = chunk_printf(out, "EC%d", bits);
7351 break;
7352 case TLSEXT_signature_dsa:
7353 len = chunk_printf(out, "DSA%d", bits);
7354 break;
7355 default:
7356 return 0;
7357 }
7358 if (len < 0)
7359 return 0;
7360 return 1;
7361}
7362
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007363/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007364int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7365{
7366 struct ssl_sock_ctx *ctx;
7367 X509 *crt;
7368
7369 if (!ssl_sock_is_ssl(conn))
7370 return 0;
7371
7372 ctx = conn->xprt_ctx;
7373
7374 crt = SSL_get_certificate(ctx->ssl);
7375 if (!crt)
7376 return 0;
7377
7378 return cert_get_pkey_algo(crt, out);
7379}
7380
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007381/* used for ppv2 cert signature (can be used for logging) */
7382const char *ssl_sock_get_cert_sig(struct connection *conn)
7383{
Christopher Faulet82004142019-09-10 10:12:03 +02007384 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007385
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007386 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7387 X509 *crt;
7388
7389 if (!ssl_sock_is_ssl(conn))
7390 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007391 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007392 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007393 if (!crt)
7394 return NULL;
7395 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7396 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7397}
7398
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007399/* used for ppv2 authority */
7400const char *ssl_sock_get_sni(struct connection *conn)
7401{
7402#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007403 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007404
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007405 if (!ssl_sock_is_ssl(conn))
7406 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007407 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007408 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007409#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007410 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007411#endif
7412}
7413
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007414/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007415const char *ssl_sock_get_cipher_name(struct connection *conn)
7416{
Christopher Faulet82004142019-09-10 10:12:03 +02007417 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007418
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007419 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007420 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007421 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007422 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007423}
7424
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007425/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007426const char *ssl_sock_get_proto_version(struct connection *conn)
7427{
Christopher Faulet82004142019-09-10 10:12:03 +02007428 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007429
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007430 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007431 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007432 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007433 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007434}
7435
Willy Tarreau8d598402012-10-22 17:58:39 +02007436/* Extract a serial from a cert, and copy it to a chunk.
7437 * Returns 1 if serial is found and copied, 0 if no serial found and
7438 * -1 if output is not large enough.
7439 */
7440static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007441ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007442{
7443 ASN1_INTEGER *serial;
7444
7445 serial = X509_get_serialNumber(crt);
7446 if (!serial)
7447 return 0;
7448
7449 if (out->size < serial->length)
7450 return -1;
7451
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007452 memcpy(out->area, serial->data, serial->length);
7453 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007454 return 1;
7455}
7456
Emeric Brun43e79582014-10-29 19:03:26 +01007457/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007458 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7459 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007460 */
7461static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007462ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007463{
7464 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007465 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007466
7467 len =i2d_X509(crt, NULL);
7468 if (len <= 0)
7469 return 1;
7470
7471 if (out->size < len)
7472 return -1;
7473
7474 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007475 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007476 return 1;
7477}
7478
Emeric Brunce5ad802012-10-22 14:11:22 +02007479
Willy Tarreau83061a82018-07-13 11:56:34 +02007480/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007481 * Returns 1 if serial is found and copied, 0 if no valid time found
7482 * and -1 if output is not large enough.
7483 */
7484static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007485ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007486{
7487 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7488 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7489
7490 if (gentm->length < 12)
7491 return 0;
7492 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7493 return 0;
7494 if (out->size < gentm->length-2)
7495 return -1;
7496
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007497 memcpy(out->area, gentm->data+2, gentm->length-2);
7498 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007499 return 1;
7500 }
7501 else if (tm->type == V_ASN1_UTCTIME) {
7502 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7503
7504 if (utctm->length < 10)
7505 return 0;
7506 if (utctm->data[0] >= 0x35)
7507 return 0;
7508 if (out->size < utctm->length)
7509 return -1;
7510
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007511 memcpy(out->area, utctm->data, utctm->length);
7512 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007513 return 1;
7514 }
7515
7516 return 0;
7517}
7518
Emeric Brun87855892012-10-17 17:39:35 +02007519/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7520 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7521 */
7522static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007523ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7524 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007525{
7526 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007527 ASN1_OBJECT *obj;
7528 ASN1_STRING *data;
7529 const unsigned char *data_ptr;
7530 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007531 int i, j, n;
7532 int cur = 0;
7533 const char *s;
7534 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007535 int name_count;
7536
7537 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007538
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007539 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007540 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007541 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007542 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007543 else
7544 j = i;
7545
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007546 ne = X509_NAME_get_entry(a, j);
7547 obj = X509_NAME_ENTRY_get_object(ne);
7548 data = X509_NAME_ENTRY_get_data(ne);
7549 data_ptr = ASN1_STRING_get0_data(data);
7550 data_len = ASN1_STRING_length(data);
7551 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007552 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007553 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007554 s = tmp;
7555 }
7556
7557 if (chunk_strcasecmp(entry, s) != 0)
7558 continue;
7559
7560 if (pos < 0)
7561 cur--;
7562 else
7563 cur++;
7564
7565 if (cur != pos)
7566 continue;
7567
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007568 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007569 return -1;
7570
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007571 memcpy(out->area, data_ptr, data_len);
7572 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007573 return 1;
7574 }
7575
7576 return 0;
7577
William Lallemandd4f946c2019-12-05 10:26:40 +01007578}
7579
7580/*
7581 * Extract and format the DNS SAN extensions and copy result into a chuink
7582 * Return 0;
7583 */
7584#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7585static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7586{
7587 int i;
7588 char *str;
7589 STACK_OF(GENERAL_NAME) *names = NULL;
7590
7591 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7592 if (names) {
7593 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7594 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7595 if (i > 0)
7596 chunk_appendf(out, ", ");
7597 if (name->type == GEN_DNS) {
7598 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7599 chunk_appendf(out, "DNS:%s", str);
7600 OPENSSL_free(str);
7601 }
7602 }
7603 }
7604 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7605 }
7606 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007607}
William Lallemandd4f946c2019-12-05 10:26:40 +01007608#endif
Emeric Brun87855892012-10-17 17:39:35 +02007609
Elliot Otchet71f82972020-01-15 08:12:14 -05007610/*
7611 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7612 * Currently supports rfc2253 for returning LDAP V3 DNs.
7613 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7614 */
7615static int
7616ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7617{
7618 BIO *bio = NULL;
7619 int ret = 0;
7620 int data_len = 0;
7621
7622 if (chunk_strcmp(format, "rfc2253") == 0) {
7623 bio = BIO_new(BIO_s_mem());
7624 if (bio == NULL)
7625 goto out;
7626
7627 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7628 goto out;
7629
7630 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7631 goto out;
7632
7633 out->data = data_len;
7634
7635 ret = 1;
7636 }
7637out:
7638 if (bio)
7639 BIO_free(bio);
7640 return ret;
7641}
7642
Emeric Brun87855892012-10-17 17:39:35 +02007643/* Extract and format full DN from a X509_NAME and copy result into a chunk
7644 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7645 */
7646static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007647ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007648{
7649 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007650 ASN1_OBJECT *obj;
7651 ASN1_STRING *data;
7652 const unsigned char *data_ptr;
7653 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007654 int i, n, ln;
7655 int l = 0;
7656 const char *s;
7657 char *p;
7658 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007659 int name_count;
7660
7661
7662 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007664 out->data = 0;
7665 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007666 for (i = 0; i < name_count; i++) {
7667 ne = X509_NAME_get_entry(a, i);
7668 obj = X509_NAME_ENTRY_get_object(ne);
7669 data = X509_NAME_ENTRY_get_data(ne);
7670 data_ptr = ASN1_STRING_get0_data(data);
7671 data_len = ASN1_STRING_length(data);
7672 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007673 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007674 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007675 s = tmp;
7676 }
7677 ln = strlen(s);
7678
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007679 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007680 if (l > out->size)
7681 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007682 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007683
7684 *(p++)='/';
7685 memcpy(p, s, ln);
7686 p += ln;
7687 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007688 memcpy(p, data_ptr, data_len);
7689 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007690 }
7691
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007692 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007693 return 0;
7694
7695 return 1;
7696}
7697
Olivier Houchardab28a322018-12-21 19:45:40 +01007698void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7699{
7700#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007701 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007702
Olivier Houcharde488ea82019-06-28 14:10:33 +02007703 if (!ssl_sock_is_ssl(conn))
7704 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007705 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007706 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007707#endif
7708}
7709
Willy Tarreau119a4082016-12-22 21:58:38 +01007710/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7711 * to disable SNI.
7712 */
Willy Tarreau63076412015-07-10 11:33:32 +02007713void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7714{
7715#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007716 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007717
Willy Tarreau119a4082016-12-22 21:58:38 +01007718 char *prev_name;
7719
Willy Tarreau63076412015-07-10 11:33:32 +02007720 if (!ssl_sock_is_ssl(conn))
7721 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007722 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007723
Willy Tarreau119a4082016-12-22 21:58:38 +01007724 /* if the SNI changes, we must destroy the reusable context so that a
7725 * new connection will present a new SNI. As an optimization we could
7726 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7727 * server.
7728 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007729 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007730 if ((!prev_name && hostname) ||
7731 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007732 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007733
Olivier Houchard66ab4982019-02-26 18:37:15 +01007734 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007735#endif
7736}
7737
Emeric Brun0abf8362014-06-24 18:26:41 +02007738/* Extract peer certificate's common name into the chunk dest
7739 * Returns
7740 * the len of the extracted common name
7741 * or 0 if no CN found in DN
7742 * or -1 on error case (i.e. no peer certificate)
7743 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007744int ssl_sock_get_remote_common_name(struct connection *conn,
7745 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007746{
Christopher Faulet82004142019-09-10 10:12:03 +02007747 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007748 X509 *crt = NULL;
7749 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007750 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007751 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007752 .area = (char *)&find_cn,
7753 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007754 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007755 int result = -1;
David Safb76832014-05-08 23:42:08 -04007756
7757 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007758 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007759 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007760
7761 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007762 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007763 if (!crt)
7764 goto out;
7765
7766 name = X509_get_subject_name(crt);
7767 if (!name)
7768 goto out;
David Safb76832014-05-08 23:42:08 -04007769
Emeric Brun0abf8362014-06-24 18:26:41 +02007770 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7771out:
David Safb76832014-05-08 23:42:08 -04007772 if (crt)
7773 X509_free(crt);
7774
7775 return result;
7776}
7777
Dave McCowan328fb582014-07-30 10:39:13 -04007778/* returns 1 if client passed a certificate for this session, 0 if not */
7779int ssl_sock_get_cert_used_sess(struct connection *conn)
7780{
Christopher Faulet82004142019-09-10 10:12:03 +02007781 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007782 X509 *crt = NULL;
7783
7784 if (!ssl_sock_is_ssl(conn))
7785 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007786 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007787
7788 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007789 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007790 if (!crt)
7791 return 0;
7792
7793 X509_free(crt);
7794 return 1;
7795}
7796
7797/* returns 1 if client passed a certificate for this connection, 0 if not */
7798int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007799{
Christopher Faulet82004142019-09-10 10:12:03 +02007800 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007801
David Safb76832014-05-08 23:42:08 -04007802 if (!ssl_sock_is_ssl(conn))
7803 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007804 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007805 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007806}
7807
7808/* returns result from SSL verify */
7809unsigned int ssl_sock_get_verify_result(struct connection *conn)
7810{
Christopher Faulet82004142019-09-10 10:12:03 +02007811 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007812
David Safb76832014-05-08 23:42:08 -04007813 if (!ssl_sock_is_ssl(conn))
7814 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007815 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007816 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007817}
7818
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007819/* Returns the application layer protocol name in <str> and <len> when known.
7820 * Zero is returned if the protocol name was not found, otherwise non-zero is
7821 * returned. The string is allocated in the SSL context and doesn't have to be
7822 * freed by the caller. NPN is also checked if available since older versions
7823 * of openssl (1.0.1) which are more common in field only support this one.
7824 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007825static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007826{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007827#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7828 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007829 struct ssl_sock_ctx *ctx = xprt_ctx;
7830 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007831 return 0;
7832
7833 *str = NULL;
7834
7835#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007836 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007837 if (*str)
7838 return 1;
7839#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007840#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007841 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007842 if (*str)
7843 return 1;
7844#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007845#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007846 return 0;
7847}
7848
Willy Tarreau7875d092012-09-10 08:20:03 +02007849/***** Below are some sample fetching functions for ACL/patterns *****/
7850
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007851static int
7852smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7853{
7854 struct connection *conn;
7855
7856 conn = objt_conn(smp->sess->origin);
7857 if (!conn || conn->xprt != &ssl_sock)
7858 return 0;
7859
7860 smp->flags = 0;
7861 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007862#ifdef OPENSSL_IS_BORINGSSL
7863 {
7864 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7865 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7866 SSL_early_data_accepted(ctx->ssl));
7867 }
7868#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007869 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007870 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007871#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007872 return 1;
7873}
7874
Emeric Brune64aef12012-09-21 13:15:06 +02007875/* boolean, returns true if client cert was present */
7876static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007877smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007878{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007879 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007880 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007881
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007882 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007883 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007884 return 0;
7885
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007886 ctx = conn->xprt_ctx;
7887
Willy Tarreau911db9b2020-01-23 16:27:54 +01007888 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007889 smp->flags |= SMP_F_MAY_CHANGE;
7890 return 0;
7891 }
7892
7893 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007894 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007895 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007896
7897 return 1;
7898}
7899
Emeric Brun43e79582014-10-29 19:03:26 +01007900/* binary, returns a certificate in a binary chunk (der/raw).
7901 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7902 * should be use.
7903 */
7904static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007905smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007906{
7907 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7908 X509 *crt = NULL;
7909 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007910 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007911 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007912 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007913
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007914 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007915 if (!conn || conn->xprt != &ssl_sock)
7916 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007917 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007918
Willy Tarreau911db9b2020-01-23 16:27:54 +01007919 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007920 smp->flags |= SMP_F_MAY_CHANGE;
7921 return 0;
7922 }
7923
7924 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007925 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007926 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007927 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007928
7929 if (!crt)
7930 goto out;
7931
7932 smp_trash = get_trash_chunk();
7933 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7934 goto out;
7935
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007936 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007937 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007938 ret = 1;
7939out:
7940 /* SSL_get_peer_certificate, it increase X509 * ref count */
7941 if (cert_peer && crt)
7942 X509_free(crt);
7943 return ret;
7944}
7945
Emeric Brunba841a12014-04-30 17:05:08 +02007946/* binary, returns serial of certificate in a binary chunk.
7947 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7948 * should be use.
7949 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007950static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007951smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007952{
Emeric Brunba841a12014-04-30 17:05:08 +02007953 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007954 X509 *crt = NULL;
7955 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007956 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007957 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007958 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007959
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007960 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007961 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007962 return 0;
7963
Olivier Houchard66ab4982019-02-26 18:37:15 +01007964 ctx = conn->xprt_ctx;
7965
Willy Tarreau911db9b2020-01-23 16:27:54 +01007966 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007967 smp->flags |= SMP_F_MAY_CHANGE;
7968 return 0;
7969 }
7970
Emeric Brunba841a12014-04-30 17:05:08 +02007971 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007972 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007973 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007974 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007975
Willy Tarreau8d598402012-10-22 17:58:39 +02007976 if (!crt)
7977 goto out;
7978
Willy Tarreau47ca5452012-12-23 20:22:19 +01007979 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007980 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7981 goto out;
7982
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007983 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007984 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007985 ret = 1;
7986out:
Emeric Brunba841a12014-04-30 17:05:08 +02007987 /* SSL_get_peer_certificate, it increase X509 * ref count */
7988 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007989 X509_free(crt);
7990 return ret;
7991}
Emeric Brune64aef12012-09-21 13:15:06 +02007992
Emeric Brunba841a12014-04-30 17:05:08 +02007993/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7994 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7995 * should be use.
7996 */
James Votha051b4a2013-05-14 20:37:59 +02007997static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007998smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007999{
Emeric Brunba841a12014-04-30 17:05:08 +02008000 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02008001 X509 *crt = NULL;
8002 const EVP_MD *digest;
8003 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01008004 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008005 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008006 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008007 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02008008
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008009 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008010 if (!conn || conn->xprt != &ssl_sock)
8011 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008012 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008013
Willy Tarreau911db9b2020-01-23 16:27:54 +01008014 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008015 smp->flags |= SMP_F_MAY_CHANGE;
8016 return 0;
8017 }
8018
Emeric Brunba841a12014-04-30 17:05:08 +02008019 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008020 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008021 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008022 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008023 if (!crt)
8024 goto out;
8025
8026 smp_trash = get_trash_chunk();
8027 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008028 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8029 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008030 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008031 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008032 ret = 1;
8033out:
Emeric Brunba841a12014-04-30 17:05:08 +02008034 /* SSL_get_peer_certificate, it increase X509 * ref count */
8035 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008036 X509_free(crt);
8037 return ret;
8038}
8039
Emeric Brunba841a12014-04-30 17:05:08 +02008040/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8041 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8042 * should be use.
8043 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008044static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008045smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008046{
Emeric Brunba841a12014-04-30 17:05:08 +02008047 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008048 X509 *crt = NULL;
8049 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008050 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008051 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008052 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008053
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008054 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008055 if (!conn || conn->xprt != &ssl_sock)
8056 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008057 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008058
Willy Tarreau911db9b2020-01-23 16:27:54 +01008059 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008060 smp->flags |= SMP_F_MAY_CHANGE;
8061 return 0;
8062 }
8063
Emeric Brunba841a12014-04-30 17:05:08 +02008064 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008065 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008066 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008067 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008068 if (!crt)
8069 goto out;
8070
Willy Tarreau47ca5452012-12-23 20:22:19 +01008071 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008072 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008073 goto out;
8074
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008075 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008076 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008077 ret = 1;
8078out:
Emeric Brunba841a12014-04-30 17:05:08 +02008079 /* SSL_get_peer_certificate, it increase X509 * ref count */
8080 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008081 X509_free(crt);
8082 return ret;
8083}
8084
Emeric Brunba841a12014-04-30 17:05:08 +02008085/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8086 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8087 * should be use.
8088 */
Emeric Brun87855892012-10-17 17:39:35 +02008089static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008090smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008091{
Emeric Brunba841a12014-04-30 17:05:08 +02008092 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008093 X509 *crt = NULL;
8094 X509_NAME *name;
8095 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008096 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008097 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008098 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008099
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008100 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008101 if (!conn || conn->xprt != &ssl_sock)
8102 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008103 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008104
Willy Tarreau911db9b2020-01-23 16:27:54 +01008105 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008106 smp->flags |= SMP_F_MAY_CHANGE;
8107 return 0;
8108 }
8109
Emeric Brunba841a12014-04-30 17:05:08 +02008110 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008111 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008112 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008113 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008114 if (!crt)
8115 goto out;
8116
8117 name = X509_get_issuer_name(crt);
8118 if (!name)
8119 goto out;
8120
Willy Tarreau47ca5452012-12-23 20:22:19 +01008121 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008122 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008123 int pos = 1;
8124
8125 if (args[1].type == ARGT_SINT)
8126 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008127
8128 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8129 goto out;
8130 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008131 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8132 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8133 goto out;
8134 }
Emeric Brun87855892012-10-17 17:39:35 +02008135 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8136 goto out;
8137
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008138 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008139 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008140 ret = 1;
8141out:
Emeric Brunba841a12014-04-30 17:05:08 +02008142 /* SSL_get_peer_certificate, it increase X509 * ref count */
8143 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008144 X509_free(crt);
8145 return ret;
8146}
8147
Emeric Brunba841a12014-04-30 17:05:08 +02008148/* string, returns notbefore date in ASN1_UTCTIME format.
8149 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8150 * should be use.
8151 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008152static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008153smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008154{
Emeric Brunba841a12014-04-30 17:05:08 +02008155 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008156 X509 *crt = NULL;
8157 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008158 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008159 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008160 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008161
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008162 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008163 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008164 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008165 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008166
Willy Tarreau911db9b2020-01-23 16:27:54 +01008167 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008168 smp->flags |= SMP_F_MAY_CHANGE;
8169 return 0;
8170 }
8171
Emeric Brunba841a12014-04-30 17:05:08 +02008172 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008173 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008174 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008175 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008176 if (!crt)
8177 goto out;
8178
Willy Tarreau47ca5452012-12-23 20:22:19 +01008179 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008180 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008181 goto out;
8182
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008183 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008184 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008185 ret = 1;
8186out:
Emeric Brunba841a12014-04-30 17:05:08 +02008187 /* SSL_get_peer_certificate, it increase X509 * ref count */
8188 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008189 X509_free(crt);
8190 return ret;
8191}
8192
Emeric Brunba841a12014-04-30 17:05:08 +02008193/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8194 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8195 * should be use.
8196 */
Emeric Brun87855892012-10-17 17:39:35 +02008197static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008198smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008199{
Emeric Brunba841a12014-04-30 17:05:08 +02008200 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008201 X509 *crt = NULL;
8202 X509_NAME *name;
8203 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008204 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008205 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008206 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008207
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008208 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008209 if (!conn || conn->xprt != &ssl_sock)
8210 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008211 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008212
Willy Tarreau911db9b2020-01-23 16:27:54 +01008213 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008214 smp->flags |= SMP_F_MAY_CHANGE;
8215 return 0;
8216 }
8217
Emeric Brunba841a12014-04-30 17:05:08 +02008218 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008219 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008220 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008221 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008222 if (!crt)
8223 goto out;
8224
8225 name = X509_get_subject_name(crt);
8226 if (!name)
8227 goto out;
8228
Willy Tarreau47ca5452012-12-23 20:22:19 +01008229 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008230 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008231 int pos = 1;
8232
8233 if (args[1].type == ARGT_SINT)
8234 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008235
8236 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8237 goto out;
8238 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008239 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8240 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8241 goto out;
8242 }
Emeric Brun87855892012-10-17 17:39:35 +02008243 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8244 goto out;
8245
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008246 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008247 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008248 ret = 1;
8249out:
Emeric Brunba841a12014-04-30 17:05:08 +02008250 /* SSL_get_peer_certificate, it increase X509 * ref count */
8251 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008252 X509_free(crt);
8253 return ret;
8254}
Emeric Brun9143d372012-12-20 15:44:16 +01008255
8256/* integer, returns true if current session use a client certificate */
8257static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008258smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008259{
8260 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008261 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008262 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008263
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008264 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008265 if (!conn || conn->xprt != &ssl_sock)
8266 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008267 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008268
Willy Tarreau911db9b2020-01-23 16:27:54 +01008269 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008270 smp->flags |= SMP_F_MAY_CHANGE;
8271 return 0;
8272 }
8273
8274 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008275 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008276 if (crt) {
8277 X509_free(crt);
8278 }
8279
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008280 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008281 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008282 return 1;
8283}
8284
Emeric Brunba841a12014-04-30 17:05:08 +02008285/* integer, returns the certificate version
8286 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8287 * should be use.
8288 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008289static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008290smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008291{
Emeric Brunba841a12014-04-30 17:05:08 +02008292 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008293 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008294 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008295 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008296
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008297 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008298 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008299 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008300 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008301
Willy Tarreau911db9b2020-01-23 16:27:54 +01008302 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008303 smp->flags |= SMP_F_MAY_CHANGE;
8304 return 0;
8305 }
8306
Emeric Brunba841a12014-04-30 17:05:08 +02008307 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008308 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008309 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008310 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008311 if (!crt)
8312 return 0;
8313
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008314 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008315 /* SSL_get_peer_certificate increase X509 * ref count */
8316 if (cert_peer)
8317 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008318 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008319
8320 return 1;
8321}
8322
Emeric Brunba841a12014-04-30 17:05:08 +02008323/* string, returns the certificate's signature algorithm.
8324 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8325 * should be use.
8326 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008327static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008328smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008329{
Emeric Brunba841a12014-04-30 17:05:08 +02008330 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008331 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008332 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008333 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008334 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008335 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008336
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008337 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008338 if (!conn || conn->xprt != &ssl_sock)
8339 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008340 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008341
Willy Tarreau911db9b2020-01-23 16:27:54 +01008342 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008343 smp->flags |= SMP_F_MAY_CHANGE;
8344 return 0;
8345 }
8346
Emeric Brunba841a12014-04-30 17:05:08 +02008347 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008348 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008349 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008350 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008351 if (!crt)
8352 return 0;
8353
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008354 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8355 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008356
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008357 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8358 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008359 /* SSL_get_peer_certificate increase X509 * ref count */
8360 if (cert_peer)
8361 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008362 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008363 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008364
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008365 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008366 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008367 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008368 /* SSL_get_peer_certificate increase X509 * ref count */
8369 if (cert_peer)
8370 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008371
8372 return 1;
8373}
8374
Emeric Brunba841a12014-04-30 17:05:08 +02008375/* string, returns the certificate's key algorithm.
8376 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8377 * should be use.
8378 */
Emeric Brun521a0112012-10-22 12:22:55 +02008379static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008380smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008381{
Emeric Brunba841a12014-04-30 17:05:08 +02008382 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008383 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008384 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008385 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008386 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008387 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008388
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008389 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008390 if (!conn || conn->xprt != &ssl_sock)
8391 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008392 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008393
Willy Tarreau911db9b2020-01-23 16:27:54 +01008394 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008395 smp->flags |= SMP_F_MAY_CHANGE;
8396 return 0;
8397 }
8398
Emeric Brunba841a12014-04-30 17:05:08 +02008399 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008400 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008401 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008402 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008403 if (!crt)
8404 return 0;
8405
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008406 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8407 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008408
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008409 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8410 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008411 /* SSL_get_peer_certificate increase X509 * ref count */
8412 if (cert_peer)
8413 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008414 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008415 }
Emeric Brun521a0112012-10-22 12:22:55 +02008416
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008417 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008418 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008419 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008420 if (cert_peer)
8421 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008422
8423 return 1;
8424}
8425
Emeric Brun645ae792014-04-30 14:21:06 +02008426/* boolean, returns true if front conn. transport layer is SSL.
8427 * This function is also usable on backend conn if the fetch keyword 5th
8428 * char is 'b'.
8429 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008430static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008431smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008432{
Emeric Bruneb8def92018-02-19 15:59:48 +01008433 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8434 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008435
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008436 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008437 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008438 return 1;
8439}
8440
Emeric Brun2525b6b2012-10-18 15:59:43 +02008441/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008442static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008443smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008444{
8445#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008446 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008447 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008448
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008449 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008450 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008451 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008452 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008453 return 1;
8454#else
8455 return 0;
8456#endif
8457}
8458
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008459/* boolean, returns true if client session has been resumed.
8460 * This function is also usable on backend conn if the fetch keyword 5th
8461 * char is 'b'.
8462 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008463static int
8464smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8465{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008466 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8467 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008468 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008469
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008470
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008471 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008472 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008473 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008474 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008475 return 1;
8476}
8477
Emeric Brun645ae792014-04-30 14:21:06 +02008478/* string, returns the used cipher if front conn. transport layer is SSL.
8479 * This function is also usable on backend conn if the fetch keyword 5th
8480 * char is 'b'.
8481 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008482static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008483smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008484{
Emeric Bruneb8def92018-02-19 15:59:48 +01008485 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8486 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008487 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008488
Willy Tarreaube508f12016-03-10 11:47:01 +01008489 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008490 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008491 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008492 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008493
Olivier Houchard66ab4982019-02-26 18:37:15 +01008494 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008495 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008496 return 0;
8497
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008498 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008499 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008500 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008501
8502 return 1;
8503}
8504
Emeric Brun645ae792014-04-30 14:21:06 +02008505/* integer, returns the algoritm's keysize if front conn. transport layer
8506 * is SSL.
8507 * This function is also usable on backend conn if the fetch keyword 5th
8508 * char is 'b'.
8509 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008510static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008511smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008512{
Emeric Bruneb8def92018-02-19 15:59:48 +01008513 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8514 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008515 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008516 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008517
Emeric Brun589fcad2012-10-16 14:13:26 +02008518 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008519 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008520 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008521 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008522
Olivier Houchard66ab4982019-02-26 18:37:15 +01008523 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008524 return 0;
8525
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008526 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008527 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008528
8529 return 1;
8530}
8531
Emeric Brun645ae792014-04-30 14:21:06 +02008532/* integer, returns the used keysize if front conn. transport layer is SSL.
8533 * This function is also usable on backend conn if the fetch keyword 5th
8534 * char is 'b'.
8535 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008536static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008537smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008538{
Emeric Bruneb8def92018-02-19 15:59:48 +01008539 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8540 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008541 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008542
Emeric Brun589fcad2012-10-16 14:13:26 +02008543 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008544 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8545 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008546 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008547
Olivier Houchard66ab4982019-02-26 18:37:15 +01008548 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008549 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008550 return 0;
8551
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008552 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008553
8554 return 1;
8555}
8556
Bernard Spil13c53f82018-02-15 13:34:58 +01008557#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008558static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008559smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008560{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008561 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008562 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008563 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008564
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008565 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008566 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008567
Olivier Houchard6b77f492018-11-22 18:18:29 +01008568 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8569 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008570 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8571 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008572 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008573
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008574 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008575 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008576 (const unsigned char **)&smp->data.u.str.area,
8577 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008578
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008579 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008580 return 0;
8581
Willy Tarreau105599c2020-02-25 08:59:23 +01008582 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008583 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008584}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008585#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008586
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008587#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008588static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008589smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008590{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008591 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008592 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008593 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008594
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008595 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008596 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008597
Olivier Houchard6b77f492018-11-22 18:18:29 +01008598 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8599 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8600
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008601 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008602 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008603 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008604
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008605 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008606 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008607 (const unsigned char **)&smp->data.u.str.area,
8608 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008609
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008610 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008611 return 0;
8612
Willy Tarreau105599c2020-02-25 08:59:23 +01008613 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008614 return 1;
8615}
8616#endif
8617
Emeric Brun645ae792014-04-30 14:21:06 +02008618/* string, returns the used protocol if front conn. transport layer is SSL.
8619 * This function is also usable on backend conn if the fetch keyword 5th
8620 * char is 'b'.
8621 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008622static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008623smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008624{
Emeric Bruneb8def92018-02-19 15:59:48 +01008625 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8626 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008627 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008628
Emeric Brun589fcad2012-10-16 14:13:26 +02008629 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008630 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8631 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008632 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008633
Olivier Houchard66ab4982019-02-26 18:37:15 +01008634 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008635 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008636 return 0;
8637
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008638 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008639 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008640 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008641
8642 return 1;
8643}
8644
Willy Tarreau87b09662015-04-03 00:22:06 +02008645/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008646 * This function is also usable on backend conn if the fetch keyword 5th
8647 * char is 'b'.
8648 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008649#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008650static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008651smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008652{
Emeric Bruneb8def92018-02-19 15:59:48 +01008653 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8654 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008655 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008656 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008657 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008658
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008659 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008660 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008661
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008662 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8663 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008664 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008665
Olivier Houchard66ab4982019-02-26 18:37:15 +01008666 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008667 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008668 return 0;
8669
Willy Tarreau105599c2020-02-25 08:59:23 +01008670 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008671 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008672 return 0;
8673
Willy Tarreau105599c2020-02-25 08:59:23 +01008674 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008675 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008676}
Patrick Hemmer41966772018-04-28 19:15:48 -04008677#endif
8678
Emeric Brunfe68f682012-10-16 14:59:28 +02008679
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008680#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008681static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008682smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8683{
8684 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8685 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8686 struct buffer *data;
8687 struct ssl_sock_ctx *ctx;
8688
8689 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8690 return 0;
8691 ctx = conn->xprt_ctx;
8692
8693 data = get_trash_chunk();
8694 if (kw[7] == 'c')
8695 data->data = SSL_get_client_random(ctx->ssl,
8696 (unsigned char *) data->area,
8697 data->size);
8698 else
8699 data->data = SSL_get_server_random(ctx->ssl,
8700 (unsigned char *) data->area,
8701 data->size);
8702 if (!data->data)
8703 return 0;
8704
8705 smp->flags = 0;
8706 smp->data.type = SMP_T_BIN;
8707 smp->data.u.str = *data;
8708
8709 return 1;
8710}
8711
8712static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008713smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8714{
8715 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8716 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8717 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008718 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008719 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008720
8721 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8722 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008723 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008724
Olivier Houchard66ab4982019-02-26 18:37:15 +01008725 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008726 if (!ssl_sess)
8727 return 0;
8728
8729 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008730 data->data = SSL_SESSION_get_master_key(ssl_sess,
8731 (unsigned char *) data->area,
8732 data->size);
8733 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008734 return 0;
8735
8736 smp->flags = 0;
8737 smp->data.type = SMP_T_BIN;
8738 smp->data.u.str = *data;
8739
8740 return 1;
8741}
8742#endif
8743
Patrick Hemmer41966772018-04-28 19:15:48 -04008744#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008745static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008746smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008747{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008748 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008749 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008750
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008751 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008752 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008753
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008754 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008755 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8756 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008757 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008758
Olivier Houchard66ab4982019-02-26 18:37:15 +01008759 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008760 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008761 return 0;
8762
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008763 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008764 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008765}
Patrick Hemmer41966772018-04-28 19:15:48 -04008766#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008767
David Sc1ad52e2014-04-08 18:48:47 -04008768static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008769smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8770{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008771 struct connection *conn;
8772 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008773 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008774
8775 conn = objt_conn(smp->sess->origin);
8776 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8777 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008778 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008779
Olivier Houchard66ab4982019-02-26 18:37:15 +01008780 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008781 if (!capture)
8782 return 0;
8783
8784 smp->flags = SMP_F_CONST;
8785 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008786 smp->data.u.str.area = capture->ciphersuite;
8787 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008788 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008789}
8790
8791static int
8792smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8793{
Willy Tarreau83061a82018-07-13 11:56:34 +02008794 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008795
8796 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8797 return 0;
8798
8799 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008800 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008801 smp->data.type = SMP_T_BIN;
8802 smp->data.u.str = *data;
8803 return 1;
8804}
8805
8806static int
8807smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8808{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008809 struct connection *conn;
8810 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008811 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008812
8813 conn = objt_conn(smp->sess->origin);
8814 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8815 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008816 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008817
Olivier Houchard66ab4982019-02-26 18:37:15 +01008818 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008819 if (!capture)
8820 return 0;
8821
8822 smp->data.type = SMP_T_SINT;
8823 smp->data.u.sint = capture->xxh64;
8824 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008825}
8826
8827static int
8828smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8829{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008830#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008831 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008832 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008833
8834 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8835 return 0;
8836
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008837 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008838 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008839 const char *str;
8840 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008841 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008842 uint16_t id = (bin[0] << 8) | bin[1];
8843#if defined(OPENSSL_IS_BORINGSSL)
8844 cipher = SSL_get_cipher_by_value(id);
8845#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008846 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008847 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8848 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008849#endif
8850 str = SSL_CIPHER_get_name(cipher);
8851 if (!str || strcmp(str, "(NONE)") == 0)
8852 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008853 else
8854 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8855 }
8856 smp->data.type = SMP_T_STR;
8857 smp->data.u.str = *data;
8858 return 1;
8859#else
8860 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8861#endif
8862}
8863
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008864#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008865static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008866smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008867{
Emeric Bruneb8def92018-02-19 15:59:48 +01008868 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8869 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008870 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008871 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008872 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008873
8874 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008875 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8876 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008877 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008878
Willy Tarreau911db9b2020-01-23 16:27:54 +01008879 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008880 smp->flags |= SMP_F_MAY_CHANGE;
8881 return 0;
8882 }
8883
8884 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008885 if (!SSL_session_reused(ctx->ssl))
8886 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008887 finished_trash->area,
8888 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008889 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008890 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008891 finished_trash->area,
8892 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008893
8894 if (!finished_len)
8895 return 0;
8896
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008897 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008898 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008899 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008900
8901 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008902}
Patrick Hemmer41966772018-04-28 19:15:48 -04008903#endif
David Sc1ad52e2014-04-08 18:48:47 -04008904
Emeric Brun2525b6b2012-10-18 15:59:43 +02008905/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008906static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008907smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008908{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008909 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008910 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008911
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008912 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008913 if (!conn || conn->xprt != &ssl_sock)
8914 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008915 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008916
Willy Tarreau911db9b2020-01-23 16:27:54 +01008917 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008918 smp->flags = SMP_F_MAY_CHANGE;
8919 return 0;
8920 }
8921
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008922 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008923 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008924 smp->flags = 0;
8925
8926 return 1;
8927}
8928
Emeric Brun2525b6b2012-10-18 15:59:43 +02008929/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008930static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008931smp_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 +02008932{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008933 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008934 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008935
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008936 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008937 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008938 return 0;
8939
Willy Tarreau911db9b2020-01-23 16:27:54 +01008940 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008941 smp->flags = SMP_F_MAY_CHANGE;
8942 return 0;
8943 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008944 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008945
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008946 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008947 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008948 smp->flags = 0;
8949
8950 return 1;
8951}
8952
Emeric Brun2525b6b2012-10-18 15:59:43 +02008953/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008955smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008956{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008957 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008958 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008959
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008960 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008961 if (!conn || conn->xprt != &ssl_sock)
8962 return 0;
8963
Willy Tarreau911db9b2020-01-23 16:27:54 +01008964 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008965 smp->flags = SMP_F_MAY_CHANGE;
8966 return 0;
8967 }
8968
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008969 ctx = conn->xprt_ctx;
8970
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008971 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008972 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008973 smp->flags = 0;
8974
8975 return 1;
8976}
8977
Emeric Brun2525b6b2012-10-18 15:59:43 +02008978/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008979static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008980smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008981{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008982 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008983 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008984
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008985 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008986 if (!conn || conn->xprt != &ssl_sock)
8987 return 0;
8988
Willy Tarreau911db9b2020-01-23 16:27:54 +01008989 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008990 smp->flags = SMP_F_MAY_CHANGE;
8991 return 0;
8992 }
8993
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008994 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008995 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008996 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008997
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008998 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008999 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009000 smp->flags = 0;
9001
9002 return 1;
9003}
9004
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009005/* for ca-file and ca-verify-file */
9006static 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 +02009007{
9008 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009009 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009010 return ERR_ALERT | ERR_FATAL;
9011 }
9012
Willy Tarreauef934602016-12-22 23:12:01 +01009013 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009014 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009015 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009016 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009017
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009018 if (!ssl_store_load_locations_file(*ca_file_p)) {
9019 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009020 return ERR_ALERT | ERR_FATAL;
9021 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009022 return 0;
9023}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009024
9025/* parse the "ca-file" bind keyword */
9026static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9027{
9028 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9029}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009030static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9031{
9032 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9033}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009034
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009035/* parse the "ca-verify-file" bind keyword */
9036static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9037{
9038 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9039}
9040static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9041{
9042 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9043}
9044
Christopher Faulet31af49d2015-06-09 17:29:50 +02009045/* parse the "ca-sign-file" bind keyword */
9046static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9047{
9048 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009049 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009050 return ERR_ALERT | ERR_FATAL;
9051 }
9052
Willy Tarreauef934602016-12-22 23:12:01 +01009053 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9054 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009055 else
9056 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9057
9058 return 0;
9059}
9060
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009061/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009062static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9063{
9064 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009065 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009066 return ERR_ALERT | ERR_FATAL;
9067 }
9068 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9069 return 0;
9070}
9071
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009072/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009073static 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 +02009074{
9075 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009076 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009077 return ERR_ALERT | ERR_FATAL;
9078 }
9079
Emeric Brun76d88952012-10-05 15:47:31 +02009080 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009081 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009082 return 0;
9083}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009084static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9085{
9086 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9087}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009088
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009089#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009090/* parse the "ciphersuites" bind keyword */
9091static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9092{
9093 if (!*args[cur_arg + 1]) {
9094 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9095 return ERR_ALERT | ERR_FATAL;
9096 }
9097
9098 free(conf->ciphersuites);
9099 conf->ciphersuites = strdup(args[cur_arg + 1]);
9100 return 0;
9101}
9102static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9103{
9104 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9105}
9106#endif
9107
Willy Tarreaubbc91962019-10-16 16:42:19 +02009108/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009109static 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 +02009110{
Willy Tarreau38011032013-08-13 16:59:39 +02009111 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009112
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009113 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009114 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009115 return ERR_ALERT | ERR_FATAL;
9116 }
9117
Willy Tarreauef934602016-12-22 23:12:01 +01009118 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9119 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009120 memprintf(err, "'%s' : path too long", args[cur_arg]);
9121 return ERR_ALERT | ERR_FATAL;
9122 }
Willy Tarreauef934602016-12-22 23:12:01 +01009123 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009124 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009125 }
9126
Willy Tarreaubbc91962019-10-16 16:42:19 +02009127 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009128}
9129
Willy Tarreaubbc91962019-10-16 16:42:19 +02009130/* 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 +01009131static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9132{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009133 int err_code;
9134
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009135 if (!*args[cur_arg + 1]) {
9136 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9137 return ERR_ALERT | ERR_FATAL;
9138 }
9139
William Lallemand6be66ec2020-03-06 22:26:32 +01009140 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009141 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009142 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009143
Willy Tarreaubbc91962019-10-16 16:42:19 +02009144 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009145}
9146
Emeric Brunfb510ea2012-10-05 12:00:26 +02009147/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009148static 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 +02009149{
Emeric Brun051cdab2012-10-02 19:25:50 +02009150#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009151 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009152 return ERR_ALERT | ERR_FATAL;
9153#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009154 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009155 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009156 return ERR_ALERT | ERR_FATAL;
9157 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009158
Willy Tarreauef934602016-12-22 23:12:01 +01009159 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9160 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009161 else
9162 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009163
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009164 if (!ssl_store_load_locations_file(conf->crl_file)) {
9165 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9166 return ERR_ALERT | ERR_FATAL;
9167 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009168 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009169#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009170}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009171static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9172{
9173 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9174}
Emeric Brun2b58d042012-09-20 17:10:03 +02009175
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009176/* parse the "curves" bind keyword keyword */
9177static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9178{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009179#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009180 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009181 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009182 return ERR_ALERT | ERR_FATAL;
9183 }
9184 conf->curves = strdup(args[cur_arg + 1]);
9185 return 0;
9186#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009187 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009188 return ERR_ALERT | ERR_FATAL;
9189#endif
9190}
9191static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9192{
9193 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9194}
9195
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009196/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009197static 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 +02009198{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009199#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009200 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009201 return ERR_ALERT | ERR_FATAL;
9202#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009203 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 +02009204 return ERR_ALERT | ERR_FATAL;
9205#else
9206 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009207 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009208 return ERR_ALERT | ERR_FATAL;
9209 }
9210
9211 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009212
9213 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009214#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009215}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009216static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9217{
9218 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9219}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009220
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009221/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009222static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9223{
9224 int code;
9225 char *p = args[cur_arg + 1];
9226 unsigned long long *ignerr = &conf->crt_ignerr;
9227
9228 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009229 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009230 return ERR_ALERT | ERR_FATAL;
9231 }
9232
9233 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9234 ignerr = &conf->ca_ignerr;
9235
9236 if (strcmp(p, "all") == 0) {
9237 *ignerr = ~0ULL;
9238 return 0;
9239 }
9240
9241 while (p) {
9242 code = atoi(p);
9243 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009244 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9245 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009246 return ERR_ALERT | ERR_FATAL;
9247 }
9248 *ignerr |= 1ULL << code;
9249 p = strchr(p, ',');
9250 if (p)
9251 p++;
9252 }
9253
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009254 return 0;
9255}
9256
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009257/* parse tls_method_options "no-xxx" and "force-xxx" */
9258static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009259{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009260 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009261 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009262 p = strchr(arg, '-');
9263 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009264 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009265 p++;
9266 if (!strcmp(p, "sslv3"))
9267 v = CONF_SSLV3;
9268 else if (!strcmp(p, "tlsv10"))
9269 v = CONF_TLSV10;
9270 else if (!strcmp(p, "tlsv11"))
9271 v = CONF_TLSV11;
9272 else if (!strcmp(p, "tlsv12"))
9273 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009274 else if (!strcmp(p, "tlsv13"))
9275 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009276 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009277 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009278 if (!strncmp(arg, "no-", 3))
9279 methods->flags |= methodVersions[v].flag;
9280 else if (!strncmp(arg, "force-", 6))
9281 methods->min = methods->max = v;
9282 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009283 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009284 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009285 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009286 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009287 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009288}
9289
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009290static 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 +02009291{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009292 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009293}
9294
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009295static 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 +02009296{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009297 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9298}
9299
9300/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9301static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9302{
9303 uint16_t i, v = 0;
9304 char *argv = args[cur_arg + 1];
9305 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009306 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009307 return ERR_ALERT | ERR_FATAL;
9308 }
9309 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9310 if (!strcmp(argv, methodVersions[i].name))
9311 v = i;
9312 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009313 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009314 return ERR_ALERT | ERR_FATAL;
9315 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009316 if (!strcmp("ssl-min-ver", args[cur_arg]))
9317 methods->min = v;
9318 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9319 methods->max = v;
9320 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009321 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009322 return ERR_ALERT | ERR_FATAL;
9323 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009324 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009325}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009326
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009327static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9328{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009329#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009330 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 +02009331#endif
9332 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9333}
9334
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009335static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9336{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009337 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009338}
9339
9340static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9341{
9342 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9343}
9344
Emeric Brun2d0c4822012-10-02 13:45:20 +02009345/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009346static 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 +02009347{
Emeric Brun89675492012-10-05 13:48:26 +02009348 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009349 return 0;
9350}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009351
Olivier Houchardc2aae742017-09-22 18:26:28 +02009352/* parse the "allow-0rtt" bind keyword */
9353static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9354{
9355 conf->early_data = 1;
9356 return 0;
9357}
9358
9359static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9360{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009361 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009362 return 0;
9363}
9364
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009365/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009366static 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 +02009367{
Bernard Spil13c53f82018-02-15 13:34:58 +01009368#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009369 char *p1, *p2;
9370
9371 if (!*args[cur_arg + 1]) {
9372 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9373 return ERR_ALERT | ERR_FATAL;
9374 }
9375
9376 free(conf->npn_str);
9377
Willy Tarreau3724da12016-02-12 17:11:12 +01009378 /* the NPN string is built as a suite of (<len> <name>)*,
9379 * so we reuse each comma to store the next <len> and need
9380 * one more for the end of the string.
9381 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009382 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009383 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009384 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9385
9386 /* replace commas with the name length */
9387 p1 = conf->npn_str;
9388 p2 = p1 + 1;
9389 while (1) {
9390 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9391 if (!p2)
9392 p2 = p1 + 1 + strlen(p1 + 1);
9393
9394 if (p2 - (p1 + 1) > 255) {
9395 *p2 = '\0';
9396 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9397 return ERR_ALERT | ERR_FATAL;
9398 }
9399
9400 *p1 = p2 - (p1 + 1);
9401 p1 = p2;
9402
9403 if (!*p2)
9404 break;
9405
9406 *(p2++) = '\0';
9407 }
9408 return 0;
9409#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009410 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009411 return ERR_ALERT | ERR_FATAL;
9412#endif
9413}
9414
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009415static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9416{
9417 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9418}
9419
Willy Tarreauab861d32013-04-02 02:30:41 +02009420/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009421static 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 +02009422{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009423#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009424 char *p1, *p2;
9425
9426 if (!*args[cur_arg + 1]) {
9427 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9428 return ERR_ALERT | ERR_FATAL;
9429 }
9430
9431 free(conf->alpn_str);
9432
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009433 /* the ALPN string is built as a suite of (<len> <name>)*,
9434 * so we reuse each comma to store the next <len> and need
9435 * one more for the end of the string.
9436 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009437 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009438 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009439 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9440
9441 /* replace commas with the name length */
9442 p1 = conf->alpn_str;
9443 p2 = p1 + 1;
9444 while (1) {
9445 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9446 if (!p2)
9447 p2 = p1 + 1 + strlen(p1 + 1);
9448
9449 if (p2 - (p1 + 1) > 255) {
9450 *p2 = '\0';
9451 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9452 return ERR_ALERT | ERR_FATAL;
9453 }
9454
9455 *p1 = p2 - (p1 + 1);
9456 p1 = p2;
9457
9458 if (!*p2)
9459 break;
9460
9461 *(p2++) = '\0';
9462 }
9463 return 0;
9464#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009465 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009466 return ERR_ALERT | ERR_FATAL;
9467#endif
9468}
9469
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009470static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9471{
9472 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9473}
9474
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009475/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009476static 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 +02009477{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009478 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009479 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009480
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009481 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9482 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009483#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009484 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9485 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9486#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009487 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009488 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9489 if (!conf->ssl_conf.ssl_methods.min)
9490 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9491 if (!conf->ssl_conf.ssl_methods.max)
9492 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009493
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009494 return 0;
9495}
9496
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009497/* parse the "prefer-client-ciphers" bind keyword */
9498static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9499{
9500 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9501 return 0;
9502}
9503
Christopher Faulet31af49d2015-06-09 17:29:50 +02009504/* parse the "generate-certificates" bind keyword */
9505static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9506{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009507#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009508 conf->generate_certs = 1;
9509#else
9510 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9511 err && *err ? *err : "");
9512#endif
9513 return 0;
9514}
9515
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009516/* parse the "strict-sni" bind keyword */
9517static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9518{
9519 conf->strict_sni = 1;
9520 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009521}
9522
9523/* parse the "tls-ticket-keys" bind keyword */
9524static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9525{
9526#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009527 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009528 int i = 0;
9529 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009530 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009531
9532 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009533 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009534 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009535 }
9536
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009537 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009538 if (keys_ref) {
9539 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009540 conf->keys_ref = keys_ref;
9541 return 0;
9542 }
9543
Christopher Faulete566f3d2019-10-21 09:55:49 +02009544 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009545 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009546 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009547 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009548 }
9549
Emeric Brun9e754772019-01-10 17:51:55 +01009550 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009551 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009552 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009553 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009554 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009555
9556 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009557 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009558 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009559 }
9560
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009561 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009562 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009563 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009564 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009565 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009566
Emeric Brun9e754772019-01-10 17:51:55 +01009567 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009568 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9569 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009570 int dec_size;
9571
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009572 /* Strip newline characters from the end */
9573 if(thisline[len - 1] == '\n')
9574 thisline[--len] = 0;
9575
9576 if(thisline[len - 1] == '\r')
9577 thisline[--len] = 0;
9578
Emeric Brun9e754772019-01-10 17:51:55 +01009579 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9580 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009581 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009582 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009583 }
Emeric Brun9e754772019-01-10 17:51:55 +01009584 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9585 keys_ref->key_size_bits = 128;
9586 }
9587 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9588 keys_ref->key_size_bits = 256;
9589 }
9590 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9591 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9592 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009593 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009594 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009595 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009596 i++;
9597 }
9598
9599 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009600 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 +02009601 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009602 }
9603
9604 fclose(f);
9605
9606 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009607 i -= 2;
9608 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009609 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009610 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009611 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009612 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009613
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009614 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9615
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009616 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009617
9618 fail:
9619 if (f)
9620 fclose(f);
9621 if (keys_ref) {
9622 free(keys_ref->filename);
9623 free(keys_ref->tlskeys);
9624 free(keys_ref);
9625 }
9626 return ERR_ALERT | ERR_FATAL;
9627
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009628#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009629 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009630 return ERR_ALERT | ERR_FATAL;
9631#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009632}
9633
Emeric Brund94b3fe2012-09-20 18:23:56 +02009634/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009635static 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 +02009636{
9637 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009638 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009639 return ERR_ALERT | ERR_FATAL;
9640 }
9641
9642 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009643 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009644 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009645 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009646 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009647 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009648 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009649 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9650 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009651 return ERR_ALERT | ERR_FATAL;
9652 }
9653
9654 return 0;
9655}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009656static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9657{
9658 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9659}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009660
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009661/* parse the "no-ca-names" bind keyword */
9662static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9663{
9664 conf->no_ca_names = 1;
9665 return 0;
9666}
9667static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9668{
9669 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9670}
9671
Willy Tarreau92faadf2012-10-10 23:04:25 +02009672/************** "server" keywords ****************/
9673
Olivier Houchardc7566002018-11-20 23:33:50 +01009674/* parse the "npn" bind keyword */
9675static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9676{
9677#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9678 char *p1, *p2;
9679
9680 if (!*args[*cur_arg + 1]) {
9681 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9682 return ERR_ALERT | ERR_FATAL;
9683 }
9684
9685 free(newsrv->ssl_ctx.npn_str);
9686
9687 /* the NPN string is built as a suite of (<len> <name>)*,
9688 * so we reuse each comma to store the next <len> and need
9689 * one more for the end of the string.
9690 */
9691 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9692 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9693 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9694 newsrv->ssl_ctx.npn_len);
9695
9696 /* replace commas with the name length */
9697 p1 = newsrv->ssl_ctx.npn_str;
9698 p2 = p1 + 1;
9699 while (1) {
9700 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9701 newsrv->ssl_ctx.npn_len - (p1 + 1));
9702 if (!p2)
9703 p2 = p1 + 1 + strlen(p1 + 1);
9704
9705 if (p2 - (p1 + 1) > 255) {
9706 *p2 = '\0';
9707 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9708 return ERR_ALERT | ERR_FATAL;
9709 }
9710
9711 *p1 = p2 - (p1 + 1);
9712 p1 = p2;
9713
9714 if (!*p2)
9715 break;
9716
9717 *(p2++) = '\0';
9718 }
9719 return 0;
9720#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009721 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009722 return ERR_ALERT | ERR_FATAL;
9723#endif
9724}
9725
Olivier Houchard92150142018-12-21 19:47:01 +01009726/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009727static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9728{
9729#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9730 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009731 char **alpn_str;
9732 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009733
Olivier Houchard92150142018-12-21 19:47:01 +01009734 if (*args[*cur_arg] == 'c') {
9735 alpn_str = &newsrv->check.alpn_str;
9736 alpn_len = &newsrv->check.alpn_len;
9737 } else {
9738 alpn_str = &newsrv->ssl_ctx.alpn_str;
9739 alpn_len = &newsrv->ssl_ctx.alpn_len;
9740
9741 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009742 if (!*args[*cur_arg + 1]) {
9743 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9744 return ERR_ALERT | ERR_FATAL;
9745 }
9746
Olivier Houchard92150142018-12-21 19:47:01 +01009747 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009748
9749 /* the ALPN string is built as a suite of (<len> <name>)*,
9750 * so we reuse each comma to store the next <len> and need
9751 * one more for the end of the string.
9752 */
Olivier Houchard92150142018-12-21 19:47:01 +01009753 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9754 *alpn_str = calloc(1, *alpn_len + 1);
9755 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009756
9757 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009758 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009759 p2 = p1 + 1;
9760 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009761 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009762 if (!p2)
9763 p2 = p1 + 1 + strlen(p1 + 1);
9764
9765 if (p2 - (p1 + 1) > 255) {
9766 *p2 = '\0';
9767 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9768 return ERR_ALERT | ERR_FATAL;
9769 }
9770
9771 *p1 = p2 - (p1 + 1);
9772 p1 = p2;
9773
9774 if (!*p2)
9775 break;
9776
9777 *(p2++) = '\0';
9778 }
9779 return 0;
9780#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009781 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009782 return ERR_ALERT | ERR_FATAL;
9783#endif
9784}
9785
Emeric Brunef42d922012-10-11 16:11:36 +02009786/* parse the "ca-file" server keyword */
9787static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9788{
9789 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009790 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009791 return ERR_ALERT | ERR_FATAL;
9792 }
9793
Willy Tarreauef934602016-12-22 23:12:01 +01009794 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9795 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009796 else
9797 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9798
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009799 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9800 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9801 return ERR_ALERT | ERR_FATAL;
9802 }
Emeric Brunef42d922012-10-11 16:11:36 +02009803 return 0;
9804}
9805
Olivier Houchard9130a962017-10-17 17:33:43 +02009806/* parse the "check-sni" server keyword */
9807static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9808{
9809 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009810 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009811 return ERR_ALERT | ERR_FATAL;
9812 }
9813
9814 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9815 if (!newsrv->check.sni) {
9816 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9817 return ERR_ALERT | ERR_FATAL;
9818 }
9819 return 0;
9820
9821}
9822
Willy Tarreau92faadf2012-10-10 23:04:25 +02009823/* parse the "check-ssl" server keyword */
9824static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9825{
9826 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009827 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9828 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009829#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009830 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9831 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9832#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009833 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009834 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9835 if (!newsrv->ssl_ctx.methods.min)
9836 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9837 if (!newsrv->ssl_ctx.methods.max)
9838 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9839
Willy Tarreau92faadf2012-10-10 23:04:25 +02009840 return 0;
9841}
9842
9843/* parse the "ciphers" server keyword */
9844static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9845{
9846 if (!*args[*cur_arg + 1]) {
9847 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9848 return ERR_ALERT | ERR_FATAL;
9849 }
9850
9851 free(newsrv->ssl_ctx.ciphers);
9852 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9853 return 0;
9854}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009855
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009856#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009857/* parse the "ciphersuites" server keyword */
9858static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9859{
9860 if (!*args[*cur_arg + 1]) {
9861 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9862 return ERR_ALERT | ERR_FATAL;
9863 }
9864
9865 free(newsrv->ssl_ctx.ciphersuites);
9866 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9867 return 0;
9868}
9869#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009870
Emeric Brunef42d922012-10-11 16:11:36 +02009871/* parse the "crl-file" server keyword */
9872static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9873{
9874#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009875 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009876 return ERR_ALERT | ERR_FATAL;
9877#else
9878 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009879 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009880 return ERR_ALERT | ERR_FATAL;
9881 }
9882
Willy Tarreauef934602016-12-22 23:12:01 +01009883 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9884 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009885 else
9886 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9887
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009888 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9889 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9890 return ERR_ALERT | ERR_FATAL;
9891 }
Emeric Brunef42d922012-10-11 16:11:36 +02009892 return 0;
9893#endif
9894}
9895
Emeric Bruna7aa3092012-10-26 12:58:00 +02009896/* parse the "crt" server keyword */
9897static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9898{
9899 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009900 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009901 return ERR_ALERT | ERR_FATAL;
9902 }
9903
Willy Tarreauef934602016-12-22 23:12:01 +01009904 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009905 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009906 else
9907 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9908
9909 return 0;
9910}
Emeric Brunef42d922012-10-11 16:11:36 +02009911
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009912/* parse the "no-check-ssl" server keyword */
9913static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9914{
9915 newsrv->check.use_ssl = 0;
9916 free(newsrv->ssl_ctx.ciphers);
9917 newsrv->ssl_ctx.ciphers = NULL;
9918 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9919 return 0;
9920}
9921
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009922/* parse the "no-send-proxy-v2-ssl" server keyword */
9923static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9924{
9925 newsrv->pp_opts &= ~SRV_PP_V2;
9926 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9927 return 0;
9928}
9929
9930/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9931static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9932{
9933 newsrv->pp_opts &= ~SRV_PP_V2;
9934 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9935 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9936 return 0;
9937}
9938
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009939/* parse the "no-ssl" server keyword */
9940static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9941{
9942 newsrv->use_ssl = 0;
9943 free(newsrv->ssl_ctx.ciphers);
9944 newsrv->ssl_ctx.ciphers = NULL;
9945 return 0;
9946}
9947
Olivier Houchard522eea72017-11-03 16:27:47 +01009948/* parse the "allow-0rtt" server keyword */
9949static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9950{
9951 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9952 return 0;
9953}
9954
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009955/* parse the "no-ssl-reuse" server keyword */
9956static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9957{
9958 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9959 return 0;
9960}
9961
Emeric Brunf9c5c472012-10-11 15:28:34 +02009962/* parse the "no-tls-tickets" server keyword */
9963static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9964{
9965 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9966 return 0;
9967}
David Safb76832014-05-08 23:42:08 -04009968/* parse the "send-proxy-v2-ssl" server keyword */
9969static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9970{
9971 newsrv->pp_opts |= SRV_PP_V2;
9972 newsrv->pp_opts |= SRV_PP_V2_SSL;
9973 return 0;
9974}
9975
9976/* parse the "send-proxy-v2-ssl-cn" server keyword */
9977static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9978{
9979 newsrv->pp_opts |= SRV_PP_V2;
9980 newsrv->pp_opts |= SRV_PP_V2_SSL;
9981 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9982 return 0;
9983}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009984
Willy Tarreau732eac42015-07-09 11:40:25 +02009985/* parse the "sni" server keyword */
9986static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9987{
9988#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9989 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9990 return ERR_ALERT | ERR_FATAL;
9991#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009992 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009993
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009994 arg = args[*cur_arg + 1];
9995 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009996 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9997 return ERR_ALERT | ERR_FATAL;
9998 }
9999
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010000 free(newsrv->sni_expr);
10001 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010002
Willy Tarreau732eac42015-07-09 11:40:25 +020010003 return 0;
10004#endif
10005}
10006
Willy Tarreau92faadf2012-10-10 23:04:25 +020010007/* parse the "ssl" server keyword */
10008static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10009{
10010 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010011 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10012 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010013#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010014 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10015 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10016#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +020010017 return 0;
10018}
10019
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010020/* parse the "ssl-reuse" server keyword */
10021static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10022{
10023 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10024 return 0;
10025}
10026
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010027/* parse the "tls-tickets" server keyword */
10028static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10029{
10030 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10031 return 0;
10032}
10033
Emeric Brunef42d922012-10-11 16:11:36 +020010034/* parse the "verify" server keyword */
10035static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10036{
10037 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010038 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010039 return ERR_ALERT | ERR_FATAL;
10040 }
10041
10042 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010043 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010044 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010045 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010046 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010047 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10048 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010049 return ERR_ALERT | ERR_FATAL;
10050 }
10051
Evan Broderbe554312013-06-27 00:05:25 -070010052 return 0;
10053}
10054
10055/* parse the "verifyhost" server keyword */
10056static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10057{
10058 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010059 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010060 return ERR_ALERT | ERR_FATAL;
10061 }
10062
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010063 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010064 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10065
Emeric Brunef42d922012-10-11 16:11:36 +020010066 return 0;
10067}
10068
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010069/* parse the "ssl-default-bind-options" keyword in global section */
10070static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10071 struct proxy *defpx, const char *file, int line,
10072 char **err) {
10073 int i = 1;
10074
10075 if (*(args[i]) == 0) {
10076 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10077 return -1;
10078 }
10079 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010080 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010081 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010082 else if (!strcmp(args[i], "prefer-client-ciphers"))
10083 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010084 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10085 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10086 i++;
10087 else {
10088 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10089 return -1;
10090 }
10091 }
10092 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010093 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10094 return -1;
10095 }
10096 i++;
10097 }
10098 return 0;
10099}
10100
10101/* parse the "ssl-default-server-options" keyword in global section */
10102static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10103 struct proxy *defpx, const char *file, int line,
10104 char **err) {
10105 int i = 1;
10106
10107 if (*(args[i]) == 0) {
10108 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10109 return -1;
10110 }
10111 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010112 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010113 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010114 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10115 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10116 i++;
10117 else {
10118 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10119 return -1;
10120 }
10121 }
10122 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010123 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10124 return -1;
10125 }
10126 i++;
10127 }
10128 return 0;
10129}
10130
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010131/* parse the "ca-base" / "crt-base" keywords in global section.
10132 * Returns <0 on alert, >0 on warning, 0 on success.
10133 */
10134static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10135 struct proxy *defpx, const char *file, int line,
10136 char **err)
10137{
10138 char **target;
10139
Willy Tarreauef934602016-12-22 23:12:01 +010010140 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010141
10142 if (too_many_args(1, args, err, NULL))
10143 return -1;
10144
10145 if (*target) {
10146 memprintf(err, "'%s' already specified.", args[0]);
10147 return -1;
10148 }
10149
10150 if (*(args[1]) == 0) {
10151 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10152 return -1;
10153 }
10154 *target = strdup(args[1]);
10155 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010156}
10157
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010158/* "issuers-chain-path" load chain certificate in global */
10159static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10160{
10161 X509 *ca;
10162 X509_NAME *name = NULL;
10163 ASN1_OCTET_STRING *skid = NULL;
10164 STACK_OF(X509) *chain = NULL;
10165 struct issuer_chain *issuer;
10166 struct eb64_node *node;
10167 char *path;
10168 u64 key;
10169 int ret = 0;
10170
10171 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10172 if (chain == NULL) {
10173 chain = sk_X509_new_null();
10174 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10175 name = X509_get_subject_name(ca);
10176 }
10177 if (!sk_X509_push(chain, ca)) {
10178 X509_free(ca);
10179 goto end;
10180 }
10181 }
10182 if (!chain) {
10183 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10184 goto end;
10185 }
10186 if (!skid) {
10187 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10188 goto end;
10189 }
10190 if (!name) {
10191 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10192 goto end;
10193 }
10194 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010195 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010196 issuer = container_of(node, typeof(*issuer), node);
10197 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10198 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10199 goto end;
10200 }
10201 }
10202 issuer = calloc(1, sizeof *issuer);
10203 path = strdup(fp);
10204 if (!issuer || !path) {
10205 free(issuer);
10206 free(path);
10207 goto end;
10208 }
10209 issuer->node.key = key;
10210 issuer->path = path;
10211 issuer->chain = chain;
10212 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010213 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010214 ret = 1;
10215 end:
10216 if (skid)
10217 ASN1_OCTET_STRING_free(skid);
10218 if (chain)
10219 sk_X509_pop_free(chain, X509_free);
10220 return ret;
10221}
10222
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010223static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010224{
10225 AUTHORITY_KEYID *akid;
10226 struct issuer_chain *issuer = NULL;
10227
10228 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10229 if (akid) {
10230 struct eb64_node *node;
10231 u64 hk;
10232 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10233 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10234 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10235 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10236 issuer = ti;
10237 break;
10238 }
10239 }
10240 AUTHORITY_KEYID_free(akid);
10241 }
10242 return issuer;
10243}
10244
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010245static void ssl_free_global_issuers(void)
10246{
10247 struct eb64_node *node, *back;
10248 struct issuer_chain *issuer;
10249
William Lallemande0f3fd52020-02-25 14:53:06 +010010250 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010251 while (node) {
10252 issuer = container_of(node, typeof(*issuer), node);
10253 back = eb64_next(node);
10254 eb64_delete(node);
10255 free(issuer->path);
10256 sk_X509_pop_free(issuer->chain, X509_free);
10257 free(issuer);
10258 node = back;
10259 }
10260}
10261
10262static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10263 struct proxy *defpx, const char *file, int line,
10264 char **err)
10265{
10266 char *path;
10267 struct dirent **de_list;
10268 int i, n;
10269 struct stat buf;
10270 char *end;
10271 char fp[MAXPATHLEN+1];
10272
10273 if (too_many_args(1, args, err, NULL))
10274 return -1;
10275
10276 path = args[1];
10277 if (*path == 0 || stat(path, &buf)) {
10278 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10279 err && *err ? *err : "", args[0]);
10280 return -1;
10281 }
10282 if (S_ISDIR(buf.st_mode) == 0) {
10283 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10284 err && *err ? *err : "", args[0], path);
10285 return -1;
10286 }
10287
10288 /* strip trailing slashes, including first one */
10289 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10290 *end = 0;
10291 /* path already parsed? */
10292 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10293 return 0;
10294 /* overwrite old issuers_chain_path */
10295 free(global_ssl.issuers_chain_path);
10296 global_ssl.issuers_chain_path = strdup(path);
10297 ssl_free_global_issuers();
10298
10299 n = scandir(path, &de_list, 0, alphasort);
10300 if (n < 0) {
10301 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10302 err && *err ? *err : "", args[0], path, strerror(errno));
10303 return -1;
10304 }
10305 for (i = 0; i < n; i++) {
10306 struct dirent *de = de_list[i];
10307 BIO *in = NULL;
10308 char *warn = NULL;
10309
10310 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10311 free(de);
10312 if (stat(fp, &buf) != 0) {
10313 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10314 goto next;
10315 }
10316 if (!S_ISREG(buf.st_mode))
10317 goto next;
10318
10319 in = BIO_new(BIO_s_file());
10320 if (in == NULL)
10321 goto next;
10322 if (BIO_read_filename(in, fp) <= 0)
10323 goto next;
10324 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10325 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010326 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010327 free(warn);
10328 warn = NULL;
10329 }
10330 next:
10331 if (in)
10332 BIO_free(in);
10333 }
10334 free(de_list);
10335
10336 return 0;
10337}
10338
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010339/* parse the "ssl-mode-async" keyword in global section.
10340 * Returns <0 on alert, >0 on warning, 0 on success.
10341 */
10342static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10343 struct proxy *defpx, const char *file, int line,
10344 char **err)
10345{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010346#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010347 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010348 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010349 return 0;
10350#else
10351 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10352 return -1;
10353#endif
10354}
10355
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010356#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010357static int ssl_check_async_engine_count(void) {
10358 int err_code = 0;
10359
Emeric Brun3854e012017-05-17 20:42:48 +020010360 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010361 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010362 err_code = ERR_ABORT;
10363 }
10364 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010365}
10366
Grant Zhang872f9c22017-01-21 01:10:18 +000010367/* parse the "ssl-engine" keyword in global section.
10368 * Returns <0 on alert, >0 on warning, 0 on success.
10369 */
10370static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10371 struct proxy *defpx, const char *file, int line,
10372 char **err)
10373{
10374 char *algo;
10375 int ret = -1;
10376
10377 if (*(args[1]) == 0) {
10378 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10379 return ret;
10380 }
10381
10382 if (*(args[2]) == 0) {
10383 /* if no list of algorithms is given, it defaults to ALL */
10384 algo = strdup("ALL");
10385 goto add_engine;
10386 }
10387
10388 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10389 if (strcmp(args[2], "algo") != 0) {
10390 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10391 return ret;
10392 }
10393
10394 if (*(args[3]) == 0) {
10395 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10396 return ret;
10397 }
10398 algo = strdup(args[3]);
10399
10400add_engine:
10401 if (ssl_init_single_engine(args[1], algo)==0) {
10402 openssl_engines_initialized++;
10403 ret = 0;
10404 }
10405 free(algo);
10406 return ret;
10407}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010408#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010409
Willy Tarreauf22e9682016-12-21 23:23:19 +010010410/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10411 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10412 */
10413static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10414 struct proxy *defpx, const char *file, int line,
10415 char **err)
10416{
10417 char **target;
10418
Willy Tarreauef934602016-12-22 23:12:01 +010010419 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010420
10421 if (too_many_args(1, args, err, NULL))
10422 return -1;
10423
10424 if (*(args[1]) == 0) {
10425 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10426 return -1;
10427 }
10428
10429 free(*target);
10430 *target = strdup(args[1]);
10431 return 0;
10432}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010433
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010434#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010435/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10436 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10437 */
10438static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10439 struct proxy *defpx, const char *file, int line,
10440 char **err)
10441{
10442 char **target;
10443
10444 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10445
10446 if (too_many_args(1, args, err, NULL))
10447 return -1;
10448
10449 if (*(args[1]) == 0) {
10450 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10451 return -1;
10452 }
10453
10454 free(*target);
10455 *target = strdup(args[1]);
10456 return 0;
10457}
10458#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010459
Willy Tarreau9ceda382016-12-21 23:13:03 +010010460/* parse various global tune.ssl settings consisting in positive integers.
10461 * Returns <0 on alert, >0 on warning, 0 on success.
10462 */
10463static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10464 struct proxy *defpx, const char *file, int line,
10465 char **err)
10466{
10467 int *target;
10468
10469 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10470 target = &global.tune.sslcachesize;
10471 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010472 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010473 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010474 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010475 else if (strcmp(args[0], "maxsslconn") == 0)
10476 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010477 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10478 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010479 else {
10480 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10481 return -1;
10482 }
10483
10484 if (too_many_args(1, args, err, NULL))
10485 return -1;
10486
10487 if (*(args[1]) == 0) {
10488 memprintf(err, "'%s' expects an integer argument.", args[0]);
10489 return -1;
10490 }
10491
10492 *target = atoi(args[1]);
10493 if (*target < 0) {
10494 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10495 return -1;
10496 }
10497 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010498}
10499
10500static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10501 struct proxy *defpx, const char *file, int line,
10502 char **err)
10503{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010504 int ret;
10505
10506 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10507 if (ret != 0)
10508 return ret;
10509
Willy Tarreaubafbe012017-11-24 17:34:44 +010010510 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010511 memprintf(err, "'%s' is already configured.", args[0]);
10512 return -1;
10513 }
10514
Willy Tarreaubafbe012017-11-24 17:34:44 +010010515 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10516 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010517 memprintf(err, "Out of memory error.");
10518 return -1;
10519 }
10520 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010521}
10522
10523/* parse "ssl.force-private-cache".
10524 * Returns <0 on alert, >0 on warning, 0 on success.
10525 */
10526static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10527 struct proxy *defpx, const char *file, int line,
10528 char **err)
10529{
10530 if (too_many_args(0, args, err, NULL))
10531 return -1;
10532
Willy Tarreauef934602016-12-22 23:12:01 +010010533 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010534 return 0;
10535}
10536
10537/* parse "ssl.lifetime".
10538 * Returns <0 on alert, >0 on warning, 0 on success.
10539 */
10540static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10541 struct proxy *defpx, const char *file, int line,
10542 char **err)
10543{
10544 const char *res;
10545
10546 if (too_many_args(1, args, err, NULL))
10547 return -1;
10548
10549 if (*(args[1]) == 0) {
10550 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10551 return -1;
10552 }
10553
Willy Tarreauef934602016-12-22 23:12:01 +010010554 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010555 if (res == PARSE_TIME_OVER) {
10556 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10557 args[1], args[0]);
10558 return -1;
10559 }
10560 else if (res == PARSE_TIME_UNDER) {
10561 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10562 args[1], args[0]);
10563 return -1;
10564 }
10565 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010566 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10567 return -1;
10568 }
10569 return 0;
10570}
10571
10572#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010573/* parse "ssl-dh-param-file".
10574 * Returns <0 on alert, >0 on warning, 0 on success.
10575 */
10576static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10577 struct proxy *defpx, const char *file, int line,
10578 char **err)
10579{
10580 if (too_many_args(1, args, err, NULL))
10581 return -1;
10582
10583 if (*(args[1]) == 0) {
10584 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10585 return -1;
10586 }
10587
10588 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10589 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10590 return -1;
10591 }
10592 return 0;
10593}
10594
Willy Tarreau9ceda382016-12-21 23:13:03 +010010595/* parse "ssl.default-dh-param".
10596 * Returns <0 on alert, >0 on warning, 0 on success.
10597 */
10598static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10599 struct proxy *defpx, const char *file, int line,
10600 char **err)
10601{
10602 if (too_many_args(1, args, err, NULL))
10603 return -1;
10604
10605 if (*(args[1]) == 0) {
10606 memprintf(err, "'%s' expects an integer argument.", args[0]);
10607 return -1;
10608 }
10609
Willy Tarreauef934602016-12-22 23:12:01 +010010610 global_ssl.default_dh_param = atoi(args[1]);
10611 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010612 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10613 return -1;
10614 }
10615 return 0;
10616}
10617#endif
10618
William Lallemand3af48e72020-02-03 17:15:52 +010010619
10620/*
10621 * parse "ssl-load-extra-files".
10622 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10623 */
10624static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10625 struct proxy *defpx, const char *file, int line,
10626 char **err)
10627{
10628 int i;
10629 int gf = SSL_GF_NONE;
10630
10631 if (*(args[1]) == 0)
10632 goto err_arg;
10633
10634 for (i = 1; *args[i]; i++) {
10635
10636 if (!strcmp("bundle", args[i])) {
10637 gf |= SSL_GF_BUNDLE;
10638
10639 } else if (!strcmp("sctl", args[i])) {
10640 gf |= SSL_GF_SCTL;
10641
10642 } else if (!strcmp("ocsp", args[i])){
10643 gf |= SSL_GF_OCSP;
10644
10645 } else if (!strcmp("issuer", args[i])){
10646 gf |= SSL_GF_OCSP_ISSUER;
10647
William Lallemand4c5adbf2020-02-24 14:23:22 +010010648 } else if (!strcmp("key", args[i])) {
10649 gf |= SSL_GF_KEY;
10650
William Lallemand3af48e72020-02-03 17:15:52 +010010651 } else if (!strcmp("none", args[i])) {
10652 if (gf != SSL_GF_NONE)
10653 goto err_alone;
10654 gf = SSL_GF_NONE;
10655 i++;
10656 break;
10657
10658 } else if (!strcmp("all", args[i])) {
10659 if (gf != SSL_GF_NONE)
10660 goto err_alone;
10661 gf = SSL_GF_ALL;
10662 i++;
10663 break;
10664 } else {
10665 goto err_arg;
10666 }
10667 }
10668 /* break from loop but there are still arguments */
10669 if (*args[i])
10670 goto err_alone;
10671
10672 global_ssl.extra_files = gf;
10673
10674 return 0;
10675
10676err_alone:
10677 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10678 return -1;
10679
10680err_arg:
10681 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10682 return -1;
10683}
10684
Willy Tarreau9ceda382016-12-21 23:13:03 +010010685
William Lallemand32af2032016-10-29 18:09:35 +020010686/* This function is used with TLS ticket keys management. It permits to browse
10687 * each reference. The variable <getnext> must contain the current node,
10688 * <end> point to the root node.
10689 */
10690#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10691static inline
10692struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10693{
10694 struct tls_keys_ref *ref = getnext;
10695
10696 while (1) {
10697
10698 /* Get next list entry. */
10699 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10700
10701 /* If the entry is the last of the list, return NULL. */
10702 if (&ref->list == end)
10703 return NULL;
10704
10705 return ref;
10706 }
10707}
10708
10709static inline
10710struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10711{
10712 int id;
10713 char *error;
10714
10715 /* If the reference starts by a '#', this is numeric id. */
10716 if (reference[0] == '#') {
10717 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10718 id = strtol(reference + 1, &error, 10);
10719 if (*error != '\0')
10720 return NULL;
10721
10722 /* Perform the unique id lookup. */
10723 return tlskeys_ref_lookupid(id);
10724 }
10725
10726 /* Perform the string lookup. */
10727 return tlskeys_ref_lookup(reference);
10728}
10729#endif
10730
10731
10732#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10733
10734static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10735
10736static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10737 return cli_io_handler_tlskeys_files(appctx);
10738}
10739
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010740/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10741 * (next index to be dumped), and cli.p0 (next key reference).
10742 */
William Lallemand32af2032016-10-29 18:09:35 +020010743static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10744
10745 struct stream_interface *si = appctx->owner;
10746
10747 switch (appctx->st2) {
10748 case STAT_ST_INIT:
10749 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010750 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010751 * later and restart at the state "STAT_ST_INIT".
10752 */
10753 chunk_reset(&trash);
10754
10755 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10756 chunk_appendf(&trash, "# id secret\n");
10757 else
10758 chunk_appendf(&trash, "# id (file)\n");
10759
Willy Tarreau06d80a92017-10-19 14:32:15 +020010760 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010761 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010762 return 0;
10763 }
10764
William Lallemand32af2032016-10-29 18:09:35 +020010765 /* Now, we start the browsing of the references lists.
10766 * Note that the following call to LIST_ELEM return bad pointer. The only
10767 * available field of this pointer is <list>. It is used with the function
10768 * tlskeys_list_get_next() for retruning the first available entry
10769 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010770 if (appctx->ctx.cli.p0 == NULL) {
10771 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10772 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010773 }
10774
10775 appctx->st2 = STAT_ST_LIST;
10776 /* fall through */
10777
10778 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010779 while (appctx->ctx.cli.p0) {
10780 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010781
10782 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010783 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010784 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010785
10786 if (appctx->ctx.cli.i1 == 0)
10787 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10788
William Lallemand32af2032016-10-29 18:09:35 +020010789 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010790 int head;
10791
10792 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10793 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010794 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010795 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010796
10797 chunk_reset(t2);
10798 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010799 if (ref->key_size_bits == 128) {
10800 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10801 sizeof(struct tls_sess_key_128),
10802 t2->area, t2->size);
10803 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10804 t2->area);
10805 }
10806 else if (ref->key_size_bits == 256) {
10807 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10808 sizeof(struct tls_sess_key_256),
10809 t2->area, t2->size);
10810 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10811 t2->area);
10812 }
10813 else {
10814 /* This case should never happen */
10815 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10816 }
William Lallemand32af2032016-10-29 18:09:35 +020010817
Willy Tarreau06d80a92017-10-19 14:32:15 +020010818 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010819 /* let's try again later from this stream. We add ourselves into
10820 * this stream's users so that it can remove us upon termination.
10821 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010822 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010823 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010824 return 0;
10825 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010826 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010827 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010828 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010829 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010830 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010831 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010832 /* let's try again later from this stream. We add ourselves into
10833 * this stream's users so that it can remove us upon termination.
10834 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010835 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010836 return 0;
10837 }
10838
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010839 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010840 break;
10841
10842 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010843 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010844 }
10845
10846 appctx->st2 = STAT_ST_FIN;
10847 /* fall through */
10848
10849 default:
10850 appctx->st2 = STAT_ST_FIN;
10851 return 1;
10852 }
10853 return 0;
10854}
10855
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010856/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010857static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010858{
William Lallemand32af2032016-10-29 18:09:35 +020010859 /* no parameter, shows only file list */
10860 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010861 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010862 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010863 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010864 }
10865
10866 if (args[2][0] == '*') {
10867 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010868 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010869 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010870 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010871 if (!appctx->ctx.cli.p0)
10872 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010873 }
William Lallemand32af2032016-10-29 18:09:35 +020010874 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010875 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010876}
10877
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010878static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010879{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010880 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010881 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010882
William Lallemand32af2032016-10-29 18:09:35 +020010883 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010884 if (!*args[3] || !*args[4])
10885 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 +020010886
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010887 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010888 if (!ref)
10889 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010890
Willy Tarreau1c913e42018-08-22 05:26:57 +020010891 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010892 if (ret < 0)
10893 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010894
Willy Tarreau1c913e42018-08-22 05:26:57 +020010895 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010896 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10897 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010898
Willy Tarreau9d008692019-08-09 11:21:01 +020010899 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010900}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010901#endif
William Lallemand32af2032016-10-29 18:09:35 +020010902
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010903/*
10904 * Take an ssl_bind_conf structure and append the configuration line used to
10905 * create it in the buffer
10906 */
10907static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10908{
10909 int space = 0;
10910
10911 if (conf == NULL)
10912 return;
10913
10914 chunk_appendf(buf, " [");
10915#ifdef OPENSSL_NPN_NEGOTIATED
10916 if (conf->npn_str) {
10917 int len = conf->npn_len;
10918 char *ptr = conf->npn_str;
10919 int comma = 0;
10920
10921 if (space) chunk_appendf(buf, " ");
10922 chunk_appendf(buf, "npn ");
10923 while (len) {
10924 unsigned short size;
10925
10926 size = *ptr;
10927 ptr++;
10928 if (comma)
10929 chunk_memcat(buf, ",", 1);
10930 chunk_memcat(buf, ptr, size);
10931 ptr += size;
10932 len -= size + 1;
10933 comma = 1;
10934 }
10935 chunk_memcat(buf, "", 1); /* finish with a \0 */
10936 space++;
10937 }
10938#endif
10939#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10940 if (conf->alpn_str) {
10941 int len = conf->alpn_len;
10942 char *ptr = conf->alpn_str;
10943 int comma = 0;
10944
10945 if (space) chunk_appendf(buf, " ");
10946 chunk_appendf(buf, "alpn ");
10947 while (len) {
10948 unsigned short size;
10949
10950 size = *ptr;
10951 ptr++;
10952 if (comma)
10953 chunk_memcat(buf, ",", 1);
10954 chunk_memcat(buf, ptr, size);
10955 ptr += size;
10956 len -= size + 1;
10957 comma = 1;
10958 }
10959 chunk_memcat(buf, "", 1); /* finish with a \0 */
10960 space++;
10961 }
10962#endif
10963 /* verify */
10964 {
10965 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
10966 if (space) chunk_appendf(buf, " ");
10967 chunk_appendf(buf, "verify none");
10968 space++;
10969 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
10970 if (space) chunk_appendf(buf, " ");
10971 chunk_appendf(buf, "verify optional");
10972 space++;
10973 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
10974 if (space) chunk_appendf(buf, " ");
10975 chunk_appendf(buf, "verify required");
10976 space++;
10977 }
10978 }
10979
10980 if (conf->no_ca_names) {
10981 if (space) chunk_appendf(buf, " ");
10982 chunk_appendf(buf, "no-ca-names");
10983 space++;
10984 }
10985
10986 if (conf->early_data) {
10987 if (space) chunk_appendf(buf, " ");
10988 chunk_appendf(buf, "allow-0rtt");
10989 space++;
10990 }
10991 if (conf->ca_file) {
10992 if (space) chunk_appendf(buf, " ");
10993 chunk_appendf(buf, "ca-file %s", conf->ca_file);
10994 space++;
10995 }
10996 if (conf->crl_file) {
10997 if (space) chunk_appendf(buf, " ");
10998 chunk_appendf(buf, "crl-file %s", conf->crl_file);
10999 space++;
11000 }
11001 if (conf->ciphers) {
11002 if (space) chunk_appendf(buf, " ");
11003 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11004 space++;
11005 }
11006#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11007 if (conf->ciphersuites) {
11008 if (space) chunk_appendf(buf, " ");
11009 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11010 space++;
11011 }
11012#endif
11013 if (conf->curves) {
11014 if (space) chunk_appendf(buf, " ");
11015 chunk_appendf(buf, "curves %s", conf->curves);
11016 space++;
11017 }
11018 if (conf->ecdhe) {
11019 if (space) chunk_appendf(buf, " ");
11020 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11021 space++;
11022 }
11023
11024 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11025 /* XXX: this part need to be revamp so we don't dump the default settings */
11026 if (conf->ssl_methods.min) {
11027 if (space) chunk_appendf(buf, " ");
11028 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11029 space++;
11030 }
11031
11032 if (conf->ssl_methods.max) {
11033 if (space) chunk_appendf(buf, " ");
11034 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11035 space++;
11036 }
11037
William Lallemand58a52222020-04-02 18:11:47 +020011038 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011039
11040 return;
11041}
11042
11043/* dump a list of filters */
11044static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11045{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011046 int i;
11047
11048 if (!entry->fcount)
11049 return;
11050
11051 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011052 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011053 }
11054 return;
11055}
11056
11057/* CLI IO handler for '(show|dump) ssl crt-list' */
11058static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11059{
11060 struct buffer *trash = alloc_trash_chunk();
11061 struct stream_interface *si = appctx->owner;
11062 struct ebmb_node *lnode;
11063
11064 if (trash == NULL)
11065 return 1;
11066
11067 /* dump the list of crt-lists */
11068 lnode = appctx->ctx.cli.p1;
11069 if (lnode == NULL)
11070 lnode = ebmb_first(&crtlists_tree);
11071 while (lnode) {
11072 chunk_appendf(trash, "%s\n", lnode->key);
11073 if (ci_putchk(si_ic(si), trash) == -1) {
11074 si_rx_room_blk(si);
11075 goto yield;
11076 }
11077 lnode = ebmb_next(lnode);
11078 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011079 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011080 return 1;
11081yield:
11082 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011083 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011084 return 0;
11085}
11086
11087/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11088static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11089{
11090 struct buffer *trash = alloc_trash_chunk();
11091 struct crtlist *crtlist;
11092 struct stream_interface *si = appctx->owner;
11093 struct crtlist_entry *entry;
11094
11095 if (trash == NULL)
11096 return 1;
11097
11098 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11099
11100 entry = appctx->ctx.cli.p1;
11101 if (entry == NULL) {
11102 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11103 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11104 if (ci_putchk(si_ic(si), trash) == -1) {
11105 si_rx_room_blk(si);
11106 goto yield;
11107 }
11108 }
11109
11110 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11111 struct ckch_store *store;
11112 const char *filename;
11113
11114 store = entry->node.key;
11115 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011116 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011117 if (appctx->ctx.cli.i0 == 's') /* show */
11118 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011119 dump_crtlist_sslconf(trash, entry->ssl_conf);
11120 dump_crtlist_filters(trash, entry);
11121 chunk_appendf(trash, "\n");
11122
11123 if (ci_putchk(si_ic(si), trash) == -1) {
11124 si_rx_room_blk(si);
11125 goto yield;
11126 }
11127 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011128 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011129 return 1;
11130yield:
11131 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011132 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011133 return 0;
11134}
11135
11136/* CLI argument parser for '(show|dump) ssl crt-list' */
11137static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11138{
11139 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011140 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011141 int mode;
11142
11143 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11144 return 1;
11145
11146 appctx->ctx.cli.p0 = NULL;
11147 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011148
William Lallemandc69f02d2020-04-06 19:07:03 +020011149 if (*args[3] && !strcmp(args[3], "-n")) {
11150 mode = 's';
11151 filename = args[4];
11152 } else {
11153 mode = 'd';
11154 filename = args[3];
11155 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011156
William Lallemandc69f02d2020-04-06 19:07:03 +020011157 if (mode == 's' && !*args[4])
11158 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11159
11160 if (filename && *filename) {
11161 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011162 if (lnode == NULL)
11163 return cli_err(appctx, "didn't find the specified filename\n");
11164
11165 appctx->ctx.cli.p0 = lnode;
11166 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11167 }
11168 appctx->ctx.cli.i0 = mode;
11169
11170 return 0;
11171}
William Lallemand44b35322019-10-17 16:28:40 +020011172
William Lallemande67c80b2020-03-25 14:42:37 +010011173/* release function of the "add ssl crt-list' command, free things and unlock
11174 the spinlock */
11175static void cli_release_add_crtlist(struct appctx *appctx)
11176{
11177 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11178
11179 if (appctx->st2 != SETCERT_ST_FIN) {
11180 struct ckch_inst *inst, *inst_s;
11181 /* upon error free the ckch_inst and everything inside */
11182 ebpt_delete(&entry->node);
11183 LIST_DEL(&entry->by_crtlist);
11184 LIST_DEL(&entry->by_ckch_store);
11185
11186 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
11187 struct sni_ctx *sni, *sni_s;
11188
11189 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +020011190 SSL_CTX_free(sni->ctx);
William Lallemande67c80b2020-03-25 14:42:37 +010011191 LIST_DEL(&sni->by_ckch_inst);
11192 free(sni);
11193 }
11194 LIST_DEL(&inst->by_ckchs);
11195 free(inst);
11196 }
11197 crtlist_free_filters(entry->filters);
11198 ssl_sock_free_ssl_conf(entry->ssl_conf);
11199 free(entry->ssl_conf);
11200 free(entry);
11201 }
11202
11203 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11204}
11205
11206
11207/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11208 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11209 *
11210 * The logic is the same as the "commit ssl cert" command but without the
11211 * freeing of the old structures, because there are none.
11212 */
11213static int cli_io_handler_add_crtlist(struct appctx *appctx)
11214{
11215 struct bind_conf_list *bind_conf_node;
11216 struct stream_interface *si = appctx->owner;
11217 struct crtlist *crtlist = appctx->ctx.cli.p0;
11218 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11219 struct ckch_store *store = entry->node.key;
11220 struct buffer *trash = alloc_trash_chunk();
11221 struct ckch_inst *new_inst;
11222 char *err = NULL;
11223 int i = 0;
11224 int errcode = 0;
11225
11226 if (trash == NULL)
11227 goto error;
11228
11229 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11230 * created.
11231 */
11232 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11233 goto error;
11234
11235 while (1) {
11236 switch (appctx->st2) {
11237 case SETCERT_ST_INIT:
11238 /* This state just print the update message */
11239 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11240 if (ci_putchk(si_ic(si), trash) == -1) {
11241 si_rx_room_blk(si);
11242 goto yield;
11243 }
11244 appctx->st2 = SETCERT_ST_GEN;
11245 /* fallthrough */
11246 case SETCERT_ST_GEN:
11247 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11248 if (bind_conf_node == NULL)
11249 bind_conf_node = crtlist->bind_conf;
11250 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11251 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11252 struct sni_ctx *sni;
11253
11254 /* yield every 10 generations */
11255 if (i > 10) {
11256 appctx->ctx.cli.p2 = bind_conf_node;
11257 goto yield;
11258 }
11259
11260 /* we don't support multi-cert bundles, only simple ones */
11261 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11262 if (errcode & ERR_CODE)
11263 goto error;
11264
11265 /* we need to initialize the SSL_CTX generated */
11266 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11267 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11268 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11269 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11270 if (errcode & ERR_CODE)
11271 goto error;
11272 }
11273 }
11274 /* display one dot for each new instance */
11275 chunk_appendf(trash, ".");
11276 i++;
11277 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11278 }
11279 appctx->st2 = SETCERT_ST_INSERT;
11280 /* fallthrough */
11281 case SETCERT_ST_INSERT:
11282 /* insert SNIs in bind_conf */
11283 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11284 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11285 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11286 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11287 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011288 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011289 appctx->st2 = SETCERT_ST_FIN;
11290 goto end;
11291 }
11292 }
11293
11294end:
11295 chunk_appendf(trash, "\n");
11296 if (errcode & ERR_WARN)
11297 chunk_appendf(trash, "%s", err);
11298 chunk_appendf(trash, "Success!\n");
11299 if (ci_putchk(si_ic(si), trash) == -1)
11300 si_rx_room_blk(si);
11301 free_trash_chunk(trash);
11302 /* success: call the release function and don't come back */
11303 return 1;
11304yield:
11305 /* store the state */
11306 if (ci_putchk(si_ic(si), trash) == -1)
11307 si_rx_room_blk(si);
11308 free_trash_chunk(trash);
11309 si_rx_endp_more(si); /* let's come back later */
11310 return 0; /* should come back */
11311
11312error:
11313 /* spin unlock and free are done in the release function */
11314 if (trash) {
11315 chunk_appendf(trash, "\n%sFailed!\n", err);
11316 if (ci_putchk(si_ic(si), trash) == -1)
11317 si_rx_room_blk(si);
11318 free_trash_chunk(trash);
11319 }
11320 /* error: call the release function and don't come back */
11321 return 1;
11322}
11323
11324
11325/*
11326 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011327 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011328 */
11329static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11330{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011331 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011332 struct ckch_store *store;
11333 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011334 char path[MAXPATHLEN+1];
11335 char *crtlist_path;
11336 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011337 struct ebmb_node *eb;
11338 struct ebpt_node *inserted;
11339 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011340 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011341
11342 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11343 return 1;
11344
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011345 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011346 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11347
11348 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011349
11350 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11351 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11352
11353 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11354 if (!eb) {
11355 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11356 goto error;
11357 }
11358 crtlist = ebmb_entry(eb, struct crtlist, node);
11359
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011360 entry = malloc(sizeof(*entry));
11361 if (entry == NULL) {
11362 memprintf(&err, "Not enough memory!");
11363 goto error;
11364 }
William Lallemanda690fed2020-04-02 12:02:54 +020011365 entry->fcount = 0;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011366 entry->filters = NULL;
11367 entry->ssl_conf = NULL;
11368
11369 if (payload) {
11370 char *lf;
11371
11372 lf = strrchr(payload, '\n');
11373 if (lf) {
11374 memprintf(&err, "only one line of payload is supported!");
11375 goto error;
11376 }
11377 /* cert_path is filled here */
11378 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11379 if (cfgerr & ERR_CODE)
11380 goto error;
11381 } else {
11382 cert_path = args[4];
11383 }
11384
11385 if (!cert_path) {
11386 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11387 cfgerr |= ERR_ALERT | ERR_FATAL;
11388 goto error;
11389 }
11390
11391 if (*cert_path != '/' && global_ssl.crt_base) {
11392 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11393 memprintf(&err, "'%s' : path too long", cert_path);
11394 cfgerr |= ERR_ALERT | ERR_FATAL;
11395 goto error;
11396 }
11397 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11398 cert_path = path;
11399 }
11400
William Lallemande67c80b2020-03-25 14:42:37 +010011401 store = ckchs_lookup(cert_path);
11402 if (store == NULL) {
11403 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11404 goto error;
11405 }
William Lallemand36ccc392020-04-08 10:57:24 +020011406 if (store->multi) {
11407 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);
11408 goto error;
11409 }
William Lallemande67c80b2020-03-25 14:42:37 +010011410 if (store->ckch == NULL || store->ckch->cert == NULL) {
11411 memprintf(&err, "certificate '%s' is empty!", cert_path);
11412 goto error;
11413 }
11414
William Lallemande67c80b2020-03-25 14:42:37 +010011415 /* check if it's possible to insert this new crtlist_entry */
11416 entry->node.key = store;
11417 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11418 if (inserted != &entry->node) {
11419 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011420 goto error;
11421 }
11422
11423 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
11424 LIST_INIT(&entry->ckch_inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011425 entry->crtlist = crtlist;
11426 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11427
11428 appctx->st2 = SETCERT_ST_INIT;
11429 appctx->ctx.cli.p0 = crtlist;
11430 appctx->ctx.cli.p1 = entry;
11431
11432 /* unlock is done in the release handler */
11433 return 0;
11434
11435error:
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011436 if (entry) {
11437 crtlist_free_filters(entry->filters);
11438 ssl_sock_free_ssl_conf(entry->ssl_conf);
11439 free(entry->ssl_conf);
11440 free(entry);
11441 }
William Lallemande67c80b2020-03-25 14:42:37 +010011442 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11443 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11444 return cli_dynerr(appctx, err);
11445}
11446
William Lallemand0a9b9412020-04-06 17:43:05 +020011447/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11448static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11449{
11450 struct ckch_store *store;
11451 char *err = NULL;
11452 char *crtlist_path, *cert_path;
11453 struct ebmb_node *ebmb;
11454 struct ebpt_node *ebpt;
11455 struct crtlist *crtlist;
11456 struct crtlist_entry *entry = NULL;
11457 struct ckch_inst *inst, *inst_s;
11458 int linenum = 0;
11459 char *colons;
11460
11461 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11462 return 1;
11463
11464 if (!*args[3] || !*args[4])
11465 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11466
William Lallemand463b5242020-04-08 10:30:44 +020011467 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11468 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11469
William Lallemand0a9b9412020-04-06 17:43:05 +020011470 crtlist_path = args[3];
11471 cert_path = args[4];
11472
11473 colons = strchr(cert_path, ':');
11474 if (colons) {
11475 char *endptr;
11476
11477 linenum = strtol(colons + 1, &endptr, 10);
11478 if (colons + 1 == endptr || *endptr != '\0') {
11479 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11480 goto error;
11481 }
11482 *colons = '\0';
11483 }
11484 /* look for crtlist */
11485 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11486 if (!ebmb) {
11487 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11488 goto error;
11489 }
11490 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11491
11492 /* look for store */
11493 store = ckchs_lookup(cert_path);
11494 if (store == NULL) {
11495 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11496 goto error;
11497 }
William Lallemand36ccc392020-04-08 10:57:24 +020011498 if (store->multi) {
11499 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);
11500 goto error;
11501 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011502 if (store->ckch == NULL || store->ckch->cert == NULL) {
11503 memprintf(&err, "certificate '%s' is empty!", cert_path);
11504 goto error;
11505 }
11506
11507 ebpt = ebpt_lookup(&crtlist->entries, store);
11508 if (!ebpt) {
11509 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11510 goto error;
11511 }
11512
11513 /* list the line number of entries for errors in err, and select the right ebpt */
11514 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11515 struct crtlist_entry *tmp;
11516
11517 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11518 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11519
11520 /* select the entry we wanted */
11521 if (linenum == 0 || tmp->linenum == linenum) {
11522 if (!entry)
11523 entry = tmp;
11524 }
11525 }
11526
11527 /* we didn't found the specified entry */
11528 if (!entry) {
11529 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);
11530 goto error;
11531 }
11532
11533 /* we didn't specified a line number but there were several entries */
11534 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11535 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11536 goto error;
11537 }
11538
11539 /* upon error free the ckch_inst and everything inside */
11540
11541 ebpt_delete(&entry->node);
11542 LIST_DEL(&entry->by_crtlist);
11543 LIST_DEL(&entry->by_ckch_store);
11544
11545 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11546 struct sni_ctx *sni, *sni_s;
11547
11548 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11549 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11550 ebmb_delete(&sni->name);
11551 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011552 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011553 free(sni);
11554 }
11555 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11556 LIST_DEL(&inst->by_ckchs);
11557 free(inst);
11558 }
11559
11560 crtlist_free_filters(entry->filters);
11561 ssl_sock_free_ssl_conf(entry->ssl_conf);
11562 free(entry->ssl_conf);
11563 free(entry);
11564
11565 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11566 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11567 return cli_dynmsg(appctx, LOG_NOTICE, err);
11568
11569error:
11570 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11571 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11572 return cli_dynerr(appctx, err);
11573}
William Lallemande67c80b2020-03-25 14:42:37 +010011574
William Lallemand44b35322019-10-17 16:28:40 +020011575/* Type of SSL payloads that can be updated over the CLI */
11576
11577enum {
11578 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011579 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011580#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011581 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011582#endif
William Lallemand44b35322019-10-17 16:28:40 +020011583 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011584#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011585 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011586#endif
William Lallemand44b35322019-10-17 16:28:40 +020011587 CERT_TYPE_MAX,
11588};
11589
11590struct {
11591 const char *ext;
11592 int type;
11593 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11594 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011595} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011596 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011597 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011598#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011599 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011600#endif
11601#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] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011603#endif
William Lallemand44b35322019-10-17 16:28:40 +020011604 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011605 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011606};
11607
William Lallemand8f840d72019-10-23 10:53:05 +020011608
William Lallemandd4f946c2019-12-05 10:26:40 +010011609/* release function of the `show ssl cert' command */
11610static void cli_release_show_cert(struct appctx *appctx)
11611{
11612 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11613}
11614
11615/* IO handler of "show ssl cert <filename>" */
11616static int cli_io_handler_show_cert(struct appctx *appctx)
11617{
11618 struct buffer *trash = alloc_trash_chunk();
11619 struct ebmb_node *node;
11620 struct stream_interface *si = appctx->owner;
11621 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011622
11623 if (trash == NULL)
11624 return 1;
11625
11626 if (!appctx->ctx.ssl.old_ckchs) {
11627 if (ckchs_transaction.old_ckchs) {
11628 ckchs = ckchs_transaction.old_ckchs;
11629 chunk_appendf(trash, "# transaction\n");
11630 if (!ckchs->multi) {
11631 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011632#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011633 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011634 int n;
11635
William Lallemandd4f946c2019-12-05 10:26:40 +010011636 chunk_appendf(trash, "*%s:", ckchs->path);
11637 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11638 if (ckchs->ckch[n].cert)
11639 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11640 }
11641 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011642#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011643 }
11644 }
11645 }
11646
11647 if (!appctx->ctx.cli.p0) {
11648 chunk_appendf(trash, "# filename\n");
11649 node = ebmb_first(&ckchs_tree);
11650 } else {
11651 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11652 }
11653 while (node) {
11654 ckchs = ebmb_entry(node, struct ckch_store, node);
11655 if (!ckchs->multi) {
11656 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011657#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011658 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011659 int n;
11660
William Lallemandd4f946c2019-12-05 10:26:40 +010011661 chunk_appendf(trash, "%s:", ckchs->path);
11662 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11663 if (ckchs->ckch[n].cert)
11664 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11665 }
11666 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011667#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011668 }
11669
11670 node = ebmb_next(node);
11671 if (ci_putchk(si_ic(si), trash) == -1) {
11672 si_rx_room_blk(si);
11673 goto yield;
11674 }
11675 }
11676
11677 appctx->ctx.cli.p0 = NULL;
11678 free_trash_chunk(trash);
11679 return 1;
11680yield:
11681
11682 free_trash_chunk(trash);
11683 appctx->ctx.cli.p0 = ckchs;
11684 return 0; /* should come back */
11685}
11686
11687/* IO handler of the details "show ssl cert <filename>" */
11688static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11689{
11690 struct stream_interface *si = appctx->owner;
11691 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11692 struct buffer *out = alloc_trash_chunk();
11693 struct buffer *tmp = alloc_trash_chunk();
11694 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011695 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011696 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011697 int write = -1;
11698 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011699 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011700
11701 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011702 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011703
11704 if (!ckchs->multi) {
11705 chunk_appendf(out, "Filename: ");
11706 if (ckchs == ckchs_transaction.new_ckchs)
11707 chunk_appendf(out, "*");
11708 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011709
William Lallemand59c16fc2020-03-19 20:26:02 +010011710 chunk_appendf(out, "Status: ");
11711 if (ckchs->ckch->cert == NULL)
11712 chunk_appendf(out, "Empty\n");
11713 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11714 chunk_appendf(out, "Unused\n");
11715 else
11716 chunk_appendf(out, "Used\n");
11717
William Lallemandea987ed2020-03-19 16:48:33 +010011718 if (ckchs->ckch->cert == NULL)
11719 goto end;
11720
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011721 chain = ckchs->ckch->chain;
11722 if (chain == NULL) {
11723 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011724 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011725 if (issuer) {
11726 chain = issuer->chain;
11727 chunk_appendf(out, "Chain Filename: ");
11728 chunk_appendf(out, "%s\n", issuer->path);
11729 }
11730 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011731 chunk_appendf(out, "Serial: ");
11732 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11733 goto end;
11734 dump_binary(out, tmp->area, tmp->data);
11735 chunk_appendf(out, "\n");
11736
11737 chunk_appendf(out, "notBefore: ");
11738 chunk_reset(tmp);
11739 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11740 goto end;
11741 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11742 goto end;
11743 write = BIO_read(bio, tmp->area, tmp->size-1);
11744 tmp->area[write] = '\0';
11745 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011746 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011747 chunk_appendf(out, "%s\n", tmp->area);
11748
11749 chunk_appendf(out, "notAfter: ");
11750 chunk_reset(tmp);
11751 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11752 goto end;
11753 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11754 goto end;
11755 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11756 goto end;
11757 tmp->area[write] = '\0';
11758 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011759 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011760 chunk_appendf(out, "%s\n", tmp->area);
11761
William Lallemandd4f946c2019-12-05 10:26:40 +010011762#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11763 chunk_appendf(out, "Subject Alternative Name: ");
11764 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11765 goto end;
11766 *(out->area + out->data) = '\0';
11767 chunk_appendf(out, "\n");
11768#endif
11769 chunk_reset(tmp);
11770 chunk_appendf(out, "Algorithm: ");
11771 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11772 goto end;
11773 chunk_appendf(out, "%s\n", tmp->area);
11774
11775 chunk_reset(tmp);
11776 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011777 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011778 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011779 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011780 dump_binary(out, tmp->area, tmp->data);
11781 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011782
William Lallemanda90e5932020-02-25 14:07:58 +010011783 chunk_appendf(out, "Subject: ");
11784 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11785 goto end;
11786 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11787 goto end;
11788 *(tmp->area + tmp->data) = '\0';
11789 chunk_appendf(out, "%s\n", tmp->area);
11790
11791 chunk_appendf(out, "Issuer: ");
11792 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11793 goto end;
11794 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11795 goto end;
11796 *(tmp->area + tmp->data) = '\0';
11797 chunk_appendf(out, "%s\n", tmp->area);
11798
William Lallemand35f4a9d2020-02-25 11:56:32 +010011799 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011800 for (i = 0; i < sk_X509_num(chain); i++) {
11801 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011802
William Lallemandbb7288a2020-02-25 14:04:33 +010011803 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011804 if ((name = X509_get_subject_name(ca)) == NULL)
11805 goto end;
11806 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11807 goto end;
11808 *(tmp->area + tmp->data) = '\0';
11809 chunk_appendf(out, "%s\n", tmp->area);
11810
William Lallemandbb7288a2020-02-25 14:04:33 +010011811 chunk_appendf(out, "Chain Issuer: ");
11812 if ((name = X509_get_issuer_name(ca)) == NULL)
11813 goto end;
11814 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11815 goto end;
11816 *(tmp->area + tmp->data) = '\0';
11817 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011818 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011819 }
11820
William Lallemandea987ed2020-03-19 16:48:33 +010011821end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011822 if (ci_putchk(si_ic(si), out) == -1) {
11823 si_rx_room_blk(si);
11824 goto yield;
11825 }
11826
William Lallemand18eeb8e2020-03-20 14:42:36 +010011827end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011828 if (bio)
11829 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011830 free_trash_chunk(tmp);
11831 free_trash_chunk(out);
11832 return 1;
11833yield:
11834 free_trash_chunk(tmp);
11835 free_trash_chunk(out);
11836 return 0; /* should come back */
11837}
11838
11839/* parsing function for 'show ssl cert [certfile]' */
11840static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11841{
11842 struct ckch_store *ckchs;
11843
11844 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11845 return cli_err(appctx, "Can't allocate memory!\n");
11846
11847 /* The operations on the CKCH architecture are locked so we can
11848 * manipulate ckch_store and ckch_inst */
11849 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11850 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11851
11852 /* check if there is a certificate to lookup */
11853 if (*args[3]) {
11854 if (*args[3] == '*') {
11855 if (!ckchs_transaction.new_ckchs)
11856 goto error;
11857
11858 ckchs = ckchs_transaction.new_ckchs;
11859
11860 if (strcmp(args[3] + 1, ckchs->path))
11861 goto error;
11862
11863 } else {
11864 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11865 goto error;
11866
11867 }
11868
11869 if (ckchs->multi)
11870 goto error;
11871
11872 appctx->ctx.cli.p0 = ckchs;
11873 /* use the IO handler that shows details */
11874 appctx->io_handler = cli_io_handler_show_cert_detail;
11875 }
11876
11877 return 0;
11878
11879error:
11880 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11881 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11882}
11883
William Lallemand430413e2019-10-28 14:30:47 +010011884/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011885static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011886{
11887 struct ckch_store *new_ckchs;
11888 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011889
William Lallemand430413e2019-10-28 14:30:47 +010011890 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011891
William Lallemand430413e2019-10-28 14:30:47 +010011892 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011893 /* 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 +010011894 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011895
William Lallemandbeea2a42019-10-30 17:45:33 +010011896 if (!new_ckchs)
11897 return;
William Lallemand8f840d72019-10-23 10:53:05 +020011898
William Lallemandbeea2a42019-10-30 17:45:33 +010011899 /* if the allocation failed, we need to free everything from the temporary list */
11900 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
11901 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020011902
William Lallemandbeea2a42019-10-30 17:45:33 +010011903 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +020011904 SSL_CTX_free(sc0->ctx);
William Lallemandbeea2a42019-10-30 17:45:33 +010011905 LIST_DEL(&sc0->by_ckch_inst);
11906 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020011907 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011908 LIST_DEL(&ckchi->by_ckchs);
11909 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020011910 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011911 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011912 }
11913}
11914
11915
11916/*
11917 * This function tries to create the new ckch_inst and their SNIs
11918 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011919static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011920{
11921 struct stream_interface *si = appctx->owner;
11922 int y = 0;
11923 char *err = NULL;
11924 int errcode = 0;
11925 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11926 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011927 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011928 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011929 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011930
William Lallemand33cc76f2019-10-31 11:43:45 +010011931 if (trash == NULL)
11932 goto error;
11933
William Lallemand8f840d72019-10-23 10:53:05 +020011934 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11935 goto error;
11936
William Lallemand430413e2019-10-28 14:30:47 +010011937 while (1) {
11938 switch (appctx->st2) {
11939 case SETCERT_ST_INIT:
11940 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011941 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011942 if (ci_putchk(si_ic(si), trash) == -1) {
11943 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011944 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011945 }
11946 appctx->st2 = SETCERT_ST_GEN;
11947 /* fallthrough */
11948 case SETCERT_ST_GEN:
11949 /*
11950 * This state generates the ckch instances with their
11951 * sni_ctxs and SSL_CTX.
11952 *
William Lallemand430413e2019-10-28 14:30:47 +010011953 * Since the SSL_CTX generation can be CPU consumer, we
11954 * yield every 10 instances.
11955 */
William Lallemand8f840d72019-10-23 10:53:05 +020011956
William Lallemandbeea2a42019-10-30 17:45:33 +010011957 old_ckchs = appctx->ctx.ssl.old_ckchs;
11958 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011959
William Lallemandbeea2a42019-10-30 17:45:33 +010011960 if (!new_ckchs)
11961 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020011962
William Lallemandbeea2a42019-10-30 17:45:33 +010011963 /* get the next ckchi to regenerate */
11964 ckchi = appctx->ctx.ssl.next_ckchi;
11965 /* we didn't start yet, set it to the first elem */
11966 if (ckchi == NULL)
11967 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011968
William Lallemandbeea2a42019-10-30 17:45:33 +010011969 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
11970 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
11971 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010011972 char **sni_filter = NULL;
11973 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020011974
William Lallemandbeea2a42019-10-30 17:45:33 +010011975 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
11976 if (y >= 10) {
11977 /* save the next ckchi to compute */
11978 appctx->ctx.ssl.next_ckchi = ckchi;
11979 goto yield;
11980 }
William Lallemandcaa16192020-04-08 16:29:15 +020011981
11982 if (ckchi->crtlist_entry) {
11983 sni_filter = ckchi->crtlist_entry->filters;
11984 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010011985 }
William Lallemand38df1c82019-12-04 15:39:35 +010011986
William Lallemandbeea2a42019-10-30 17:45:33 +010011987 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010011988 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 +010011989 else
William Lallemand38df1c82019-12-04 15:39:35 +010011990 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
11991
William Lallemandbeea2a42019-10-30 17:45:33 +010011992 if (errcode & ERR_CODE)
11993 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020011994
William Lallemand21724f02019-11-04 17:56:13 +010011995 /* if the previous ckchi was used as the default */
11996 if (ckchi->is_default)
11997 new_inst->is_default = 1;
11998
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011999 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010012000 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
12001 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012002 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 +010012003 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
12004 if (errcode & ERR_CODE)
12005 goto error;
12006 }
12007 }
12008
12009
William Lallemandbeea2a42019-10-30 17:45:33 +010012010 /* display one dot per new instance */
12011 chunk_appendf(trash, ".");
12012 /* link the new ckch_inst to the duplicate */
12013 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
12014 y++;
12015 }
William Lallemand430413e2019-10-28 14:30:47 +010012016 appctx->st2 = SETCERT_ST_INSERT;
12017 /* fallthrough */
12018 case SETCERT_ST_INSERT:
12019 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012020
William Lallemandbeea2a42019-10-30 17:45:33 +010012021 old_ckchs = appctx->ctx.ssl.old_ckchs;
12022 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012023
William Lallemandbeea2a42019-10-30 17:45:33 +010012024 if (!new_ckchs)
12025 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012026
William Lallemand90afe902020-03-30 19:29:45 +020012027 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12028 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12029 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12030 ebpt_delete(&entry->node);
12031 /* change the ptr and reinsert the node */
12032 entry->node.key = new_ckchs;
12033 ebpt_insert(&entry->crtlist->entries, &entry->node);
12034 }
12035
William Lallemand21724f02019-11-04 17:56:13 +010012036 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012037 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12038 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12039 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12040 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12041 }
William Lallemand8f840d72019-10-23 10:53:05 +020012042
William Lallemandbeea2a42019-10-30 17:45:33 +010012043 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12044 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010012045
William Lallemandbeea2a42019-10-30 17:45:33 +010012046 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12047 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +020012048 SSL_CTX_free(sc0->ctx);
William Lallemandbeea2a42019-10-30 17:45:33 +010012049 ebmb_delete(&sc0->name);
12050 LIST_DEL(&sc0->by_ckch_inst);
12051 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010012052 }
William Lallemandbeea2a42019-10-30 17:45:33 +010012053 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12054 LIST_DEL(&ckchi->by_ckchs);
12055 free(ckchi);
12056 }
William Lallemand8f840d72019-10-23 10:53:05 +020012057
William Lallemandbeea2a42019-10-30 17:45:33 +010012058 /* Replace the old ckchs by the new one */
12059 ebmb_delete(&old_ckchs->node);
12060 ckchs_free(old_ckchs);
12061 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012062 appctx->st2 = SETCERT_ST_FIN;
12063 /* fallthrough */
12064 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012065 /* we achieved the transaction, we can set everything to NULL */
12066 free(ckchs_transaction.path);
12067 ckchs_transaction.path = NULL;
12068 ckchs_transaction.new_ckchs = NULL;
12069 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012070 goto end;
12071 }
William Lallemand8f840d72019-10-23 10:53:05 +020012072 }
William Lallemand430413e2019-10-28 14:30:47 +010012073end:
William Lallemand8f840d72019-10-23 10:53:05 +020012074
William Lallemanded442432019-11-21 16:41:07 +010012075 chunk_appendf(trash, "\n");
12076 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012077 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012078 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012079 if (ci_putchk(si_ic(si), trash) == -1)
12080 si_rx_room_blk(si);
12081 free_trash_chunk(trash);
12082 /* success: call the release function and don't come back */
12083 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012084yield:
12085 /* store the state */
12086 if (ci_putchk(si_ic(si), trash) == -1)
12087 si_rx_room_blk(si);
12088 free_trash_chunk(trash);
12089 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012090 return 0; /* should come back */
12091
12092error:
12093 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012094 if (trash) {
12095 chunk_appendf(trash, "\n%sFailed!\n", err);
12096 if (ci_putchk(si_ic(si), trash) == -1)
12097 si_rx_room_blk(si);
12098 free_trash_chunk(trash);
12099 }
William Lallemand430413e2019-10-28 14:30:47 +010012100 /* error: call the release function and don't come back */
12101 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012102}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012103
12104/*
12105 * Parsing function of 'commit ssl cert'
12106 */
12107static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12108{
12109 char *err = NULL;
12110
William Lallemand230662a2019-12-03 13:32:54 +010012111 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12112 return 1;
12113
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012114 if (!*args[3])
12115 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12116
12117 /* The operations on the CKCH architecture are locked so we can
12118 * manipulate ckch_store and ckch_inst */
12119 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12120 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12121
12122 if (!ckchs_transaction.path) {
12123 memprintf(&err, "No ongoing transaction! !\n");
12124 goto error;
12125 }
12126
12127 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12128 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12129 goto error;
12130 }
12131
William Lallemand4c5adbf2020-02-24 14:23:22 +010012132#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12133 if (ckchs_transaction.new_ckchs->multi) {
12134 int n;
12135
12136 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12137 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)) {
12138 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12139 goto error;
12140 }
12141 }
12142 } else
12143#endif
12144 {
12145 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12146 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12147 goto error;
12148 }
12149 }
12150
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012151 /* init the appctx structure */
12152 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012153 appctx->ctx.ssl.next_ckchi = NULL;
12154 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12155 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12156
12157 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12158 return 0;
12159
12160error:
12161
12162 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12163 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12164
12165 return cli_dynerr(appctx, err);
12166}
12167
William Lallemand8f840d72019-10-23 10:53:05 +020012168/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012169 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012170 */
William Lallemand150bfa82019-09-19 17:12:49 +020012171static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12172{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012173 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012174 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012175 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012176 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012177 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012178 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012179 char *end;
12180 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012181 struct cert_key_and_chain *ckch;
12182 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012183
William Lallemand230662a2019-12-03 13:32:54 +010012184 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12185 return 1;
12186
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012187 if ((buf = alloc_trash_chunk()) == NULL)
12188 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012189
12190 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012191 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012192
12193 /* The operations on the CKCH architecture are locked so we can
12194 * manipulate ckch_store and ckch_inst */
12195 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12196 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12197
William Lallemand8f840d72019-10-23 10:53:05 +020012198 if (!chunk_strcpy(buf, args[3])) {
12199 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12200 errcode |= ERR_ALERT | ERR_FATAL;
12201 goto end;
12202 }
12203
William Lallemand44b35322019-10-17 16:28:40 +020012204 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012205 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012206 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012207 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12208 *end = '\0';
12209 type = cert_exts[i].type;
12210 break;
12211 }
12212 }
12213
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012214 appctx->ctx.ssl.old_ckchs = NULL;
12215 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012216
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012217 /* if there is an ongoing transaction */
12218 if (ckchs_transaction.path) {
12219 /* 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 +020012220#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012221 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012222 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012223 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012224
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012225 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012226 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012227 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012228 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012229 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12230 bundle = j; /* keep the type of certificate so we insert it at the right place */
12231 *end = '\0'; /* it's a bundle let's end the string*/
12232 break;
12233 }
William Lallemand150bfa82019-09-19 17:12:49 +020012234 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012235 if (bundle < 0) {
12236 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);
12237 errcode |= ERR_ALERT | ERR_FATAL;
12238 goto end;
12239 }
12240 }
12241#endif
12242
12243 /* if there is an ongoing transaction, check if this is the same file */
12244 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12245 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12246 errcode |= ERR_ALERT | ERR_FATAL;
12247 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012248 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012249
12250 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12251
12252 } else {
12253 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12254
12255 /* lookup for the certificate in the tree:
12256 * check if this is used as a bundle AND as a unique certificate */
12257 for (i = 0; i < 2; i++) {
12258
12259 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12260 /* only the bundle name is in the tree and you should
12261 * never update a bundle name, only a filename */
12262 if (bundle < 0 && find_ckchs[i]->multi) {
12263 /* we tried to look for a non-bundle and we found a bundle */
12264 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12265 err ? err : "", args[3], args[3]);
12266 errcode |= ERR_ALERT | ERR_FATAL;
12267 goto end;
12268 }
William Lallemand3246d942019-11-04 14:02:11 +010012269 /* If we want a bundle but this is not a bundle
12270 * example: When you try to update <file>.rsa, but
12271 * <file> is a regular file */
12272 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12273 find_ckchs[i] = NULL;
12274 break;
12275 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012276 }
12277#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12278 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012279 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012280 int j;
12281
12282 /* check if it was used in a bundle by removing the
12283 * .dsa/.rsa/.ecdsa at the end of the filename */
12284 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012285 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012286 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12287 bundle = j; /* keep the type of certificate so we insert it at the right place */
12288 *end = '\0'; /* it's a bundle let's end the string*/
12289 break;
12290 }
12291 }
William Lallemand37031b82019-11-04 13:38:53 +010012292 if (bundle < 0) /* we didn't find a bundle extension */
12293 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012294 }
William Lallemand963b2e72019-10-14 11:38:36 +020012295#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012296 /* bundles are not supported here, so we don't need to lookup again */
12297 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012298#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012299 }
12300
12301 if (find_ckchs[0] && find_ckchs[1]) {
12302 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",
12303 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12304 errcode |= ERR_ALERT | ERR_FATAL;
12305 goto end;
12306 }
12307
12308 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012309 }
12310
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012311 if (!appctx->ctx.ssl.old_ckchs) {
12312 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012313 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012314 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012315 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012316 }
12317
William Lallemand8a7fdf02019-11-04 10:59:32 +010012318 if (!appctx->ctx.ssl.path) {
12319 /* this is a new transaction, set the path of the transaction */
12320 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12321 if (!appctx->ctx.ssl.path) {
12322 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12323 errcode |= ERR_ALERT | ERR_FATAL;
12324 goto end;
12325 }
12326 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012327
12328 old_ckchs = appctx->ctx.ssl.old_ckchs;
12329
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012330 /* duplicate the ckch store */
12331 new_ckchs = ckchs_dup(old_ckchs);
12332 if (!new_ckchs) {
12333 memprintf(&err, "%sCannot allocate memory!\n",
12334 err ? err : "");
12335 errcode |= ERR_ALERT | ERR_FATAL;
12336 goto end;
12337 }
12338
12339 if (!new_ckchs->multi)
12340 ckch = new_ckchs->ckch;
12341 else
12342 ckch = &new_ckchs->ckch[bundle];
12343
12344 /* appply the change on the duplicate */
12345 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12346 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12347 errcode |= ERR_ALERT | ERR_FATAL;
12348 goto end;
12349 }
12350
12351 appctx->ctx.ssl.new_ckchs = new_ckchs;
12352
12353 /* we succeed, we can save the ckchs in the transaction */
12354
12355 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012356 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012357 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12358 ckchs_transaction.path = appctx->ctx.ssl.path;
12359 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12360 } else {
12361 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12362
12363 }
12364
12365 /* free the previous ckchs if there was a transaction */
12366 ckchs_free(ckchs_transaction.new_ckchs);
12367
12368 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12369
12370
William Lallemand8f840d72019-10-23 10:53:05 +020012371 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012372
William Lallemand8f840d72019-10-23 10:53:05 +020012373end:
12374 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012375
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012376 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012377
12378 ckchs_free(appctx->ctx.ssl.new_ckchs);
12379 appctx->ctx.ssl.new_ckchs = NULL;
12380
12381 appctx->ctx.ssl.old_ckchs = NULL;
12382
12383 free(appctx->ctx.ssl.path);
12384 appctx->ctx.ssl.path = NULL;
12385
12386 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012387 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012388 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012389
12390 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12391 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012392 }
William Lallemand8f840d72019-10-23 10:53:05 +020012393 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012394}
12395
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012396/* parsing function of 'abort ssl cert' */
12397static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12398{
12399 char *err = NULL;
12400
William Lallemand230662a2019-12-03 13:32:54 +010012401 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12402 return 1;
12403
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012404 if (!*args[3])
12405 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12406
12407 /* The operations on the CKCH architecture are locked so we can
12408 * manipulate ckch_store and ckch_inst */
12409 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12410 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12411
12412 if (!ckchs_transaction.path) {
12413 memprintf(&err, "No ongoing transaction!\n");
12414 goto error;
12415 }
12416
12417 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12418 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12419 goto error;
12420 }
12421
12422 /* Only free the ckchs there, because the SNI and instances were not generated yet */
12423 ckchs_free(ckchs_transaction.new_ckchs);
12424 ckchs_transaction.new_ckchs = NULL;
12425 ckchs_free(ckchs_transaction.old_ckchs);
12426 ckchs_transaction.old_ckchs = NULL;
12427 free(ckchs_transaction.path);
12428 ckchs_transaction.path = NULL;
12429
12430 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12431
12432 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12433 return cli_dynmsg(appctx, LOG_NOTICE, err);
12434
12435error:
12436 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12437
12438 return cli_dynerr(appctx, err);
12439}
12440
William Lallemandea987ed2020-03-19 16:48:33 +010012441/* parsing function of 'new ssl cert' */
12442static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12443{
12444 struct ckch_store *store;
12445 char *err = NULL;
12446 char *path;
12447
12448 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12449 return 1;
12450
12451 if (!*args[3])
12452 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12453
12454 path = args[3];
12455
12456 /* The operations on the CKCH architecture are locked so we can
12457 * manipulate ckch_store and ckch_inst */
12458 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12459 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12460
12461 store = ckchs_lookup(path);
12462 if (store != NULL) {
12463 memprintf(&err, "Certificate '%s' already exists!\n", path);
12464 store = NULL; /* we don't want to free it */
12465 goto error;
12466 }
12467 store = calloc(1, sizeof(*store) + strlen(path) + 1);
12468 if (!store) {
12469 memprintf(&err, "unable to allocate memory.\n");
12470 goto error;
12471 }
12472 store->ckch = calloc(1, sizeof(*store->ckch));
12473 if (!store->ckch) {
12474 memprintf(&err, "unable to allocate memory.\n");
12475 goto error;
12476 }
12477 /* we won't create any instance */
12478 LIST_INIT(&store->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +020012479 LIST_INIT(&store->crtlist_entry);
William Lallemandea987ed2020-03-19 16:48:33 +010012480
12481 /* we won't support multi-certificate bundle here */
12482 store->multi = 0;
12483
12484 /* insert into the ckchs tree */
12485 memcpy(store->path, path, strlen(path) + 1);
12486 ebst_insert(&ckchs_tree, &store->node);
12487 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12488
12489 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12490 return cli_dynmsg(appctx, LOG_NOTICE, err);
12491error:
12492 free(store);
12493 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12494 return cli_dynerr(appctx, err);
12495}
12496
William Lallemand419e6342020-04-08 12:05:39 +020012497/* parsing function of 'del ssl cert' */
12498static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12499{
12500 struct ckch_store *store;
12501 char *err = NULL;
12502 char *filename;
12503
12504 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12505 return 1;
12506
12507 if (!*args[3])
12508 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12509
12510 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12511 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12512
12513 filename = args[3];
12514
12515 store = ckchs_lookup(filename);
12516 if (store == NULL) {
12517 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12518 goto error;
12519 }
12520 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12521 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12522 goto error;
12523 }
12524
12525 ebmb_delete(&store->node);
12526 ckchs_free(store);
12527
12528 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12529
12530 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12531 return cli_dynmsg(appctx, LOG_NOTICE, err);
12532
12533error:
12534 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12535 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12536 return cli_dynerr(appctx, err);
12537}
12538
12539
12540
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012541static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012542{
12543#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12544 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012545 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012546
12547 if (!payload)
12548 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012549
12550 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012551 if (!*payload)
12552 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012553
12554 /* remove \r and \n from the payload */
12555 for (i = 0, j = 0; payload[i]; i++) {
12556 if (payload[i] == '\r' || payload[i] == '\n')
12557 continue;
12558 payload[j++] = payload[i];
12559 }
12560 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012561
Willy Tarreau1c913e42018-08-22 05:26:57 +020012562 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012563 if (ret < 0)
12564 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012565
Willy Tarreau1c913e42018-08-22 05:26:57 +020012566 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012567 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012568 if (err)
12569 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12570 else
12571 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012572 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012573
12574 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012575#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012576 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 +020012577#endif
12578
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012579}
12580
Willy Tarreau86a394e2019-05-09 14:15:32 +020012581#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012582static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12583{
12584 switch (arg->type) {
12585 case ARGT_STR:
12586 smp->data.type = SMP_T_STR;
12587 smp->data.u.str = arg->data.str;
12588 return 1;
12589 case ARGT_VAR:
12590 if (!vars_get_by_desc(&arg->data.var, smp))
12591 return 0;
12592 if (!sample_casts[smp->data.type][SMP_T_STR])
12593 return 0;
12594 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12595 return 0;
12596 return 1;
12597 default:
12598 return 0;
12599 }
12600}
12601
12602static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12603 const char *file, int line, char **err)
12604{
12605 switch(args[0].data.sint) {
12606 case 128:
12607 case 192:
12608 case 256:
12609 break;
12610 default:
12611 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12612 return 0;
12613 }
12614 /* Try to decode a variable. */
12615 vars_check_arg(&args[1], NULL);
12616 vars_check_arg(&args[2], NULL);
12617 vars_check_arg(&args[3], NULL);
12618 return 1;
12619}
12620
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012621/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012622static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12623{
12624 struct sample nonce, key, aead_tag;
12625 struct buffer *smp_trash, *smp_trash_alloc;
12626 EVP_CIPHER_CTX *ctx;
12627 int dec_size, ret;
12628
12629 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12630 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12631 return 0;
12632
12633 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12634 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12635 return 0;
12636
12637 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12638 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12639 return 0;
12640
12641 smp_trash = get_trash_chunk();
12642 smp_trash_alloc = alloc_trash_chunk();
12643 if (!smp_trash_alloc)
12644 return 0;
12645
12646 ctx = EVP_CIPHER_CTX_new();
12647
12648 if (!ctx)
12649 goto err;
12650
12651 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12652 if (dec_size < 0)
12653 goto err;
12654 smp_trash->data = dec_size;
12655
12656 /* Set cipher type and mode */
12657 switch(arg_p[0].data.sint) {
12658 case 128:
12659 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12660 break;
12661 case 192:
12662 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12663 break;
12664 case 256:
12665 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12666 break;
12667 }
12668
12669 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12670
12671 /* Initialise IV */
12672 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12673 goto err;
12674
12675 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12676 if (dec_size < 0)
12677 goto err;
12678 smp_trash->data = dec_size;
12679
12680 /* Initialise key */
12681 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12682 goto err;
12683
12684 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12685 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12686 goto err;
12687
12688 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12689 if (dec_size < 0)
12690 goto err;
12691 smp_trash_alloc->data = dec_size;
12692 dec_size = smp_trash->data;
12693
12694 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12695 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12696
12697 if (ret <= 0)
12698 goto err;
12699
12700 smp->data.u.str.data = dec_size + smp_trash->data;
12701 smp->data.u.str.area = smp_trash->area;
12702 smp->data.type = SMP_T_BIN;
12703 smp->flags &= ~SMP_F_CONST;
12704 free_trash_chunk(smp_trash_alloc);
12705 return 1;
12706
12707err:
12708 free_trash_chunk(smp_trash_alloc);
12709 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012710}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012711# endif
William Lallemand32af2032016-10-29 18:09:35 +020012712
Elliot Otchet71f82972020-01-15 08:12:14 -050012713/* Argument validation functions */
12714
12715/* This function is used to validate the arguments passed to any "x_dn" ssl
12716 * keywords. These keywords support specifying a third parameter that must be
12717 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12718 */
12719int val_dnfmt(struct arg *arg, char **err_msg)
12720{
12721 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12722 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12723 return 0;
12724 }
12725 return 1;
12726}
12727
William Lallemand32af2032016-10-29 18:09:35 +020012728/* register cli keywords */
12729static struct cli_kw_list cli_kws = {{ },{
12730#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12731 { { "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 +020012732 { { "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 +020012733#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012734 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012735 { { "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 +010012736 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12737 { { "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 +010012738 { { "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 +020012739 { { "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 +010012740 { { "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 +010012741 { { "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 +020012742 { { "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 +020012743 { { "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 +020012744 { { NULL }, NULL, NULL, NULL }
12745}};
12746
Willy Tarreau0108d902018-11-25 19:14:37 +010012747INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012748
Willy Tarreau7875d092012-09-10 08:20:03 +020012749/* Note: must not be declared <const> as its list will be overwritten.
12750 * Please take care of keeping this list alphabetically sorted.
12751 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012752static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012753 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012754 { "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 +010012755#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012756 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012757#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012758 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012759#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12760 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12761#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012762 { "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 +020012763 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012764 { "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 +020012765 { "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 +020012766#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012767 { "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 -040012768#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012769#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012770 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12771 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012772 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12773#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012774 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12775 { "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 +010012776 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012777 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012778 { "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 +020012779 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12780 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12781 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12782 { "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 -050012783 { "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 +020012784 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12785 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012786 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012787 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12788 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012789 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012790 { "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 +020012791 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12792 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12793 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12794 { "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 -050012795 { "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 +020012796 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012797 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012798 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012799 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012800 { "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 +010012801 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012802 { "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 +020012803 { "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 +010012804 { "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 +020012805 { "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 +010012806#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012807 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012808#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012809#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012810 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012811#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012812 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012813#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012814 { "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 -040012815#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012816 { "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 +020012817#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012818 { "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 -040012819#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012820#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012821 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12822 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012823 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12824#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012825#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012826 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012827#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012828 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12829 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12830 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12831 { "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 +020012832 { NULL, NULL, 0, 0, 0 },
12833}};
12834
Willy Tarreau0108d902018-11-25 19:14:37 +010012835INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12836
Willy Tarreau7875d092012-09-10 08:20:03 +020012837/* Note: must not be declared <const> as its list will be overwritten.
12838 * Please take care of keeping this list alphabetically sorted.
12839 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012840static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012841 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12842 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012843 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012844}};
12845
Willy Tarreau0108d902018-11-25 19:14:37 +010012846INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12847
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012848/* Note: must not be declared <const> as its list will be overwritten.
12849 * Please take care of keeping this list alphabetically sorted, doing so helps
12850 * all code contributors.
12851 * Optional keywords are also declared with a NULL ->parse() function so that
12852 * the config parser can report an appropriate error when a known keyword was
12853 * not enabled.
12854 */
William Lallemand557823f2020-04-01 17:42:47 +020012855
12856/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12857 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012858static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012859 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012860 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012861 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12862 { "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 +010012863 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012864#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012865 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12866#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012867 { "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 +010012868 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012869 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012870 { "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 +010012871 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012872 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12873 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012874 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12875 { NULL, NULL, 0 },
12876};
12877
Willy Tarreau0108d902018-11-25 19:14:37 +010012878/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12879
Willy Tarreau51fb7652012-09-18 18:24:39 +020012880static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012881 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012882 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012883 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12884 { "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 +020012885 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12886 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12887 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12888 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012889#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012890 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12891#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012892 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012893 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012894 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012895 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12896 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12897 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12898 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12899 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12900 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12901 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012902 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012903 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012904 { "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 +020012905 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12906 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12907 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12908 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012909 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012910 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12911 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012912 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12913 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012914 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12915 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12916 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12917 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12918 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012919 { NULL, NULL, 0 },
12920}};
Emeric Brun46591952012-05-18 15:47:34 +020012921
Willy Tarreau0108d902018-11-25 19:14:37 +010012922INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12923
Willy Tarreau92faadf2012-10-10 23:04:25 +020012924/* Note: must not be declared <const> as its list will be overwritten.
12925 * Please take care of keeping this list alphabetically sorted, doing so helps
12926 * all code contributors.
12927 * Optional keywords are also declared with a NULL ->parse() function so that
12928 * the config parser can report an appropriate error when a known keyword was
12929 * not enabled.
12930 */
12931static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012932 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012933 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012934 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012935 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012936 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012937 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12938 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012939#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012940 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12941#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012942 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12943 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12944 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12945 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12946 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12947 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12948 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12949 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12950 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12951 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12952 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12953 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12954 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12955 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12956 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12957 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12958 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12959 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012960 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012961 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12962 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12963 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12964 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12965 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12966 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12967 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12968 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12969 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12970 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012971 { NULL, NULL, 0, 0 },
12972}};
12973
Willy Tarreau0108d902018-11-25 19:14:37 +010012974INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12975
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012976static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012977 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12978 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012979 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012980 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012981 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12982 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012983#ifndef OPENSSL_NO_DH
12984 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12985#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012986 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012987#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012988 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012989#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010012990 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12991#ifndef OPENSSL_NO_DH
12992 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12993#endif
12994 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12995 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12996 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12997 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012998 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012999 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
13000 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013001#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013002 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
13003 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
13004#endif
William Lallemand3af48e72020-02-03 17:15:52 +010013005 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013006 { 0, NULL, NULL },
13007}};
13008
Willy Tarreau0108d902018-11-25 19:14:37 +010013009INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
13010
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010013011/* Note: must not be declared <const> as its list will be overwritten */
13012static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020013013#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010013014 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
13015#endif
13016 { NULL, NULL, 0, 0, 0 },
13017}};
13018
13019INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
13020
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020013021/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010013022static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020013023 .snd_buf = ssl_sock_from_buf,
13024 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010013025 .subscribe = ssl_subscribe,
13026 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020013027 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020013028 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020013029 .rcv_pipe = NULL,
13030 .snd_pipe = NULL,
13031 .shutr = NULL,
13032 .shutw = ssl_sock_shutw,
13033 .close = ssl_sock_close,
13034 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010013035 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010013036 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010013037 .prepare_srv = ssl_sock_prepare_srv_ctx,
13038 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013039 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013040 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013041};
13042
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013043enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13044 struct session *sess, struct stream *s, int flags)
13045{
13046 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013047 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013048
13049 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013050 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013051
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013052 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013053 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013054 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013055 s->req.flags |= CF_READ_NULL;
13056 return ACT_RET_YIELD;
13057 }
13058 }
13059 return (ACT_RET_CONT);
13060}
13061
13062static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13063{
13064 rule->action_ptr = ssl_action_wait_for_hs;
13065
13066 return ACT_RET_PRS_OK;
13067}
13068
13069static struct action_kw_list http_req_actions = {ILH, {
13070 { "wait-for-handshake", ssl_parse_wait_for_hs },
13071 { /* END */ }
13072}};
13073
Willy Tarreau0108d902018-11-25 19:14:37 +010013074INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13075
Willy Tarreau5db847a2019-05-09 14:13:35 +020013076#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013077
13078static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13079{
13080 if (ptr) {
13081 chunk_destroy(ptr);
13082 free(ptr);
13083 }
13084}
13085
13086#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013087static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13088{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013089 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013090}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013091
Emeric Brun46591952012-05-18 15:47:34 +020013092__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013093static void __ssl_sock_init(void)
13094{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013095#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013096 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013097 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013098#endif
Emeric Brun46591952012-05-18 15:47:34 +020013099
Willy Tarreauef934602016-12-22 23:12:01 +010013100 if (global_ssl.listen_default_ciphers)
13101 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13102 if (global_ssl.connect_default_ciphers)
13103 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013104#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013105 if (global_ssl.listen_default_ciphersuites)
13106 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13107 if (global_ssl.connect_default_ciphersuites)
13108 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13109#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013110
Willy Tarreau13e14102016-12-22 20:25:26 +010013111 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013112#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013113 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013114#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013115#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013116 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013117 n = sk_SSL_COMP_num(cm);
13118 while (n--) {
13119 (void) sk_SSL_COMP_pop(cm);
13120 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013121#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013122
Willy Tarreau5db847a2019-05-09 14:13:35 +020013123#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013124 ssl_locking_init();
13125#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013126#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013127 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13128#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013129 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013130 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 +020013131#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013132 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013133 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013134#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013135#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13136 hap_register_post_check(tlskeys_finalize_config);
13137#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013138
13139 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13140 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13141
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013142 hap_register_post_deinit(ssl_free_global_issuers);
13143
Willy Tarreau80713382018-11-26 10:19:54 +010013144#ifndef OPENSSL_NO_DH
13145 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13146 hap_register_post_deinit(ssl_free_dh);
13147#endif
13148#ifndef OPENSSL_NO_ENGINE
13149 hap_register_post_deinit(ssl_free_engines);
13150#endif
13151 /* Load SSL string for the verbose & debug mode. */
13152 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013153 ha_meth = BIO_meth_new(0x666, "ha methods");
13154 BIO_meth_set_write(ha_meth, ha_ssl_write);
13155 BIO_meth_set_read(ha_meth, ha_ssl_read);
13156 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13157 BIO_meth_set_create(ha_meth, ha_ssl_new);
13158 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13159 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13160 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013161
13162 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013163}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013164
Willy Tarreau80713382018-11-26 10:19:54 +010013165/* Compute and register the version string */
13166static void ssl_register_build_options()
13167{
13168 char *ptr = NULL;
13169 int i;
13170
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013171 memprintf(&ptr, "Built with OpenSSL version : "
13172#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013173 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013174#else /* OPENSSL_IS_BORINGSSL */
13175 OPENSSL_VERSION_TEXT
13176 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013177 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013178 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013179#endif
13180 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013181#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013182 "no (library version too old)"
13183#elif defined(OPENSSL_NO_TLSEXT)
13184 "no (disabled via OPENSSL_NO_TLSEXT)"
13185#else
13186 "yes"
13187#endif
13188 "", ptr);
13189
13190 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13191#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13192 "yes"
13193#else
13194#ifdef OPENSSL_NO_TLSEXT
13195 "no (because of OPENSSL_NO_TLSEXT)"
13196#else
13197 "no (version might be too old, 0.9.8f min needed)"
13198#endif
13199#endif
13200 "", ptr);
13201
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013202 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13203 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13204 if (methodVersions[i].option)
13205 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013206
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013207 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013208}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013209
Willy Tarreau80713382018-11-26 10:19:54 +010013210INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013211
Emeric Brun46591952012-05-18 15:47:34 +020013212
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013213#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013214void ssl_free_engines(void) {
13215 struct ssl_engine_list *wl, *wlb;
13216 /* free up engine list */
13217 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13218 ENGINE_finish(wl->e);
13219 ENGINE_free(wl->e);
13220 LIST_DEL(&wl->list);
13221 free(wl);
13222 }
13223}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013224#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013225
Remi Gacogned3a23c32015-05-28 16:39:47 +020013226#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013227void ssl_free_dh(void) {
13228 if (local_dh_1024) {
13229 DH_free(local_dh_1024);
13230 local_dh_1024 = NULL;
13231 }
13232 if (local_dh_2048) {
13233 DH_free(local_dh_2048);
13234 local_dh_2048 = NULL;
13235 }
13236 if (local_dh_4096) {
13237 DH_free(local_dh_4096);
13238 local_dh_4096 = NULL;
13239 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013240 if (global_dh) {
13241 DH_free(global_dh);
13242 global_dh = NULL;
13243 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013244}
13245#endif
13246
13247__attribute__((destructor))
13248static void __ssl_sock_deinit(void)
13249{
13250#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013251 if (ssl_ctx_lru_tree) {
13252 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013253 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013254 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013255#endif
13256
Willy Tarreau5db847a2019-05-09 14:13:35 +020013257#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013258 ERR_remove_state(0);
13259 ERR_free_strings();
13260
13261 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013262#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013263
Willy Tarreau5db847a2019-05-09 14:13:35 +020013264#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013265 CRYPTO_cleanup_all_ex_data();
13266#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013267 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013268}
13269
13270
Emeric Brun46591952012-05-18 15:47:34 +020013271/*
13272 * Local variables:
13273 * c-indent-level: 8
13274 * c-basic-offset: 8
13275 * End:
13276 */