blob: 09c507d86fbb2748f97c5392f88ebe6d8784561d [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 Lallemandd9d5d1b2020-04-09 16:31:05 +02002901/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
2902/* The caller must use the lock of the bind_conf if used with inserted SNIs */
2903static void ckch_inst_free(struct ckch_inst *inst)
2904{
2905 struct sni_ctx *sni, *sni_s;
2906
2907 if (inst == NULL)
2908 return;
2909
2910 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
2911 SSL_CTX_free(sni->ctx);
2912 LIST_DEL(&sni->by_ckch_inst);
2913 ebmb_delete(&sni->name);
2914 free(sni);
2915 }
2916 LIST_DEL(&inst->by_ckchs);
2917 LIST_DEL(&inst->by_crtlist_entry);
2918 free(inst);
2919}
2920
William Lallemand9117de92019-10-04 00:29:42 +02002921/* Alloc and init a ckch_inst */
2922static struct ckch_inst *ckch_inst_new()
2923{
2924 struct ckch_inst *ckch_inst;
2925
2926 ckch_inst = calloc(1, sizeof *ckch_inst);
William Lallemand9cef2e22020-04-09 16:25:10 +02002927 if (!ckch_inst)
2928 return NULL;
2929
2930 LIST_INIT(&ckch_inst->sni_ctx);
2931 LIST_INIT(&ckch_inst->by_ckchs);
2932 LIST_INIT(&ckch_inst->by_crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02002933
2934 return ckch_inst;
2935}
2936
2937
2938/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002939static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002940 struct bind_conf *s, struct ssl_bind_conf *conf,
2941 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002942{
2943 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002944 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002945
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002946 if (*name == '!') {
2947 neg = 1;
2948 name++;
2949 }
2950 if (*name == '*') {
2951 wild = 1;
2952 name++;
2953 }
2954 /* !* filter is a nop */
2955 if (neg && wild)
2956 return order;
2957 if (*name) {
2958 int j, len;
2959 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002960 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002961 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002962 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002963 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002964 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002965
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002966 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002967 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002968 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002969 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02002970 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002971 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002972 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002973 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002974 sc->order = order++;
2975 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002976 sc->wild = wild;
2977 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01002978 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02002979 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002980 }
2981 return order;
2982}
2983
William Lallemand6af03992019-07-23 15:00:54 +02002984/*
William Lallemand1d29c742019-10-04 00:53:29 +02002985 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2986 * This function can't return an error.
2987 *
2988 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2989 */
2990static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2991{
2992
2993 struct sni_ctx *sc0, *sc0b, *sc1;
2994 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002995 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002996
2997 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2998
2999 /* ignore if sc0 was already inserted in a tree */
3000 if (sc0->name.node.leaf_p)
3001 continue;
3002
3003 /* Check for duplicates. */
3004 if (sc0->wild)
3005 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
3006 else
3007 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
3008
3009 for (; node; node = ebmb_next_dup(node)) {
3010 sc1 = ebmb_entry(node, struct sni_ctx, name);
3011 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
3012 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
3013 /* it's a duplicate, we should remove and free it */
3014 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02003015 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02003016 free(sc0);
3017 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02003018 break;
William Lallemand1d29c742019-10-04 00:53:29 +02003019 }
3020 }
3021
3022 /* if duplicate, ignore the insertion */
3023 if (!sc0)
3024 continue;
3025
3026 if (sc0->wild)
3027 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3028 else
3029 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003030
3031 /* replace the default_ctx if required with the first ctx */
3032 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02003033 SSL_CTX_free(bind_conf->default_ctx);
3034 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01003035 bind_conf->default_ctx = sc0->ctx;
3036 def = 1;
3037 }
William Lallemand1d29c742019-10-04 00:53:29 +02003038 }
3039}
3040
3041/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003042 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003043 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003044struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003045
William Lallemand2954c472020-03-06 21:54:13 +01003046/* tree of crtlist (crt-list/directory) */
3047static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003048
Emeric Brun7a883362019-10-17 13:27:40 +02003049/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003050 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003051 * DH parameter is loaded into the SSL_CTX and if there is no
3052 * DH parameter available in ckchs nor in global, the default
3053 * DH parameters are applied on the SSL_CTX.
3054 * Returns a bitfield containing the flags:
3055 * ERR_FATAL in any fatal error case
3056 * ERR_ALERT if a reason of the error is availabine in err
3057 * ERR_WARN if a warning is available into err
3058 * The value 0 means there is no error nor warning and
3059 * the operation succeed.
3060 */
William Lallemandfa892222019-07-23 16:06:08 +02003061#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003062static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3063 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003064{
Emeric Brun7a883362019-10-17 13:27:40 +02003065 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003066 DH *dh = NULL;
3067
William Lallemanda8c73742019-07-31 18:31:34 +02003068 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003069 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003070 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3071 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3072 err && *err ? *err : "", path);
3073#if defined(SSL_CTX_set_dh_auto)
3074 SSL_CTX_set_dh_auto(ctx, 1);
3075 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3076 err && *err ? *err : "");
3077#else
3078 memprintf(err, "%s, DH ciphers won't be available.\n",
3079 err && *err ? *err : "");
3080#endif
3081 ret |= ERR_WARN;
3082 goto end;
3083 }
William Lallemandfa892222019-07-23 16:06:08 +02003084
3085 if (ssl_dh_ptr_index >= 0) {
3086 /* store a pointer to the DH params to avoid complaining about
3087 ssl-default-dh-param not being set for this SSL_CTX */
3088 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3089 }
3090 }
3091 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003092 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3093 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3094 err && *err ? *err : "", path);
3095#if defined(SSL_CTX_set_dh_auto)
3096 SSL_CTX_set_dh_auto(ctx, 1);
3097 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3098 err && *err ? *err : "");
3099#else
3100 memprintf(err, "%s, DH ciphers won't be available.\n",
3101 err && *err ? *err : "");
3102#endif
3103 ret |= ERR_WARN;
3104 goto end;
3105 }
William Lallemandfa892222019-07-23 16:06:08 +02003106 }
3107 else {
3108 /* Clear openssl global errors stack */
3109 ERR_clear_error();
3110
3111 if (global_ssl.default_dh_param <= 1024) {
3112 /* we are limited to DH parameter of 1024 bits anyway */
3113 if (local_dh_1024 == NULL)
3114 local_dh_1024 = ssl_get_dh_1024();
3115
Emeric Brun7a883362019-10-17 13:27:40 +02003116 if (local_dh_1024 == NULL) {
3117 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3118 err && *err ? *err : "", path);
3119 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003120 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003121 }
William Lallemandfa892222019-07-23 16:06:08 +02003122
Emeric Bruna9363eb2019-10-17 14:53:03 +02003123 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3124 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3125 err && *err ? *err : "", path);
3126#if defined(SSL_CTX_set_dh_auto)
3127 SSL_CTX_set_dh_auto(ctx, 1);
3128 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3129 err && *err ? *err : "");
3130#else
3131 memprintf(err, "%s, DH ciphers won't be available.\n",
3132 err && *err ? *err : "");
3133#endif
3134 ret |= ERR_WARN;
3135 goto end;
3136 }
William Lallemandfa892222019-07-23 16:06:08 +02003137 }
3138 else {
3139 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3140 }
William Lallemandfa892222019-07-23 16:06:08 +02003141 }
3142
3143end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003144 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003145 return ret;
3146}
3147#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003148
yanbzhu488a4d22015-12-01 15:16:07 -05003149/* Frees the contents of a cert_key_and_chain
3150 */
3151static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3152{
yanbzhu488a4d22015-12-01 15:16:07 -05003153 if (!ckch)
3154 return;
3155
3156 /* Free the certificate and set pointer to NULL */
3157 if (ckch->cert)
3158 X509_free(ckch->cert);
3159 ckch->cert = NULL;
3160
3161 /* Free the key and set pointer to NULL */
3162 if (ckch->key)
3163 EVP_PKEY_free(ckch->key);
3164 ckch->key = NULL;
3165
3166 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003167 if (ckch->chain)
3168 sk_X509_pop_free(ckch->chain, X509_free);
3169 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003170
William Lallemand455af502019-10-17 18:04:45 +02003171 if (ckch->dh)
3172 DH_free(ckch->dh);
3173 ckch->dh = NULL;
3174
3175 if (ckch->sctl) {
3176 free(ckch->sctl->area);
3177 ckch->sctl->area = NULL;
3178 free(ckch->sctl);
3179 ckch->sctl = NULL;
3180 }
3181
3182 if (ckch->ocsp_response) {
3183 free(ckch->ocsp_response->area);
3184 ckch->ocsp_response->area = NULL;
3185 free(ckch->ocsp_response);
3186 ckch->ocsp_response = NULL;
3187 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003188
3189 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003190 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003191 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003192}
3193
William Lallemand8d0f8932019-10-17 18:03:58 +02003194/*
3195 *
3196 * This function copy a cert_key_and_chain in memory
3197 *
3198 * It's used to try to apply changes on a ckch before committing them, because
3199 * most of the time it's not possible to revert those changes
3200 *
3201 * Return a the dst or NULL
3202 */
3203static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3204 struct cert_key_and_chain *dst)
3205{
3206 if (src->cert) {
3207 dst->cert = src->cert;
3208 X509_up_ref(src->cert);
3209 }
3210
3211 if (src->key) {
3212 dst->key = src->key;
3213 EVP_PKEY_up_ref(src->key);
3214 }
3215
3216 if (src->chain) {
3217 dst->chain = X509_chain_up_ref(src->chain);
3218 }
3219
3220 if (src->dh) {
3221 DH_up_ref(src->dh);
3222 dst->dh = src->dh;
3223 }
3224
3225 if (src->sctl) {
3226 struct buffer *sctl;
3227
3228 sctl = calloc(1, sizeof(*sctl));
3229 if (!chunk_dup(sctl, src->sctl)) {
3230 free(sctl);
3231 sctl = NULL;
3232 goto error;
3233 }
3234 dst->sctl = sctl;
3235 }
3236
3237 if (src->ocsp_response) {
3238 struct buffer *ocsp_response;
3239
3240 ocsp_response = calloc(1, sizeof(*ocsp_response));
3241 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3242 free(ocsp_response);
3243 ocsp_response = NULL;
3244 goto error;
3245 }
3246 dst->ocsp_response = ocsp_response;
3247 }
3248
3249 if (src->ocsp_issuer) {
3250 X509_up_ref(src->ocsp_issuer);
3251 dst->ocsp_issuer = src->ocsp_issuer;
3252 }
3253
3254 return dst;
3255
3256error:
3257
3258 /* free everything */
3259 ssl_sock_free_cert_key_and_chain_contents(dst);
3260
3261 return NULL;
3262}
3263
3264
yanbzhu488a4d22015-12-01 15:16:07 -05003265/* checks if a key and cert exists in the ckch
3266 */
William Lallemand1633e392019-09-30 12:58:13 +02003267#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003268static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3269{
3270 return (ckch->cert != NULL && ckch->key != NULL);
3271}
William Lallemand1633e392019-09-30 12:58:13 +02003272#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003273
William Lallemandf9568fc2019-10-16 18:27:58 +02003274/*
3275 * return 0 on success or != 0 on failure
3276 */
3277static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3278{
3279 int ret = 1;
3280 BIO *in = NULL;
3281 X509 *issuer;
3282
3283 if (buf) {
3284 /* reading from a buffer */
3285 in = BIO_new_mem_buf(buf, -1);
3286 if (in == NULL) {
3287 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3288 goto end;
3289 }
3290
3291 } else {
3292 /* reading from a file */
3293 in = BIO_new(BIO_s_file());
3294 if (in == NULL)
3295 goto end;
3296
3297 if (BIO_read_filename(in, path) <= 0)
3298 goto end;
3299 }
3300
3301 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3302 if (!issuer) {
3303 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003304 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003305 goto end;
3306 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003307 /* no error, fill ckch with new context, old context must be free */
3308 if (ckch->ocsp_issuer)
3309 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003310 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003311 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003312
3313end:
3314
3315 ERR_clear_error();
3316 if (in)
3317 BIO_free(in);
3318
3319 return ret;
3320}
3321
William Lallemand96a9c972019-10-17 11:56:17 +02003322
3323/*
3324 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003325 * The PEM must contain at least a Certificate,
3326 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003327 *
William Lallemand96a9c972019-10-17 11:56:17 +02003328 * If it failed you should not attempt to use the ckch but free it.
3329 *
3330 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003331 */
William Lallemand96a9c972019-10-17 11:56:17 +02003332static 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 -05003333{
William Lallemandf11365b2019-09-19 14:25:58 +02003334 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003335 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003336 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003337 X509 *cert = NULL;
3338 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003339 DH *dh = NULL;
3340 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003341
3342 if (buf) {
3343 /* reading from a buffer */
3344 in = BIO_new_mem_buf(buf, -1);
3345 if (in == NULL) {
3346 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3347 goto end;
3348 }
yanbzhu488a4d22015-12-01 15:16:07 -05003349
William Lallemand96a9c972019-10-17 11:56:17 +02003350 } else {
3351 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003352 in = BIO_new(BIO_s_file());
William Lallemand7fd01b32020-04-07 14:16:32 +02003353 if (in == NULL) {
3354 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
William Lallemandf11365b2019-09-19 14:25:58 +02003355 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003356 }
yanbzhu488a4d22015-12-01 15:16:07 -05003357
William Lallemand7fd01b32020-04-07 14:16:32 +02003358 if (BIO_read_filename(in, path) <= 0) {
3359 memprintf(err, "%scannot open the file '%s'.\n",
3360 err && *err ? *err : "", path);
William Lallemandf11365b2019-09-19 14:25:58 +02003361 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003362 }
William Lallemandf11365b2019-09-19 14:25:58 +02003363 }
yanbzhu488a4d22015-12-01 15:16:07 -05003364
yanbzhu488a4d22015-12-01 15:16:07 -05003365 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003366 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003367 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003368
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003369#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003370 /* Seek back to beginning of file */
3371 if (BIO_reset(in) == -1) {
3372 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3373 err && *err ? *err : "", path);
3374 goto end;
3375 }
3376
William Lallemand96a9c972019-10-17 11:56:17 +02003377 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3378 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003379#endif
William Lallemandfa892222019-07-23 16:06:08 +02003380
Willy Tarreaubb137a82016-04-06 19:02:38 +02003381 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003382 if (BIO_reset(in) == -1) {
3383 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3384 err && *err ? *err : "", path);
3385 goto end;
3386 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003387
3388 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003389 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3390 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003391 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003392 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003393 goto end;
3394 }
3395
William Lallemand96a9c972019-10-17 11:56:17 +02003396 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003397 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3398 if (chain == NULL)
3399 chain = sk_X509_new_null();
3400 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003401 X509_free(ca);
3402 goto end;
3403 }
3404 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003405
yanbzhu488a4d22015-12-01 15:16:07 -05003406 ret = ERR_get_error();
3407 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3408 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003409 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003410 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003411 }
3412
William Lallemand75b15f72020-01-23 10:56:05 +01003413 /* once it loaded the PEM, it should remove everything else in the ckch */
3414 if (ckch->ocsp_response) {
3415 free(ckch->ocsp_response->area);
3416 ckch->ocsp_response->area = NULL;
3417 free(ckch->ocsp_response);
3418 ckch->ocsp_response = NULL;
3419 }
3420
3421 if (ckch->sctl) {
3422 free(ckch->sctl->area);
3423 ckch->sctl->area = NULL;
3424 free(ckch->sctl);
3425 ckch->sctl = NULL;
3426 }
3427
3428 if (ckch->ocsp_issuer) {
3429 X509_free(ckch->ocsp_issuer);
3430 ckch->ocsp_issuer = NULL;
3431 }
3432
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003433 /* no error, fill ckch with new context, old context will be free at end: */
3434 SWAP(ckch->key, key);
3435 SWAP(ckch->dh, dh);
3436 SWAP(ckch->cert, cert);
3437 SWAP(ckch->chain, chain);
3438
William Lallemand246c0242019-10-11 08:59:13 +02003439 ret = 0;
3440
William Lallemand96a9c972019-10-17 11:56:17 +02003441end:
William Lallemand246c0242019-10-11 08:59:13 +02003442
3443 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003444 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003445 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003446 if (key)
3447 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003448 if (dh)
3449 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003450 if (cert)
3451 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003452 if (chain)
3453 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003454
William Lallemand96a9c972019-10-17 11:56:17 +02003455 return ret;
3456}
3457
3458/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003459 * Try to load a private key file from a <path> or a buffer <buf>
3460 *
3461 * If it failed you should not attempt to use the ckch but free it.
3462 *
3463 * Return 0 on success or != 0 on failure
3464 */
3465static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3466{
3467 BIO *in = NULL;
3468 int ret = 1;
3469 EVP_PKEY *key = NULL;
3470
3471 if (buf) {
3472 /* reading from a buffer */
3473 in = BIO_new_mem_buf(buf, -1);
3474 if (in == NULL) {
3475 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3476 goto end;
3477 }
3478
3479 } else {
3480 /* reading from a file */
3481 in = BIO_new(BIO_s_file());
3482 if (in == NULL)
3483 goto end;
3484
3485 if (BIO_read_filename(in, path) <= 0)
3486 goto end;
3487 }
3488
3489 /* Read Private Key */
3490 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3491 if (key == NULL) {
3492 memprintf(err, "%sunable to load private key from file '%s'.\n",
3493 err && *err ? *err : "", path);
3494 goto end;
3495 }
3496
3497 ret = 0;
3498
3499 SWAP(ckch->key, key);
3500
3501end:
3502
3503 ERR_clear_error();
3504 if (in)
3505 BIO_free(in);
3506 if (key)
3507 EVP_PKEY_free(key);
3508
3509 return ret;
3510}
3511
3512/*
William Lallemand96a9c972019-10-17 11:56:17 +02003513 * Try to load in a ckch every files related to a ckch.
3514 * (PEM, sctl, ocsp, issuer etc.)
3515 *
3516 * This function is only used to load files during the configuration parsing,
3517 * it is not used with the CLI.
3518 *
3519 * This allows us to carry the contents of the file without having to read the
3520 * file multiple times. The caller must call
3521 * ssl_sock_free_cert_key_and_chain_contents.
3522 *
3523 * returns:
3524 * 0 on Success
3525 * 1 on SSL Failure
3526 */
3527static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3528{
3529 int ret = 1;
3530
3531 /* try to load the PEM */
3532 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3533 goto end;
3534 }
3535
William Lallemand4c5adbf2020-02-24 14:23:22 +01003536 /* try to load an external private key if it wasn't in the PEM */
3537 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3538 char fp[MAXPATHLEN+1];
3539 struct stat st;
3540
3541 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3542 if (stat(fp, &st) == 0) {
3543 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3544 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3545 err && *err ? *err : "", fp);
3546 goto end;
3547 }
3548 }
3549 }
3550
3551 if (ckch->key == NULL) {
3552 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3553 goto end;
3554 }
3555
3556 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3557 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3558 err && *err ? *err : "", path);
3559 goto end;
3560 }
3561
William Lallemanda17f4112019-10-10 15:16:44 +02003562#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3563 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003564 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003565 char fp[MAXPATHLEN+1];
3566 struct stat st;
3567
3568 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3569 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003570 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003571 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003572 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003573 ret = 1;
3574 goto end;
3575 }
3576 }
3577 }
3578#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003579
William Lallemand246c0242019-10-11 08:59:13 +02003580 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003581 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003582 char fp[MAXPATHLEN+1];
3583 struct stat st;
3584
3585 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3586 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003587 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003588 ret = 1;
3589 goto end;
3590 }
3591 }
3592 }
3593
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003594#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003595 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003596 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003597 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003598 struct stat st;
3599 char fp[MAXPATHLEN+1];
3600
3601 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3602 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003603 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003604 ret = 1;
3605 goto end;
3606 }
3607
3608 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003609 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003610 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003611 ret = 1;
3612 goto end;
3613 }
William Lallemand246c0242019-10-11 08:59:13 +02003614 }
3615 }
3616 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003617#endif
William Lallemand246c0242019-10-11 08:59:13 +02003618
yanbzhu488a4d22015-12-01 15:16:07 -05003619 ret = 0;
3620
3621end:
3622
3623 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003624
3625 /* Something went wrong in one of the reads */
3626 if (ret != 0)
3627 ssl_sock_free_cert_key_and_chain_contents(ckch);
3628
3629 return ret;
3630}
3631
3632/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003633 * Returns a bitfield containing the flags:
3634 * ERR_FATAL in any fatal error case
3635 * ERR_ALERT if the reason of the error is available in err
3636 * ERR_WARN if a warning is available into err
3637 * The value 0 means there is no error nor warning and
3638 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003639 */
3640static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3641{
Emeric Bruna96b5822019-10-17 13:25:14 +02003642 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003643 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003644
yanbzhu488a4d22015-12-01 15:16:07 -05003645 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3646 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3647 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003648 errcode |= ERR_ALERT | ERR_FATAL;
3649 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003650 }
3651
3652 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3653 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3654 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003655 errcode |= ERR_ALERT | ERR_FATAL;
3656 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003657 }
3658
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003659 if (ckch->chain) {
3660 find_chain = ckch->chain;
3661 } else {
3662 /* Find Certificate Chain in global */
3663 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003664 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003665 if (issuer)
3666 find_chain = issuer->chain;
3667 }
William Lallemand85888572020-02-27 14:48:35 +01003668
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003669 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003670 if (find_chain) {
3671 int i;
3672 X509 *ca;
3673 for (i = 0; i < sk_X509_num(find_chain); i++) {
3674 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003675 /* skip self issued (Root CA) */
3676 if (!X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca)))
3677 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003678 /*
3679 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3680 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3681 */
3682 X509_up_ref(ca);
3683 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3684 X509_free(ca);
3685 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3686 err && *err ? *err : "", path);
3687 errcode |= ERR_ALERT | ERR_FATAL;
3688 goto end;
3689 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003690 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003691 }
yanbzhu488a4d22015-12-01 15:16:07 -05003692
William Lallemandfa892222019-07-23 16:06:08 +02003693#ifndef OPENSSL_NO_DH
3694 /* store a NULL pointer to indicate we have not yet loaded
3695 a custom DH param file */
3696 if (ssl_dh_ptr_index >= 0) {
3697 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3698 }
3699
Emeric Brun7a883362019-10-17 13:27:40 +02003700 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3701 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003702 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3703 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003704 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003705 }
3706#endif
3707
William Lallemanda17f4112019-10-10 15:16:44 +02003708#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3709 if (sctl_ex_index >= 0 && ckch->sctl) {
3710 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3711 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003712 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003713 errcode |= ERR_ALERT | ERR_FATAL;
3714 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003715 }
3716 }
3717#endif
3718
William Lallemand4a660132019-10-14 14:51:41 +02003719#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003720 /* Load OCSP Info into context */
3721 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003722 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003723 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",
3724 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003725 errcode |= ERR_ALERT | ERR_FATAL;
3726 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003727 }
3728 }
William Lallemand246c0242019-10-11 08:59:13 +02003729#endif
3730
Emeric Bruna96b5822019-10-17 13:25:14 +02003731 end:
3732 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003733}
3734
William Lallemandc4ecddf2019-07-31 16:50:08 +02003735#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003736
William Lallemand28a8fce2019-10-04 17:36:55 +02003737static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003738{
3739 struct sni_keytype *s_kt = NULL;
3740 struct ebmb_node *node;
3741 int i;
3742
3743 for (i = 0; i < trash.size; i++) {
3744 if (!str[i])
3745 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003746 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003747 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003748 trash.area[i] = 0;
3749 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003750 if (!node) {
3751 /* CN not found in tree */
3752 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3753 /* Using memcpy here instead of strncpy.
3754 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3755 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3756 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003757 if (!s_kt)
3758 return -1;
3759
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003760 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003761 s_kt->keytypes = 0;
3762 ebst_insert(sni_keytypes, &s_kt->name);
3763 } else {
3764 /* CN found in tree */
3765 s_kt = container_of(node, struct sni_keytype, name);
3766 }
3767
3768 /* Mark that this CN has the keytype of key_index via keytypes mask */
3769 s_kt->keytypes |= 1<<key_index;
3770
William Lallemand28a8fce2019-10-04 17:36:55 +02003771 return 0;
3772
yanbzhu08ce6ab2015-12-02 13:01:29 -05003773}
3774
William Lallemandc4ecddf2019-07-31 16:50:08 +02003775#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003776/*
William Lallemandba1c33f2020-04-08 17:55:45 +02003777 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
William Lallemand8c1cdde2019-10-18 10:58:14 +02003778 */
William Lallemandba1c33f2020-04-08 17:55:45 +02003779static void ckch_store_free(struct ckch_store *store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003780{
William Lallemandba1c33f2020-04-08 17:55:45 +02003781 struct ckch_inst *inst, *inst_s;
3782
3783 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003784 return;
3785
William Lallemandba1c33f2020-04-08 17:55:45 +02003786#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L
3787 if (store->multi) {
William Lallemand8c1cdde2019-10-18 10:58:14 +02003788 int n;
3789
William Lallemandba1c33f2020-04-08 17:55:45 +02003790 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3791 ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003792 } else
3793#endif
3794 {
William Lallemandba1c33f2020-04-08 17:55:45 +02003795 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003796 }
3797
William Lallemandba1c33f2020-04-08 17:55:45 +02003798 free(store->ckch);
3799 store->ckch = NULL;
3800
3801 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
3802 ckch_inst_free(inst);
3803 }
3804 ebmb_delete(&store->node);
3805 free(store);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003806}
3807
William Lallemand8a874e42020-04-09 10:32:53 +02003808/*
3809 * create and initialize a ckch_store
3810 * <path> is the key name
3811 * <nmemb> is the number of store->ckch objects to allocate
3812 *
3813 * Return a ckch_store or NULL upon failure.
3814 */
3815static struct ckch_store *ckch_store_new(const char *filename, int nmemb)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003816{
William Lallemand8a874e42020-04-09 10:32:53 +02003817 struct ckch_store *store;
William Lallemand8c1cdde2019-10-18 10:58:14 +02003818 int pathlen;
3819
William Lallemand8a874e42020-04-09 10:32:53 +02003820 pathlen = strlen(filename);
3821 store = calloc(1, sizeof(*store) + pathlen + 1);
3822 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003823 return NULL;
William Lallemand8a874e42020-04-09 10:32:53 +02003824
3825 if (nmemb > 1)
3826 store->multi = 1;
3827 else
3828 store->multi = 0;
3829
3830 memcpy(store->path, filename, pathlen + 1);
3831
3832 LIST_INIT(&store->ckch_inst);
3833 LIST_INIT(&store->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003834
William Lallemand8a874e42020-04-09 10:32:53 +02003835 store->ckch = calloc(nmemb, sizeof(*store->ckch));
3836 if (!store->ckch)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003837 goto error;
3838
William Lallemand8a874e42020-04-09 10:32:53 +02003839 return store;
3840error:
3841 ckch_store_free(store);
3842 return NULL;
3843}
3844
3845/* allocate and duplicate a ckch_store
3846 * Return a new ckch_store or NULL */
3847static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3848{
3849 struct ckch_store *dst;
3850
3851 dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1);
3852
William Lallemand8c1cdde2019-10-18 10:58:14 +02003853#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3854 if (src->multi) {
3855 int n;
3856
3857 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3858 if (&src->ckch[n]) {
3859 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3860 goto error;
3861 }
3862 }
3863 } else
3864#endif
3865 {
3866 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3867 goto error;
3868 }
3869
3870 return dst;
3871
3872error:
William Lallemandba1c33f2020-04-08 17:55:45 +02003873 ckch_store_free(dst);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003874
3875 return NULL;
3876}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003877
William Lallemand36b84632019-07-18 19:28:17 +02003878/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003879 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003880 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003881static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003882{
3883 struct ebmb_node *eb;
3884
William Lallemande3af8fb2019-10-08 11:36:53 +02003885 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003886 if (!eb)
3887 return NULL;
3888
William Lallemande3af8fb2019-10-08 11:36:53 +02003889 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003890}
3891
3892/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003893 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003894 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003895static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003896{
William Lallemande3af8fb2019-10-08 11:36:53 +02003897 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003898
William Lallemand8a874e42020-04-09 10:32:53 +02003899 ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1);
William Lallemande3af8fb2019-10-08 11:36:53 +02003900 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003901 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3902 goto end;
3903 }
William Lallemand36b84632019-07-18 19:28:17 +02003904 if (!multi) {
3905
William Lallemand96a9c972019-10-17 11:56:17 +02003906 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003907 goto end;
3908
William Lallemande3af8fb2019-10-08 11:36:53 +02003909 /* insert into the ckchs tree */
3910 memcpy(ckchs->path, path, strlen(path) + 1);
3911 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003912 } else {
3913 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003914#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3915 char fp[MAXPATHLEN+1] = {0};
3916 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003917
3918 /* Load all possible certs and keys */
3919 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3920 struct stat buf;
3921 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3922 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003923 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003924 goto end;
3925 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003926 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003927 }
3928 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003929#endif
William Lallemand36b84632019-07-18 19:28:17 +02003930
3931 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003932 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003933 goto end;
3934 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003935 /* insert into the ckchs tree */
3936 memcpy(ckchs->path, path, strlen(path) + 1);
3937 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003938 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003939 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003940
William Lallemand36b84632019-07-18 19:28:17 +02003941end:
William Lallemandba1c33f2020-04-08 17:55:45 +02003942 ckch_store_free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003943
3944 return NULL;
3945}
3946
William Lallemandc4ecddf2019-07-31 16:50:08 +02003947#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3948
William Lallemand36b84632019-07-18 19:28:17 +02003949/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003950 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003951 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003952 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3953 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003954 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003955 *
Emeric Brun054563d2019-10-17 13:16:58 +02003956 * Returns a bitfield containing the flags:
3957 * ERR_FATAL in any fatal error case
3958 * ERR_ALERT if the reason of the error is available in err
3959 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003960 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003961 */
Emeric Brun054563d2019-10-17 13:16:58 +02003962static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3963 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3964 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003965{
William Lallemand36b84632019-07-18 19:28:17 +02003966 int i = 0, n = 0;
3967 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003968 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003969 struct ebmb_node *node;
3970 struct ebmb_node *next;
3971 /* Array of SSL_CTX pointers corresponding to each possible combo
3972 * of keytypes
3973 */
3974 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003975 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003976 X509_NAME *xname = NULL;
3977 char *str = NULL;
3978#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3979 STACK_OF(GENERAL_NAME) *names = NULL;
3980#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003981 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003982
Emeric Brun054563d2019-10-17 13:16:58 +02003983 *ckchi = NULL;
3984
William Lallemande3af8fb2019-10-08 11:36:53 +02003985 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003986 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3987 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003988 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003989 }
3990
3991 ckch_inst = ckch_inst_new();
3992 if (!ckch_inst) {
3993 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3994 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003995 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003996 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003997 }
3998
William Lallemande3af8fb2019-10-08 11:36:53 +02003999 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004000
yanbzhu08ce6ab2015-12-02 13:01:29 -05004001 /* Process each ckch and update keytypes for each CN/SAN
4002 * for example, if CN/SAN www.a.com is associated with
4003 * certs with keytype 0 and 2, then at the end of the loop,
4004 * www.a.com will have:
4005 * keyindex = 0 | 1 | 4 = 5
4006 */
4007 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004008 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004009
4010 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4011 continue;
4012
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004013 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004014 for (i = 0; i < fcount; i++) {
4015 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4016 if (ret < 0) {
4017 memprintf(err, "%sunable to allocate SSL context.\n",
4018 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004019 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004020 goto end;
4021 }
4022 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004023 } else {
4024 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4025 * so the line that contains logic is marked via comments
4026 */
4027 xname = X509_get_subject_name(certs_and_keys[n].cert);
4028 i = -1;
4029 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4030 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004031 ASN1_STRING *value;
4032 value = X509_NAME_ENTRY_get_data(entry);
4033 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004034 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004035 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004036
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004037 OPENSSL_free(str);
4038 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004039 if (ret < 0) {
4040 memprintf(err, "%sunable to allocate SSL context.\n",
4041 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004042 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004043 goto end;
4044 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004045 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004046 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004047
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004048 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004049#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004050 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4051 if (names) {
4052 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4053 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004054
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004055 if (name->type == GEN_DNS) {
4056 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4057 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004058 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004059
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004060 OPENSSL_free(str);
4061 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004062 if (ret < 0) {
4063 memprintf(err, "%sunable to allocate SSL context.\n",
4064 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004065 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004066 goto end;
4067 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004068 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004069 }
4070 }
4071 }
4072 }
4073#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4074 }
4075
4076 /* If no files found, return error */
4077 if (eb_is_empty(&sni_keytypes_map)) {
4078 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4079 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004080 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004081 goto end;
4082 }
4083
4084 /* We now have a map of CN/SAN to keytypes that are loaded in
4085 * Iterate through the map to create the SSL_CTX's (if needed)
4086 * and add each CTX to the SNI tree
4087 *
4088 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004089 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004090 * combination is denoted by the key in the map. Each key
4091 * has a value between 1 and 2^n - 1. Conveniently, the array
4092 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4093 * entry in the array to correspond to the unique combo (key)
4094 * associated with i. This unique key combo (i) will be associated
4095 * with combos[i-1]
4096 */
4097
4098 node = ebmb_first(&sni_keytypes_map);
4099 while (node) {
4100 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004101 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004102 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004103
4104 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4105 i = container_of(node, struct sni_keytype, name)->keytypes;
4106 cur_ctx = key_combos[i-1].ctx;
4107
4108 if (cur_ctx == NULL) {
4109 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004110 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004111 if (cur_ctx == NULL) {
4112 memprintf(err, "%sunable to allocate SSL context.\n",
4113 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004114 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004115 goto end;
4116 }
4117
yanbzhube2774d2015-12-10 15:07:30 -05004118 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004119 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4120 if (i & (1<<n)) {
4121 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004122 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004123 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4124 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004125 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004126 }
4127 }
4128
yanbzhu08ce6ab2015-12-02 13:01:29 -05004129 /* Update key_combos */
4130 key_combos[i-1].ctx = cur_ctx;
4131 }
4132
4133 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004134
William Lallemand1d29c742019-10-04 00:53:29 +02004135 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 +02004136 kinfo, str, key_combos[i-1].order);
4137 if (key_combos[i-1].order < 0) {
4138 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004139 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004140 goto end;
4141 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004142 node = ebmb_next(node);
4143 }
4144
4145
4146 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4147 if (!bind_conf->default_ctx) {
4148 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4149 if (key_combos[i].ctx) {
4150 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004151 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004152 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004153 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004154 break;
4155 }
4156 }
4157 }
4158
William Lallemand614ca0d2019-10-07 13:52:11 +02004159 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004160 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004161 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004162
yanbzhu08ce6ab2015-12-02 13:01:29 -05004163end:
4164
4165 if (names)
4166 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4167
yanbzhu08ce6ab2015-12-02 13:01:29 -05004168 node = ebmb_first(&sni_keytypes_map);
4169 while (node) {
4170 next = ebmb_next(node);
4171 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004172 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004173 node = next;
4174 }
4175
William Lallemand02e19a52020-04-08 16:11:26 +02004176 /* we need to free the ctx since we incremented the refcount where it's used */
4177 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4178 if (key_combos[i].ctx)
4179 SSL_CTX_free(key_combos[i].ctx);
4180 }
4181
Emeric Brun054563d2019-10-17 13:16:58 +02004182 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004183 if (ckch_inst->is_default) {
4184 SSL_CTX_free(bind_conf->default_ctx);
4185 bind_conf->default_ctx = NULL;
4186 }
4187
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004188 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004189 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004190 }
4191
Emeric Brun054563d2019-10-17 13:16:58 +02004192 *ckchi = ckch_inst;
4193 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004194}
4195#else
4196/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004197static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4198 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4199 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004200{
4201 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4202 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004203 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004204}
4205
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004206#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004207
William Lallemand614ca0d2019-10-07 13:52:11 +02004208/*
4209 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004210 *
4211 * Returns a bitfield containing the flags:
4212 * ERR_FATAL in any fatal error case
4213 * ERR_ALERT if the reason of the error is available in err
4214 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004215 */
Emeric Brun054563d2019-10-17 13:16:58 +02004216static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4217 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004218{
William Lallemandc9402072019-05-15 15:33:54 +02004219 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004220 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004221 int order = 0;
4222 X509_NAME *xname;
4223 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004224 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004225 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004226#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4227 STACK_OF(GENERAL_NAME) *names;
4228#endif
William Lallemand36b84632019-07-18 19:28:17 +02004229 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004230 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004231 int errcode = 0;
4232
4233 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004234
William Lallemande3af8fb2019-10-08 11:36:53 +02004235 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004236 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004237
William Lallemande3af8fb2019-10-08 11:36:53 +02004238 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004239
William Lallemandc9402072019-05-15 15:33:54 +02004240 ctx = SSL_CTX_new(SSLv23_server_method());
4241 if (!ctx) {
4242 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4243 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004244 errcode |= ERR_ALERT | ERR_FATAL;
4245 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004246 }
4247
Emeric Bruna96b5822019-10-17 13:25:14 +02004248 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4249 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004250 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004251
4252 ckch_inst = ckch_inst_new();
4253 if (!ckch_inst) {
4254 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4255 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004256 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004257 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004258 }
4259
William Lallemand36b84632019-07-18 19:28:17 +02004260 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004261 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004262 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004263 switch(EVP_PKEY_base_id(pkey)) {
4264 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004265 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004266 break;
4267 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004268 kinfo.sig = TLSEXT_signature_ecdsa;
4269 break;
4270 case EVP_PKEY_DSA:
4271 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004272 break;
4273 }
4274 EVP_PKEY_free(pkey);
4275 }
4276
Emeric Brun50bcecc2013-04-22 13:05:23 +02004277 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004278 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004279 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 +02004280 if (order < 0) {
4281 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004282 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004283 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004284 }
4285 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004286 }
4287 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004288#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004289 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004290 if (names) {
4291 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4292 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4293 if (name->type == GEN_DNS) {
4294 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 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 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004305 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004306 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004307#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004308 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004309 i = -1;
4310 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4311 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004312 ASN1_STRING *value;
4313
4314 value = X509_NAME_ENTRY_get_data(entry);
4315 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004316 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004317 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004318 if (order < 0) {
4319 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004320 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004321 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004322 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004323 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004324 }
4325 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004326 /* we must not free the SSL_CTX anymore below, since it's already in
4327 * the tree, so it will be discovered and cleaned in time.
4328 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004329
Emeric Brunfc0421f2012-09-07 17:30:07 +02004330#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004331 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004332 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4333 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004334 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004335 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004336 }
4337#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004338 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004339 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004340 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004341 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004342 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004343 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004344
William Lallemand9117de92019-10-04 00:29:42 +02004345 /* everything succeed, the ckch instance can be used */
4346 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004347 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004348 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004349
William Lallemand02e19a52020-04-08 16:11:26 +02004350 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4351
Emeric Brun054563d2019-10-17 13:16:58 +02004352 *ckchi = ckch_inst;
4353 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004354
4355error:
4356 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004357 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004358 if (ckch_inst->is_default)
4359 SSL_CTX_free(ctx);
4360
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004361 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004362 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004363 }
William Lallemandd9199372019-10-04 15:37:05 +02004364 SSL_CTX_free(ctx);
4365
Emeric Brun054563d2019-10-17 13:16:58 +02004366 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004367}
4368
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004369/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004370static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4371 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004372 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004373{
Emeric Brun054563d2019-10-17 13:16:58 +02004374 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004375
4376 /* we found the ckchs in the tree, we can use it directly */
4377 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004378 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 +02004379 else
William Lallemand24bde432020-03-09 16:48:43 +01004380 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 +02004381
Emeric Brun054563d2019-10-17 13:16:58 +02004382 if (errcode & ERR_CODE)
4383 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004384
William Lallemand24bde432020-03-09 16:48:43 +01004385 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004386
4387 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004388 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004389 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004390}
4391
William Lallemand6be66ec2020-03-06 22:26:32 +01004392
William Lallemand4c68bba2020-03-30 18:45:10 +02004393
4394
4395/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4396 * done once. Zero is returned if the operation fails. No error is returned
4397 * if the random is said as not implemented, because we expect that openssl
4398 * will use another method once needed.
4399 */
4400static int ssl_initialize_random()
4401{
4402 unsigned char random;
4403 static int random_initialized = 0;
4404
4405 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4406 random_initialized = 1;
4407
4408 return random_initialized;
4409}
4410
4411/* release ssl bind conf */
4412void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
4413{
4414 if (conf) {
4415#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4416 free(conf->npn_str);
4417 conf->npn_str = NULL;
4418#endif
4419#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4420 free(conf->alpn_str);
4421 conf->alpn_str = NULL;
4422#endif
4423 free(conf->ca_file);
4424 conf->ca_file = NULL;
4425 free(conf->ca_verify_file);
4426 conf->ca_verify_file = NULL;
4427 free(conf->crl_file);
4428 conf->crl_file = NULL;
4429 free(conf->ciphers);
4430 conf->ciphers = NULL;
4431#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
4432 free(conf->ciphersuites);
4433 conf->ciphersuites = NULL;
4434#endif
4435 free(conf->curves);
4436 conf->curves = NULL;
4437 free(conf->ecdhe);
4438 conf->ecdhe = NULL;
4439 }
4440}
4441/* free sni filters */
4442static void crtlist_free_filters(char **args)
4443{
4444 int i;
4445
4446 if (!args)
4447 return;
4448
4449 for (i = 0; args[i]; i++)
4450 free(args[i]);
4451
4452 free(args);
4453}
4454
4455/* Alloc and duplicate a char ** array */
4456static char **crtlist_dup_filters(char **args, int fcount)
4457{
4458 char **dst;
4459 int i;
4460
4461 if (fcount == 0)
4462 return NULL;
4463
4464 dst = calloc(fcount + 1, sizeof(*dst));
4465 if (!dst)
4466 return NULL;
4467
4468 for (i = 0; i < fcount; i++) {
4469 dst[i] = strdup(args[i]);
4470 if (!dst[i])
4471 goto error;
4472 }
4473 return dst;
4474
4475error:
4476 crtlist_free_filters(dst);
4477 return NULL;
4478}
4479
4480
4481/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
4482static void crtlist_free(struct crtlist *crtlist)
4483{
4484 struct crtlist_entry *entry, *s_entry;
4485
4486 if (crtlist == NULL)
4487 return;
4488
4489 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
4490 ebpt_delete(&entry->node);
4491 LIST_DEL(&entry->by_crtlist);
4492 crtlist_free_filters(entry->filters);
4493 ssl_sock_free_ssl_conf(entry->ssl_conf);
4494 free(entry->ssl_conf);
4495 free(entry);
4496 }
4497 ebmb_delete(&crtlist->node);
4498 free(crtlist);
4499}
4500
William Lallemand6be66ec2020-03-06 22:26:32 +01004501/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4502 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4503 *
4504 * This function tries to open and store certificate files.
4505 */
4506static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004507{
William Lallemand6be66ec2020-03-06 22:26:32 +01004508 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004509 struct dirent **de_list;
4510 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004511 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004512 char *end;
4513 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004514 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004515 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004516#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004517 int is_bundle;
4518 int j;
4519#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004520
William Lallemand6be66ec2020-03-06 22:26:32 +01004521 dir = malloc(sizeof(*dir) + strlen(path) + 1);
4522 if (dir == NULL) {
4523 memprintf(err, "not enough memory");
4524 return ERR_ALERT | ERR_FATAL;
4525 }
4526 memcpy(dir->node.key, path, strlen(path) + 1);
4527 dir->entries = EB_ROOT_UNIQUE; /* it's a directory, files are unique */
William Lallemand79d31ec2020-03-25 15:10:49 +01004528 dir->bind_conf = NULL;
William Lallemandc69f02d2020-04-06 19:07:03 +02004529 dir->linecount = 0;
William Lallemand83918e22020-03-16 17:21:51 +01004530 LIST_INIT(&dir->ord_entries);
William Lallemand6be66ec2020-03-06 22:26:32 +01004531
William Lallemand2d232c22020-03-06 22:12:35 +01004532 n = scandir(path, &de_list, 0, alphasort);
4533 if (n < 0) {
4534 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4535 err && *err ? *err : "", path, strerror(errno));
4536 cfgerr |= ERR_ALERT | ERR_FATAL;
4537 }
4538 else {
4539 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004540 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004541 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004542
William Lallemand2d232c22020-03-06 22:12:35 +01004543 end = strrchr(de->d_name, '.');
4544 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4545 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004546
William Lallemanda64593c2020-03-17 20:11:41 +01004547 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4548 if (stat(fp, &buf) != 0) {
4549 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4550 err && *err ? *err : "", fp, strerror(errno));
4551 cfgerr |= ERR_ALERT | ERR_FATAL;
4552 goto ignore_entry;
4553 }
4554 if (!S_ISREG(buf.st_mode))
4555 goto ignore_entry;
4556
William Lallemand6be66ec2020-03-06 22:26:32 +01004557 entry = malloc(sizeof(*entry));
4558 if (entry == NULL) {
4559 memprintf(err, "not enough memory '%s'", fp);
4560 cfgerr |= ERR_ALERT | ERR_FATAL;
4561 goto ignore_entry;
4562 }
4563
William Lallemand83918e22020-03-16 17:21:51 +01004564 /* directories don't use ssl_conf and filters */
William Lallemandc69f02d2020-04-06 19:07:03 +02004565 entry->linenum = 0;
William Lallemand83918e22020-03-16 17:21:51 +01004566 entry->fcount = 0;
4567 entry->filters = NULL;
4568 entry->ssl_conf = NULL;
William Lallemandc2e3b722020-03-31 14:31:41 +02004569 LIST_INIT(&entry->ckch_inst);
William Lallemand83918e22020-03-16 17:21:51 +01004570
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004571#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004572 is_bundle = 0;
4573 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004574
William Lallemand2d232c22020-03-06 22:12:35 +01004575 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4576 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4577 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4578 is_bundle = 1;
4579 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004580 }
William Lallemand2d232c22020-03-06 22:12:35 +01004581 }
yanbzhu63ea8462015-12-09 13:35:14 -05004582
William Lallemand2d232c22020-03-06 22:12:35 +01004583 if (is_bundle) {
4584 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004585
William Lallemand2d232c22020-03-06 22:12:35 +01004586 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004587
William Lallemand2d232c22020-03-06 22:12:35 +01004588 /* increment i and free de until we get to a non-bundle cert
4589 * Note here that we look at de_list[i + 1] before freeing de
4590 * this is important since ignore_entry will free de. This also
4591 * guarantees that de->d_name continues to hold the same prefix.
4592 */
4593 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4594 free(de);
4595 i++;
4596 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004597 }
William Lallemand2d232c22020-03-06 22:12:35 +01004598
4599 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004600 ckchs = ckchs_lookup(fp);
4601 if (ckchs == NULL)
4602 ckchs = ckchs_load_cert_file(fp, 1, err);
4603 if (ckchs == NULL) {
4604 free(de);
4605 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004606 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004607 goto end;
4608 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004609 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004610 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004611 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004612 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004613 ebpt_insert(&dir->entries, &entry->node);
4614
William Lallemand2d232c22020-03-06 22:12:35 +01004615 /* Successfully processed the bundle */
4616 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004617 }
William Lallemand2d232c22020-03-06 22:12:35 +01004618 }
yanbzhu63ea8462015-12-09 13:35:14 -05004619
4620#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004621 ckchs = ckchs_lookup(fp);
4622 if (ckchs == NULL)
4623 ckchs = ckchs_load_cert_file(fp, 0, err);
4624 if (ckchs == NULL) {
4625 free(de);
4626 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004627 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004628 goto end;
4629 }
4630 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004631 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004632 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004633 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004634 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004635
yanbzhu08ce6ab2015-12-02 13:01:29 -05004636ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004637 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004638 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004639end:
William Lallemand2d232c22020-03-06 22:12:35 +01004640 free(de_list);
4641 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004642
4643 if (cfgerr & ERR_CODE) {
4644 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004645 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004646 } else {
4647 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004648 }
William Lallemand2d232c22020-03-06 22:12:35 +01004649 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004650
William Lallemand2d232c22020-03-06 22:12:35 +01004651}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004652
William Lallemand97b08102020-04-01 10:14:00 +02004653/*
4654 * Read a single crt-list line. /!\ alter the <line> string.
4655 * Fill <crt_path> and <crtlist_entry>
4656 * <crtlist_entry> must be alloc and free by the caller
4657 * <crtlist_entry->ssl_conf> is alloc by the function
4658 * <crtlist_entry->filters> is alloc by the function
4659 * <crt_path> is a ptr in <line>
4660 * Return an error code
4661 */
4662static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4663{
4664 int cfgerr = 0;
4665 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4666 char *end;
4667 char *args[MAX_CRT_ARGS + 1];
4668 struct ssl_bind_conf *ssl_conf = NULL;
4669
4670 if (!line || !crt_path || !entry)
4671 return ERR_ALERT | ERR_FATAL;
4672
4673 end = line + strlen(line);
4674 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4675 /* Check if we reached the limit and the last char is not \n.
4676 * Watch out for the last line without the terminating '\n'!
4677 */
4678 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4679 linenum, file, CRT_LINESIZE-1);
4680 cfgerr |= ERR_ALERT | ERR_FATAL;
4681 goto error;
4682 }
4683 arg = 0;
4684 newarg = 1;
4685 while (*line) {
4686 if (isspace((unsigned char)*line)) {
4687 newarg = 1;
4688 *line = 0;
4689 } else if (*line == '[') {
4690 if (ssl_b) {
4691 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4692 cfgerr |= ERR_ALERT | ERR_FATAL;
4693 goto error;
4694 }
4695 if (!arg) {
4696 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4697 cfgerr |= ERR_ALERT | ERR_FATAL;
4698 goto error;
4699 }
4700 ssl_b = arg;
4701 newarg = 1;
4702 *line = 0;
4703 } else if (*line == ']') {
4704 if (ssl_e) {
4705 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4706 cfgerr |= ERR_ALERT | ERR_FATAL;
4707 goto error;
4708 }
4709 if (!ssl_b) {
4710 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4711 cfgerr |= ERR_ALERT | ERR_FATAL;
4712 goto error;
4713 }
4714 ssl_e = arg;
4715 newarg = 1;
4716 *line = 0;
4717 } else if (newarg) {
4718 if (arg == MAX_CRT_ARGS) {
4719 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4720 cfgerr |= ERR_ALERT | ERR_FATAL;
4721 goto error;
4722 }
4723 newarg = 0;
4724 args[arg++] = line;
4725 }
4726 line++;
4727 }
4728 args[arg++] = line;
4729
4730 /* empty line */
4731 if (!*args[0]) {
4732 cfgerr |= ERR_NONE;
4733 goto error;
4734 }
4735
4736 *crt_path = args[0];
4737
4738 ssl_conf = calloc(1, sizeof *ssl_conf);
4739 if (!ssl_conf) {
4740 memprintf(err, "not enough memory!");
4741 cfgerr |= ERR_ALERT | ERR_FATAL;
4742 goto error;
4743 }
4744 cur_arg = ssl_b ? ssl_b : 1;
4745 while (cur_arg < ssl_e) {
4746 newarg = 0;
4747 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4748 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4749 newarg = 1;
4750 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4751 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4752 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4753 args[cur_arg], linenum, file);
4754 cfgerr |= ERR_ALERT | ERR_FATAL;
4755 goto error;
4756 }
4757 cur_arg += 1 + ssl_bind_kws[i].skip;
4758 break;
4759 }
4760 }
4761 if (!cfgerr && !newarg) {
4762 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4763 args[cur_arg], linenum, file);
4764 cfgerr |= ERR_ALERT | ERR_FATAL;
4765 goto error;
4766 }
4767 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004768 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004769 entry->ssl_conf = ssl_conf;
4770 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4771 entry->fcount = arg - cur_arg - 1;
4772
4773 return cfgerr;
4774
4775error:
4776 crtlist_free_filters(entry->filters);
4777 entry->filters = NULL;
4778 ssl_sock_free_ssl_conf(entry->ssl_conf);
4779 free(entry->ssl_conf);
4780 entry->ssl_conf = NULL;
4781 return cfgerr;
4782}
4783
William Lallemand2954c472020-03-06 21:54:13 +01004784/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4785 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4786 *
4787 * This function tries to open and store certificate files.
4788 */
4789static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4790{
4791 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004792 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004793 char thisline[CRT_LINESIZE];
4794 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004795 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004796 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004797 int linenum = 0;
4798 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004799
Willy Tarreauad1731d2013-04-02 17:35:58 +02004800 if ((f = fopen(file, "r")) == NULL) {
4801 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004802 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004803 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004804
William Lallemand2954c472020-03-06 21:54:13 +01004805 newlist = malloc(sizeof(*newlist) + strlen(file) + 1);
4806 if (newlist == NULL) {
4807 memprintf(err, "Not enough memory!");
4808 cfgerr |= ERR_ALERT | ERR_FATAL;
4809 goto error;
4810 }
4811 memcpy(newlist->node.key, file, strlen(file) + 1);
4812 newlist->entries = EB_ROOT;
William Lallemand79d31ec2020-03-25 15:10:49 +01004813 newlist->bind_conf = NULL;
William Lallemand97b08102020-04-01 10:14:00 +02004814 newlist->node.node.leaf_p = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004815 LIST_INIT(&newlist->ord_entries);
4816
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004817 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004818 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004819 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004820 char *crt_path;
4821 struct ssl_bind_conf *ssl_conf = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004822 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004823
4824 linenum++;
4825 end = line + strlen(line);
4826 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4827 /* Check if we reached the limit and the last char is not \n.
4828 * Watch out for the last line without the terminating '\n'!
4829 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004830 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4831 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004832 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004833 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004834 }
William Lallemand97b08102020-04-01 10:14:00 +02004835
4836 if (*line == '#' || *line == '\n' || *line == '\r')
4837 continue;
4838
4839 entry = malloc(sizeof(*entry));
4840 if (entry == NULL) {
4841 memprintf(err, "Not enough memory!");
4842 cfgerr |= ERR_ALERT | ERR_FATAL;
4843 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004844 }
William Lallemand97b08102020-04-01 10:14:00 +02004845 entry->fcount = 0;
4846 entry->filters = NULL;
4847 entry->ssl_conf = NULL;
4848 entry->node.key = NULL;
4849 LIST_INIT(&entry->by_crtlist);
4850
4851 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4852 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004853 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004854 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004855
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004856 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004857 if (!crt_path || !*crt_path) {
4858 free(entry);
4859 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004860 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004861 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004862
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004863 if (*crt_path != '/' && global_ssl.crt_base) {
4864 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4865 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4866 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004867 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004868 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004869 }
4870 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4871 crt_path = path;
4872 }
4873
William Lallemand2954c472020-03-06 21:54:13 +01004874 /* Look for a ckch_store or create one */
4875 ckchs = ckchs_lookup(crt_path);
4876 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004877 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004878 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004879 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004880 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004881 }
William Lallemand909086e2020-03-17 16:53:27 +01004882 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004883 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004884
William Lallemand97b08102020-04-01 10:14:00 +02004885 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004886 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004887
William Lallemand2954c472020-03-06 21:54:13 +01004888 entry->node.key = ckchs;
4889 entry->ssl_conf = ssl_conf;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004890 entry->crtlist = newlist;
William Lallemandc2e3b722020-03-31 14:31:41 +02004891 LIST_INIT(&entry->ckch_inst);
William Lallemand2954c472020-03-06 21:54:13 +01004892 ebpt_insert(&newlist->entries, &entry->node);
4893 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004894 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004895
4896 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004897 }
William Lallemand2954c472020-03-06 21:54:13 +01004898 if (cfgerr & ERR_CODE)
4899 goto error;
4900
William Lallemandc69f02d2020-04-06 19:07:03 +02004901 newlist->linecount = linenum;
4902
William Lallemand2954c472020-03-06 21:54:13 +01004903 fclose(f);
4904 *crtlist = newlist;
4905
4906 return cfgerr;
4907error:
William Lallemand97b08102020-04-01 10:14:00 +02004908 if (entry) {
4909 crtlist_free_filters(entry->filters);
4910 entry->filters = NULL;
4911 ssl_sock_free_ssl_conf(entry->ssl_conf);
4912 free(entry->ssl_conf);
4913 entry->ssl_conf = NULL;
4914 free(entry);
4915 }
4916
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004917 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004918 crtlist_free(newlist);
4919 return cfgerr;
4920}
4921
4922/* Load a crt-list file, this is done in 2 parts:
4923 * - store the content of the file in a crtlist structure with crtlist_entry structures
4924 * - generate the instances by iterating on entries in the crtlist struct
4925 *
4926 * Nothing is locked there, this function is used in the configuration parser.
4927 *
4928 * Returns a set of ERR_* flags possibly with an error in <err>.
4929 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004930int 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 +01004931{
4932 struct crtlist *crtlist = NULL;
4933 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004934 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004935 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004936 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004937 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004938
William Lallemand79d31ec2020-03-25 15:10:49 +01004939 bind_conf_node = malloc(sizeof(*bind_conf_node));
4940 if (!bind_conf_node) {
4941 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4942 cfgerr |= ERR_FATAL | ERR_ALERT;
4943 goto error;
4944 }
4945 bind_conf_node->next = NULL;
4946 bind_conf_node->bind_conf = bind_conf;
4947
William Lallemand41ca9302020-04-08 13:15:18 +02004948 /* strip trailing slashes, including first one */
4949 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4950 *end = 0;
4951
William Lallemand2954c472020-03-06 21:54:13 +01004952 /* look for an existing crtlist or create one */
4953 eb = ebst_lookup(&crtlists_tree, file);
4954 if (eb) {
4955 crtlist = ebmb_entry(eb, struct crtlist, node);
4956 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004957 /* load a crt-list OR a directory */
4958 if (dir)
4959 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4960 else
4961 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4962
William Lallemand2954c472020-03-06 21:54:13 +01004963 if (!(cfgerr & ERR_CODE))
4964 ebst_insert(&crtlists_tree, &crtlist->node);
4965 }
4966
4967 if (cfgerr & ERR_CODE) {
4968 cfgerr |= ERR_FATAL | ERR_ALERT;
4969 goto error;
4970 }
4971
4972 /* generates ckch instance from the crtlist_entry */
4973 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4974 struct ckch_store *store;
4975 struct ckch_inst *ckch_inst = NULL;
4976
4977 store = entry->node.key;
4978 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
4979 if (cfgerr & ERR_CODE) {
4980 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
4981 goto error;
4982 }
William Lallemand49398312020-03-30 17:01:33 +02004983 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02004984 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01004985 }
William Lallemand2954c472020-03-06 21:54:13 +01004986
William Lallemand79d31ec2020-03-25 15:10:49 +01004987 /* add the bind_conf to the list */
4988 bind_conf_node->next = crtlist->bind_conf;
4989 crtlist->bind_conf = bind_conf_node;
4990
William Lallemand2954c472020-03-06 21:54:13 +01004991 return cfgerr;
4992error:
4993 {
William Lallemand49398312020-03-30 17:01:33 +02004994 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01004995 struct ckch_inst *inst, *s_inst;
4996
William Lallemand49398312020-03-30 17:01:33 +02004997 lastentry = entry; /* which entry we tried to generate last */
4998 if (lastentry) {
4999 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5000 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
5001 break;
5002
5003 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01005004
William Lallemand49398312020-03-30 17:01:33 +02005005 /* this was not generated for this bind_conf, skip */
5006 if (inst->bind_conf != bind_conf)
5007 continue;
5008
William Lallemandd9d5d1b2020-04-09 16:31:05 +02005009 /* free the sni_ctx and instance */
5010 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02005011 }
William Lallemand2954c472020-03-06 21:54:13 +01005012 }
William Lallemand2954c472020-03-06 21:54:13 +01005013 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005014 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005015 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005016 return cfgerr;
5017}
5018
William Lallemand06b22a82020-03-16 14:45:55 +01005019/* Returns a set of ERR_* flags possibly with an error in <err>. */
5020int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5021{
5022 struct stat buf;
5023 char fp[MAXPATHLEN+1];
5024 int cfgerr = 0;
5025 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005026 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005027
5028 if ((ckchs = ckchs_lookup(path))) {
5029 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005030 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005031 }
5032 if (stat(path, &buf) == 0) {
5033 if (S_ISDIR(buf.st_mode) == 0) {
5034 ckchs = ckchs_load_cert_file(path, 0, err);
5035 if (!ckchs)
5036 return ERR_ALERT | ERR_FATAL;
5037
William Lallemand24bde432020-03-09 16:48:43 +01005038 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005039 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005040 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005041 }
5042 } else {
5043 /* stat failed, could be a bundle */
5044 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5045 /* try to load a bundle if it is permitted */
5046 ckchs = ckchs_load_cert_file(path, 1, err);
5047 if (!ckchs)
5048 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005049 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005050 } else {
5051 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5052 err && *err ? *err : "", fp, strerror(errno));
5053 cfgerr |= ERR_ALERT | ERR_FATAL;
5054 }
5055 }
5056
5057 return cfgerr;
5058}
5059
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005060/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005061static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005062ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005063{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005064 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005065 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005066 SSL_OP_ALL | /* all known workarounds for bugs */
5067 SSL_OP_NO_SSLv2 |
5068 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005069 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005070 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005071 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005072 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005073 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005074 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005075 SSL_MODE_ENABLE_PARTIAL_WRITE |
5076 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005077 SSL_MODE_RELEASE_BUFFERS |
5078 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005079 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005080 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005081 int flags = MC_SSL_O_ALL;
5082 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005083
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005084 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005085 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005086
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005087 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005088 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5089 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5090 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005091 else
5092 flags = conf_ssl_methods->flags;
5093
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005094 min = conf_ssl_methods->min;
5095 max = conf_ssl_methods->max;
5096 /* start with TLSv10 to remove SSLv3 per default */
5097 if (!min && (!max || max >= CONF_TLSV10))
5098 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005099 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005100 if (min)
5101 flags |= (methodVersions[min].flag - 1);
5102 if (max)
5103 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005104 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005105 min = max = CONF_TLSV_NONE;
5106 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005107 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005108 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005109 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005110 if (min) {
5111 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005112 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5113 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5114 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5115 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005116 hole = 0;
5117 }
5118 max = i;
5119 }
5120 else {
5121 min = max = i;
5122 }
5123 }
5124 else {
5125 if (min)
5126 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005127 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005128 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005129 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5130 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005131 cfgerr += 1;
5132 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005133 /* save real min/max in bind_conf */
5134 conf_ssl_methods->min = min;
5135 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005136
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005137#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005138 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005139 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005140 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005141 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005142 else
5143 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5144 if (flags & methodVersions[i].flag)
5145 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005146#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005147 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005148 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5149 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005150#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005151
5152 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5153 options |= SSL_OP_NO_TICKET;
5154 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5155 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005156
5157#ifdef SSL_OP_NO_RENEGOTIATION
5158 options |= SSL_OP_NO_RENEGOTIATION;
5159#endif
5160
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005161 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005162
Willy Tarreau5db847a2019-05-09 14:13:35 +02005163#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005164 if (global_ssl.async)
5165 mode |= SSL_MODE_ASYNC;
5166#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005167 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005168 if (global_ssl.life_time)
5169 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005170
5171#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5172#ifdef OPENSSL_IS_BORINGSSL
5173 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5174 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005175#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005176 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005177 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005178 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5179 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005180#else
5181 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005182#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005183 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005184#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005185 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005186}
5187
William Lallemand4f45bb92017-10-30 20:08:51 +01005188
5189static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5190{
5191 if (first == block) {
5192 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5193 if (first->len > 0)
5194 sh_ssl_sess_tree_delete(sh_ssl_sess);
5195 }
5196}
5197
5198/* return first block from sh_ssl_sess */
5199static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5200{
5201 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5202
5203}
5204
5205/* store a session into the cache
5206 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5207 * data: asn1 encoded session
5208 * data_len: asn1 encoded session length
5209 * Returns 1 id session was stored (else 0)
5210 */
5211static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5212{
5213 struct shared_block *first;
5214 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5215
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005216 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005217 if (!first) {
5218 /* Could not retrieve enough free blocks to store that session */
5219 return 0;
5220 }
5221
5222 /* STORE the key in the first elem */
5223 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5224 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5225 first->len = sizeof(struct sh_ssl_sess_hdr);
5226
5227 /* it returns the already existing node
5228 or current node if none, never returns null */
5229 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5230 if (oldsh_ssl_sess != sh_ssl_sess) {
5231 /* NOTE: Row couldn't be in use because we lock read & write function */
5232 /* release the reserved row */
5233 shctx_row_dec_hot(ssl_shctx, first);
5234 /* replace the previous session already in the tree */
5235 sh_ssl_sess = oldsh_ssl_sess;
5236 /* ignore the previous session data, only use the header */
5237 first = sh_ssl_sess_first_block(sh_ssl_sess);
5238 shctx_row_inc_hot(ssl_shctx, first);
5239 first->len = sizeof(struct sh_ssl_sess_hdr);
5240 }
5241
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005242 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005243 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005244 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005245 }
5246
5247 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005248
5249 return 1;
5250}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005251
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005252/* SSL callback used when a new session is created while connecting to a server */
5253static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5254{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005255 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005256 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005257
Willy Tarreau07d94e42018-09-20 10:57:52 +02005258 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005259
Olivier Houcharde6060c52017-11-16 17:42:52 +01005260 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5261 int len;
5262 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005263
Olivier Houcharde6060c52017-11-16 17:42:52 +01005264 len = i2d_SSL_SESSION(sess, NULL);
5265 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5266 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5267 } else {
5268 free(s->ssl_ctx.reused_sess[tid].ptr);
5269 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5270 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5271 }
5272 if (s->ssl_ctx.reused_sess[tid].ptr) {
5273 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5274 &ptr);
5275 }
5276 } else {
5277 free(s->ssl_ctx.reused_sess[tid].ptr);
5278 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5279 }
5280
5281 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005282}
5283
Olivier Houcharde6060c52017-11-16 17:42:52 +01005284
William Lallemanded0b5ad2017-10-30 19:36:36 +01005285/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005286int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005287{
5288 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5289 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5290 unsigned char *p;
5291 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005292 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005293 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005294
5295 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005296 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005297 * note: SSL_SESSION_set1_id is using
5298 * a memcpy so we need to use a different pointer
5299 * than sid_data or sid_ctx_data to avoid valgrind
5300 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005301 */
5302
5303 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005304
5305 /* copy value in an other buffer */
5306 memcpy(encid, sid_data, sid_length);
5307
5308 /* pad with 0 */
5309 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5310 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5311
5312 /* force length to zero to avoid ASN1 encoding */
5313 SSL_SESSION_set1_id(sess, encid, 0);
5314
5315 /* force length to zero to avoid ASN1 encoding */
5316 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005317
5318 /* check if buffer is large enough for the ASN1 encoded session */
5319 data_len = i2d_SSL_SESSION(sess, NULL);
5320 if (data_len > SHSESS_MAX_DATA_LEN)
5321 goto err;
5322
5323 p = encsess;
5324
5325 /* process ASN1 session encoding before the lock */
5326 i2d_SSL_SESSION(sess, &p);
5327
William Lallemanded0b5ad2017-10-30 19:36:36 +01005328
William Lallemanda3c77cf2017-10-30 23:44:40 +01005329 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005330 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005331 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005332 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005333err:
5334 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005335 SSL_SESSION_set1_id(sess, encid, sid_length);
5336 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005337
5338 return 0; /* do not increment session reference count */
5339}
5340
5341/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005342SSL_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 +01005343{
William Lallemand4f45bb92017-10-30 20:08:51 +01005344 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005345 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5346 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005347 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005348 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005349
5350 global.shctx_lookups++;
5351
5352 /* allow the session to be freed automatically by openssl */
5353 *do_copy = 0;
5354
5355 /* tree key is zeros padded sessionid */
5356 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5357 memcpy(tmpkey, key, key_len);
5358 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5359 key = tmpkey;
5360 }
5361
5362 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005363 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005364
5365 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005366 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5367 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005368 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005369 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005370 global.shctx_misses++;
5371 return NULL;
5372 }
5373
William Lallemand4f45bb92017-10-30 20:08:51 +01005374 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5375 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005376
William Lallemand4f45bb92017-10-30 20:08:51 +01005377 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 +01005378
William Lallemanda3c77cf2017-10-30 23:44:40 +01005379 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005380
5381 /* decode ASN1 session */
5382 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005383 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005384 /* Reset session id and session id contenxt */
5385 if (sess) {
5386 SSL_SESSION_set1_id(sess, key, key_len);
5387 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5388 }
5389
5390 return sess;
5391}
5392
William Lallemand4f45bb92017-10-30 20:08:51 +01005393
William Lallemanded0b5ad2017-10-30 19:36:36 +01005394/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005395void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005396{
William Lallemand4f45bb92017-10-30 20:08:51 +01005397 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005398 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5399 unsigned int sid_length;
5400 const unsigned char *sid_data;
5401 (void)ctx;
5402
5403 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5404 /* tree key is zeros padded sessionid */
5405 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5406 memcpy(tmpkey, sid_data, sid_length);
5407 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5408 sid_data = tmpkey;
5409 }
5410
William Lallemanda3c77cf2017-10-30 23:44:40 +01005411 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005412
5413 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005414 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5415 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005416 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005417 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005418 }
5419
5420 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005421 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005422}
5423
5424/* Set session cache mode to server and disable openssl internal cache.
5425 * Set shared cache callbacks on an ssl context.
5426 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005427void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005428{
5429 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5430
5431 if (!ssl_shctx) {
5432 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5433 return;
5434 }
5435
5436 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5437 SSL_SESS_CACHE_NO_INTERNAL |
5438 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5439
5440 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005441 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5442 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5443 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005444}
5445
William Lallemand8b453912019-11-21 15:48:10 +01005446/*
5447 * This function applies the SSL configuration on a SSL_CTX
5448 * It returns an error code and fills the <err> buffer
5449 */
5450int 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 +01005451{
5452 struct proxy *curproxy = bind_conf->frontend;
5453 int cfgerr = 0;
5454 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005455 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005456 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005457#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005458 const char *conf_ciphersuites;
5459#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005460 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005461
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005462 if (ssl_conf) {
5463 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5464 int i, min, max;
5465 int flags = MC_SSL_O_ALL;
5466
5467 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005468 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5469 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005470 if (min)
5471 flags |= (methodVersions[min].flag - 1);
5472 if (max)
5473 flags |= ~((methodVersions[max].flag << 1) - 1);
5474 min = max = CONF_TLSV_NONE;
5475 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5476 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5477 if (min)
5478 max = i;
5479 else
5480 min = max = i;
5481 }
5482 /* save real min/max */
5483 conf_ssl_methods->min = min;
5484 conf_ssl_methods->max = max;
5485 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005486 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5487 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005488 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005489 }
5490 }
5491
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005492 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005493 case SSL_SOCK_VERIFY_NONE:
5494 verify = SSL_VERIFY_NONE;
5495 break;
5496 case SSL_SOCK_VERIFY_OPTIONAL:
5497 verify = SSL_VERIFY_PEER;
5498 break;
5499 case SSL_SOCK_VERIFY_REQUIRED:
5500 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5501 break;
5502 }
5503 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5504 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005505 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 +01005506 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 +01005507 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 +01005508 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005509 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005510 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005511 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005512 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005513 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005514 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005515 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5516 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5517 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5518 cfgerr |= ERR_ALERT | ERR_FATAL;
5519 }
5520 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005521 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005522 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 +02005523 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005524 }
Emeric Brun850efd52014-01-29 12:24:34 +01005525 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005526 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5527 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005528 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005529 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005530#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005531 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005532 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5533
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005534 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005535 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5536 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005537 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005538 }
Emeric Brun561e5742012-10-02 15:20:55 +02005539 else {
5540 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5541 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005542 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005543#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005544 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005545 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005546#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005547 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005548 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005549 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5550 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005551 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005552 }
5553 }
5554#endif
5555
William Lallemand4f45bb92017-10-30 20:08:51 +01005556 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005557 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5558 if (conf_ciphers &&
5559 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005560 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5561 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005562 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005563 }
5564
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005565#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005566 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5567 if (conf_ciphersuites &&
5568 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005569 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5570 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005571 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005572 }
5573#endif
5574
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005575#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005576 /* If tune.ssl.default-dh-param has not been set,
5577 neither has ssl-default-dh-file and no static DH
5578 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005579 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005580 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005581 (ssl_dh_ptr_index == -1 ||
5582 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005583 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5584 const SSL_CIPHER * cipher = NULL;
5585 char cipher_description[128];
5586 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5587 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5588 which is not ephemeral DH. */
5589 const char dhe_description[] = " Kx=DH ";
5590 const char dhe_export_description[] = " Kx=DH(";
5591 int idx = 0;
5592 int dhe_found = 0;
5593 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005594
Remi Gacogne23d5d372014-10-10 17:04:26 +02005595 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005596
Remi Gacogne23d5d372014-10-10 17:04:26 +02005597 if (ssl) {
5598 ciphers = SSL_get_ciphers(ssl);
5599
5600 if (ciphers) {
5601 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5602 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5603 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5604 if (strstr(cipher_description, dhe_description) != NULL ||
5605 strstr(cipher_description, dhe_export_description) != NULL) {
5606 dhe_found = 1;
5607 break;
5608 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005609 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005610 }
5611 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005612 SSL_free(ssl);
5613 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005614 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005615
Lukas Tribus90132722014-08-18 00:56:33 +02005616 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005617 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",
5618 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005619 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005620 }
5621
Willy Tarreauef934602016-12-22 23:12:01 +01005622 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005623 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005624
Willy Tarreauef934602016-12-22 23:12:01 +01005625 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005626 if (local_dh_1024 == NULL) {
5627 local_dh_1024 = ssl_get_dh_1024();
5628 }
Willy Tarreauef934602016-12-22 23:12:01 +01005629 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005630 if (local_dh_2048 == NULL) {
5631 local_dh_2048 = ssl_get_dh_2048();
5632 }
Willy Tarreauef934602016-12-22 23:12:01 +01005633 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005634 if (local_dh_4096 == NULL) {
5635 local_dh_4096 = ssl_get_dh_4096();
5636 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005637 }
5638 }
5639 }
5640#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005641
Emeric Brunfc0421f2012-09-07 17:30:07 +02005642 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005643#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005644 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005645#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005646
Bernard Spil13c53f82018-02-15 13:34:58 +01005647#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005648 ssl_conf_cur = NULL;
5649 if (ssl_conf && ssl_conf->npn_str)
5650 ssl_conf_cur = ssl_conf;
5651 else if (bind_conf->ssl_conf.npn_str)
5652 ssl_conf_cur = &bind_conf->ssl_conf;
5653 if (ssl_conf_cur)
5654 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005655#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005656#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005657 ssl_conf_cur = NULL;
5658 if (ssl_conf && ssl_conf->alpn_str)
5659 ssl_conf_cur = ssl_conf;
5660 else if (bind_conf->ssl_conf.alpn_str)
5661 ssl_conf_cur = &bind_conf->ssl_conf;
5662 if (ssl_conf_cur)
5663 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005664#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005665#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005666 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5667 if (conf_curves) {
5668 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005669 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5670 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005671 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005672 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005673 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005674 }
5675#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005676#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005677 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005678 int i;
5679 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005680#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005681 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005682 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5683 NULL);
5684
5685 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005686 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005687 return cfgerr;
5688 }
5689#else
5690 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5691 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5692 ECDHE_DEFAULT_CURVE);
5693#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005694
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005695 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005696 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005697 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5698 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005699 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005700 }
5701 else {
5702 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5703 EC_KEY_free(ecdh);
5704 }
5705 }
5706#endif
5707
Emeric Brunfc0421f2012-09-07 17:30:07 +02005708 return cfgerr;
5709}
5710
Evan Broderbe554312013-06-27 00:05:25 -07005711static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5712{
5713 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5714 size_t prefixlen, suffixlen;
5715
5716 /* Trivial case */
5717 if (strcmp(pattern, hostname) == 0)
5718 return 1;
5719
Evan Broderbe554312013-06-27 00:05:25 -07005720 /* The rest of this logic is based on RFC 6125, section 6.4.3
5721 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5722
Emeric Bruna848dae2013-10-08 11:27:28 +02005723 pattern_wildcard = NULL;
5724 pattern_left_label_end = pattern;
5725 while (*pattern_left_label_end != '.') {
5726 switch (*pattern_left_label_end) {
5727 case 0:
5728 /* End of label not found */
5729 return 0;
5730 case '*':
5731 /* If there is more than one wildcards */
5732 if (pattern_wildcard)
5733 return 0;
5734 pattern_wildcard = pattern_left_label_end;
5735 break;
5736 }
5737 pattern_left_label_end++;
5738 }
5739
5740 /* If it's not trivial and there is no wildcard, it can't
5741 * match */
5742 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005743 return 0;
5744
5745 /* Make sure all labels match except the leftmost */
5746 hostname_left_label_end = strchr(hostname, '.');
5747 if (!hostname_left_label_end
5748 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5749 return 0;
5750
5751 /* Make sure the leftmost label of the hostname is long enough
5752 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005753 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005754 return 0;
5755
5756 /* Finally compare the string on either side of the
5757 * wildcard */
5758 prefixlen = pattern_wildcard - pattern;
5759 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005760 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5761 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005762 return 0;
5763
5764 return 1;
5765}
5766
5767static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5768{
5769 SSL *ssl;
5770 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005771 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005772 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005773 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005774
5775 int depth;
5776 X509 *cert;
5777 STACK_OF(GENERAL_NAME) *alt_names;
5778 int i;
5779 X509_NAME *cert_subject;
5780 char *str;
5781
5782 if (ok == 0)
5783 return ok;
5784
5785 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005786 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005787 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005788
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005789 /* We're checking if the provided hostnames match the desired one. The
5790 * desired hostname comes from the SNI we presented if any, or if not
5791 * provided then it may have been explicitly stated using a "verifyhost"
5792 * directive. If neither is set, we don't care about the name so the
5793 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005794 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005795 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005796 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005797 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005798 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005799 if (!servername)
5800 return ok;
5801 }
Evan Broderbe554312013-06-27 00:05:25 -07005802
5803 /* We only need to verify the CN on the actual server cert,
5804 * not the indirect CAs */
5805 depth = X509_STORE_CTX_get_error_depth(ctx);
5806 if (depth != 0)
5807 return ok;
5808
5809 /* At this point, the cert is *not* OK unless we can find a
5810 * hostname match */
5811 ok = 0;
5812
5813 cert = X509_STORE_CTX_get_current_cert(ctx);
5814 /* It seems like this might happen if verify peer isn't set */
5815 if (!cert)
5816 return ok;
5817
5818 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5819 if (alt_names) {
5820 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5821 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5822 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005823#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005824 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5825#else
Evan Broderbe554312013-06-27 00:05:25 -07005826 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005827#endif
Evan Broderbe554312013-06-27 00:05:25 -07005828 ok = ssl_sock_srv_hostcheck(str, servername);
5829 OPENSSL_free(str);
5830 }
5831 }
5832 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005833 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005834 }
5835
5836 cert_subject = X509_get_subject_name(cert);
5837 i = -1;
5838 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5839 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005840 ASN1_STRING *value;
5841 value = X509_NAME_ENTRY_get_data(entry);
5842 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005843 ok = ssl_sock_srv_hostcheck(str, servername);
5844 OPENSSL_free(str);
5845 }
5846 }
5847
Willy Tarreau71d058c2017-07-26 20:09:56 +02005848 /* report the mismatch and indicate if SNI was used or not */
5849 if (!ok && !conn->err_code)
5850 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005851 return ok;
5852}
5853
Emeric Brun94324a42012-10-11 14:00:19 +02005854/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005855int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005856{
Willy Tarreau03209342016-12-22 17:08:28 +01005857 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005858 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005859 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005860 SSL_OP_ALL | /* all known workarounds for bugs */
5861 SSL_OP_NO_SSLv2 |
5862 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005863 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005864 SSL_MODE_ENABLE_PARTIAL_WRITE |
5865 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005866 SSL_MODE_RELEASE_BUFFERS |
5867 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005868 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005869 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005870 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005871 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005872 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005873
Thierry Fournier383085f2013-01-24 14:15:43 +01005874 /* Make sure openssl opens /dev/urandom before the chroot */
5875 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005876 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005877 cfgerr++;
5878 }
5879
Willy Tarreaufce03112015-01-15 21:32:40 +01005880 /* Automatic memory computations need to know we use SSL there */
5881 global.ssl_used_backend = 1;
5882
5883 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005884 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005885 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005886 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5887 curproxy->id, srv->id,
5888 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005889 cfgerr++;
5890 return cfgerr;
5891 }
5892 }
Emeric Brun94324a42012-10-11 14:00:19 +02005893 if (srv->use_ssl)
5894 srv->xprt = &ssl_sock;
5895 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005896 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005897
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005898 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005899 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005900 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5901 proxy_type_str(curproxy), curproxy->id,
5902 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005903 cfgerr++;
5904 return cfgerr;
5905 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005906
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005907 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005908 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5909 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5910 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005911 else
5912 flags = conf_ssl_methods->flags;
5913
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005914 /* Real min and max should be determinate with configuration and openssl's capabilities */
5915 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005916 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005917 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005918 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005919
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005920 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005921 min = max = CONF_TLSV_NONE;
5922 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005923 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005924 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005925 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005926 if (min) {
5927 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005928 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5929 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5930 proxy_type_str(curproxy), curproxy->id, srv->id,
5931 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005932 hole = 0;
5933 }
5934 max = i;
5935 }
5936 else {
5937 min = max = i;
5938 }
5939 }
5940 else {
5941 if (min)
5942 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005943 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005944 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005945 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5946 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005947 cfgerr += 1;
5948 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005949
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005950#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005951 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005952 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005953 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005954 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005955 else
5956 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5957 if (flags & methodVersions[i].flag)
5958 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005959#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005960 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005961 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5962 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005963#endif
5964
5965 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5966 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005967 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005968
Willy Tarreau5db847a2019-05-09 14:13:35 +02005969#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005970 if (global_ssl.async)
5971 mode |= SSL_MODE_ASYNC;
5972#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005973 SSL_CTX_set_mode(ctx, mode);
5974 srv->ssl_ctx.ctx = ctx;
5975
Emeric Bruna7aa3092012-10-26 12:58:00 +02005976 if (srv->ssl_ctx.client_crt) {
5977 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 +01005978 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5979 proxy_type_str(curproxy), curproxy->id,
5980 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005981 cfgerr++;
5982 }
5983 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 +01005984 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5985 proxy_type_str(curproxy), curproxy->id,
5986 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005987 cfgerr++;
5988 }
5989 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005990 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5991 proxy_type_str(curproxy), curproxy->id,
5992 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005993 cfgerr++;
5994 }
5995 }
Emeric Brun94324a42012-10-11 14:00:19 +02005996
Emeric Brun850efd52014-01-29 12:24:34 +01005997 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5998 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005999 switch (srv->ssl_ctx.verify) {
6000 case SSL_SOCK_VERIFY_NONE:
6001 verify = SSL_VERIFY_NONE;
6002 break;
6003 case SSL_SOCK_VERIFY_REQUIRED:
6004 verify = SSL_VERIFY_PEER;
6005 break;
6006 }
Evan Broderbe554312013-06-27 00:05:25 -07006007 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01006008 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02006009 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01006010 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02006011 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02006012 /* set CAfile to verify */
6013 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6014 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006015 curproxy->id, srv->id,
6016 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006017 cfgerr++;
6018 }
6019 }
Emeric Brun850efd52014-01-29 12:24:34 +01006020 else {
6021 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006022 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",
6023 curproxy->id, srv->id,
6024 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006025 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006026 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6027 curproxy->id, srv->id,
6028 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006029 cfgerr++;
6030 }
Emeric Brunef42d922012-10-11 16:11:36 +02006031#ifdef X509_V_FLAG_CRL_CHECK
6032 if (srv->ssl_ctx.crl_file) {
6033 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6034
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006035 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006036 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6037 curproxy->id, srv->id,
6038 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006039 cfgerr++;
6040 }
6041 else {
6042 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6043 }
6044 }
6045#endif
6046 }
6047
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006048 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6049 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6050 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006051 if (srv->ssl_ctx.ciphers &&
6052 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006053 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6054 curproxy->id, srv->id,
6055 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006056 cfgerr++;
6057 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006058
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006059#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006060 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006061 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006062 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6063 curproxy->id, srv->id,
6064 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6065 cfgerr++;
6066 }
6067#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006068#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6069 if (srv->ssl_ctx.npn_str)
6070 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6071#endif
6072#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6073 if (srv->ssl_ctx.alpn_str)
6074 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6075#endif
6076
Emeric Brun94324a42012-10-11 14:00:19 +02006077
6078 return cfgerr;
6079}
6080
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006081/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006082 * be NULL, in which case nothing is done. Returns the number of errors
6083 * encountered.
6084 */
Willy Tarreau03209342016-12-22 17:08:28 +01006085int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006086{
6087 struct ebmb_node *node;
6088 struct sni_ctx *sni;
6089 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006090 int errcode = 0;
6091 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006092
Willy Tarreaufce03112015-01-15 21:32:40 +01006093 /* Automatic memory computations need to know we use SSL there */
6094 global.ssl_used_frontend = 1;
6095
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006096 /* Make sure openssl opens /dev/urandom before the chroot */
6097 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006098 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006099 err++;
6100 }
6101 /* Create initial_ctx used to start the ssl connection before do switchctx */
6102 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006103 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006104 /* It should not be necessary to call this function, but it's
6105 necessary first to check and move all initialisation related
6106 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006107 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006108 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006109 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006110 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006111
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006112 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006113 while (node) {
6114 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006115 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6116 /* only initialize the CTX on its first occurrence and
6117 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006118 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006119 node = ebmb_next(node);
6120 }
6121
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006122 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006123 while (node) {
6124 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006125 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006126 /* only initialize the CTX on its first occurrence and
6127 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006128 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6129 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006130 node = ebmb_next(node);
6131 }
William Lallemand8b453912019-11-21 15:48:10 +01006132
6133 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006134 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006135 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006136 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006137 err++;
6138 }
6139
6140 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006141 return err;
6142}
6143
Willy Tarreau55d37912016-12-21 23:38:39 +01006144/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6145 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6146 * alerts are directly emitted since the rest of the stack does it below.
6147 */
6148int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6149{
6150 struct proxy *px = bind_conf->frontend;
6151 int alloc_ctx;
6152 int err;
6153
6154 if (!bind_conf->is_ssl) {
6155 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006156 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6157 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006158 }
6159 return 0;
6160 }
6161 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006162 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006163 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6164 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006165 }
6166 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006167 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6168 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006169 return -1;
6170 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006171 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006172 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006173 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006174 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006175 sizeof(*sh_ssl_sess_tree),
6176 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006177 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006178 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6179 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");
6180 else
6181 ha_alert("Unable to allocate SSL session cache.\n");
6182 return -1;
6183 }
6184 /* free block callback */
6185 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6186 /* init the root tree within the extra space */
6187 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6188 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006189 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006190 err = 0;
6191 /* initialize all certificate contexts */
6192 err += ssl_sock_prepare_all_ctx(bind_conf);
6193
6194 /* initialize CA variables if the certificates generation is enabled */
6195 err += ssl_sock_load_ca(bind_conf);
6196
6197 return -err;
6198}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006199
6200/* release ssl context allocated for servers. */
6201void ssl_sock_free_srv_ctx(struct server *srv)
6202{
Olivier Houchardc7566002018-11-20 23:33:50 +01006203#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6204 if (srv->ssl_ctx.alpn_str)
6205 free(srv->ssl_ctx.alpn_str);
6206#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006207#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006208 if (srv->ssl_ctx.npn_str)
6209 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006210#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006211 if (srv->ssl_ctx.ctx)
6212 SSL_CTX_free(srv->ssl_ctx.ctx);
6213}
6214
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006215/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006216 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6217 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006218void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006219{
6220 struct ebmb_node *node, *back;
6221 struct sni_ctx *sni;
6222
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006223 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006224 while (node) {
6225 sni = ebmb_entry(node, struct sni_ctx, name);
6226 back = ebmb_next(node);
6227 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006228 SSL_CTX_free(sni->ctx);
6229 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006230 ssl_sock_free_ssl_conf(sni->conf);
6231 free(sni->conf);
6232 sni->conf = NULL;
6233 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006234 free(sni);
6235 node = back;
6236 }
6237
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006238 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006239 while (node) {
6240 sni = ebmb_entry(node, struct sni_ctx, name);
6241 back = ebmb_next(node);
6242 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006243 SSL_CTX_free(sni->ctx);
6244 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006245 ssl_sock_free_ssl_conf(sni->conf);
6246 free(sni->conf);
6247 sni->conf = NULL;
6248 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006249 free(sni);
6250 node = back;
6251 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006252 SSL_CTX_free(bind_conf->initial_ctx);
6253 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006254 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006255 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006256 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006257}
6258
Willy Tarreau795cdab2016-12-22 17:30:54 +01006259/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6260void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6261{
6262 ssl_sock_free_ca(bind_conf);
6263 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006264 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006265 free(bind_conf->ca_sign_file);
6266 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006267 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006268 free(bind_conf->keys_ref->filename);
6269 free(bind_conf->keys_ref->tlskeys);
6270 LIST_DEL(&bind_conf->keys_ref->list);
6271 free(bind_conf->keys_ref);
6272 }
6273 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006274 bind_conf->ca_sign_pass = NULL;
6275 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006276}
6277
Christopher Faulet31af49d2015-06-09 17:29:50 +02006278/* Load CA cert file and private key used to generate certificates */
6279int
Willy Tarreau03209342016-12-22 17:08:28 +01006280ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006281{
Willy Tarreau03209342016-12-22 17:08:28 +01006282 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006283 FILE *fp;
6284 X509 *cacert = NULL;
6285 EVP_PKEY *capkey = NULL;
6286 int err = 0;
6287
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006288 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006289 return err;
6290
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006291#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006292 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006293 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006294 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006295 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006296 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006297#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006298
Christopher Faulet31af49d2015-06-09 17:29:50 +02006299 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006300 ha_alert("Proxy '%s': cannot enable certificate generation, "
6301 "no CA certificate File configured at [%s:%d].\n",
6302 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006303 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006304 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006305
6306 /* read in the CA certificate */
6307 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
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 Faulet31af49d2015-06-09 17:29:50 +02006310 goto load_error;
6311 }
6312 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006313 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6314 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006315 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006316 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006317 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006318 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006319 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6320 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006321 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006322 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006323
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006324 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006325 bind_conf->ca_sign_cert = cacert;
6326 bind_conf->ca_sign_pkey = capkey;
6327 return err;
6328
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006329 read_error:
6330 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006331 if (capkey) EVP_PKEY_free(capkey);
6332 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006333 load_error:
6334 bind_conf->generate_certs = 0;
6335 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006336 return err;
6337}
6338
6339/* Release CA cert and private key used to generate certificated */
6340void
6341ssl_sock_free_ca(struct bind_conf *bind_conf)
6342{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006343 if (bind_conf->ca_sign_pkey)
6344 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6345 if (bind_conf->ca_sign_cert)
6346 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006347 bind_conf->ca_sign_pkey = NULL;
6348 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006349}
6350
Emeric Brun46591952012-05-18 15:47:34 +02006351/*
6352 * This function is called if SSL * context is not yet allocated. The function
6353 * is designed to be called before any other data-layer operation and sets the
6354 * handshake flag on the connection. It is safe to call it multiple times.
6355 * It returns 0 on success and -1 in error case.
6356 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006357static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006358{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006359 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006360 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006361 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006362 return 0;
6363
Willy Tarreau3c728722014-01-23 13:50:42 +01006364 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006365 return 0;
6366
Olivier Houchard66ab4982019-02-26 18:37:15 +01006367 ctx = pool_alloc(ssl_sock_ctx_pool);
6368 if (!ctx) {
6369 conn->err_code = CO_ER_SSL_NO_MEM;
6370 return -1;
6371 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006372 ctx->wait_event.tasklet = tasklet_new();
6373 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006374 conn->err_code = CO_ER_SSL_NO_MEM;
6375 pool_free(ssl_sock_ctx_pool, ctx);
6376 return -1;
6377 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006378 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6379 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006380 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006381 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006382 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006383 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006384 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006385 ctx->xprt_st = 0;
6386 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006387
6388 /* Only work with sockets for now, this should be adapted when we'll
6389 * add QUIC support.
6390 */
6391 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006392 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006393 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6394 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006395 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006396
Willy Tarreau20879a02012-12-03 16:32:10 +01006397 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6398 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006399 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006400 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006401
Emeric Brun46591952012-05-18 15:47:34 +02006402 /* If it is in client mode initiate SSL session
6403 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006404 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006405 int may_retry = 1;
6406
6407 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006408 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006409 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6410 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006411 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006412 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006413 goto retry_connect;
6414 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006415 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006416 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006417 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006418 ctx->bio = BIO_new(ha_meth);
6419 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006420 SSL_free(ctx->ssl);
6421 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006422 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006423 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006424 goto retry_connect;
6425 }
Emeric Brun55476152014-11-12 17:35:37 +01006426 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006427 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006428 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006429 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006430 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006431
Evan Broderbe554312013-06-27 00:05:25 -07006432 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006433 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6434 SSL_free(ctx->ssl);
6435 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006436 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006437 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006438 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006439 goto retry_connect;
6440 }
Emeric Brun55476152014-11-12 17:35:37 +01006441 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006442 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006443 }
6444
Olivier Houchard66ab4982019-02-26 18:37:15 +01006445 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006446 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6447 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6448 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 +01006449 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006450 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006451 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6452 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006453 } else if (sess) {
6454 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006455 }
6456 }
Evan Broderbe554312013-06-27 00:05:25 -07006457
Emeric Brun46591952012-05-18 15:47:34 +02006458 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006459 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006460
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006461 _HA_ATOMIC_ADD(&sslconns, 1);
6462 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006463 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006464 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006465 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006466 return 0;
6467 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006468 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006469 int may_retry = 1;
6470
6471 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006472 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006473 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6474 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006475 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006476 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006477 goto retry_accept;
6478 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006479 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006480 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006481 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006482 ctx->bio = BIO_new(ha_meth);
6483 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006484 SSL_free(ctx->ssl);
6485 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006486 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006487 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006488 goto retry_accept;
6489 }
Emeric Brun55476152014-11-12 17:35:37 +01006490 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006491 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006492 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006493 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006494 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006495
Emeric Brune1f38db2012-09-03 20:36:47 +02006496 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006497 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6498 SSL_free(ctx->ssl);
6499 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006500 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006501 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006502 goto retry_accept;
6503 }
Emeric Brun55476152014-11-12 17:35:37 +01006504 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006505 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006506 }
6507
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006508#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6509 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6510 b_alloc(&ctx->early_buf);
6511 SSL_set_max_early_data(ctx->ssl,
6512 /* Only allow early data if we managed to allocate
6513 * a buffer.
6514 */
6515 (!b_is_null(&ctx->early_buf)) ?
6516 global.tune.bufsize - global.tune.maxrewrite : 0);
6517 }
6518#endif
6519
Olivier Houchard66ab4982019-02-26 18:37:15 +01006520 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006521
Emeric Brun46591952012-05-18 15:47:34 +02006522 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006523 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006524#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006525 conn->flags |= CO_FL_EARLY_SSL_HS;
6526#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006527
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006528 _HA_ATOMIC_ADD(&sslconns, 1);
6529 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006530 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006531 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006532 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006533 return 0;
6534 }
6535 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006536 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006537err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006538 if (ctx && ctx->wait_event.tasklet)
6539 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006540 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006541 return -1;
6542}
6543
6544
6545/* This is the callback which is used when an SSL handshake is pending. It
6546 * updates the FD status if it wants some polling before being called again.
6547 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6548 * otherwise it returns non-zero and removes itself from the connection's
6549 * flags (the bit is provided in <flag> by the caller).
6550 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006551static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006552{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006553 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006554 int ret;
6555
Willy Tarreau3c728722014-01-23 13:50:42 +01006556 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006557 return 0;
6558
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006559 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006560 goto out_error;
6561
Willy Tarreau5db847a2019-05-09 14:13:35 +02006562#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006563 /*
6564 * Check if we have early data. If we do, we have to read them
6565 * before SSL_do_handshake() is called, And there's no way to
6566 * detect early data, except to try to read them
6567 */
6568 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006569 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006570
Olivier Houchard54907bb2019-12-19 15:02:39 +01006571 while (1) {
6572 ret = SSL_read_early_data(ctx->ssl,
6573 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6574 &read_data);
6575 if (ret == SSL_READ_EARLY_DATA_ERROR)
6576 goto check_error;
6577 if (read_data > 0) {
6578 conn->flags |= CO_FL_EARLY_DATA;
6579 b_add(&ctx->early_buf, read_data);
6580 }
6581 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6582 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6583 if (!b_data(&ctx->early_buf))
6584 b_free(&ctx->early_buf);
6585 break;
6586 }
6587 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006588 }
6589#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006590 /* If we use SSL_do_handshake to process a reneg initiated by
6591 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6592 * Usually SSL_write and SSL_read are used and process implicitly
6593 * the reneg handshake.
6594 * Here we use SSL_peek as a workaround for reneg.
6595 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006596 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006597 char c;
6598
Olivier Houchard66ab4982019-02-26 18:37:15 +01006599 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006600 if (ret <= 0) {
6601 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006602 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006603
Emeric Brun674b7432012-11-08 19:21:55 +01006604 if (ret == SSL_ERROR_WANT_WRITE) {
6605 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006606 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006607 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006608 return 0;
6609 }
6610 else if (ret == SSL_ERROR_WANT_READ) {
6611 /* handshake may have been completed but we have
6612 * no more data to read.
6613 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006614 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006615 ret = 1;
6616 goto reneg_ok;
6617 }
6618 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006619 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006620 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006621 return 0;
6622 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006623#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006624 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006625 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006626 return 0;
6627 }
6628#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006629 else if (ret == SSL_ERROR_SYSCALL) {
6630 /* if errno is null, then connection was successfully established */
6631 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6632 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006633 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006634#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6635 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006636 conn->err_code = CO_ER_SSL_HANDSHAKE;
6637#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006638 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006639#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006640 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006641 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006642 empty_handshake = state == TLS_ST_BEFORE;
6643#else
Lukas Tribus49799162019-07-08 14:29:15 +02006644 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6645 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006646#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006647 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006648 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006649 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006650 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6651 else
6652 conn->err_code = CO_ER_SSL_EMPTY;
6653 }
6654 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006655 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006656 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6657 else
6658 conn->err_code = CO_ER_SSL_ABORT;
6659 }
6660 }
6661 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006662 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006663 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006664 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006665 conn->err_code = CO_ER_SSL_HANDSHAKE;
6666 }
Lukas Tribus49799162019-07-08 14:29:15 +02006667#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006668 }
Emeric Brun674b7432012-11-08 19:21:55 +01006669 goto out_error;
6670 }
6671 else {
6672 /* Fail on all other handshake errors */
6673 /* Note: OpenSSL may leave unread bytes in the socket's
6674 * buffer, causing an RST to be emitted upon close() on
6675 * TCP sockets. We first try to drain possibly pending
6676 * data to avoid this as much as possible.
6677 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006678 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006679 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006680 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006681 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006682 goto out_error;
6683 }
6684 }
6685 /* read some data: consider handshake completed */
6686 goto reneg_ok;
6687 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006688 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006689check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006690 if (ret != 1) {
6691 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006692 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006693
6694 if (ret == SSL_ERROR_WANT_WRITE) {
6695 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006696 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006697 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006698 return 0;
6699 }
6700 else if (ret == SSL_ERROR_WANT_READ) {
6701 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006702 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006703 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6704 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006705 return 0;
6706 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006707#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006708 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006709 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006710 return 0;
6711 }
6712#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006713 else if (ret == SSL_ERROR_SYSCALL) {
6714 /* if errno is null, then connection was successfully established */
6715 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6716 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006717 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006718#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6719 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006720 conn->err_code = CO_ER_SSL_HANDSHAKE;
6721#else
6722 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006723#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006724 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006725 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006726 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006727#else
Lukas Tribus49799162019-07-08 14:29:15 +02006728 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6729 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006730#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006731 if (empty_handshake) {
6732 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006733 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006734 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6735 else
6736 conn->err_code = CO_ER_SSL_EMPTY;
6737 }
6738 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006739 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006740 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6741 else
6742 conn->err_code = CO_ER_SSL_ABORT;
6743 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006744 }
6745 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006746 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006747 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6748 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006749 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006750 }
Lukas Tribus49799162019-07-08 14:29:15 +02006751#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006752 }
Willy Tarreau89230192012-09-28 20:22:13 +02006753 goto out_error;
6754 }
Emeric Brun46591952012-05-18 15:47:34 +02006755 else {
6756 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006757 /* Note: OpenSSL may leave unread bytes in the socket's
6758 * buffer, causing an RST to be emitted upon close() on
6759 * TCP sockets. We first try to drain possibly pending
6760 * data to avoid this as much as possible.
6761 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006762 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006763 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006764 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006765 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006766 goto out_error;
6767 }
6768 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006769#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006770 else {
6771 /*
6772 * If the server refused the early data, we have to send a
6773 * 425 to the client, as we no longer have the data to sent
6774 * them again.
6775 */
6776 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006777 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006778 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6779 goto out_error;
6780 }
6781 }
6782 }
6783#endif
6784
Emeric Brun46591952012-05-18 15:47:34 +02006785
Emeric Brun674b7432012-11-08 19:21:55 +01006786reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006787
Willy Tarreau5db847a2019-05-09 14:13:35 +02006788#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006789 /* ASYNC engine API doesn't support moving read/write
6790 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006791 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006792 */
6793 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006794 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006795#endif
Emeric Brun46591952012-05-18 15:47:34 +02006796 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006797 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006798 if (objt_server(conn->target)) {
6799 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6800 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6801 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006802 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006803 else {
6804 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6805 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6806 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6807 }
Emeric Brun46591952012-05-18 15:47:34 +02006808 }
6809
6810 /* The connection is now established at both layers, it's time to leave */
6811 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6812 return 1;
6813
6814 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006815 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006816 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006817 ERR_clear_error();
6818
Emeric Brun9fa89732012-10-04 17:09:56 +02006819 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006820 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6821 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6822 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006823 }
6824
Emeric Brun46591952012-05-18 15:47:34 +02006825 /* Fail on all other handshake errors */
6826 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006827 if (!conn->err_code)
6828 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006829 return 0;
6830}
6831
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006832/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6833 * event subscriber <es> is not allowed to change from a previous call as long
6834 * as at least one event is still subscribed. The <event_type> must only be a
6835 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6836 * unless the transport layer was already released.
6837 */
6838static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006839{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006840 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006841
Olivier Houchard0ff28652019-06-24 18:57:39 +02006842 if (!ctx)
6843 return -1;
6844
Willy Tarreau113d52b2020-01-10 09:20:26 +01006845 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006846 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006847
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006848 ctx->subs = es;
6849 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006850
6851 /* we may have to subscribe to lower layers for new events */
6852 event_type &= ~ctx->wait_event.events;
6853 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6854 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006855 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006856}
6857
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006858/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6859 * The <es> pointer is not allowed to differ from the one passed to the
6860 * subscribe() call. It always returns zero.
6861 */
6862static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006863{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006864 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006865
Willy Tarreau113d52b2020-01-10 09:20:26 +01006866 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006867 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006868
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006869 es->events &= ~event_type;
6870 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006871 ctx->subs = NULL;
6872
6873 /* If we subscribed, and we're not doing the handshake,
6874 * then we subscribed because the upper layer asked for it,
6875 * as the upper layer is no longer interested, we can
6876 * unsubscribe too.
6877 */
6878 event_type &= ctx->wait_event.events;
6879 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6880 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006881
6882 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006883}
6884
Olivier Houchard2e055482019-05-27 19:50:12 +02006885/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6886 * Returns 0 on success, and non-zero on failure.
6887 */
6888static 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)
6889{
6890 struct ssl_sock_ctx *ctx = xprt_ctx;
6891
6892 if (oldxprt_ops != NULL)
6893 *oldxprt_ops = ctx->xprt;
6894 if (oldxprt_ctx != NULL)
6895 *oldxprt_ctx = ctx->xprt_ctx;
6896 ctx->xprt = toadd_ops;
6897 ctx->xprt_ctx = toadd_ctx;
6898 return 0;
6899}
6900
Olivier Houchard5149b592019-05-23 17:47:36 +02006901/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6902 * return 0, otherwise just call the remove_xprt method from the underlying
6903 * XPRT.
6904 */
6905static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6906{
6907 struct ssl_sock_ctx *ctx = xprt_ctx;
6908
6909 if (ctx->xprt_ctx == toremove_ctx) {
6910 ctx->xprt_ctx = newctx;
6911 ctx->xprt = newops;
6912 return 0;
6913 }
6914 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6915}
6916
Olivier Houchardea8dd942019-05-20 14:02:16 +02006917static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6918{
6919 struct ssl_sock_ctx *ctx = context;
6920
6921 /* First if we're doing an handshake, try that */
6922 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6923 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6924 /* If we had an error, or the handshake is done and I/O is available,
6925 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006926 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006927 * we can't be sure conn_fd_handler() will be called again.
6928 */
6929 if ((ctx->conn->flags & CO_FL_ERROR) ||
6930 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6931 int ret = 0;
6932 int woke = 0;
6933
6934 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006935 if (ctx->subs) {
6936 tasklet_wakeup(ctx->subs->tasklet);
6937 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006938 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006939 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006940 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006941
Olivier Houchardea8dd942019-05-20 14:02:16 +02006942 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006943 * upper layers know. If we have no mux, create it,
6944 * and once we have a mux, call its wake method if we didn't
6945 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006946 */
6947 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006948 if (!ctx->conn->mux)
6949 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006950 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6951 ctx->conn->mux->wake(ctx->conn);
6952 return NULL;
6953 }
6954 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006955#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6956 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006957 else if (b_data(&ctx->early_buf) && ctx->subs &&
6958 ctx->subs->events & SUB_RETRY_RECV) {
6959 tasklet_wakeup(ctx->subs->tasklet);
6960 ctx->subs->events &= ~SUB_RETRY_RECV;
6961 if (!ctx->subs->events)
6962 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006963 }
6964#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006965 return NULL;
6966}
6967
Emeric Brun46591952012-05-18 15:47:34 +02006968/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006969 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006970 * buffer wraps, in which case a second call may be performed. The connection's
6971 * flags are updated with whatever special event is detected (error, read0,
6972 * empty). The caller is responsible for taking care of those events and
6973 * avoiding the call if inappropriate. The function does not call the
6974 * connection's polling update function, so the caller is responsible for this.
6975 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006976static 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 +02006977{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006978 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006979 ssize_t ret;
6980 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006981
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006982 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006983 goto out_error;
6984
Olivier Houchard54907bb2019-12-19 15:02:39 +01006985#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6986 if (b_data(&ctx->early_buf)) {
6987 try = b_contig_space(buf);
6988 if (try > b_data(&ctx->early_buf))
6989 try = b_data(&ctx->early_buf);
6990 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6991 b_add(buf, try);
6992 b_del(&ctx->early_buf, try);
6993 if (b_data(&ctx->early_buf) == 0)
6994 b_free(&ctx->early_buf);
6995 return try;
6996 }
6997#endif
6998
Willy Tarreau911db9b2020-01-23 16:27:54 +01006999 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007000 /* a handshake was requested */
7001 return 0;
7002
Emeric Brun46591952012-05-18 15:47:34 +02007003 /* read the largest possible block. For this, we perform only one call
7004 * to recv() unless the buffer wraps and we exactly fill the first hunk,
7005 * in which case we accept to do it once again. A new attempt is made on
7006 * EINTR too.
7007 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01007008 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007009
Willy Tarreau591d4452018-06-15 17:21:00 +02007010 try = b_contig_space(buf);
7011 if (!try)
7012 break;
7013
Willy Tarreauabf08d92014-01-14 11:31:27 +01007014 if (try > count)
7015 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007016
Olivier Houchard66ab4982019-02-26 18:37:15 +01007017 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007018
Emeric Brune1f38db2012-09-03 20:36:47 +02007019 if (conn->flags & CO_FL_ERROR) {
7020 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007021 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007022 }
Emeric Brun46591952012-05-18 15:47:34 +02007023 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007024 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007025 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007026 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007027 }
Emeric Brun46591952012-05-18 15:47:34 +02007028 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007029 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007030 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007031 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007032 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007033 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007034#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007035 /* Async mode can be re-enabled, because we're leaving data state.*/
7036 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007037 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007038#endif
Emeric Brun46591952012-05-18 15:47:34 +02007039 break;
7040 }
7041 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007042 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007043 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7044 SUB_RETRY_RECV,
7045 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007046 /* handshake is running, and it may need to re-enable read */
7047 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007048#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007049 /* Async mode can be re-enabled, because we're leaving data state.*/
7050 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007051 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007052#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007053 break;
7054 }
Emeric Brun46591952012-05-18 15:47:34 +02007055 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007056 } else if (ret == SSL_ERROR_ZERO_RETURN)
7057 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007058 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7059 * stack before shutting down the connection for
7060 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007061 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7062 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007063 /* otherwise it's a real error */
7064 goto out_error;
7065 }
7066 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007067 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007068 return done;
7069
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007070 clear_ssl_error:
7071 /* Clear openssl global errors stack */
7072 ssl_sock_dump_errors(conn);
7073 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007074 read0:
7075 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007076 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007077
Emeric Brun46591952012-05-18 15:47:34 +02007078 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007079 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007080 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007081 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007082 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007083 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007084}
7085
7086
Willy Tarreau787db9a2018-06-14 18:31:46 +02007087/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7088 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7089 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007090 * Only one call to send() is performed, unless the buffer wraps, in which case
7091 * a second call may be performed. The connection's flags are updated with
7092 * whatever special event is detected (error, empty). The caller is responsible
7093 * for taking care of those events and avoiding the call if inappropriate. The
7094 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007095 * is responsible for this. The buffer's output is not adjusted, it's up to the
7096 * caller to take care of this. It's up to the caller to update the buffer's
7097 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007098 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007099static 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 +02007100{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007101 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007102 ssize_t ret;
7103 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007104
7105 done = 0;
7106
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007107 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007108 goto out_error;
7109
Willy Tarreau911db9b2020-01-23 16:27:54 +01007110 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007111 /* a handshake was requested */
7112 return 0;
7113
7114 /* send the largest possible block. For this we perform only one call
7115 * to send() unless the buffer wraps and we exactly fill the first hunk,
7116 * in which case we accept to do it once again.
7117 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007118 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007119#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007120 size_t written_data;
7121#endif
7122
Willy Tarreau787db9a2018-06-14 18:31:46 +02007123 try = b_contig_data(buf, done);
7124 if (try > count)
7125 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007126
Willy Tarreau7bed9452014-02-02 02:00:24 +01007127 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007128 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007129 global_ssl.max_record && try > global_ssl.max_record) {
7130 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007131 }
7132 else {
7133 /* we need to keep the information about the fact that
7134 * we're not limiting the upcoming send(), because if it
7135 * fails, we'll have to retry with at least as many data.
7136 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007137 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007138 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007139
Willy Tarreau5db847a2019-05-09 14:13:35 +02007140#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007141 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007142 unsigned int max_early;
7143
Olivier Houchard522eea72017-11-03 16:27:47 +01007144 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007145 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007146 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007147 if (SSL_get0_session(ctx->ssl))
7148 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007149 else
7150 max_early = 0;
7151 }
7152
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007153 if (try + ctx->sent_early_data > max_early) {
7154 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007155 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007156 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007157 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007158 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007159 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007160 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007161 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007162 if (ret == 1) {
7163 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007164 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007165 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007166 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007167 /* Initiate the handshake, now */
7168 tasklet_wakeup(ctx->wait_event.tasklet);
7169 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007170
Olivier Houchardc2aae742017-09-22 18:26:28 +02007171 }
7172
7173 } else
7174#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007175 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007176
Emeric Brune1f38db2012-09-03 20:36:47 +02007177 if (conn->flags & CO_FL_ERROR) {
7178 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007179 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007180 }
Emeric Brun46591952012-05-18 15:47:34 +02007181 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007182 /* A send succeeded, so we can consider ourself connected */
7183 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007184 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007185 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007186 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007187 }
7188 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007189 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007190
Emeric Brun46591952012-05-18 15:47:34 +02007191 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007192 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007193 /* handshake is running, and it may need to re-enable write */
7194 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007195 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007196#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007197 /* Async mode can be re-enabled, because we're leaving data state.*/
7198 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007199 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007200#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007201 break;
7202 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007203
Emeric Brun46591952012-05-18 15:47:34 +02007204 break;
7205 }
7206 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007207 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007208 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007209 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7210 SUB_RETRY_RECV,
7211 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007212#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007213 /* Async mode can be re-enabled, because we're leaving data state.*/
7214 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007215 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007216#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007217 break;
7218 }
Emeric Brun46591952012-05-18 15:47:34 +02007219 goto out_error;
7220 }
7221 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007222 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007223 return done;
7224
7225 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007226 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007227 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007228 ERR_clear_error();
7229
Emeric Brun46591952012-05-18 15:47:34 +02007230 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007231 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007232}
7233
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007234static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007235
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007236 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007237
Olivier Houchardea8dd942019-05-20 14:02:16 +02007238
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007239 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007240 if (ctx->wait_event.events != 0)
7241 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7242 ctx->wait_event.events,
7243 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007244 if (ctx->subs) {
7245 ctx->subs->events = 0;
7246 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007247 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007248
Olivier Houchard692c1d02019-05-23 18:41:47 +02007249 if (ctx->xprt->close)
7250 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007251#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007252 if (global_ssl.async) {
7253 OSSL_ASYNC_FD all_fd[32], afd;
7254 size_t num_all_fds = 0;
7255 int i;
7256
Olivier Houchard66ab4982019-02-26 18:37:15 +01007257 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007258 if (num_all_fds > 32) {
7259 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7260 return;
7261 }
7262
Olivier Houchard66ab4982019-02-26 18:37:15 +01007263 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007264
7265 /* If an async job is pending, we must try to
7266 to catch the end using polling before calling
7267 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007268 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007269 for (i=0 ; i < num_all_fds ; i++) {
7270 /* switch on an handler designed to
7271 * handle the SSL_free
7272 */
7273 afd = all_fd[i];
7274 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007275 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007276 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007277 /* To ensure that the fd cache won't be used
7278 * and we'll catch a real RD event.
7279 */
7280 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007281 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007282 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007283 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007284 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007285 return;
7286 }
Emeric Brun3854e012017-05-17 20:42:48 +02007287 /* Else we can remove the fds from the fdtab
7288 * and call SSL_free.
7289 * note: we do a fd_remove and not a delete
7290 * because the fd is owned by the engine.
7291 * the engine is responsible to close
7292 */
7293 for (i=0 ; i < num_all_fds ; i++)
7294 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007295 }
7296#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007297 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007298 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007299 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007300 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007301 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007302 }
Emeric Brun46591952012-05-18 15:47:34 +02007303}
7304
7305/* This function tries to perform a clean shutdown on an SSL connection, and in
7306 * any case, flags the connection as reusable if no handshake was in progress.
7307 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007308static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007309{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007310 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007311
Willy Tarreau911db9b2020-01-23 16:27:54 +01007312 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007313 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007314 if (!clean)
7315 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007316 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007317 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007318 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007319 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007320 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007321 ERR_clear_error();
7322 }
Emeric Brun46591952012-05-18 15:47:34 +02007323}
7324
William Lallemandd4f946c2019-12-05 10:26:40 +01007325/* fill a buffer with the algorithm and size of a public key */
7326static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007327{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007328 int bits = 0;
7329 int sig = TLSEXT_signature_anonymous;
7330 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007331 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007332
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007333 pkey = X509_get_pubkey(crt);
7334 if (pkey) {
7335 bits = EVP_PKEY_bits(pkey);
7336 switch(EVP_PKEY_base_id(pkey)) {
7337 case EVP_PKEY_RSA:
7338 sig = TLSEXT_signature_rsa;
7339 break;
7340 case EVP_PKEY_EC:
7341 sig = TLSEXT_signature_ecdsa;
7342 break;
7343 case EVP_PKEY_DSA:
7344 sig = TLSEXT_signature_dsa;
7345 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007346 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007347 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007348 }
7349
7350 switch(sig) {
7351 case TLSEXT_signature_rsa:
7352 len = chunk_printf(out, "RSA%d", bits);
7353 break;
7354 case TLSEXT_signature_ecdsa:
7355 len = chunk_printf(out, "EC%d", bits);
7356 break;
7357 case TLSEXT_signature_dsa:
7358 len = chunk_printf(out, "DSA%d", bits);
7359 break;
7360 default:
7361 return 0;
7362 }
7363 if (len < 0)
7364 return 0;
7365 return 1;
7366}
7367
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007368/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007369int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7370{
7371 struct ssl_sock_ctx *ctx;
7372 X509 *crt;
7373
7374 if (!ssl_sock_is_ssl(conn))
7375 return 0;
7376
7377 ctx = conn->xprt_ctx;
7378
7379 crt = SSL_get_certificate(ctx->ssl);
7380 if (!crt)
7381 return 0;
7382
7383 return cert_get_pkey_algo(crt, out);
7384}
7385
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007386/* used for ppv2 cert signature (can be used for logging) */
7387const char *ssl_sock_get_cert_sig(struct connection *conn)
7388{
Christopher Faulet82004142019-09-10 10:12:03 +02007389 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007390
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007391 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7392 X509 *crt;
7393
7394 if (!ssl_sock_is_ssl(conn))
7395 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007396 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007397 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007398 if (!crt)
7399 return NULL;
7400 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7401 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7402}
7403
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007404/* used for ppv2 authority */
7405const char *ssl_sock_get_sni(struct connection *conn)
7406{
7407#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007408 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007409
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007410 if (!ssl_sock_is_ssl(conn))
7411 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007412 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007413 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007414#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007415 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007416#endif
7417}
7418
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007419/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007420const char *ssl_sock_get_cipher_name(struct connection *conn)
7421{
Christopher Faulet82004142019-09-10 10:12:03 +02007422 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007423
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007424 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007425 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007426 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007427 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007428}
7429
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007430/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007431const char *ssl_sock_get_proto_version(struct connection *conn)
7432{
Christopher Faulet82004142019-09-10 10:12:03 +02007433 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007434
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007435 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007436 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007437 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007438 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007439}
7440
Willy Tarreau8d598402012-10-22 17:58:39 +02007441/* Extract a serial from a cert, and copy it to a chunk.
7442 * Returns 1 if serial is found and copied, 0 if no serial found and
7443 * -1 if output is not large enough.
7444 */
7445static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007446ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007447{
7448 ASN1_INTEGER *serial;
7449
7450 serial = X509_get_serialNumber(crt);
7451 if (!serial)
7452 return 0;
7453
7454 if (out->size < serial->length)
7455 return -1;
7456
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007457 memcpy(out->area, serial->data, serial->length);
7458 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007459 return 1;
7460}
7461
Emeric Brun43e79582014-10-29 19:03:26 +01007462/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007463 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7464 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007465 */
7466static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007467ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007468{
7469 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007470 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007471
7472 len =i2d_X509(crt, NULL);
7473 if (len <= 0)
7474 return 1;
7475
7476 if (out->size < len)
7477 return -1;
7478
7479 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007480 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007481 return 1;
7482}
7483
Emeric Brunce5ad802012-10-22 14:11:22 +02007484
Willy Tarreau83061a82018-07-13 11:56:34 +02007485/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007486 * Returns 1 if serial is found and copied, 0 if no valid time found
7487 * and -1 if output is not large enough.
7488 */
7489static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007490ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007491{
7492 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7493 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7494
7495 if (gentm->length < 12)
7496 return 0;
7497 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7498 return 0;
7499 if (out->size < gentm->length-2)
7500 return -1;
7501
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007502 memcpy(out->area, gentm->data+2, gentm->length-2);
7503 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007504 return 1;
7505 }
7506 else if (tm->type == V_ASN1_UTCTIME) {
7507 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7508
7509 if (utctm->length < 10)
7510 return 0;
7511 if (utctm->data[0] >= 0x35)
7512 return 0;
7513 if (out->size < utctm->length)
7514 return -1;
7515
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007516 memcpy(out->area, utctm->data, utctm->length);
7517 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007518 return 1;
7519 }
7520
7521 return 0;
7522}
7523
Emeric Brun87855892012-10-17 17:39:35 +02007524/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7525 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7526 */
7527static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007528ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7529 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007530{
7531 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007532 ASN1_OBJECT *obj;
7533 ASN1_STRING *data;
7534 const unsigned char *data_ptr;
7535 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007536 int i, j, n;
7537 int cur = 0;
7538 const char *s;
7539 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007540 int name_count;
7541
7542 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007544 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007545 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007546 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007547 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007548 else
7549 j = i;
7550
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007551 ne = X509_NAME_get_entry(a, j);
7552 obj = X509_NAME_ENTRY_get_object(ne);
7553 data = X509_NAME_ENTRY_get_data(ne);
7554 data_ptr = ASN1_STRING_get0_data(data);
7555 data_len = ASN1_STRING_length(data);
7556 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007557 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007558 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007559 s = tmp;
7560 }
7561
7562 if (chunk_strcasecmp(entry, s) != 0)
7563 continue;
7564
7565 if (pos < 0)
7566 cur--;
7567 else
7568 cur++;
7569
7570 if (cur != pos)
7571 continue;
7572
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007573 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007574 return -1;
7575
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007576 memcpy(out->area, data_ptr, data_len);
7577 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007578 return 1;
7579 }
7580
7581 return 0;
7582
William Lallemandd4f946c2019-12-05 10:26:40 +01007583}
7584
7585/*
7586 * Extract and format the DNS SAN extensions and copy result into a chuink
7587 * Return 0;
7588 */
7589#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7590static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7591{
7592 int i;
7593 char *str;
7594 STACK_OF(GENERAL_NAME) *names = NULL;
7595
7596 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7597 if (names) {
7598 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7599 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7600 if (i > 0)
7601 chunk_appendf(out, ", ");
7602 if (name->type == GEN_DNS) {
7603 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7604 chunk_appendf(out, "DNS:%s", str);
7605 OPENSSL_free(str);
7606 }
7607 }
7608 }
7609 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7610 }
7611 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007612}
William Lallemandd4f946c2019-12-05 10:26:40 +01007613#endif
Emeric Brun87855892012-10-17 17:39:35 +02007614
Elliot Otchet71f82972020-01-15 08:12:14 -05007615/*
7616 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7617 * Currently supports rfc2253 for returning LDAP V3 DNs.
7618 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7619 */
7620static int
7621ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7622{
7623 BIO *bio = NULL;
7624 int ret = 0;
7625 int data_len = 0;
7626
7627 if (chunk_strcmp(format, "rfc2253") == 0) {
7628 bio = BIO_new(BIO_s_mem());
7629 if (bio == NULL)
7630 goto out;
7631
7632 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7633 goto out;
7634
7635 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7636 goto out;
7637
7638 out->data = data_len;
7639
7640 ret = 1;
7641 }
7642out:
7643 if (bio)
7644 BIO_free(bio);
7645 return ret;
7646}
7647
Emeric Brun87855892012-10-17 17:39:35 +02007648/* Extract and format full DN from a X509_NAME and copy result into a chunk
7649 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7650 */
7651static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007652ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007653{
7654 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007655 ASN1_OBJECT *obj;
7656 ASN1_STRING *data;
7657 const unsigned char *data_ptr;
7658 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007659 int i, n, ln;
7660 int l = 0;
7661 const char *s;
7662 char *p;
7663 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007664 int name_count;
7665
7666
7667 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007668
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007669 out->data = 0;
7670 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007671 for (i = 0; i < name_count; i++) {
7672 ne = X509_NAME_get_entry(a, i);
7673 obj = X509_NAME_ENTRY_get_object(ne);
7674 data = X509_NAME_ENTRY_get_data(ne);
7675 data_ptr = ASN1_STRING_get0_data(data);
7676 data_len = ASN1_STRING_length(data);
7677 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007678 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007679 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007680 s = tmp;
7681 }
7682 ln = strlen(s);
7683
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007684 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007685 if (l > out->size)
7686 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007687 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007688
7689 *(p++)='/';
7690 memcpy(p, s, ln);
7691 p += ln;
7692 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007693 memcpy(p, data_ptr, data_len);
7694 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007695 }
7696
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007697 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007698 return 0;
7699
7700 return 1;
7701}
7702
Olivier Houchardab28a322018-12-21 19:45:40 +01007703void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7704{
7705#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007706 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007707
Olivier Houcharde488ea82019-06-28 14:10:33 +02007708 if (!ssl_sock_is_ssl(conn))
7709 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007710 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007711 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007712#endif
7713}
7714
Willy Tarreau119a4082016-12-22 21:58:38 +01007715/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7716 * to disable SNI.
7717 */
Willy Tarreau63076412015-07-10 11:33:32 +02007718void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7719{
7720#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007721 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007722
Willy Tarreau119a4082016-12-22 21:58:38 +01007723 char *prev_name;
7724
Willy Tarreau63076412015-07-10 11:33:32 +02007725 if (!ssl_sock_is_ssl(conn))
7726 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007727 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007728
Willy Tarreau119a4082016-12-22 21:58:38 +01007729 /* if the SNI changes, we must destroy the reusable context so that a
7730 * new connection will present a new SNI. As an optimization we could
7731 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7732 * server.
7733 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007734 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007735 if ((!prev_name && hostname) ||
7736 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007737 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007738
Olivier Houchard66ab4982019-02-26 18:37:15 +01007739 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007740#endif
7741}
7742
Emeric Brun0abf8362014-06-24 18:26:41 +02007743/* Extract peer certificate's common name into the chunk dest
7744 * Returns
7745 * the len of the extracted common name
7746 * or 0 if no CN found in DN
7747 * or -1 on error case (i.e. no peer certificate)
7748 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007749int ssl_sock_get_remote_common_name(struct connection *conn,
7750 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007751{
Christopher Faulet82004142019-09-10 10:12:03 +02007752 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007753 X509 *crt = NULL;
7754 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007755 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007756 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007757 .area = (char *)&find_cn,
7758 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007759 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007760 int result = -1;
David Safb76832014-05-08 23:42:08 -04007761
7762 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007763 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007764 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007765
7766 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007767 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007768 if (!crt)
7769 goto out;
7770
7771 name = X509_get_subject_name(crt);
7772 if (!name)
7773 goto out;
David Safb76832014-05-08 23:42:08 -04007774
Emeric Brun0abf8362014-06-24 18:26:41 +02007775 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7776out:
David Safb76832014-05-08 23:42:08 -04007777 if (crt)
7778 X509_free(crt);
7779
7780 return result;
7781}
7782
Dave McCowan328fb582014-07-30 10:39:13 -04007783/* returns 1 if client passed a certificate for this session, 0 if not */
7784int ssl_sock_get_cert_used_sess(struct connection *conn)
7785{
Christopher Faulet82004142019-09-10 10:12:03 +02007786 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007787 X509 *crt = NULL;
7788
7789 if (!ssl_sock_is_ssl(conn))
7790 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007791 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007792
7793 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007794 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007795 if (!crt)
7796 return 0;
7797
7798 X509_free(crt);
7799 return 1;
7800}
7801
7802/* returns 1 if client passed a certificate for this connection, 0 if not */
7803int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007804{
Christopher Faulet82004142019-09-10 10:12:03 +02007805 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007806
David Safb76832014-05-08 23:42:08 -04007807 if (!ssl_sock_is_ssl(conn))
7808 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007809 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007810 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007811}
7812
7813/* returns result from SSL verify */
7814unsigned int ssl_sock_get_verify_result(struct connection *conn)
7815{
Christopher Faulet82004142019-09-10 10:12:03 +02007816 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007817
David Safb76832014-05-08 23:42:08 -04007818 if (!ssl_sock_is_ssl(conn))
7819 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007820 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007821 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007822}
7823
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007824/* Returns the application layer protocol name in <str> and <len> when known.
7825 * Zero is returned if the protocol name was not found, otherwise non-zero is
7826 * returned. The string is allocated in the SSL context and doesn't have to be
7827 * freed by the caller. NPN is also checked if available since older versions
7828 * of openssl (1.0.1) which are more common in field only support this one.
7829 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007830static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007831{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007832#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7833 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007834 struct ssl_sock_ctx *ctx = xprt_ctx;
7835 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007836 return 0;
7837
7838 *str = NULL;
7839
7840#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007841 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007842 if (*str)
7843 return 1;
7844#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007845#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007846 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007847 if (*str)
7848 return 1;
7849#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007850#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007851 return 0;
7852}
7853
Willy Tarreau7875d092012-09-10 08:20:03 +02007854/***** Below are some sample fetching functions for ACL/patterns *****/
7855
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007856static int
7857smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7858{
7859 struct connection *conn;
7860
7861 conn = objt_conn(smp->sess->origin);
7862 if (!conn || conn->xprt != &ssl_sock)
7863 return 0;
7864
7865 smp->flags = 0;
7866 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007867#ifdef OPENSSL_IS_BORINGSSL
7868 {
7869 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7870 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7871 SSL_early_data_accepted(ctx->ssl));
7872 }
7873#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007874 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007875 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007876#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007877 return 1;
7878}
7879
Emeric Brune64aef12012-09-21 13:15:06 +02007880/* boolean, returns true if client cert was present */
7881static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007882smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007883{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007884 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007885 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007886
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007887 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007888 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007889 return 0;
7890
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007891 ctx = conn->xprt_ctx;
7892
Willy Tarreau911db9b2020-01-23 16:27:54 +01007893 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007894 smp->flags |= SMP_F_MAY_CHANGE;
7895 return 0;
7896 }
7897
7898 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007899 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007900 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007901
7902 return 1;
7903}
7904
Emeric Brun43e79582014-10-29 19:03:26 +01007905/* binary, returns a certificate in a binary chunk (der/raw).
7906 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7907 * should be use.
7908 */
7909static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007910smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007911{
7912 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7913 X509 *crt = NULL;
7914 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007915 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007916 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007917 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007918
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007919 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007920 if (!conn || conn->xprt != &ssl_sock)
7921 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007922 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007923
Willy Tarreau911db9b2020-01-23 16:27:54 +01007924 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007925 smp->flags |= SMP_F_MAY_CHANGE;
7926 return 0;
7927 }
7928
7929 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007930 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007931 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007932 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007933
7934 if (!crt)
7935 goto out;
7936
7937 smp_trash = get_trash_chunk();
7938 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7939 goto out;
7940
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007941 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007942 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007943 ret = 1;
7944out:
7945 /* SSL_get_peer_certificate, it increase X509 * ref count */
7946 if (cert_peer && crt)
7947 X509_free(crt);
7948 return ret;
7949}
7950
Emeric Brunba841a12014-04-30 17:05:08 +02007951/* binary, returns serial of certificate in a binary chunk.
7952 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7953 * should be use.
7954 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007955static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007956smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007957{
Emeric Brunba841a12014-04-30 17:05:08 +02007958 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007959 X509 *crt = NULL;
7960 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007961 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007962 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007963 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007964
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007965 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007966 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007967 return 0;
7968
Olivier Houchard66ab4982019-02-26 18:37:15 +01007969 ctx = conn->xprt_ctx;
7970
Willy Tarreau911db9b2020-01-23 16:27:54 +01007971 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007972 smp->flags |= SMP_F_MAY_CHANGE;
7973 return 0;
7974 }
7975
Emeric Brunba841a12014-04-30 17:05:08 +02007976 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007977 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007978 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007979 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007980
Willy Tarreau8d598402012-10-22 17:58:39 +02007981 if (!crt)
7982 goto out;
7983
Willy Tarreau47ca5452012-12-23 20:22:19 +01007984 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007985 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7986 goto out;
7987
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007988 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007989 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007990 ret = 1;
7991out:
Emeric Brunba841a12014-04-30 17:05:08 +02007992 /* SSL_get_peer_certificate, it increase X509 * ref count */
7993 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007994 X509_free(crt);
7995 return ret;
7996}
Emeric Brune64aef12012-09-21 13:15:06 +02007997
Emeric Brunba841a12014-04-30 17:05:08 +02007998/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7999 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8000 * should be use.
8001 */
James Votha051b4a2013-05-14 20:37:59 +02008002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008003smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02008004{
Emeric Brunba841a12014-04-30 17:05:08 +02008005 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02008006 X509 *crt = NULL;
8007 const EVP_MD *digest;
8008 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01008009 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008010 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008011 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008012 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02008013
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008014 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008015 if (!conn || conn->xprt != &ssl_sock)
8016 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008017 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008018
Willy Tarreau911db9b2020-01-23 16:27:54 +01008019 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008020 smp->flags |= SMP_F_MAY_CHANGE;
8021 return 0;
8022 }
8023
Emeric Brunba841a12014-04-30 17:05:08 +02008024 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008025 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008026 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008027 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008028 if (!crt)
8029 goto out;
8030
8031 smp_trash = get_trash_chunk();
8032 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008033 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8034 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008035 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008036 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008037 ret = 1;
8038out:
Emeric Brunba841a12014-04-30 17:05:08 +02008039 /* SSL_get_peer_certificate, it increase X509 * ref count */
8040 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008041 X509_free(crt);
8042 return ret;
8043}
8044
Emeric Brunba841a12014-04-30 17:05:08 +02008045/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8046 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8047 * should be use.
8048 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008050smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008051{
Emeric Brunba841a12014-04-30 17:05:08 +02008052 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008053 X509 *crt = NULL;
8054 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008055 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008056 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008057 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008058
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008059 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008060 if (!conn || conn->xprt != &ssl_sock)
8061 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008062 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008063
Willy Tarreau911db9b2020-01-23 16:27:54 +01008064 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008065 smp->flags |= SMP_F_MAY_CHANGE;
8066 return 0;
8067 }
8068
Emeric Brunba841a12014-04-30 17:05:08 +02008069 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008070 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008071 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008072 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008073 if (!crt)
8074 goto out;
8075
Willy Tarreau47ca5452012-12-23 20:22:19 +01008076 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008077 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008078 goto out;
8079
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008080 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008081 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008082 ret = 1;
8083out:
Emeric Brunba841a12014-04-30 17:05:08 +02008084 /* SSL_get_peer_certificate, it increase X509 * ref count */
8085 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008086 X509_free(crt);
8087 return ret;
8088}
8089
Emeric Brunba841a12014-04-30 17:05:08 +02008090/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8091 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8092 * should be use.
8093 */
Emeric Brun87855892012-10-17 17:39:35 +02008094static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008095smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008096{
Emeric Brunba841a12014-04-30 17:05:08 +02008097 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008098 X509 *crt = NULL;
8099 X509_NAME *name;
8100 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008101 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008102 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008103 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008104
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008105 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008106 if (!conn || conn->xprt != &ssl_sock)
8107 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008108 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008109
Willy Tarreau911db9b2020-01-23 16:27:54 +01008110 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008111 smp->flags |= SMP_F_MAY_CHANGE;
8112 return 0;
8113 }
8114
Emeric Brunba841a12014-04-30 17:05:08 +02008115 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008116 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008117 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008118 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008119 if (!crt)
8120 goto out;
8121
8122 name = X509_get_issuer_name(crt);
8123 if (!name)
8124 goto out;
8125
Willy Tarreau47ca5452012-12-23 20:22:19 +01008126 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008127 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008128 int pos = 1;
8129
8130 if (args[1].type == ARGT_SINT)
8131 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008132
8133 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8134 goto out;
8135 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008136 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8137 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8138 goto out;
8139 }
Emeric Brun87855892012-10-17 17:39:35 +02008140 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8141 goto out;
8142
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008143 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008144 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008145 ret = 1;
8146out:
Emeric Brunba841a12014-04-30 17:05:08 +02008147 /* SSL_get_peer_certificate, it increase X509 * ref count */
8148 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008149 X509_free(crt);
8150 return ret;
8151}
8152
Emeric Brunba841a12014-04-30 17:05:08 +02008153/* string, returns notbefore date in ASN1_UTCTIME format.
8154 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8155 * should be use.
8156 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008157static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008158smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008159{
Emeric Brunba841a12014-04-30 17:05:08 +02008160 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008161 X509 *crt = NULL;
8162 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008163 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008164 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008165 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008166
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008167 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008168 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008169 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008170 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008171
Willy Tarreau911db9b2020-01-23 16:27:54 +01008172 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008173 smp->flags |= SMP_F_MAY_CHANGE;
8174 return 0;
8175 }
8176
Emeric Brunba841a12014-04-30 17:05:08 +02008177 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008178 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008179 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008180 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008181 if (!crt)
8182 goto out;
8183
Willy Tarreau47ca5452012-12-23 20:22:19 +01008184 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008185 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008186 goto out;
8187
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008188 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008189 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008190 ret = 1;
8191out:
Emeric Brunba841a12014-04-30 17:05:08 +02008192 /* SSL_get_peer_certificate, it increase X509 * ref count */
8193 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008194 X509_free(crt);
8195 return ret;
8196}
8197
Emeric Brunba841a12014-04-30 17:05:08 +02008198/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8199 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8200 * should be use.
8201 */
Emeric Brun87855892012-10-17 17:39:35 +02008202static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008203smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008204{
Emeric Brunba841a12014-04-30 17:05:08 +02008205 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008206 X509 *crt = NULL;
8207 X509_NAME *name;
8208 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008209 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008210 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008211 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008212
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008213 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008214 if (!conn || conn->xprt != &ssl_sock)
8215 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008216 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008217
Willy Tarreau911db9b2020-01-23 16:27:54 +01008218 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008219 smp->flags |= SMP_F_MAY_CHANGE;
8220 return 0;
8221 }
8222
Emeric Brunba841a12014-04-30 17:05:08 +02008223 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008224 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008225 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008226 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008227 if (!crt)
8228 goto out;
8229
8230 name = X509_get_subject_name(crt);
8231 if (!name)
8232 goto out;
8233
Willy Tarreau47ca5452012-12-23 20:22:19 +01008234 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008235 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008236 int pos = 1;
8237
8238 if (args[1].type == ARGT_SINT)
8239 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008240
8241 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8242 goto out;
8243 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008244 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8245 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8246 goto out;
8247 }
Emeric Brun87855892012-10-17 17:39:35 +02008248 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8249 goto out;
8250
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008251 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008252 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008253 ret = 1;
8254out:
Emeric Brunba841a12014-04-30 17:05:08 +02008255 /* SSL_get_peer_certificate, it increase X509 * ref count */
8256 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008257 X509_free(crt);
8258 return ret;
8259}
Emeric Brun9143d372012-12-20 15:44:16 +01008260
8261/* integer, returns true if current session use a client certificate */
8262static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008263smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008264{
8265 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008266 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008267 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008268
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008269 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008270 if (!conn || conn->xprt != &ssl_sock)
8271 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008272 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008273
Willy Tarreau911db9b2020-01-23 16:27:54 +01008274 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008275 smp->flags |= SMP_F_MAY_CHANGE;
8276 return 0;
8277 }
8278
8279 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008280 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008281 if (crt) {
8282 X509_free(crt);
8283 }
8284
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008285 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008286 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008287 return 1;
8288}
8289
Emeric Brunba841a12014-04-30 17:05:08 +02008290/* integer, returns the certificate version
8291 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8292 * should be use.
8293 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008295smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008296{
Emeric Brunba841a12014-04-30 17:05:08 +02008297 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008298 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008299 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008300 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008301
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008302 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008303 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008304 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008305 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008306
Willy Tarreau911db9b2020-01-23 16:27:54 +01008307 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008308 smp->flags |= SMP_F_MAY_CHANGE;
8309 return 0;
8310 }
8311
Emeric Brunba841a12014-04-30 17:05:08 +02008312 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008313 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008314 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008315 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008316 if (!crt)
8317 return 0;
8318
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008319 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008320 /* SSL_get_peer_certificate increase X509 * ref count */
8321 if (cert_peer)
8322 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008323 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008324
8325 return 1;
8326}
8327
Emeric Brunba841a12014-04-30 17:05:08 +02008328/* string, returns the certificate's signature algorithm.
8329 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8330 * should be use.
8331 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008333smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008334{
Emeric Brunba841a12014-04-30 17:05:08 +02008335 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008336 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008337 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008338 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008339 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008340 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008341
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008342 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008343 if (!conn || conn->xprt != &ssl_sock)
8344 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008345 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008346
Willy Tarreau911db9b2020-01-23 16:27:54 +01008347 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008348 smp->flags |= SMP_F_MAY_CHANGE;
8349 return 0;
8350 }
8351
Emeric Brunba841a12014-04-30 17:05:08 +02008352 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008353 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008354 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008355 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008356 if (!crt)
8357 return 0;
8358
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008359 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8360 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008361
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008362 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8363 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008364 /* SSL_get_peer_certificate increase X509 * ref count */
8365 if (cert_peer)
8366 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008367 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008368 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008369
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008370 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008371 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008372 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008373 /* SSL_get_peer_certificate increase X509 * ref count */
8374 if (cert_peer)
8375 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008376
8377 return 1;
8378}
8379
Emeric Brunba841a12014-04-30 17:05:08 +02008380/* string, returns the certificate's key algorithm.
8381 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8382 * should be use.
8383 */
Emeric Brun521a0112012-10-22 12:22:55 +02008384static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008385smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008386{
Emeric Brunba841a12014-04-30 17:05:08 +02008387 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008388 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008389 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008390 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008391 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008392 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008393
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008394 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008395 if (!conn || conn->xprt != &ssl_sock)
8396 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008397 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008398
Willy Tarreau911db9b2020-01-23 16:27:54 +01008399 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008400 smp->flags |= SMP_F_MAY_CHANGE;
8401 return 0;
8402 }
8403
Emeric Brunba841a12014-04-30 17:05:08 +02008404 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008405 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008406 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008407 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008408 if (!crt)
8409 return 0;
8410
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008411 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8412 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008413
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008414 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8415 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008416 /* SSL_get_peer_certificate increase X509 * ref count */
8417 if (cert_peer)
8418 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008419 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008420 }
Emeric Brun521a0112012-10-22 12:22:55 +02008421
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008422 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008423 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008424 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008425 if (cert_peer)
8426 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008427
8428 return 1;
8429}
8430
Emeric Brun645ae792014-04-30 14:21:06 +02008431/* boolean, returns true if front conn. transport layer is SSL.
8432 * This function is also usable on backend conn if the fetch keyword 5th
8433 * char is 'b'.
8434 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008435static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008436smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008437{
Emeric Bruneb8def92018-02-19 15:59:48 +01008438 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8439 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008440
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008441 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008442 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008443 return 1;
8444}
8445
Emeric Brun2525b6b2012-10-18 15:59:43 +02008446/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008448smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008449{
8450#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008451 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008452 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008454 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008455 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008456 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008457 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008458 return 1;
8459#else
8460 return 0;
8461#endif
8462}
8463
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008464/* boolean, returns true if client session has been resumed.
8465 * This function is also usable on backend conn if the fetch keyword 5th
8466 * char is 'b'.
8467 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008468static int
8469smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8470{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008471 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8472 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008473 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008474
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008475
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008476 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008477 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008478 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008479 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008480 return 1;
8481}
8482
Emeric Brun645ae792014-04-30 14:21:06 +02008483/* string, returns the used cipher if front conn. transport layer is SSL.
8484 * This function is also usable on backend conn if the fetch keyword 5th
8485 * char is 'b'.
8486 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008487static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008488smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008489{
Emeric Bruneb8def92018-02-19 15:59:48 +01008490 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8491 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008492 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008493
Willy Tarreaube508f12016-03-10 11:47:01 +01008494 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008495 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008496 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008497 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008498
Olivier Houchard66ab4982019-02-26 18:37:15 +01008499 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008500 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008501 return 0;
8502
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008503 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008504 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008505 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008506
8507 return 1;
8508}
8509
Emeric Brun645ae792014-04-30 14:21:06 +02008510/* integer, returns the algoritm's keysize if front conn. transport layer
8511 * is SSL.
8512 * This function is also usable on backend conn if the fetch keyword 5th
8513 * char is 'b'.
8514 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008515static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008516smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008517{
Emeric Bruneb8def92018-02-19 15:59:48 +01008518 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8519 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008520 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008521 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008522
Emeric Brun589fcad2012-10-16 14:13:26 +02008523 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008524 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008525 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008526 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008527
Olivier Houchard66ab4982019-02-26 18:37:15 +01008528 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008529 return 0;
8530
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008531 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008532 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008533
8534 return 1;
8535}
8536
Emeric Brun645ae792014-04-30 14:21:06 +02008537/* integer, returns the used keysize if front conn. transport layer is SSL.
8538 * This function is also usable on backend conn if the fetch keyword 5th
8539 * char is 'b'.
8540 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008541static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008542smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008543{
Emeric Bruneb8def92018-02-19 15:59:48 +01008544 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8545 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008546 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008547
Emeric Brun589fcad2012-10-16 14:13:26 +02008548 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008549 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8550 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008551 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008552
Olivier Houchard66ab4982019-02-26 18:37:15 +01008553 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008554 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008555 return 0;
8556
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008557 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008558
8559 return 1;
8560}
8561
Bernard Spil13c53f82018-02-15 13:34:58 +01008562#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008563static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008564smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008565{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008566 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008567 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008568 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008569
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008570 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008571 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008572
Olivier Houchard6b77f492018-11-22 18:18:29 +01008573 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8574 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008575 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8576 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008577 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008578
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008579 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008580 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008581 (const unsigned char **)&smp->data.u.str.area,
8582 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008583
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008584 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008585 return 0;
8586
Willy Tarreau105599c2020-02-25 08:59:23 +01008587 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008588 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008589}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008590#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008591
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008592#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008593static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008594smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008595{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008596 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008597 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008598 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008599
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008600 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008601 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008602
Olivier Houchard6b77f492018-11-22 18:18:29 +01008603 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8604 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8605
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008606 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008607 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008608 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008609
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008610 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008611 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008612 (const unsigned char **)&smp->data.u.str.area,
8613 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008614
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008615 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008616 return 0;
8617
Willy Tarreau105599c2020-02-25 08:59:23 +01008618 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008619 return 1;
8620}
8621#endif
8622
Emeric Brun645ae792014-04-30 14:21:06 +02008623/* string, returns the used protocol if front conn. transport layer is SSL.
8624 * This function is also usable on backend conn if the fetch keyword 5th
8625 * char is 'b'.
8626 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008627static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008628smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008629{
Emeric Bruneb8def92018-02-19 15:59:48 +01008630 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8631 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008632 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008633
Emeric Brun589fcad2012-10-16 14:13:26 +02008634 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008635 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8636 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008637 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008638
Olivier Houchard66ab4982019-02-26 18:37:15 +01008639 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008640 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008641 return 0;
8642
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008643 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008644 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008645 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008646
8647 return 1;
8648}
8649
Willy Tarreau87b09662015-04-03 00:22:06 +02008650/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008651 * This function is also usable on backend conn if the fetch keyword 5th
8652 * char is 'b'.
8653 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008654#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008655static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008656smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008657{
Emeric Bruneb8def92018-02-19 15:59:48 +01008658 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8659 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008660 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008661 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008662 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008663
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008664 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008665 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008666
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008667 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8668 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008669 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008670
Olivier Houchard66ab4982019-02-26 18:37:15 +01008671 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008672 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008673 return 0;
8674
Willy Tarreau105599c2020-02-25 08:59:23 +01008675 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008676 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008677 return 0;
8678
Willy Tarreau105599c2020-02-25 08:59:23 +01008679 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008680 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008681}
Patrick Hemmer41966772018-04-28 19:15:48 -04008682#endif
8683
Emeric Brunfe68f682012-10-16 14:59:28 +02008684
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008685#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008686static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008687smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8688{
8689 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8690 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8691 struct buffer *data;
8692 struct ssl_sock_ctx *ctx;
8693
8694 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8695 return 0;
8696 ctx = conn->xprt_ctx;
8697
8698 data = get_trash_chunk();
8699 if (kw[7] == 'c')
8700 data->data = SSL_get_client_random(ctx->ssl,
8701 (unsigned char *) data->area,
8702 data->size);
8703 else
8704 data->data = SSL_get_server_random(ctx->ssl,
8705 (unsigned char *) data->area,
8706 data->size);
8707 if (!data->data)
8708 return 0;
8709
8710 smp->flags = 0;
8711 smp->data.type = SMP_T_BIN;
8712 smp->data.u.str = *data;
8713
8714 return 1;
8715}
8716
8717static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008718smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8719{
8720 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8721 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8722 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008723 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008724 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008725
8726 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8727 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008728 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008729
Olivier Houchard66ab4982019-02-26 18:37:15 +01008730 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008731 if (!ssl_sess)
8732 return 0;
8733
8734 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008735 data->data = SSL_SESSION_get_master_key(ssl_sess,
8736 (unsigned char *) data->area,
8737 data->size);
8738 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008739 return 0;
8740
8741 smp->flags = 0;
8742 smp->data.type = SMP_T_BIN;
8743 smp->data.u.str = *data;
8744
8745 return 1;
8746}
8747#endif
8748
Patrick Hemmer41966772018-04-28 19:15:48 -04008749#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008750static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008751smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008752{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008753 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008754 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008755
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008756 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008757 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008758
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008759 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008760 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8761 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008762 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008763
Olivier Houchard66ab4982019-02-26 18:37:15 +01008764 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008765 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008766 return 0;
8767
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008768 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008769 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008770}
Patrick Hemmer41966772018-04-28 19:15:48 -04008771#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008772
David Sc1ad52e2014-04-08 18:48:47 -04008773static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008774smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8775{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008776 struct connection *conn;
8777 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008778 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008779
8780 conn = objt_conn(smp->sess->origin);
8781 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8782 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008783 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008784
Olivier Houchard66ab4982019-02-26 18:37:15 +01008785 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008786 if (!capture)
8787 return 0;
8788
8789 smp->flags = SMP_F_CONST;
8790 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008791 smp->data.u.str.area = capture->ciphersuite;
8792 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008793 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008794}
8795
8796static int
8797smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8798{
Willy Tarreau83061a82018-07-13 11:56:34 +02008799 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008800
8801 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8802 return 0;
8803
8804 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008805 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008806 smp->data.type = SMP_T_BIN;
8807 smp->data.u.str = *data;
8808 return 1;
8809}
8810
8811static int
8812smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8813{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008814 struct connection *conn;
8815 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008816 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008817
8818 conn = objt_conn(smp->sess->origin);
8819 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8820 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008821 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008822
Olivier Houchard66ab4982019-02-26 18:37:15 +01008823 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008824 if (!capture)
8825 return 0;
8826
8827 smp->data.type = SMP_T_SINT;
8828 smp->data.u.sint = capture->xxh64;
8829 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008830}
8831
8832static int
8833smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8834{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008835#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008836 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008837 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008838
8839 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8840 return 0;
8841
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008842 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008843 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008844 const char *str;
8845 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008846 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008847 uint16_t id = (bin[0] << 8) | bin[1];
8848#if defined(OPENSSL_IS_BORINGSSL)
8849 cipher = SSL_get_cipher_by_value(id);
8850#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008851 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008852 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8853 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008854#endif
8855 str = SSL_CIPHER_get_name(cipher);
8856 if (!str || strcmp(str, "(NONE)") == 0)
8857 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008858 else
8859 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8860 }
8861 smp->data.type = SMP_T_STR;
8862 smp->data.u.str = *data;
8863 return 1;
8864#else
8865 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8866#endif
8867}
8868
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008869#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008871smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008872{
Emeric Bruneb8def92018-02-19 15:59:48 +01008873 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8874 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008875 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008876 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008877 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008878
8879 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008880 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8881 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008882 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008883
Willy Tarreau911db9b2020-01-23 16:27:54 +01008884 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008885 smp->flags |= SMP_F_MAY_CHANGE;
8886 return 0;
8887 }
8888
8889 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008890 if (!SSL_session_reused(ctx->ssl))
8891 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008892 finished_trash->area,
8893 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008894 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008895 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008896 finished_trash->area,
8897 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008898
8899 if (!finished_len)
8900 return 0;
8901
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008902 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008903 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008904 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008905
8906 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008907}
Patrick Hemmer41966772018-04-28 19:15:48 -04008908#endif
David Sc1ad52e2014-04-08 18:48:47 -04008909
Emeric Brun2525b6b2012-10-18 15:59:43 +02008910/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008911static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008912smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008913{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008914 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008915 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008916
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008917 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008918 if (!conn || conn->xprt != &ssl_sock)
8919 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008920 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008921
Willy Tarreau911db9b2020-01-23 16:27:54 +01008922 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008923 smp->flags = SMP_F_MAY_CHANGE;
8924 return 0;
8925 }
8926
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008927 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008928 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008929 smp->flags = 0;
8930
8931 return 1;
8932}
8933
Emeric Brun2525b6b2012-10-18 15:59:43 +02008934/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008935static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008936smp_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 +02008937{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008938 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008939 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008940
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008941 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008942 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008943 return 0;
8944
Willy Tarreau911db9b2020-01-23 16:27:54 +01008945 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008946 smp->flags = SMP_F_MAY_CHANGE;
8947 return 0;
8948 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008949 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008950
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008951 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008952 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008953 smp->flags = 0;
8954
8955 return 1;
8956}
8957
Emeric Brun2525b6b2012-10-18 15:59:43 +02008958/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008959static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008960smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008961{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008962 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008963 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008964
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008965 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008966 if (!conn || conn->xprt != &ssl_sock)
8967 return 0;
8968
Willy Tarreau911db9b2020-01-23 16:27:54 +01008969 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008970 smp->flags = SMP_F_MAY_CHANGE;
8971 return 0;
8972 }
8973
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008974 ctx = conn->xprt_ctx;
8975
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008976 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008977 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008978 smp->flags = 0;
8979
8980 return 1;
8981}
8982
Emeric Brun2525b6b2012-10-18 15:59:43 +02008983/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008984static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008985smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008986{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008987 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008988 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008989
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008990 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008991 if (!conn || conn->xprt != &ssl_sock)
8992 return 0;
8993
Willy Tarreau911db9b2020-01-23 16:27:54 +01008994 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008995 smp->flags = SMP_F_MAY_CHANGE;
8996 return 0;
8997 }
8998
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008999 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009000 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009001 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009002
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009003 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009004 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009005 smp->flags = 0;
9006
9007 return 1;
9008}
9009
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009010/* for ca-file and ca-verify-file */
9011static 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 +02009012{
9013 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009014 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009015 return ERR_ALERT | ERR_FATAL;
9016 }
9017
Willy Tarreauef934602016-12-22 23:12:01 +01009018 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009019 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009020 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009021 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009022
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009023 if (!ssl_store_load_locations_file(*ca_file_p)) {
9024 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009025 return ERR_ALERT | ERR_FATAL;
9026 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009027 return 0;
9028}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009029
9030/* parse the "ca-file" bind keyword */
9031static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9032{
9033 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9034}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009035static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9036{
9037 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9038}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009039
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009040/* parse the "ca-verify-file" bind keyword */
9041static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9042{
9043 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9044}
9045static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9046{
9047 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9048}
9049
Christopher Faulet31af49d2015-06-09 17:29:50 +02009050/* parse the "ca-sign-file" bind keyword */
9051static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9052{
9053 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009054 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009055 return ERR_ALERT | ERR_FATAL;
9056 }
9057
Willy Tarreauef934602016-12-22 23:12:01 +01009058 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9059 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009060 else
9061 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9062
9063 return 0;
9064}
9065
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009066/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009067static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9068{
9069 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009070 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009071 return ERR_ALERT | ERR_FATAL;
9072 }
9073 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9074 return 0;
9075}
9076
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009077/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009078static 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 +02009079{
9080 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009081 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009082 return ERR_ALERT | ERR_FATAL;
9083 }
9084
Emeric Brun76d88952012-10-05 15:47:31 +02009085 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009086 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009087 return 0;
9088}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009089static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9090{
9091 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9092}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009093
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009094#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009095/* parse the "ciphersuites" bind keyword */
9096static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9097{
9098 if (!*args[cur_arg + 1]) {
9099 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9100 return ERR_ALERT | ERR_FATAL;
9101 }
9102
9103 free(conf->ciphersuites);
9104 conf->ciphersuites = strdup(args[cur_arg + 1]);
9105 return 0;
9106}
9107static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9108{
9109 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9110}
9111#endif
9112
Willy Tarreaubbc91962019-10-16 16:42:19 +02009113/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009114static 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 +02009115{
Willy Tarreau38011032013-08-13 16:59:39 +02009116 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009117
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009118 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009119 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009120 return ERR_ALERT | ERR_FATAL;
9121 }
9122
Willy Tarreauef934602016-12-22 23:12:01 +01009123 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9124 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009125 memprintf(err, "'%s' : path too long", args[cur_arg]);
9126 return ERR_ALERT | ERR_FATAL;
9127 }
Willy Tarreauef934602016-12-22 23:12:01 +01009128 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009129 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009130 }
9131
Willy Tarreaubbc91962019-10-16 16:42:19 +02009132 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009133}
9134
Willy Tarreaubbc91962019-10-16 16:42:19 +02009135/* 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 +01009136static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9137{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009138 int err_code;
9139
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009140 if (!*args[cur_arg + 1]) {
9141 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9142 return ERR_ALERT | ERR_FATAL;
9143 }
9144
William Lallemand6be66ec2020-03-06 22:26:32 +01009145 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009146 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009147 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009148
Willy Tarreaubbc91962019-10-16 16:42:19 +02009149 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009150}
9151
Emeric Brunfb510ea2012-10-05 12:00:26 +02009152/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009153static 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 +02009154{
Emeric Brun051cdab2012-10-02 19:25:50 +02009155#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009156 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009157 return ERR_ALERT | ERR_FATAL;
9158#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009159 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009160 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009161 return ERR_ALERT | ERR_FATAL;
9162 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009163
Willy Tarreauef934602016-12-22 23:12:01 +01009164 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9165 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009166 else
9167 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009168
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009169 if (!ssl_store_load_locations_file(conf->crl_file)) {
9170 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9171 return ERR_ALERT | ERR_FATAL;
9172 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009173 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009174#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009175}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009176static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9177{
9178 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9179}
Emeric Brun2b58d042012-09-20 17:10:03 +02009180
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009181/* parse the "curves" bind keyword keyword */
9182static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9183{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009184#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009185 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009186 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009187 return ERR_ALERT | ERR_FATAL;
9188 }
9189 conf->curves = strdup(args[cur_arg + 1]);
9190 return 0;
9191#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009192 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009193 return ERR_ALERT | ERR_FATAL;
9194#endif
9195}
9196static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9197{
9198 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9199}
9200
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009201/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009202static 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 +02009203{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009204#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009205 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009206 return ERR_ALERT | ERR_FATAL;
9207#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009208 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 +02009209 return ERR_ALERT | ERR_FATAL;
9210#else
9211 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009212 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009213 return ERR_ALERT | ERR_FATAL;
9214 }
9215
9216 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009217
9218 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009219#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009220}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009221static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9222{
9223 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9224}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009225
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009226/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009227static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9228{
9229 int code;
9230 char *p = args[cur_arg + 1];
9231 unsigned long long *ignerr = &conf->crt_ignerr;
9232
9233 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009234 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009235 return ERR_ALERT | ERR_FATAL;
9236 }
9237
9238 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9239 ignerr = &conf->ca_ignerr;
9240
9241 if (strcmp(p, "all") == 0) {
9242 *ignerr = ~0ULL;
9243 return 0;
9244 }
9245
9246 while (p) {
9247 code = atoi(p);
9248 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009249 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9250 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009251 return ERR_ALERT | ERR_FATAL;
9252 }
9253 *ignerr |= 1ULL << code;
9254 p = strchr(p, ',');
9255 if (p)
9256 p++;
9257 }
9258
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009259 return 0;
9260}
9261
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009262/* parse tls_method_options "no-xxx" and "force-xxx" */
9263static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009264{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009265 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009266 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009267 p = strchr(arg, '-');
9268 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009269 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009270 p++;
9271 if (!strcmp(p, "sslv3"))
9272 v = CONF_SSLV3;
9273 else if (!strcmp(p, "tlsv10"))
9274 v = CONF_TLSV10;
9275 else if (!strcmp(p, "tlsv11"))
9276 v = CONF_TLSV11;
9277 else if (!strcmp(p, "tlsv12"))
9278 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009279 else if (!strcmp(p, "tlsv13"))
9280 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009281 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009282 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009283 if (!strncmp(arg, "no-", 3))
9284 methods->flags |= methodVersions[v].flag;
9285 else if (!strncmp(arg, "force-", 6))
9286 methods->min = methods->max = v;
9287 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009288 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009289 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009290 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009291 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009292 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009293}
9294
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009295static 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 +02009296{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009297 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009298}
9299
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009300static 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 +02009301{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009302 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9303}
9304
9305/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9306static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9307{
9308 uint16_t i, v = 0;
9309 char *argv = args[cur_arg + 1];
9310 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009311 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009312 return ERR_ALERT | ERR_FATAL;
9313 }
9314 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9315 if (!strcmp(argv, methodVersions[i].name))
9316 v = i;
9317 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009318 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009319 return ERR_ALERT | ERR_FATAL;
9320 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009321 if (!strcmp("ssl-min-ver", args[cur_arg]))
9322 methods->min = v;
9323 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9324 methods->max = v;
9325 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009326 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009327 return ERR_ALERT | ERR_FATAL;
9328 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009329 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009330}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009331
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009332static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9333{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009334#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009335 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 +02009336#endif
9337 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9338}
9339
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009340static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9341{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009342 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009343}
9344
9345static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9346{
9347 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9348}
9349
Emeric Brun2d0c4822012-10-02 13:45:20 +02009350/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009351static 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 +02009352{
Emeric Brun89675492012-10-05 13:48:26 +02009353 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009354 return 0;
9355}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009356
Olivier Houchardc2aae742017-09-22 18:26:28 +02009357/* parse the "allow-0rtt" bind keyword */
9358static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9359{
9360 conf->early_data = 1;
9361 return 0;
9362}
9363
9364static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9365{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009366 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009367 return 0;
9368}
9369
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009370/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009371static 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 +02009372{
Bernard Spil13c53f82018-02-15 13:34:58 +01009373#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009374 char *p1, *p2;
9375
9376 if (!*args[cur_arg + 1]) {
9377 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9378 return ERR_ALERT | ERR_FATAL;
9379 }
9380
9381 free(conf->npn_str);
9382
Willy Tarreau3724da12016-02-12 17:11:12 +01009383 /* the NPN string is built as a suite of (<len> <name>)*,
9384 * so we reuse each comma to store the next <len> and need
9385 * one more for the end of the string.
9386 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009387 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009388 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009389 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9390
9391 /* replace commas with the name length */
9392 p1 = conf->npn_str;
9393 p2 = p1 + 1;
9394 while (1) {
9395 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9396 if (!p2)
9397 p2 = p1 + 1 + strlen(p1 + 1);
9398
9399 if (p2 - (p1 + 1) > 255) {
9400 *p2 = '\0';
9401 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9402 return ERR_ALERT | ERR_FATAL;
9403 }
9404
9405 *p1 = p2 - (p1 + 1);
9406 p1 = p2;
9407
9408 if (!*p2)
9409 break;
9410
9411 *(p2++) = '\0';
9412 }
9413 return 0;
9414#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009415 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009416 return ERR_ALERT | ERR_FATAL;
9417#endif
9418}
9419
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009420static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9421{
9422 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9423}
9424
Willy Tarreauab861d32013-04-02 02:30:41 +02009425/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009426static 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 +02009427{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009428#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009429 char *p1, *p2;
9430
9431 if (!*args[cur_arg + 1]) {
9432 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9433 return ERR_ALERT | ERR_FATAL;
9434 }
9435
9436 free(conf->alpn_str);
9437
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009438 /* the ALPN string is built as a suite of (<len> <name>)*,
9439 * so we reuse each comma to store the next <len> and need
9440 * one more for the end of the string.
9441 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009442 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009443 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009444 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9445
9446 /* replace commas with the name length */
9447 p1 = conf->alpn_str;
9448 p2 = p1 + 1;
9449 while (1) {
9450 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9451 if (!p2)
9452 p2 = p1 + 1 + strlen(p1 + 1);
9453
9454 if (p2 - (p1 + 1) > 255) {
9455 *p2 = '\0';
9456 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9457 return ERR_ALERT | ERR_FATAL;
9458 }
9459
9460 *p1 = p2 - (p1 + 1);
9461 p1 = p2;
9462
9463 if (!*p2)
9464 break;
9465
9466 *(p2++) = '\0';
9467 }
9468 return 0;
9469#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009470 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009471 return ERR_ALERT | ERR_FATAL;
9472#endif
9473}
9474
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009475static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9476{
9477 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9478}
9479
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009480/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009481static 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 +02009482{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009483 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009484 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009485
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009486 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9487 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009488#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009489 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9490 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9491#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009492 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009493 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9494 if (!conf->ssl_conf.ssl_methods.min)
9495 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9496 if (!conf->ssl_conf.ssl_methods.max)
9497 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009498
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009499 return 0;
9500}
9501
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009502/* parse the "prefer-client-ciphers" bind keyword */
9503static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9504{
9505 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9506 return 0;
9507}
9508
Christopher Faulet31af49d2015-06-09 17:29:50 +02009509/* parse the "generate-certificates" bind keyword */
9510static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9511{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009512#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009513 conf->generate_certs = 1;
9514#else
9515 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9516 err && *err ? *err : "");
9517#endif
9518 return 0;
9519}
9520
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009521/* parse the "strict-sni" bind keyword */
9522static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9523{
9524 conf->strict_sni = 1;
9525 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009526}
9527
9528/* parse the "tls-ticket-keys" bind keyword */
9529static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9530{
9531#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009532 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009533 int i = 0;
9534 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009535 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009536
9537 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009538 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009539 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009540 }
9541
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009542 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009543 if (keys_ref) {
9544 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009545 conf->keys_ref = keys_ref;
9546 return 0;
9547 }
9548
Christopher Faulete566f3d2019-10-21 09:55:49 +02009549 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009550 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009551 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009552 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009553 }
9554
Emeric Brun9e754772019-01-10 17:51:55 +01009555 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009556 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009557 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009558 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009559 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009560
9561 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009562 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009563 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009564 }
9565
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009566 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009567 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009568 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009569 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009570 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009571
Emeric Brun9e754772019-01-10 17:51:55 +01009572 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009573 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9574 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009575 int dec_size;
9576
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009577 /* Strip newline characters from the end */
9578 if(thisline[len - 1] == '\n')
9579 thisline[--len] = 0;
9580
9581 if(thisline[len - 1] == '\r')
9582 thisline[--len] = 0;
9583
Emeric Brun9e754772019-01-10 17:51:55 +01009584 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9585 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009586 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009587 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009588 }
Emeric Brun9e754772019-01-10 17:51:55 +01009589 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9590 keys_ref->key_size_bits = 128;
9591 }
9592 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9593 keys_ref->key_size_bits = 256;
9594 }
9595 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9596 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9597 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009598 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009599 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009600 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009601 i++;
9602 }
9603
9604 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009605 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 +02009606 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009607 }
9608
9609 fclose(f);
9610
9611 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009612 i -= 2;
9613 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009614 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009615 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009616 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009617 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009618
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009619 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9620
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009621 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009622
9623 fail:
9624 if (f)
9625 fclose(f);
9626 if (keys_ref) {
9627 free(keys_ref->filename);
9628 free(keys_ref->tlskeys);
9629 free(keys_ref);
9630 }
9631 return ERR_ALERT | ERR_FATAL;
9632
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009633#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009634 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009635 return ERR_ALERT | ERR_FATAL;
9636#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009637}
9638
Emeric Brund94b3fe2012-09-20 18:23:56 +02009639/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009640static 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 +02009641{
9642 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009643 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009644 return ERR_ALERT | ERR_FATAL;
9645 }
9646
9647 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009648 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009649 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009650 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009651 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009652 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009653 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009654 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9655 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009656 return ERR_ALERT | ERR_FATAL;
9657 }
9658
9659 return 0;
9660}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009661static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9662{
9663 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9664}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009665
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009666/* parse the "no-ca-names" bind keyword */
9667static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9668{
9669 conf->no_ca_names = 1;
9670 return 0;
9671}
9672static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9673{
9674 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9675}
9676
Willy Tarreau92faadf2012-10-10 23:04:25 +02009677/************** "server" keywords ****************/
9678
Olivier Houchardc7566002018-11-20 23:33:50 +01009679/* parse the "npn" bind keyword */
9680static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9681{
9682#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9683 char *p1, *p2;
9684
9685 if (!*args[*cur_arg + 1]) {
9686 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9687 return ERR_ALERT | ERR_FATAL;
9688 }
9689
9690 free(newsrv->ssl_ctx.npn_str);
9691
9692 /* the NPN string is built as a suite of (<len> <name>)*,
9693 * so we reuse each comma to store the next <len> and need
9694 * one more for the end of the string.
9695 */
9696 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9697 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9698 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9699 newsrv->ssl_ctx.npn_len);
9700
9701 /* replace commas with the name length */
9702 p1 = newsrv->ssl_ctx.npn_str;
9703 p2 = p1 + 1;
9704 while (1) {
9705 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9706 newsrv->ssl_ctx.npn_len - (p1 + 1));
9707 if (!p2)
9708 p2 = p1 + 1 + strlen(p1 + 1);
9709
9710 if (p2 - (p1 + 1) > 255) {
9711 *p2 = '\0';
9712 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9713 return ERR_ALERT | ERR_FATAL;
9714 }
9715
9716 *p1 = p2 - (p1 + 1);
9717 p1 = p2;
9718
9719 if (!*p2)
9720 break;
9721
9722 *(p2++) = '\0';
9723 }
9724 return 0;
9725#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009726 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009727 return ERR_ALERT | ERR_FATAL;
9728#endif
9729}
9730
Olivier Houchard92150142018-12-21 19:47:01 +01009731/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009732static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9733{
9734#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9735 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009736 char **alpn_str;
9737 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009738
Olivier Houchard92150142018-12-21 19:47:01 +01009739 if (*args[*cur_arg] == 'c') {
9740 alpn_str = &newsrv->check.alpn_str;
9741 alpn_len = &newsrv->check.alpn_len;
9742 } else {
9743 alpn_str = &newsrv->ssl_ctx.alpn_str;
9744 alpn_len = &newsrv->ssl_ctx.alpn_len;
9745
9746 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009747 if (!*args[*cur_arg + 1]) {
9748 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9749 return ERR_ALERT | ERR_FATAL;
9750 }
9751
Olivier Houchard92150142018-12-21 19:47:01 +01009752 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009753
9754 /* the ALPN string is built as a suite of (<len> <name>)*,
9755 * so we reuse each comma to store the next <len> and need
9756 * one more for the end of the string.
9757 */
Olivier Houchard92150142018-12-21 19:47:01 +01009758 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9759 *alpn_str = calloc(1, *alpn_len + 1);
9760 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009761
9762 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009763 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009764 p2 = p1 + 1;
9765 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009766 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009767 if (!p2)
9768 p2 = p1 + 1 + strlen(p1 + 1);
9769
9770 if (p2 - (p1 + 1) > 255) {
9771 *p2 = '\0';
9772 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9773 return ERR_ALERT | ERR_FATAL;
9774 }
9775
9776 *p1 = p2 - (p1 + 1);
9777 p1 = p2;
9778
9779 if (!*p2)
9780 break;
9781
9782 *(p2++) = '\0';
9783 }
9784 return 0;
9785#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009786 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009787 return ERR_ALERT | ERR_FATAL;
9788#endif
9789}
9790
Emeric Brunef42d922012-10-11 16:11:36 +02009791/* parse the "ca-file" server keyword */
9792static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9793{
9794 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009795 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009796 return ERR_ALERT | ERR_FATAL;
9797 }
9798
Willy Tarreauef934602016-12-22 23:12:01 +01009799 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9800 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009801 else
9802 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9803
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009804 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9805 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9806 return ERR_ALERT | ERR_FATAL;
9807 }
Emeric Brunef42d922012-10-11 16:11:36 +02009808 return 0;
9809}
9810
Olivier Houchard9130a962017-10-17 17:33:43 +02009811/* parse the "check-sni" server keyword */
9812static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9813{
9814 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009815 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009816 return ERR_ALERT | ERR_FATAL;
9817 }
9818
9819 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9820 if (!newsrv->check.sni) {
9821 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9822 return ERR_ALERT | ERR_FATAL;
9823 }
9824 return 0;
9825
9826}
9827
Willy Tarreau92faadf2012-10-10 23:04:25 +02009828/* parse the "check-ssl" server keyword */
9829static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9830{
9831 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009832 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9833 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009834#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009835 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9836 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9837#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009838 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009839 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9840 if (!newsrv->ssl_ctx.methods.min)
9841 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9842 if (!newsrv->ssl_ctx.methods.max)
9843 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9844
Willy Tarreau92faadf2012-10-10 23:04:25 +02009845 return 0;
9846}
9847
9848/* parse the "ciphers" server keyword */
9849static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9850{
9851 if (!*args[*cur_arg + 1]) {
9852 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9853 return ERR_ALERT | ERR_FATAL;
9854 }
9855
9856 free(newsrv->ssl_ctx.ciphers);
9857 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9858 return 0;
9859}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009860
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009861#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009862/* parse the "ciphersuites" server keyword */
9863static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9864{
9865 if (!*args[*cur_arg + 1]) {
9866 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9867 return ERR_ALERT | ERR_FATAL;
9868 }
9869
9870 free(newsrv->ssl_ctx.ciphersuites);
9871 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9872 return 0;
9873}
9874#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009875
Emeric Brunef42d922012-10-11 16:11:36 +02009876/* parse the "crl-file" server keyword */
9877static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9878{
9879#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009880 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009881 return ERR_ALERT | ERR_FATAL;
9882#else
9883 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009884 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009885 return ERR_ALERT | ERR_FATAL;
9886 }
9887
Willy Tarreauef934602016-12-22 23:12:01 +01009888 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9889 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009890 else
9891 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9892
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009893 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9894 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9895 return ERR_ALERT | ERR_FATAL;
9896 }
Emeric Brunef42d922012-10-11 16:11:36 +02009897 return 0;
9898#endif
9899}
9900
Emeric Bruna7aa3092012-10-26 12:58:00 +02009901/* parse the "crt" server keyword */
9902static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9903{
9904 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009905 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009906 return ERR_ALERT | ERR_FATAL;
9907 }
9908
Willy Tarreauef934602016-12-22 23:12:01 +01009909 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009910 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009911 else
9912 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9913
9914 return 0;
9915}
Emeric Brunef42d922012-10-11 16:11:36 +02009916
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009917/* parse the "no-check-ssl" server keyword */
9918static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9919{
9920 newsrv->check.use_ssl = 0;
9921 free(newsrv->ssl_ctx.ciphers);
9922 newsrv->ssl_ctx.ciphers = NULL;
9923 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9924 return 0;
9925}
9926
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009927/* parse the "no-send-proxy-v2-ssl" server keyword */
9928static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9929{
9930 newsrv->pp_opts &= ~SRV_PP_V2;
9931 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9932 return 0;
9933}
9934
9935/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9936static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9937{
9938 newsrv->pp_opts &= ~SRV_PP_V2;
9939 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9940 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9941 return 0;
9942}
9943
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009944/* parse the "no-ssl" server keyword */
9945static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9946{
9947 newsrv->use_ssl = 0;
9948 free(newsrv->ssl_ctx.ciphers);
9949 newsrv->ssl_ctx.ciphers = NULL;
9950 return 0;
9951}
9952
Olivier Houchard522eea72017-11-03 16:27:47 +01009953/* parse the "allow-0rtt" server keyword */
9954static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9955{
9956 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9957 return 0;
9958}
9959
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009960/* parse the "no-ssl-reuse" server keyword */
9961static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9962{
9963 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9964 return 0;
9965}
9966
Emeric Brunf9c5c472012-10-11 15:28:34 +02009967/* parse the "no-tls-tickets" server keyword */
9968static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9969{
9970 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9971 return 0;
9972}
David Safb76832014-05-08 23:42:08 -04009973/* parse the "send-proxy-v2-ssl" server keyword */
9974static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9975{
9976 newsrv->pp_opts |= SRV_PP_V2;
9977 newsrv->pp_opts |= SRV_PP_V2_SSL;
9978 return 0;
9979}
9980
9981/* parse the "send-proxy-v2-ssl-cn" server keyword */
9982static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9983{
9984 newsrv->pp_opts |= SRV_PP_V2;
9985 newsrv->pp_opts |= SRV_PP_V2_SSL;
9986 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9987 return 0;
9988}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009989
Willy Tarreau732eac42015-07-09 11:40:25 +02009990/* parse the "sni" server keyword */
9991static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9992{
9993#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9994 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9995 return ERR_ALERT | ERR_FATAL;
9996#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009997 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009998
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009999 arg = args[*cur_arg + 1];
10000 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +020010001 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
10002 return ERR_ALERT | ERR_FATAL;
10003 }
10004
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010005 free(newsrv->sni_expr);
10006 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010007
Willy Tarreau732eac42015-07-09 11:40:25 +020010008 return 0;
10009#endif
10010}
10011
Willy Tarreau92faadf2012-10-10 23:04:25 +020010012/* parse the "ssl" server keyword */
10013static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10014{
10015 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010016 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10017 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010018#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010019 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10020 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10021#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +020010022 return 0;
10023}
10024
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010025/* parse the "ssl-reuse" server keyword */
10026static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10027{
10028 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10029 return 0;
10030}
10031
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010032/* parse the "tls-tickets" server keyword */
10033static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10034{
10035 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10036 return 0;
10037}
10038
Emeric Brunef42d922012-10-11 16:11:36 +020010039/* parse the "verify" server keyword */
10040static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10041{
10042 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010043 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010044 return ERR_ALERT | ERR_FATAL;
10045 }
10046
10047 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010048 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010049 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010050 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010051 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010052 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10053 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010054 return ERR_ALERT | ERR_FATAL;
10055 }
10056
Evan Broderbe554312013-06-27 00:05:25 -070010057 return 0;
10058}
10059
10060/* parse the "verifyhost" server keyword */
10061static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10062{
10063 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010064 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010065 return ERR_ALERT | ERR_FATAL;
10066 }
10067
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010068 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010069 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10070
Emeric Brunef42d922012-10-11 16:11:36 +020010071 return 0;
10072}
10073
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010074/* parse the "ssl-default-bind-options" keyword in global section */
10075static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10076 struct proxy *defpx, const char *file, int line,
10077 char **err) {
10078 int i = 1;
10079
10080 if (*(args[i]) == 0) {
10081 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10082 return -1;
10083 }
10084 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010085 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010086 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010087 else if (!strcmp(args[i], "prefer-client-ciphers"))
10088 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010089 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10090 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10091 i++;
10092 else {
10093 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10094 return -1;
10095 }
10096 }
10097 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010098 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10099 return -1;
10100 }
10101 i++;
10102 }
10103 return 0;
10104}
10105
10106/* parse the "ssl-default-server-options" keyword in global section */
10107static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10108 struct proxy *defpx, const char *file, int line,
10109 char **err) {
10110 int i = 1;
10111
10112 if (*(args[i]) == 0) {
10113 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10114 return -1;
10115 }
10116 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010117 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010118 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010119 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10120 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10121 i++;
10122 else {
10123 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10124 return -1;
10125 }
10126 }
10127 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010128 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10129 return -1;
10130 }
10131 i++;
10132 }
10133 return 0;
10134}
10135
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010136/* parse the "ca-base" / "crt-base" keywords in global section.
10137 * Returns <0 on alert, >0 on warning, 0 on success.
10138 */
10139static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10140 struct proxy *defpx, const char *file, int line,
10141 char **err)
10142{
10143 char **target;
10144
Willy Tarreauef934602016-12-22 23:12:01 +010010145 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010146
10147 if (too_many_args(1, args, err, NULL))
10148 return -1;
10149
10150 if (*target) {
10151 memprintf(err, "'%s' already specified.", args[0]);
10152 return -1;
10153 }
10154
10155 if (*(args[1]) == 0) {
10156 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10157 return -1;
10158 }
10159 *target = strdup(args[1]);
10160 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010161}
10162
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010163/* "issuers-chain-path" load chain certificate in global */
10164static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10165{
10166 X509 *ca;
10167 X509_NAME *name = NULL;
10168 ASN1_OCTET_STRING *skid = NULL;
10169 STACK_OF(X509) *chain = NULL;
10170 struct issuer_chain *issuer;
10171 struct eb64_node *node;
10172 char *path;
10173 u64 key;
10174 int ret = 0;
10175
10176 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10177 if (chain == NULL) {
10178 chain = sk_X509_new_null();
10179 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10180 name = X509_get_subject_name(ca);
10181 }
10182 if (!sk_X509_push(chain, ca)) {
10183 X509_free(ca);
10184 goto end;
10185 }
10186 }
10187 if (!chain) {
10188 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10189 goto end;
10190 }
10191 if (!skid) {
10192 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10193 goto end;
10194 }
10195 if (!name) {
10196 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10197 goto end;
10198 }
10199 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010200 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010201 issuer = container_of(node, typeof(*issuer), node);
10202 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10203 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10204 goto end;
10205 }
10206 }
10207 issuer = calloc(1, sizeof *issuer);
10208 path = strdup(fp);
10209 if (!issuer || !path) {
10210 free(issuer);
10211 free(path);
10212 goto end;
10213 }
10214 issuer->node.key = key;
10215 issuer->path = path;
10216 issuer->chain = chain;
10217 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010218 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010219 ret = 1;
10220 end:
10221 if (skid)
10222 ASN1_OCTET_STRING_free(skid);
10223 if (chain)
10224 sk_X509_pop_free(chain, X509_free);
10225 return ret;
10226}
10227
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010228static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010229{
10230 AUTHORITY_KEYID *akid;
10231 struct issuer_chain *issuer = NULL;
10232
10233 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10234 if (akid) {
10235 struct eb64_node *node;
10236 u64 hk;
10237 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10238 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10239 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10240 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10241 issuer = ti;
10242 break;
10243 }
10244 }
10245 AUTHORITY_KEYID_free(akid);
10246 }
10247 return issuer;
10248}
10249
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010250static void ssl_free_global_issuers(void)
10251{
10252 struct eb64_node *node, *back;
10253 struct issuer_chain *issuer;
10254
William Lallemande0f3fd52020-02-25 14:53:06 +010010255 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010256 while (node) {
10257 issuer = container_of(node, typeof(*issuer), node);
10258 back = eb64_next(node);
10259 eb64_delete(node);
10260 free(issuer->path);
10261 sk_X509_pop_free(issuer->chain, X509_free);
10262 free(issuer);
10263 node = back;
10264 }
10265}
10266
10267static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10268 struct proxy *defpx, const char *file, int line,
10269 char **err)
10270{
10271 char *path;
10272 struct dirent **de_list;
10273 int i, n;
10274 struct stat buf;
10275 char *end;
10276 char fp[MAXPATHLEN+1];
10277
10278 if (too_many_args(1, args, err, NULL))
10279 return -1;
10280
10281 path = args[1];
10282 if (*path == 0 || stat(path, &buf)) {
10283 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10284 err && *err ? *err : "", args[0]);
10285 return -1;
10286 }
10287 if (S_ISDIR(buf.st_mode) == 0) {
10288 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10289 err && *err ? *err : "", args[0], path);
10290 return -1;
10291 }
10292
10293 /* strip trailing slashes, including first one */
10294 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10295 *end = 0;
10296 /* path already parsed? */
10297 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10298 return 0;
10299 /* overwrite old issuers_chain_path */
10300 free(global_ssl.issuers_chain_path);
10301 global_ssl.issuers_chain_path = strdup(path);
10302 ssl_free_global_issuers();
10303
10304 n = scandir(path, &de_list, 0, alphasort);
10305 if (n < 0) {
10306 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10307 err && *err ? *err : "", args[0], path, strerror(errno));
10308 return -1;
10309 }
10310 for (i = 0; i < n; i++) {
10311 struct dirent *de = de_list[i];
10312 BIO *in = NULL;
10313 char *warn = NULL;
10314
10315 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10316 free(de);
10317 if (stat(fp, &buf) != 0) {
10318 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10319 goto next;
10320 }
10321 if (!S_ISREG(buf.st_mode))
10322 goto next;
10323
10324 in = BIO_new(BIO_s_file());
10325 if (in == NULL)
10326 goto next;
10327 if (BIO_read_filename(in, fp) <= 0)
10328 goto next;
10329 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10330 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010331 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010332 free(warn);
10333 warn = NULL;
10334 }
10335 next:
10336 if (in)
10337 BIO_free(in);
10338 }
10339 free(de_list);
10340
10341 return 0;
10342}
10343
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010344/* parse the "ssl-mode-async" keyword in global section.
10345 * Returns <0 on alert, >0 on warning, 0 on success.
10346 */
10347static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10348 struct proxy *defpx, const char *file, int line,
10349 char **err)
10350{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010351#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010352 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010353 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010354 return 0;
10355#else
10356 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10357 return -1;
10358#endif
10359}
10360
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010361#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010362static int ssl_check_async_engine_count(void) {
10363 int err_code = 0;
10364
Emeric Brun3854e012017-05-17 20:42:48 +020010365 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010366 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010367 err_code = ERR_ABORT;
10368 }
10369 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010370}
10371
Grant Zhang872f9c22017-01-21 01:10:18 +000010372/* parse the "ssl-engine" keyword in global section.
10373 * Returns <0 on alert, >0 on warning, 0 on success.
10374 */
10375static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10376 struct proxy *defpx, const char *file, int line,
10377 char **err)
10378{
10379 char *algo;
10380 int ret = -1;
10381
10382 if (*(args[1]) == 0) {
10383 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10384 return ret;
10385 }
10386
10387 if (*(args[2]) == 0) {
10388 /* if no list of algorithms is given, it defaults to ALL */
10389 algo = strdup("ALL");
10390 goto add_engine;
10391 }
10392
10393 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10394 if (strcmp(args[2], "algo") != 0) {
10395 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10396 return ret;
10397 }
10398
10399 if (*(args[3]) == 0) {
10400 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10401 return ret;
10402 }
10403 algo = strdup(args[3]);
10404
10405add_engine:
10406 if (ssl_init_single_engine(args[1], algo)==0) {
10407 openssl_engines_initialized++;
10408 ret = 0;
10409 }
10410 free(algo);
10411 return ret;
10412}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010413#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010414
Willy Tarreauf22e9682016-12-21 23:23:19 +010010415/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10416 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10417 */
10418static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10419 struct proxy *defpx, const char *file, int line,
10420 char **err)
10421{
10422 char **target;
10423
Willy Tarreauef934602016-12-22 23:12:01 +010010424 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010425
10426 if (too_many_args(1, args, err, NULL))
10427 return -1;
10428
10429 if (*(args[1]) == 0) {
10430 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10431 return -1;
10432 }
10433
10434 free(*target);
10435 *target = strdup(args[1]);
10436 return 0;
10437}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010438
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010439#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010440/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10441 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10442 */
10443static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10444 struct proxy *defpx, const char *file, int line,
10445 char **err)
10446{
10447 char **target;
10448
10449 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10450
10451 if (too_many_args(1, args, err, NULL))
10452 return -1;
10453
10454 if (*(args[1]) == 0) {
10455 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10456 return -1;
10457 }
10458
10459 free(*target);
10460 *target = strdup(args[1]);
10461 return 0;
10462}
10463#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010464
Willy Tarreau9ceda382016-12-21 23:13:03 +010010465/* parse various global tune.ssl settings consisting in positive integers.
10466 * Returns <0 on alert, >0 on warning, 0 on success.
10467 */
10468static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10469 struct proxy *defpx, const char *file, int line,
10470 char **err)
10471{
10472 int *target;
10473
10474 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10475 target = &global.tune.sslcachesize;
10476 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010477 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010478 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010479 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010480 else if (strcmp(args[0], "maxsslconn") == 0)
10481 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010482 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10483 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010484 else {
10485 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10486 return -1;
10487 }
10488
10489 if (too_many_args(1, args, err, NULL))
10490 return -1;
10491
10492 if (*(args[1]) == 0) {
10493 memprintf(err, "'%s' expects an integer argument.", args[0]);
10494 return -1;
10495 }
10496
10497 *target = atoi(args[1]);
10498 if (*target < 0) {
10499 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10500 return -1;
10501 }
10502 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010503}
10504
10505static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10506 struct proxy *defpx, const char *file, int line,
10507 char **err)
10508{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010509 int ret;
10510
10511 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10512 if (ret != 0)
10513 return ret;
10514
Willy Tarreaubafbe012017-11-24 17:34:44 +010010515 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010516 memprintf(err, "'%s' is already configured.", args[0]);
10517 return -1;
10518 }
10519
Willy Tarreaubafbe012017-11-24 17:34:44 +010010520 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10521 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010522 memprintf(err, "Out of memory error.");
10523 return -1;
10524 }
10525 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010526}
10527
10528/* parse "ssl.force-private-cache".
10529 * Returns <0 on alert, >0 on warning, 0 on success.
10530 */
10531static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10532 struct proxy *defpx, const char *file, int line,
10533 char **err)
10534{
10535 if (too_many_args(0, args, err, NULL))
10536 return -1;
10537
Willy Tarreauef934602016-12-22 23:12:01 +010010538 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010539 return 0;
10540}
10541
10542/* parse "ssl.lifetime".
10543 * Returns <0 on alert, >0 on warning, 0 on success.
10544 */
10545static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10546 struct proxy *defpx, const char *file, int line,
10547 char **err)
10548{
10549 const char *res;
10550
10551 if (too_many_args(1, args, err, NULL))
10552 return -1;
10553
10554 if (*(args[1]) == 0) {
10555 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10556 return -1;
10557 }
10558
Willy Tarreauef934602016-12-22 23:12:01 +010010559 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010560 if (res == PARSE_TIME_OVER) {
10561 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10562 args[1], args[0]);
10563 return -1;
10564 }
10565 else if (res == PARSE_TIME_UNDER) {
10566 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10567 args[1], args[0]);
10568 return -1;
10569 }
10570 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010571 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10572 return -1;
10573 }
10574 return 0;
10575}
10576
10577#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010578/* parse "ssl-dh-param-file".
10579 * Returns <0 on alert, >0 on warning, 0 on success.
10580 */
10581static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10582 struct proxy *defpx, const char *file, int line,
10583 char **err)
10584{
10585 if (too_many_args(1, args, err, NULL))
10586 return -1;
10587
10588 if (*(args[1]) == 0) {
10589 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10590 return -1;
10591 }
10592
10593 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10594 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10595 return -1;
10596 }
10597 return 0;
10598}
10599
Willy Tarreau9ceda382016-12-21 23:13:03 +010010600/* parse "ssl.default-dh-param".
10601 * Returns <0 on alert, >0 on warning, 0 on success.
10602 */
10603static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10604 struct proxy *defpx, const char *file, int line,
10605 char **err)
10606{
10607 if (too_many_args(1, args, err, NULL))
10608 return -1;
10609
10610 if (*(args[1]) == 0) {
10611 memprintf(err, "'%s' expects an integer argument.", args[0]);
10612 return -1;
10613 }
10614
Willy Tarreauef934602016-12-22 23:12:01 +010010615 global_ssl.default_dh_param = atoi(args[1]);
10616 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010617 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10618 return -1;
10619 }
10620 return 0;
10621}
10622#endif
10623
William Lallemand3af48e72020-02-03 17:15:52 +010010624
10625/*
10626 * parse "ssl-load-extra-files".
10627 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10628 */
10629static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10630 struct proxy *defpx, const char *file, int line,
10631 char **err)
10632{
10633 int i;
10634 int gf = SSL_GF_NONE;
10635
10636 if (*(args[1]) == 0)
10637 goto err_arg;
10638
10639 for (i = 1; *args[i]; i++) {
10640
10641 if (!strcmp("bundle", args[i])) {
10642 gf |= SSL_GF_BUNDLE;
10643
10644 } else if (!strcmp("sctl", args[i])) {
10645 gf |= SSL_GF_SCTL;
10646
10647 } else if (!strcmp("ocsp", args[i])){
10648 gf |= SSL_GF_OCSP;
10649
10650 } else if (!strcmp("issuer", args[i])){
10651 gf |= SSL_GF_OCSP_ISSUER;
10652
William Lallemand4c5adbf2020-02-24 14:23:22 +010010653 } else if (!strcmp("key", args[i])) {
10654 gf |= SSL_GF_KEY;
10655
William Lallemand3af48e72020-02-03 17:15:52 +010010656 } else if (!strcmp("none", args[i])) {
10657 if (gf != SSL_GF_NONE)
10658 goto err_alone;
10659 gf = SSL_GF_NONE;
10660 i++;
10661 break;
10662
10663 } else if (!strcmp("all", args[i])) {
10664 if (gf != SSL_GF_NONE)
10665 goto err_alone;
10666 gf = SSL_GF_ALL;
10667 i++;
10668 break;
10669 } else {
10670 goto err_arg;
10671 }
10672 }
10673 /* break from loop but there are still arguments */
10674 if (*args[i])
10675 goto err_alone;
10676
10677 global_ssl.extra_files = gf;
10678
10679 return 0;
10680
10681err_alone:
10682 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10683 return -1;
10684
10685err_arg:
10686 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10687 return -1;
10688}
10689
Willy Tarreau9ceda382016-12-21 23:13:03 +010010690
William Lallemand32af2032016-10-29 18:09:35 +020010691/* This function is used with TLS ticket keys management. It permits to browse
10692 * each reference. The variable <getnext> must contain the current node,
10693 * <end> point to the root node.
10694 */
10695#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10696static inline
10697struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10698{
10699 struct tls_keys_ref *ref = getnext;
10700
10701 while (1) {
10702
10703 /* Get next list entry. */
10704 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10705
10706 /* If the entry is the last of the list, return NULL. */
10707 if (&ref->list == end)
10708 return NULL;
10709
10710 return ref;
10711 }
10712}
10713
10714static inline
10715struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10716{
10717 int id;
10718 char *error;
10719
10720 /* If the reference starts by a '#', this is numeric id. */
10721 if (reference[0] == '#') {
10722 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10723 id = strtol(reference + 1, &error, 10);
10724 if (*error != '\0')
10725 return NULL;
10726
10727 /* Perform the unique id lookup. */
10728 return tlskeys_ref_lookupid(id);
10729 }
10730
10731 /* Perform the string lookup. */
10732 return tlskeys_ref_lookup(reference);
10733}
10734#endif
10735
10736
10737#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10738
10739static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10740
10741static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10742 return cli_io_handler_tlskeys_files(appctx);
10743}
10744
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010745/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10746 * (next index to be dumped), and cli.p0 (next key reference).
10747 */
William Lallemand32af2032016-10-29 18:09:35 +020010748static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10749
10750 struct stream_interface *si = appctx->owner;
10751
10752 switch (appctx->st2) {
10753 case STAT_ST_INIT:
10754 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010755 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010756 * later and restart at the state "STAT_ST_INIT".
10757 */
10758 chunk_reset(&trash);
10759
10760 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10761 chunk_appendf(&trash, "# id secret\n");
10762 else
10763 chunk_appendf(&trash, "# id (file)\n");
10764
Willy Tarreau06d80a92017-10-19 14:32:15 +020010765 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010766 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010767 return 0;
10768 }
10769
William Lallemand32af2032016-10-29 18:09:35 +020010770 /* Now, we start the browsing of the references lists.
10771 * Note that the following call to LIST_ELEM return bad pointer. The only
10772 * available field of this pointer is <list>. It is used with the function
10773 * tlskeys_list_get_next() for retruning the first available entry
10774 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010775 if (appctx->ctx.cli.p0 == NULL) {
10776 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10777 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010778 }
10779
10780 appctx->st2 = STAT_ST_LIST;
10781 /* fall through */
10782
10783 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010784 while (appctx->ctx.cli.p0) {
10785 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010786
10787 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010788 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010789 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010790
10791 if (appctx->ctx.cli.i1 == 0)
10792 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10793
William Lallemand32af2032016-10-29 18:09:35 +020010794 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010795 int head;
10796
10797 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10798 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010799 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010800 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010801
10802 chunk_reset(t2);
10803 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010804 if (ref->key_size_bits == 128) {
10805 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10806 sizeof(struct tls_sess_key_128),
10807 t2->area, t2->size);
10808 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10809 t2->area);
10810 }
10811 else if (ref->key_size_bits == 256) {
10812 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10813 sizeof(struct tls_sess_key_256),
10814 t2->area, t2->size);
10815 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10816 t2->area);
10817 }
10818 else {
10819 /* This case should never happen */
10820 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10821 }
William Lallemand32af2032016-10-29 18:09:35 +020010822
Willy Tarreau06d80a92017-10-19 14:32:15 +020010823 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010824 /* let's try again later from this stream. We add ourselves into
10825 * this stream's users so that it can remove us upon termination.
10826 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010827 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010828 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010829 return 0;
10830 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010831 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010832 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010833 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010834 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010835 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010836 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010837 /* let's try again later from this stream. We add ourselves into
10838 * this stream's users so that it can remove us upon termination.
10839 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010840 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010841 return 0;
10842 }
10843
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010844 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010845 break;
10846
10847 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010848 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010849 }
10850
10851 appctx->st2 = STAT_ST_FIN;
10852 /* fall through */
10853
10854 default:
10855 appctx->st2 = STAT_ST_FIN;
10856 return 1;
10857 }
10858 return 0;
10859}
10860
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010861/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010862static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010863{
William Lallemand32af2032016-10-29 18:09:35 +020010864 /* no parameter, shows only file list */
10865 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010866 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010867 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010868 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010869 }
10870
10871 if (args[2][0] == '*') {
10872 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010873 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010874 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010875 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010876 if (!appctx->ctx.cli.p0)
10877 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010878 }
William Lallemand32af2032016-10-29 18:09:35 +020010879 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010880 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010881}
10882
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010883static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010884{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010885 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010886 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010887
William Lallemand32af2032016-10-29 18:09:35 +020010888 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010889 if (!*args[3] || !*args[4])
10890 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 +020010891
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010892 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010893 if (!ref)
10894 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010895
Willy Tarreau1c913e42018-08-22 05:26:57 +020010896 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010897 if (ret < 0)
10898 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010899
Willy Tarreau1c913e42018-08-22 05:26:57 +020010900 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010901 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10902 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010903
Willy Tarreau9d008692019-08-09 11:21:01 +020010904 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010905}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010906#endif
William Lallemand32af2032016-10-29 18:09:35 +020010907
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010908/*
10909 * Take an ssl_bind_conf structure and append the configuration line used to
10910 * create it in the buffer
10911 */
10912static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10913{
10914 int space = 0;
10915
10916 if (conf == NULL)
10917 return;
10918
10919 chunk_appendf(buf, " [");
10920#ifdef OPENSSL_NPN_NEGOTIATED
10921 if (conf->npn_str) {
10922 int len = conf->npn_len;
10923 char *ptr = conf->npn_str;
10924 int comma = 0;
10925
10926 if (space) chunk_appendf(buf, " ");
10927 chunk_appendf(buf, "npn ");
10928 while (len) {
10929 unsigned short size;
10930
10931 size = *ptr;
10932 ptr++;
10933 if (comma)
10934 chunk_memcat(buf, ",", 1);
10935 chunk_memcat(buf, ptr, size);
10936 ptr += size;
10937 len -= size + 1;
10938 comma = 1;
10939 }
10940 chunk_memcat(buf, "", 1); /* finish with a \0 */
10941 space++;
10942 }
10943#endif
10944#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10945 if (conf->alpn_str) {
10946 int len = conf->alpn_len;
10947 char *ptr = conf->alpn_str;
10948 int comma = 0;
10949
10950 if (space) chunk_appendf(buf, " ");
10951 chunk_appendf(buf, "alpn ");
10952 while (len) {
10953 unsigned short size;
10954
10955 size = *ptr;
10956 ptr++;
10957 if (comma)
10958 chunk_memcat(buf, ",", 1);
10959 chunk_memcat(buf, ptr, size);
10960 ptr += size;
10961 len -= size + 1;
10962 comma = 1;
10963 }
10964 chunk_memcat(buf, "", 1); /* finish with a \0 */
10965 space++;
10966 }
10967#endif
10968 /* verify */
10969 {
10970 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
10971 if (space) chunk_appendf(buf, " ");
10972 chunk_appendf(buf, "verify none");
10973 space++;
10974 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
10975 if (space) chunk_appendf(buf, " ");
10976 chunk_appendf(buf, "verify optional");
10977 space++;
10978 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
10979 if (space) chunk_appendf(buf, " ");
10980 chunk_appendf(buf, "verify required");
10981 space++;
10982 }
10983 }
10984
10985 if (conf->no_ca_names) {
10986 if (space) chunk_appendf(buf, " ");
10987 chunk_appendf(buf, "no-ca-names");
10988 space++;
10989 }
10990
10991 if (conf->early_data) {
10992 if (space) chunk_appendf(buf, " ");
10993 chunk_appendf(buf, "allow-0rtt");
10994 space++;
10995 }
10996 if (conf->ca_file) {
10997 if (space) chunk_appendf(buf, " ");
10998 chunk_appendf(buf, "ca-file %s", conf->ca_file);
10999 space++;
11000 }
11001 if (conf->crl_file) {
11002 if (space) chunk_appendf(buf, " ");
11003 chunk_appendf(buf, "crl-file %s", conf->crl_file);
11004 space++;
11005 }
11006 if (conf->ciphers) {
11007 if (space) chunk_appendf(buf, " ");
11008 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11009 space++;
11010 }
11011#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11012 if (conf->ciphersuites) {
11013 if (space) chunk_appendf(buf, " ");
11014 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11015 space++;
11016 }
11017#endif
11018 if (conf->curves) {
11019 if (space) chunk_appendf(buf, " ");
11020 chunk_appendf(buf, "curves %s", conf->curves);
11021 space++;
11022 }
11023 if (conf->ecdhe) {
11024 if (space) chunk_appendf(buf, " ");
11025 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11026 space++;
11027 }
11028
11029 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11030 /* XXX: this part need to be revamp so we don't dump the default settings */
11031 if (conf->ssl_methods.min) {
11032 if (space) chunk_appendf(buf, " ");
11033 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11034 space++;
11035 }
11036
11037 if (conf->ssl_methods.max) {
11038 if (space) chunk_appendf(buf, " ");
11039 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11040 space++;
11041 }
11042
William Lallemand58a52222020-04-02 18:11:47 +020011043 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011044
11045 return;
11046}
11047
11048/* dump a list of filters */
11049static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11050{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011051 int i;
11052
11053 if (!entry->fcount)
11054 return;
11055
11056 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011057 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011058 }
11059 return;
11060}
11061
11062/* CLI IO handler for '(show|dump) ssl crt-list' */
11063static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11064{
11065 struct buffer *trash = alloc_trash_chunk();
11066 struct stream_interface *si = appctx->owner;
11067 struct ebmb_node *lnode;
11068
11069 if (trash == NULL)
11070 return 1;
11071
11072 /* dump the list of crt-lists */
11073 lnode = appctx->ctx.cli.p1;
11074 if (lnode == NULL)
11075 lnode = ebmb_first(&crtlists_tree);
11076 while (lnode) {
11077 chunk_appendf(trash, "%s\n", lnode->key);
11078 if (ci_putchk(si_ic(si), trash) == -1) {
11079 si_rx_room_blk(si);
11080 goto yield;
11081 }
11082 lnode = ebmb_next(lnode);
11083 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011084 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011085 return 1;
11086yield:
11087 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011088 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011089 return 0;
11090}
11091
11092/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11093static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11094{
11095 struct buffer *trash = alloc_trash_chunk();
11096 struct crtlist *crtlist;
11097 struct stream_interface *si = appctx->owner;
11098 struct crtlist_entry *entry;
11099
11100 if (trash == NULL)
11101 return 1;
11102
11103 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11104
11105 entry = appctx->ctx.cli.p1;
11106 if (entry == NULL) {
11107 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11108 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11109 if (ci_putchk(si_ic(si), trash) == -1) {
11110 si_rx_room_blk(si);
11111 goto yield;
11112 }
11113 }
11114
11115 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11116 struct ckch_store *store;
11117 const char *filename;
11118
11119 store = entry->node.key;
11120 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011121 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011122 if (appctx->ctx.cli.i0 == 's') /* show */
11123 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011124 dump_crtlist_sslconf(trash, entry->ssl_conf);
11125 dump_crtlist_filters(trash, entry);
11126 chunk_appendf(trash, "\n");
11127
11128 if (ci_putchk(si_ic(si), trash) == -1) {
11129 si_rx_room_blk(si);
11130 goto yield;
11131 }
11132 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011133 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011134 return 1;
11135yield:
11136 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011137 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011138 return 0;
11139}
11140
11141/* CLI argument parser for '(show|dump) ssl crt-list' */
11142static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11143{
11144 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011145 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011146 int mode;
11147
11148 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11149 return 1;
11150
11151 appctx->ctx.cli.p0 = NULL;
11152 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011153
William Lallemandc69f02d2020-04-06 19:07:03 +020011154 if (*args[3] && !strcmp(args[3], "-n")) {
11155 mode = 's';
11156 filename = args[4];
11157 } else {
11158 mode = 'd';
11159 filename = args[3];
11160 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011161
William Lallemandc69f02d2020-04-06 19:07:03 +020011162 if (mode == 's' && !*args[4])
11163 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11164
11165 if (filename && *filename) {
11166 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011167 if (lnode == NULL)
11168 return cli_err(appctx, "didn't find the specified filename\n");
11169
11170 appctx->ctx.cli.p0 = lnode;
11171 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11172 }
11173 appctx->ctx.cli.i0 = mode;
11174
11175 return 0;
11176}
William Lallemand44b35322019-10-17 16:28:40 +020011177
William Lallemande67c80b2020-03-25 14:42:37 +010011178/* release function of the "add ssl crt-list' command, free things and unlock
11179 the spinlock */
11180static void cli_release_add_crtlist(struct appctx *appctx)
11181{
11182 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11183
11184 if (appctx->st2 != SETCERT_ST_FIN) {
11185 struct ckch_inst *inst, *inst_s;
11186 /* upon error free the ckch_inst and everything inside */
11187 ebpt_delete(&entry->node);
11188 LIST_DEL(&entry->by_crtlist);
11189 LIST_DEL(&entry->by_ckch_store);
11190
11191 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +020011192 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011193 }
11194 crtlist_free_filters(entry->filters);
11195 ssl_sock_free_ssl_conf(entry->ssl_conf);
11196 free(entry->ssl_conf);
11197 free(entry);
11198 }
11199
11200 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11201}
11202
11203
11204/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11205 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11206 *
11207 * The logic is the same as the "commit ssl cert" command but without the
11208 * freeing of the old structures, because there are none.
11209 */
11210static int cli_io_handler_add_crtlist(struct appctx *appctx)
11211{
11212 struct bind_conf_list *bind_conf_node;
11213 struct stream_interface *si = appctx->owner;
11214 struct crtlist *crtlist = appctx->ctx.cli.p0;
11215 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11216 struct ckch_store *store = entry->node.key;
11217 struct buffer *trash = alloc_trash_chunk();
11218 struct ckch_inst *new_inst;
11219 char *err = NULL;
11220 int i = 0;
11221 int errcode = 0;
11222
11223 if (trash == NULL)
11224 goto error;
11225
11226 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11227 * created.
11228 */
11229 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11230 goto error;
11231
11232 while (1) {
11233 switch (appctx->st2) {
11234 case SETCERT_ST_INIT:
11235 /* This state just print the update message */
11236 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11237 if (ci_putchk(si_ic(si), trash) == -1) {
11238 si_rx_room_blk(si);
11239 goto yield;
11240 }
11241 appctx->st2 = SETCERT_ST_GEN;
11242 /* fallthrough */
11243 case SETCERT_ST_GEN:
11244 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11245 if (bind_conf_node == NULL)
11246 bind_conf_node = crtlist->bind_conf;
11247 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11248 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11249 struct sni_ctx *sni;
11250
11251 /* yield every 10 generations */
11252 if (i > 10) {
11253 appctx->ctx.cli.p2 = bind_conf_node;
11254 goto yield;
11255 }
11256
11257 /* we don't support multi-cert bundles, only simple ones */
11258 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11259 if (errcode & ERR_CODE)
11260 goto error;
11261
11262 /* we need to initialize the SSL_CTX generated */
11263 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11264 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11265 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11266 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11267 if (errcode & ERR_CODE)
11268 goto error;
11269 }
11270 }
11271 /* display one dot for each new instance */
11272 chunk_appendf(trash, ".");
11273 i++;
11274 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11275 }
11276 appctx->st2 = SETCERT_ST_INSERT;
11277 /* fallthrough */
11278 case SETCERT_ST_INSERT:
11279 /* insert SNIs in bind_conf */
11280 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11281 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11282 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11283 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11284 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011285 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011286 appctx->st2 = SETCERT_ST_FIN;
11287 goto end;
11288 }
11289 }
11290
11291end:
11292 chunk_appendf(trash, "\n");
11293 if (errcode & ERR_WARN)
11294 chunk_appendf(trash, "%s", err);
11295 chunk_appendf(trash, "Success!\n");
11296 if (ci_putchk(si_ic(si), trash) == -1)
11297 si_rx_room_blk(si);
11298 free_trash_chunk(trash);
11299 /* success: call the release function and don't come back */
11300 return 1;
11301yield:
11302 /* store the state */
11303 if (ci_putchk(si_ic(si), trash) == -1)
11304 si_rx_room_blk(si);
11305 free_trash_chunk(trash);
11306 si_rx_endp_more(si); /* let's come back later */
11307 return 0; /* should come back */
11308
11309error:
11310 /* spin unlock and free are done in the release function */
11311 if (trash) {
11312 chunk_appendf(trash, "\n%sFailed!\n", err);
11313 if (ci_putchk(si_ic(si), trash) == -1)
11314 si_rx_room_blk(si);
11315 free_trash_chunk(trash);
11316 }
11317 /* error: call the release function and don't come back */
11318 return 1;
11319}
11320
11321
11322/*
11323 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011324 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011325 */
11326static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11327{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011328 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011329 struct ckch_store *store;
11330 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011331 char path[MAXPATHLEN+1];
11332 char *crtlist_path;
11333 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011334 struct ebmb_node *eb;
11335 struct ebpt_node *inserted;
11336 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011337 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011338
11339 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11340 return 1;
11341
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011342 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011343 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11344
11345 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011346
11347 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11348 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11349
11350 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11351 if (!eb) {
11352 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11353 goto error;
11354 }
11355 crtlist = ebmb_entry(eb, struct crtlist, node);
11356
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011357 entry = malloc(sizeof(*entry));
11358 if (entry == NULL) {
11359 memprintf(&err, "Not enough memory!");
11360 goto error;
11361 }
William Lallemanda690fed2020-04-02 12:02:54 +020011362 entry->fcount = 0;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011363 entry->filters = NULL;
11364 entry->ssl_conf = NULL;
11365
11366 if (payload) {
11367 char *lf;
11368
11369 lf = strrchr(payload, '\n');
11370 if (lf) {
11371 memprintf(&err, "only one line of payload is supported!");
11372 goto error;
11373 }
11374 /* cert_path is filled here */
11375 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11376 if (cfgerr & ERR_CODE)
11377 goto error;
11378 } else {
11379 cert_path = args[4];
11380 }
11381
11382 if (!cert_path) {
11383 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11384 cfgerr |= ERR_ALERT | ERR_FATAL;
11385 goto error;
11386 }
11387
11388 if (*cert_path != '/' && global_ssl.crt_base) {
11389 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11390 memprintf(&err, "'%s' : path too long", cert_path);
11391 cfgerr |= ERR_ALERT | ERR_FATAL;
11392 goto error;
11393 }
11394 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11395 cert_path = path;
11396 }
11397
William Lallemande67c80b2020-03-25 14:42:37 +010011398 store = ckchs_lookup(cert_path);
11399 if (store == NULL) {
11400 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11401 goto error;
11402 }
William Lallemand36ccc392020-04-08 10:57:24 +020011403 if (store->multi) {
11404 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);
11405 goto error;
11406 }
William Lallemande67c80b2020-03-25 14:42:37 +010011407 if (store->ckch == NULL || store->ckch->cert == NULL) {
11408 memprintf(&err, "certificate '%s' is empty!", cert_path);
11409 goto error;
11410 }
11411
William Lallemande67c80b2020-03-25 14:42:37 +010011412 /* check if it's possible to insert this new crtlist_entry */
11413 entry->node.key = store;
11414 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11415 if (inserted != &entry->node) {
11416 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011417 goto error;
11418 }
11419
11420 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
11421 LIST_INIT(&entry->ckch_inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011422 entry->crtlist = crtlist;
11423 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11424
11425 appctx->st2 = SETCERT_ST_INIT;
11426 appctx->ctx.cli.p0 = crtlist;
11427 appctx->ctx.cli.p1 = entry;
11428
11429 /* unlock is done in the release handler */
11430 return 0;
11431
11432error:
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011433 if (entry) {
11434 crtlist_free_filters(entry->filters);
11435 ssl_sock_free_ssl_conf(entry->ssl_conf);
11436 free(entry->ssl_conf);
11437 free(entry);
11438 }
William Lallemande67c80b2020-03-25 14:42:37 +010011439 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11440 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11441 return cli_dynerr(appctx, err);
11442}
11443
William Lallemand0a9b9412020-04-06 17:43:05 +020011444/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11445static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11446{
11447 struct ckch_store *store;
11448 char *err = NULL;
11449 char *crtlist_path, *cert_path;
11450 struct ebmb_node *ebmb;
11451 struct ebpt_node *ebpt;
11452 struct crtlist *crtlist;
11453 struct crtlist_entry *entry = NULL;
11454 struct ckch_inst *inst, *inst_s;
11455 int linenum = 0;
11456 char *colons;
11457
11458 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11459 return 1;
11460
11461 if (!*args[3] || !*args[4])
11462 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11463
William Lallemand463b5242020-04-08 10:30:44 +020011464 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11465 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11466
William Lallemand0a9b9412020-04-06 17:43:05 +020011467 crtlist_path = args[3];
11468 cert_path = args[4];
11469
11470 colons = strchr(cert_path, ':');
11471 if (colons) {
11472 char *endptr;
11473
11474 linenum = strtol(colons + 1, &endptr, 10);
11475 if (colons + 1 == endptr || *endptr != '\0') {
11476 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11477 goto error;
11478 }
11479 *colons = '\0';
11480 }
11481 /* look for crtlist */
11482 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11483 if (!ebmb) {
11484 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11485 goto error;
11486 }
11487 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11488
11489 /* look for store */
11490 store = ckchs_lookup(cert_path);
11491 if (store == NULL) {
11492 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11493 goto error;
11494 }
William Lallemand36ccc392020-04-08 10:57:24 +020011495 if (store->multi) {
11496 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);
11497 goto error;
11498 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011499 if (store->ckch == NULL || store->ckch->cert == NULL) {
11500 memprintf(&err, "certificate '%s' is empty!", cert_path);
11501 goto error;
11502 }
11503
11504 ebpt = ebpt_lookup(&crtlist->entries, store);
11505 if (!ebpt) {
11506 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11507 goto error;
11508 }
11509
11510 /* list the line number of entries for errors in err, and select the right ebpt */
11511 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11512 struct crtlist_entry *tmp;
11513
11514 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11515 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11516
11517 /* select the entry we wanted */
11518 if (linenum == 0 || tmp->linenum == linenum) {
11519 if (!entry)
11520 entry = tmp;
11521 }
11522 }
11523
11524 /* we didn't found the specified entry */
11525 if (!entry) {
11526 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);
11527 goto error;
11528 }
11529
11530 /* we didn't specified a line number but there were several entries */
11531 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11532 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11533 goto error;
11534 }
11535
11536 /* upon error free the ckch_inst and everything inside */
11537
11538 ebpt_delete(&entry->node);
11539 LIST_DEL(&entry->by_crtlist);
11540 LIST_DEL(&entry->by_ckch_store);
11541
11542 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11543 struct sni_ctx *sni, *sni_s;
11544
11545 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11546 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11547 ebmb_delete(&sni->name);
11548 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011549 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011550 free(sni);
11551 }
11552 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11553 LIST_DEL(&inst->by_ckchs);
11554 free(inst);
11555 }
11556
11557 crtlist_free_filters(entry->filters);
11558 ssl_sock_free_ssl_conf(entry->ssl_conf);
11559 free(entry->ssl_conf);
11560 free(entry);
11561
11562 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11563 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11564 return cli_dynmsg(appctx, LOG_NOTICE, err);
11565
11566error:
11567 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11568 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11569 return cli_dynerr(appctx, err);
11570}
William Lallemande67c80b2020-03-25 14:42:37 +010011571
William Lallemand44b35322019-10-17 16:28:40 +020011572/* Type of SSL payloads that can be updated over the CLI */
11573
11574enum {
11575 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011576 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011577#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011578 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011579#endif
William Lallemand44b35322019-10-17 16:28:40 +020011580 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011581#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011582 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011583#endif
William Lallemand44b35322019-10-17 16:28:40 +020011584 CERT_TYPE_MAX,
11585};
11586
11587struct {
11588 const char *ext;
11589 int type;
11590 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11591 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011592} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011593 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011594 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011595#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011596 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011597#endif
11598#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011599 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011600#endif
William Lallemand44b35322019-10-17 16:28:40 +020011601 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011602 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011603};
11604
William Lallemand8f840d72019-10-23 10:53:05 +020011605
William Lallemandd4f946c2019-12-05 10:26:40 +010011606/* release function of the `show ssl cert' command */
11607static void cli_release_show_cert(struct appctx *appctx)
11608{
11609 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11610}
11611
11612/* IO handler of "show ssl cert <filename>" */
11613static int cli_io_handler_show_cert(struct appctx *appctx)
11614{
11615 struct buffer *trash = alloc_trash_chunk();
11616 struct ebmb_node *node;
11617 struct stream_interface *si = appctx->owner;
11618 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011619
11620 if (trash == NULL)
11621 return 1;
11622
11623 if (!appctx->ctx.ssl.old_ckchs) {
11624 if (ckchs_transaction.old_ckchs) {
11625 ckchs = ckchs_transaction.old_ckchs;
11626 chunk_appendf(trash, "# transaction\n");
11627 if (!ckchs->multi) {
11628 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011629#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011630 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011631 int n;
11632
William Lallemandd4f946c2019-12-05 10:26:40 +010011633 chunk_appendf(trash, "*%s:", ckchs->path);
11634 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11635 if (ckchs->ckch[n].cert)
11636 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11637 }
11638 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011639#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011640 }
11641 }
11642 }
11643
11644 if (!appctx->ctx.cli.p0) {
11645 chunk_appendf(trash, "# filename\n");
11646 node = ebmb_first(&ckchs_tree);
11647 } else {
11648 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11649 }
11650 while (node) {
11651 ckchs = ebmb_entry(node, struct ckch_store, node);
11652 if (!ckchs->multi) {
11653 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011654#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011655 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011656 int n;
11657
William Lallemandd4f946c2019-12-05 10:26:40 +010011658 chunk_appendf(trash, "%s:", ckchs->path);
11659 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11660 if (ckchs->ckch[n].cert)
11661 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11662 }
11663 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011664#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011665 }
11666
11667 node = ebmb_next(node);
11668 if (ci_putchk(si_ic(si), trash) == -1) {
11669 si_rx_room_blk(si);
11670 goto yield;
11671 }
11672 }
11673
11674 appctx->ctx.cli.p0 = NULL;
11675 free_trash_chunk(trash);
11676 return 1;
11677yield:
11678
11679 free_trash_chunk(trash);
11680 appctx->ctx.cli.p0 = ckchs;
11681 return 0; /* should come back */
11682}
11683
11684/* IO handler of the details "show ssl cert <filename>" */
11685static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11686{
11687 struct stream_interface *si = appctx->owner;
11688 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11689 struct buffer *out = alloc_trash_chunk();
11690 struct buffer *tmp = alloc_trash_chunk();
11691 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011692 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011693 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011694 int write = -1;
11695 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011696 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011697
11698 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011699 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011700
11701 if (!ckchs->multi) {
11702 chunk_appendf(out, "Filename: ");
11703 if (ckchs == ckchs_transaction.new_ckchs)
11704 chunk_appendf(out, "*");
11705 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011706
William Lallemand59c16fc2020-03-19 20:26:02 +010011707 chunk_appendf(out, "Status: ");
11708 if (ckchs->ckch->cert == NULL)
11709 chunk_appendf(out, "Empty\n");
11710 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11711 chunk_appendf(out, "Unused\n");
11712 else
11713 chunk_appendf(out, "Used\n");
11714
William Lallemandea987ed2020-03-19 16:48:33 +010011715 if (ckchs->ckch->cert == NULL)
11716 goto end;
11717
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011718 chain = ckchs->ckch->chain;
11719 if (chain == NULL) {
11720 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011721 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011722 if (issuer) {
11723 chain = issuer->chain;
11724 chunk_appendf(out, "Chain Filename: ");
11725 chunk_appendf(out, "%s\n", issuer->path);
11726 }
11727 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011728 chunk_appendf(out, "Serial: ");
11729 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11730 goto end;
11731 dump_binary(out, tmp->area, tmp->data);
11732 chunk_appendf(out, "\n");
11733
11734 chunk_appendf(out, "notBefore: ");
11735 chunk_reset(tmp);
11736 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11737 goto end;
11738 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11739 goto end;
11740 write = BIO_read(bio, tmp->area, tmp->size-1);
11741 tmp->area[write] = '\0';
11742 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011743 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011744 chunk_appendf(out, "%s\n", tmp->area);
11745
11746 chunk_appendf(out, "notAfter: ");
11747 chunk_reset(tmp);
11748 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11749 goto end;
11750 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11751 goto end;
11752 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11753 goto end;
11754 tmp->area[write] = '\0';
11755 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011756 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011757 chunk_appendf(out, "%s\n", tmp->area);
11758
William Lallemandd4f946c2019-12-05 10:26:40 +010011759#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11760 chunk_appendf(out, "Subject Alternative Name: ");
11761 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11762 goto end;
11763 *(out->area + out->data) = '\0';
11764 chunk_appendf(out, "\n");
11765#endif
11766 chunk_reset(tmp);
11767 chunk_appendf(out, "Algorithm: ");
11768 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11769 goto end;
11770 chunk_appendf(out, "%s\n", tmp->area);
11771
11772 chunk_reset(tmp);
11773 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011774 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011775 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011776 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011777 dump_binary(out, tmp->area, tmp->data);
11778 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011779
William Lallemanda90e5932020-02-25 14:07:58 +010011780 chunk_appendf(out, "Subject: ");
11781 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11782 goto end;
11783 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11784 goto end;
11785 *(tmp->area + tmp->data) = '\0';
11786 chunk_appendf(out, "%s\n", tmp->area);
11787
11788 chunk_appendf(out, "Issuer: ");
11789 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11790 goto end;
11791 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11792 goto end;
11793 *(tmp->area + tmp->data) = '\0';
11794 chunk_appendf(out, "%s\n", tmp->area);
11795
William Lallemand35f4a9d2020-02-25 11:56:32 +010011796 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011797 for (i = 0; i < sk_X509_num(chain); i++) {
11798 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011799
William Lallemandbb7288a2020-02-25 14:04:33 +010011800 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011801 if ((name = X509_get_subject_name(ca)) == NULL)
11802 goto end;
11803 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11804 goto end;
11805 *(tmp->area + tmp->data) = '\0';
11806 chunk_appendf(out, "%s\n", tmp->area);
11807
William Lallemandbb7288a2020-02-25 14:04:33 +010011808 chunk_appendf(out, "Chain Issuer: ");
11809 if ((name = X509_get_issuer_name(ca)) == NULL)
11810 goto end;
11811 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11812 goto end;
11813 *(tmp->area + tmp->data) = '\0';
11814 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011815 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011816 }
11817
William Lallemandea987ed2020-03-19 16:48:33 +010011818end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011819 if (ci_putchk(si_ic(si), out) == -1) {
11820 si_rx_room_blk(si);
11821 goto yield;
11822 }
11823
William Lallemand18eeb8e2020-03-20 14:42:36 +010011824end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011825 if (bio)
11826 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011827 free_trash_chunk(tmp);
11828 free_trash_chunk(out);
11829 return 1;
11830yield:
11831 free_trash_chunk(tmp);
11832 free_trash_chunk(out);
11833 return 0; /* should come back */
11834}
11835
11836/* parsing function for 'show ssl cert [certfile]' */
11837static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11838{
11839 struct ckch_store *ckchs;
11840
11841 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11842 return cli_err(appctx, "Can't allocate memory!\n");
11843
11844 /* The operations on the CKCH architecture are locked so we can
11845 * manipulate ckch_store and ckch_inst */
11846 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11847 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11848
11849 /* check if there is a certificate to lookup */
11850 if (*args[3]) {
11851 if (*args[3] == '*') {
11852 if (!ckchs_transaction.new_ckchs)
11853 goto error;
11854
11855 ckchs = ckchs_transaction.new_ckchs;
11856
11857 if (strcmp(args[3] + 1, ckchs->path))
11858 goto error;
11859
11860 } else {
11861 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11862 goto error;
11863
11864 }
11865
11866 if (ckchs->multi)
11867 goto error;
11868
11869 appctx->ctx.cli.p0 = ckchs;
11870 /* use the IO handler that shows details */
11871 appctx->io_handler = cli_io_handler_show_cert_detail;
11872 }
11873
11874 return 0;
11875
11876error:
11877 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11878 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11879}
11880
William Lallemand430413e2019-10-28 14:30:47 +010011881/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011882static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011883{
11884 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011885
William Lallemand430413e2019-10-28 14:30:47 +010011886 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011887
William Lallemand430413e2019-10-28 14:30:47 +010011888 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011889 /* 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 +010011890 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011891
William Lallemandbeea2a42019-10-30 17:45:33 +010011892 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020011893 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011894 }
11895}
11896
11897
11898/*
11899 * This function tries to create the new ckch_inst and their SNIs
11900 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011901static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011902{
11903 struct stream_interface *si = appctx->owner;
11904 int y = 0;
11905 char *err = NULL;
11906 int errcode = 0;
11907 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11908 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011909 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011910 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011911 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011912
William Lallemand33cc76f2019-10-31 11:43:45 +010011913 if (trash == NULL)
11914 goto error;
11915
William Lallemand8f840d72019-10-23 10:53:05 +020011916 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11917 goto error;
11918
William Lallemand430413e2019-10-28 14:30:47 +010011919 while (1) {
11920 switch (appctx->st2) {
11921 case SETCERT_ST_INIT:
11922 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011923 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011924 if (ci_putchk(si_ic(si), trash) == -1) {
11925 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011926 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011927 }
11928 appctx->st2 = SETCERT_ST_GEN;
11929 /* fallthrough */
11930 case SETCERT_ST_GEN:
11931 /*
11932 * This state generates the ckch instances with their
11933 * sni_ctxs and SSL_CTX.
11934 *
William Lallemand430413e2019-10-28 14:30:47 +010011935 * Since the SSL_CTX generation can be CPU consumer, we
11936 * yield every 10 instances.
11937 */
William Lallemand8f840d72019-10-23 10:53:05 +020011938
William Lallemandbeea2a42019-10-30 17:45:33 +010011939 old_ckchs = appctx->ctx.ssl.old_ckchs;
11940 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011941
William Lallemandbeea2a42019-10-30 17:45:33 +010011942 if (!new_ckchs)
11943 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020011944
William Lallemandbeea2a42019-10-30 17:45:33 +010011945 /* get the next ckchi to regenerate */
11946 ckchi = appctx->ctx.ssl.next_ckchi;
11947 /* we didn't start yet, set it to the first elem */
11948 if (ckchi == NULL)
11949 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011950
William Lallemandbeea2a42019-10-30 17:45:33 +010011951 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
11952 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
11953 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010011954 char **sni_filter = NULL;
11955 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020011956
William Lallemandbeea2a42019-10-30 17:45:33 +010011957 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
11958 if (y >= 10) {
11959 /* save the next ckchi to compute */
11960 appctx->ctx.ssl.next_ckchi = ckchi;
11961 goto yield;
11962 }
William Lallemandcaa16192020-04-08 16:29:15 +020011963
11964 if (ckchi->crtlist_entry) {
11965 sni_filter = ckchi->crtlist_entry->filters;
11966 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010011967 }
William Lallemand38df1c82019-12-04 15:39:35 +010011968
William Lallemandbeea2a42019-10-30 17:45:33 +010011969 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010011970 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 +010011971 else
William Lallemand38df1c82019-12-04 15:39:35 +010011972 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
11973
William Lallemandbeea2a42019-10-30 17:45:33 +010011974 if (errcode & ERR_CODE)
11975 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020011976
William Lallemand21724f02019-11-04 17:56:13 +010011977 /* if the previous ckchi was used as the default */
11978 if (ckchi->is_default)
11979 new_inst->is_default = 1;
11980
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011981 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010011982 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11983 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011984 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 +010011985 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
11986 if (errcode & ERR_CODE)
11987 goto error;
11988 }
11989 }
11990
11991
William Lallemandbeea2a42019-10-30 17:45:33 +010011992 /* display one dot per new instance */
11993 chunk_appendf(trash, ".");
11994 /* link the new ckch_inst to the duplicate */
11995 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
11996 y++;
11997 }
William Lallemand430413e2019-10-28 14:30:47 +010011998 appctx->st2 = SETCERT_ST_INSERT;
11999 /* fallthrough */
12000 case SETCERT_ST_INSERT:
12001 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012002
William Lallemandbeea2a42019-10-30 17:45:33 +010012003 old_ckchs = appctx->ctx.ssl.old_ckchs;
12004 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012005
William Lallemandbeea2a42019-10-30 17:45:33 +010012006 if (!new_ckchs)
12007 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012008
William Lallemand90afe902020-03-30 19:29:45 +020012009 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12010 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12011 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12012 ebpt_delete(&entry->node);
12013 /* change the ptr and reinsert the node */
12014 entry->node.key = new_ckchs;
12015 ebpt_insert(&entry->crtlist->entries, &entry->node);
12016 }
12017
William Lallemand21724f02019-11-04 17:56:13 +010012018 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012019 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12020 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12021 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12022 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12023 }
William Lallemand8f840d72019-10-23 10:53:05 +020012024
William Lallemandbeea2a42019-10-30 17:45:33 +010012025 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12026 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemandd5e93772020-04-09 17:12:16 +020012027 struct bind_conf *bind_conf = ckchi->bind_conf;
12028
12029 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020012030 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020012031 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010012032 }
William Lallemand8f840d72019-10-23 10:53:05 +020012033
William Lallemandbeea2a42019-10-30 17:45:33 +010012034 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020012035 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010012036 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012037 appctx->st2 = SETCERT_ST_FIN;
12038 /* fallthrough */
12039 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012040 /* we achieved the transaction, we can set everything to NULL */
12041 free(ckchs_transaction.path);
12042 ckchs_transaction.path = NULL;
12043 ckchs_transaction.new_ckchs = NULL;
12044 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012045 goto end;
12046 }
William Lallemand8f840d72019-10-23 10:53:05 +020012047 }
William Lallemand430413e2019-10-28 14:30:47 +010012048end:
William Lallemand8f840d72019-10-23 10:53:05 +020012049
William Lallemanded442432019-11-21 16:41:07 +010012050 chunk_appendf(trash, "\n");
12051 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012052 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012053 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012054 if (ci_putchk(si_ic(si), trash) == -1)
12055 si_rx_room_blk(si);
12056 free_trash_chunk(trash);
12057 /* success: call the release function and don't come back */
12058 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012059yield:
12060 /* store the state */
12061 if (ci_putchk(si_ic(si), trash) == -1)
12062 si_rx_room_blk(si);
12063 free_trash_chunk(trash);
12064 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012065 return 0; /* should come back */
12066
12067error:
12068 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012069 if (trash) {
12070 chunk_appendf(trash, "\n%sFailed!\n", err);
12071 if (ci_putchk(si_ic(si), trash) == -1)
12072 si_rx_room_blk(si);
12073 free_trash_chunk(trash);
12074 }
William Lallemand430413e2019-10-28 14:30:47 +010012075 /* error: call the release function and don't come back */
12076 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012077}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012078
12079/*
12080 * Parsing function of 'commit ssl cert'
12081 */
12082static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12083{
12084 char *err = NULL;
12085
William Lallemand230662a2019-12-03 13:32:54 +010012086 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12087 return 1;
12088
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012089 if (!*args[3])
12090 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12091
12092 /* The operations on the CKCH architecture are locked so we can
12093 * manipulate ckch_store and ckch_inst */
12094 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12095 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12096
12097 if (!ckchs_transaction.path) {
12098 memprintf(&err, "No ongoing transaction! !\n");
12099 goto error;
12100 }
12101
12102 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12103 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12104 goto error;
12105 }
12106
William Lallemand4c5adbf2020-02-24 14:23:22 +010012107#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12108 if (ckchs_transaction.new_ckchs->multi) {
12109 int n;
12110
12111 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12112 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)) {
12113 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12114 goto error;
12115 }
12116 }
12117 } else
12118#endif
12119 {
12120 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12121 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12122 goto error;
12123 }
12124 }
12125
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012126 /* init the appctx structure */
12127 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012128 appctx->ctx.ssl.next_ckchi = NULL;
12129 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12130 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12131
12132 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12133 return 0;
12134
12135error:
12136
12137 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12138 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12139
12140 return cli_dynerr(appctx, err);
12141}
12142
William Lallemand8f840d72019-10-23 10:53:05 +020012143/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012144 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012145 */
William Lallemand150bfa82019-09-19 17:12:49 +020012146static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12147{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012148 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012149 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012150 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012151 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012152 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012153 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012154 char *end;
12155 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012156 struct cert_key_and_chain *ckch;
12157 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012158
William Lallemand230662a2019-12-03 13:32:54 +010012159 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12160 return 1;
12161
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012162 if ((buf = alloc_trash_chunk()) == NULL)
12163 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012164
12165 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012166 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012167
12168 /* The operations on the CKCH architecture are locked so we can
12169 * manipulate ckch_store and ckch_inst */
12170 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12171 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12172
William Lallemand8f840d72019-10-23 10:53:05 +020012173 if (!chunk_strcpy(buf, args[3])) {
12174 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12175 errcode |= ERR_ALERT | ERR_FATAL;
12176 goto end;
12177 }
12178
William Lallemand44b35322019-10-17 16:28:40 +020012179 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012180 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012181 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012182 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12183 *end = '\0';
12184 type = cert_exts[i].type;
12185 break;
12186 }
12187 }
12188
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012189 appctx->ctx.ssl.old_ckchs = NULL;
12190 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012191
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012192 /* if there is an ongoing transaction */
12193 if (ckchs_transaction.path) {
12194 /* 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 +020012195#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012196 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012197 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012198 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012199
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012200 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012201 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012202 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012203 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012204 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12205 bundle = j; /* keep the type of certificate so we insert it at the right place */
12206 *end = '\0'; /* it's a bundle let's end the string*/
12207 break;
12208 }
William Lallemand150bfa82019-09-19 17:12:49 +020012209 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012210 if (bundle < 0) {
12211 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);
12212 errcode |= ERR_ALERT | ERR_FATAL;
12213 goto end;
12214 }
12215 }
12216#endif
12217
12218 /* if there is an ongoing transaction, check if this is the same file */
12219 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12220 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12221 errcode |= ERR_ALERT | ERR_FATAL;
12222 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012223 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012224
12225 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12226
12227 } else {
12228 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12229
12230 /* lookup for the certificate in the tree:
12231 * check if this is used as a bundle AND as a unique certificate */
12232 for (i = 0; i < 2; i++) {
12233
12234 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12235 /* only the bundle name is in the tree and you should
12236 * never update a bundle name, only a filename */
12237 if (bundle < 0 && find_ckchs[i]->multi) {
12238 /* we tried to look for a non-bundle and we found a bundle */
12239 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12240 err ? err : "", args[3], args[3]);
12241 errcode |= ERR_ALERT | ERR_FATAL;
12242 goto end;
12243 }
William Lallemand3246d942019-11-04 14:02:11 +010012244 /* If we want a bundle but this is not a bundle
12245 * example: When you try to update <file>.rsa, but
12246 * <file> is a regular file */
12247 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12248 find_ckchs[i] = NULL;
12249 break;
12250 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012251 }
12252#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12253 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012254 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012255 int j;
12256
12257 /* check if it was used in a bundle by removing the
12258 * .dsa/.rsa/.ecdsa at the end of the filename */
12259 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012260 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012261 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12262 bundle = j; /* keep the type of certificate so we insert it at the right place */
12263 *end = '\0'; /* it's a bundle let's end the string*/
12264 break;
12265 }
12266 }
William Lallemand37031b82019-11-04 13:38:53 +010012267 if (bundle < 0) /* we didn't find a bundle extension */
12268 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012269 }
William Lallemand963b2e72019-10-14 11:38:36 +020012270#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012271 /* bundles are not supported here, so we don't need to lookup again */
12272 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012273#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012274 }
12275
12276 if (find_ckchs[0] && find_ckchs[1]) {
12277 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",
12278 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12279 errcode |= ERR_ALERT | ERR_FATAL;
12280 goto end;
12281 }
12282
12283 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012284 }
12285
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012286 if (!appctx->ctx.ssl.old_ckchs) {
12287 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012288 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012289 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012290 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012291 }
12292
William Lallemand8a7fdf02019-11-04 10:59:32 +010012293 if (!appctx->ctx.ssl.path) {
12294 /* this is a new transaction, set the path of the transaction */
12295 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12296 if (!appctx->ctx.ssl.path) {
12297 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12298 errcode |= ERR_ALERT | ERR_FATAL;
12299 goto end;
12300 }
12301 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012302
12303 old_ckchs = appctx->ctx.ssl.old_ckchs;
12304
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012305 /* duplicate the ckch store */
12306 new_ckchs = ckchs_dup(old_ckchs);
12307 if (!new_ckchs) {
12308 memprintf(&err, "%sCannot allocate memory!\n",
12309 err ? err : "");
12310 errcode |= ERR_ALERT | ERR_FATAL;
12311 goto end;
12312 }
12313
12314 if (!new_ckchs->multi)
12315 ckch = new_ckchs->ckch;
12316 else
12317 ckch = &new_ckchs->ckch[bundle];
12318
12319 /* appply the change on the duplicate */
12320 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12321 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12322 errcode |= ERR_ALERT | ERR_FATAL;
12323 goto end;
12324 }
12325
12326 appctx->ctx.ssl.new_ckchs = new_ckchs;
12327
12328 /* we succeed, we can save the ckchs in the transaction */
12329
12330 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012331 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012332 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12333 ckchs_transaction.path = appctx->ctx.ssl.path;
12334 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12335 } else {
12336 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12337
12338 }
12339
12340 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020012341 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012342
12343 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12344
12345
William Lallemand8f840d72019-10-23 10:53:05 +020012346 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012347
William Lallemand8f840d72019-10-23 10:53:05 +020012348end:
12349 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012350
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012351 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012352
William Lallemandba1c33f2020-04-08 17:55:45 +020012353 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012354 appctx->ctx.ssl.new_ckchs = NULL;
12355
12356 appctx->ctx.ssl.old_ckchs = NULL;
12357
12358 free(appctx->ctx.ssl.path);
12359 appctx->ctx.ssl.path = NULL;
12360
12361 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012362 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012363 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012364
12365 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12366 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012367 }
William Lallemand8f840d72019-10-23 10:53:05 +020012368 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012369}
12370
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012371/* parsing function of 'abort ssl cert' */
12372static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12373{
12374 char *err = NULL;
12375
William Lallemand230662a2019-12-03 13:32:54 +010012376 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12377 return 1;
12378
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012379 if (!*args[3])
12380 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12381
12382 /* The operations on the CKCH architecture are locked so we can
12383 * manipulate ckch_store and ckch_inst */
12384 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12385 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12386
12387 if (!ckchs_transaction.path) {
12388 memprintf(&err, "No ongoing transaction!\n");
12389 goto error;
12390 }
12391
12392 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12393 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12394 goto error;
12395 }
12396
12397 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020012398 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012399 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020012400 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012401 ckchs_transaction.old_ckchs = NULL;
12402 free(ckchs_transaction.path);
12403 ckchs_transaction.path = NULL;
12404
12405 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12406
12407 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12408 return cli_dynmsg(appctx, LOG_NOTICE, err);
12409
12410error:
12411 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12412
12413 return cli_dynerr(appctx, err);
12414}
12415
William Lallemandea987ed2020-03-19 16:48:33 +010012416/* parsing function of 'new ssl cert' */
12417static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12418{
12419 struct ckch_store *store;
12420 char *err = NULL;
12421 char *path;
12422
12423 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12424 return 1;
12425
12426 if (!*args[3])
12427 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12428
12429 path = args[3];
12430
12431 /* The operations on the CKCH architecture are locked so we can
12432 * manipulate ckch_store and ckch_inst */
12433 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12434 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12435
12436 store = ckchs_lookup(path);
12437 if (store != NULL) {
12438 memprintf(&err, "Certificate '%s' already exists!\n", path);
12439 store = NULL; /* we don't want to free it */
12440 goto error;
12441 }
William Lallemand8a874e42020-04-09 10:32:53 +020012442 /* we won't support multi-certificate bundle here */
12443 store = ckch_store_new(path, 1);
William Lallemandea987ed2020-03-19 16:48:33 +010012444 if (!store) {
12445 memprintf(&err, "unable to allocate memory.\n");
12446 goto error;
12447 }
William Lallemandea987ed2020-03-19 16:48:33 +010012448
12449 /* insert into the ckchs tree */
William Lallemandea987ed2020-03-19 16:48:33 +010012450 ebst_insert(&ckchs_tree, &store->node);
12451 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12452
12453 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12454 return cli_dynmsg(appctx, LOG_NOTICE, err);
12455error:
12456 free(store);
12457 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12458 return cli_dynerr(appctx, err);
12459}
12460
William Lallemand419e6342020-04-08 12:05:39 +020012461/* parsing function of 'del ssl cert' */
12462static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12463{
12464 struct ckch_store *store;
12465 char *err = NULL;
12466 char *filename;
12467
12468 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12469 return 1;
12470
12471 if (!*args[3])
12472 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12473
12474 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12475 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12476
12477 filename = args[3];
12478
12479 store = ckchs_lookup(filename);
12480 if (store == NULL) {
12481 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12482 goto error;
12483 }
12484 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12485 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12486 goto error;
12487 }
12488
12489 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020012490 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020012491
12492 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12493
12494 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12495 return cli_dynmsg(appctx, LOG_NOTICE, err);
12496
12497error:
12498 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12499 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12500 return cli_dynerr(appctx, err);
12501}
12502
12503
12504
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012505static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012506{
12507#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12508 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012509 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012510
12511 if (!payload)
12512 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012513
12514 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012515 if (!*payload)
12516 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012517
12518 /* remove \r and \n from the payload */
12519 for (i = 0, j = 0; payload[i]; i++) {
12520 if (payload[i] == '\r' || payload[i] == '\n')
12521 continue;
12522 payload[j++] = payload[i];
12523 }
12524 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012525
Willy Tarreau1c913e42018-08-22 05:26:57 +020012526 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012527 if (ret < 0)
12528 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012529
Willy Tarreau1c913e42018-08-22 05:26:57 +020012530 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012531 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012532 if (err)
12533 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12534 else
12535 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012536 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012537
12538 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012539#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012540 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 +020012541#endif
12542
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012543}
12544
Willy Tarreau86a394e2019-05-09 14:15:32 +020012545#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012546static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12547{
12548 switch (arg->type) {
12549 case ARGT_STR:
12550 smp->data.type = SMP_T_STR;
12551 smp->data.u.str = arg->data.str;
12552 return 1;
12553 case ARGT_VAR:
12554 if (!vars_get_by_desc(&arg->data.var, smp))
12555 return 0;
12556 if (!sample_casts[smp->data.type][SMP_T_STR])
12557 return 0;
12558 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12559 return 0;
12560 return 1;
12561 default:
12562 return 0;
12563 }
12564}
12565
12566static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12567 const char *file, int line, char **err)
12568{
12569 switch(args[0].data.sint) {
12570 case 128:
12571 case 192:
12572 case 256:
12573 break;
12574 default:
12575 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12576 return 0;
12577 }
12578 /* Try to decode a variable. */
12579 vars_check_arg(&args[1], NULL);
12580 vars_check_arg(&args[2], NULL);
12581 vars_check_arg(&args[3], NULL);
12582 return 1;
12583}
12584
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012585/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012586static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12587{
12588 struct sample nonce, key, aead_tag;
12589 struct buffer *smp_trash, *smp_trash_alloc;
12590 EVP_CIPHER_CTX *ctx;
12591 int dec_size, ret;
12592
12593 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12594 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12595 return 0;
12596
12597 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12598 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12599 return 0;
12600
12601 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12602 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12603 return 0;
12604
12605 smp_trash = get_trash_chunk();
12606 smp_trash_alloc = alloc_trash_chunk();
12607 if (!smp_trash_alloc)
12608 return 0;
12609
12610 ctx = EVP_CIPHER_CTX_new();
12611
12612 if (!ctx)
12613 goto err;
12614
12615 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12616 if (dec_size < 0)
12617 goto err;
12618 smp_trash->data = dec_size;
12619
12620 /* Set cipher type and mode */
12621 switch(arg_p[0].data.sint) {
12622 case 128:
12623 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12624 break;
12625 case 192:
12626 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12627 break;
12628 case 256:
12629 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12630 break;
12631 }
12632
12633 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12634
12635 /* Initialise IV */
12636 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12637 goto err;
12638
12639 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12640 if (dec_size < 0)
12641 goto err;
12642 smp_trash->data = dec_size;
12643
12644 /* Initialise key */
12645 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12646 goto err;
12647
12648 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12649 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12650 goto err;
12651
12652 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12653 if (dec_size < 0)
12654 goto err;
12655 smp_trash_alloc->data = dec_size;
12656 dec_size = smp_trash->data;
12657
12658 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12659 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12660
12661 if (ret <= 0)
12662 goto err;
12663
12664 smp->data.u.str.data = dec_size + smp_trash->data;
12665 smp->data.u.str.area = smp_trash->area;
12666 smp->data.type = SMP_T_BIN;
12667 smp->flags &= ~SMP_F_CONST;
12668 free_trash_chunk(smp_trash_alloc);
12669 return 1;
12670
12671err:
12672 free_trash_chunk(smp_trash_alloc);
12673 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012674}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012675# endif
William Lallemand32af2032016-10-29 18:09:35 +020012676
Elliot Otchet71f82972020-01-15 08:12:14 -050012677/* Argument validation functions */
12678
12679/* This function is used to validate the arguments passed to any "x_dn" ssl
12680 * keywords. These keywords support specifying a third parameter that must be
12681 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12682 */
12683int val_dnfmt(struct arg *arg, char **err_msg)
12684{
12685 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12686 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12687 return 0;
12688 }
12689 return 1;
12690}
12691
William Lallemand32af2032016-10-29 18:09:35 +020012692/* register cli keywords */
12693static struct cli_kw_list cli_kws = {{ },{
12694#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12695 { { "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 +020012696 { { "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 +020012697#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012698 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012699 { { "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 +010012700 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12701 { { "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 +010012702 { { "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 +020012703 { { "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 +010012704 { { "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 +010012705 { { "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 +020012706 { { "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 +020012707 { { "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 +020012708 { { NULL }, NULL, NULL, NULL }
12709}};
12710
Willy Tarreau0108d902018-11-25 19:14:37 +010012711INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012712
Willy Tarreau7875d092012-09-10 08:20:03 +020012713/* Note: must not be declared <const> as its list will be overwritten.
12714 * Please take care of keeping this list alphabetically sorted.
12715 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012716static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012717 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012718 { "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 +010012719#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012720 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012721#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012722 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012723#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12724 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12725#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012726 { "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 +020012727 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012728 { "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 +020012729 { "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 +020012730#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012731 { "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 -040012732#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012733#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012734 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12735 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012736 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12737#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012738 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12739 { "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 +010012740 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012741 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012742 { "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 +020012743 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12744 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12745 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12746 { "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 -050012747 { "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 +020012748 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12749 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012750 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012751 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12752 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012753 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012754 { "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 +020012755 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12756 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12757 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12758 { "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 -050012759 { "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 +020012760 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012761 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012762 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012763 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012764 { "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 +010012765 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012766 { "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 +020012767 { "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 +010012768 { "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 +020012769 { "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 +010012770#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012771 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012772#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012773#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012774 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012775#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012776 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012777#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012778 { "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 -040012779#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012780 { "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 +020012781#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012782 { "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 -040012783#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012784#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012785 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12786 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012787 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12788#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012789#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012790 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012791#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012792 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12793 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12794 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12795 { "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 +020012796 { NULL, NULL, 0, 0, 0 },
12797}};
12798
Willy Tarreau0108d902018-11-25 19:14:37 +010012799INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12800
Willy Tarreau7875d092012-09-10 08:20:03 +020012801/* Note: must not be declared <const> as its list will be overwritten.
12802 * Please take care of keeping this list alphabetically sorted.
12803 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012804static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012805 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12806 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012807 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012808}};
12809
Willy Tarreau0108d902018-11-25 19:14:37 +010012810INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12811
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012812/* Note: must not be declared <const> as its list will be overwritten.
12813 * Please take care of keeping this list alphabetically sorted, doing so helps
12814 * all code contributors.
12815 * Optional keywords are also declared with a NULL ->parse() function so that
12816 * the config parser can report an appropriate error when a known keyword was
12817 * not enabled.
12818 */
William Lallemand557823f2020-04-01 17:42:47 +020012819
12820/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12821 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012822static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012823 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012824 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012825 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12826 { "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 +010012827 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012828#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012829 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12830#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012831 { "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 +010012832 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012833 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012834 { "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 +010012835 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012836 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12837 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012838 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12839 { NULL, NULL, 0 },
12840};
12841
Willy Tarreau0108d902018-11-25 19:14:37 +010012842/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12843
Willy Tarreau51fb7652012-09-18 18:24:39 +020012844static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012845 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012846 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012847 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12848 { "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 +020012849 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12850 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12851 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12852 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012853#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012854 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12855#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012856 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012857 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012858 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012859 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12860 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12861 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12862 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12863 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12864 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12865 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012866 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012867 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012868 { "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 +020012869 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12870 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12871 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12872 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012873 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012874 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12875 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012876 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12877 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012878 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12879 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12880 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12881 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12882 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012883 { NULL, NULL, 0 },
12884}};
Emeric Brun46591952012-05-18 15:47:34 +020012885
Willy Tarreau0108d902018-11-25 19:14:37 +010012886INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12887
Willy Tarreau92faadf2012-10-10 23:04:25 +020012888/* Note: must not be declared <const> as its list will be overwritten.
12889 * Please take care of keeping this list alphabetically sorted, doing so helps
12890 * all code contributors.
12891 * Optional keywords are also declared with a NULL ->parse() function so that
12892 * the config parser can report an appropriate error when a known keyword was
12893 * not enabled.
12894 */
12895static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012896 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012897 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012898 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012899 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012900 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012901 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12902 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012903#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012904 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12905#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012906 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12907 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12908 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12909 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12910 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12911 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12912 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12913 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12914 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12915 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12916 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12917 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12918 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12919 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12920 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12921 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12922 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12923 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012924 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012925 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12926 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12927 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12928 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12929 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12930 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12931 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12932 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12933 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12934 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012935 { NULL, NULL, 0, 0 },
12936}};
12937
Willy Tarreau0108d902018-11-25 19:14:37 +010012938INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12939
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012940static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012941 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12942 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012943 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012944 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012945 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12946 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012947#ifndef OPENSSL_NO_DH
12948 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12949#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012950 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012951#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012952 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012953#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010012954 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12955#ifndef OPENSSL_NO_DH
12956 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12957#endif
12958 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12959 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12960 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12961 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012962 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012963 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
12964 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012965#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012966 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
12967 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
12968#endif
William Lallemand3af48e72020-02-03 17:15:52 +010012969 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012970 { 0, NULL, NULL },
12971}};
12972
Willy Tarreau0108d902018-11-25 19:14:37 +010012973INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
12974
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012975/* Note: must not be declared <const> as its list will be overwritten */
12976static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020012977#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012978 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
12979#endif
12980 { NULL, NULL, 0, 0, 0 },
12981}};
12982
12983INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
12984
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020012985/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010012986static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020012987 .snd_buf = ssl_sock_from_buf,
12988 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010012989 .subscribe = ssl_subscribe,
12990 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020012991 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020012992 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020012993 .rcv_pipe = NULL,
12994 .snd_pipe = NULL,
12995 .shutr = NULL,
12996 .shutw = ssl_sock_shutw,
12997 .close = ssl_sock_close,
12998 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010012999 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010013000 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010013001 .prepare_srv = ssl_sock_prepare_srv_ctx,
13002 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013003 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013004 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013005};
13006
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013007enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13008 struct session *sess, struct stream *s, int flags)
13009{
13010 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013011 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013012
13013 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013014 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013015
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013016 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013017 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013018 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013019 s->req.flags |= CF_READ_NULL;
13020 return ACT_RET_YIELD;
13021 }
13022 }
13023 return (ACT_RET_CONT);
13024}
13025
13026static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13027{
13028 rule->action_ptr = ssl_action_wait_for_hs;
13029
13030 return ACT_RET_PRS_OK;
13031}
13032
13033static struct action_kw_list http_req_actions = {ILH, {
13034 { "wait-for-handshake", ssl_parse_wait_for_hs },
13035 { /* END */ }
13036}};
13037
Willy Tarreau0108d902018-11-25 19:14:37 +010013038INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13039
Willy Tarreau5db847a2019-05-09 14:13:35 +020013040#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013041
13042static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13043{
13044 if (ptr) {
13045 chunk_destroy(ptr);
13046 free(ptr);
13047 }
13048}
13049
13050#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013051static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13052{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013053 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013054}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013055
Emeric Brun46591952012-05-18 15:47:34 +020013056__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013057static void __ssl_sock_init(void)
13058{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013059#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013060 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013061 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013062#endif
Emeric Brun46591952012-05-18 15:47:34 +020013063
Willy Tarreauef934602016-12-22 23:12:01 +010013064 if (global_ssl.listen_default_ciphers)
13065 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13066 if (global_ssl.connect_default_ciphers)
13067 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013068#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013069 if (global_ssl.listen_default_ciphersuites)
13070 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13071 if (global_ssl.connect_default_ciphersuites)
13072 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13073#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013074
Willy Tarreau13e14102016-12-22 20:25:26 +010013075 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013076#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013077 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013078#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013079#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013080 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013081 n = sk_SSL_COMP_num(cm);
13082 while (n--) {
13083 (void) sk_SSL_COMP_pop(cm);
13084 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013085#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013086
Willy Tarreau5db847a2019-05-09 14:13:35 +020013087#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013088 ssl_locking_init();
13089#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013090#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013091 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13092#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013093 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013094 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 +020013095#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013096 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013097 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013098#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013099#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13100 hap_register_post_check(tlskeys_finalize_config);
13101#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013102
13103 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13104 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13105
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013106 hap_register_post_deinit(ssl_free_global_issuers);
13107
Willy Tarreau80713382018-11-26 10:19:54 +010013108#ifndef OPENSSL_NO_DH
13109 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13110 hap_register_post_deinit(ssl_free_dh);
13111#endif
13112#ifndef OPENSSL_NO_ENGINE
13113 hap_register_post_deinit(ssl_free_engines);
13114#endif
13115 /* Load SSL string for the verbose & debug mode. */
13116 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013117 ha_meth = BIO_meth_new(0x666, "ha methods");
13118 BIO_meth_set_write(ha_meth, ha_ssl_write);
13119 BIO_meth_set_read(ha_meth, ha_ssl_read);
13120 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13121 BIO_meth_set_create(ha_meth, ha_ssl_new);
13122 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13123 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13124 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013125
13126 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013127}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013128
Willy Tarreau80713382018-11-26 10:19:54 +010013129/* Compute and register the version string */
13130static void ssl_register_build_options()
13131{
13132 char *ptr = NULL;
13133 int i;
13134
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013135 memprintf(&ptr, "Built with OpenSSL version : "
13136#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013137 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013138#else /* OPENSSL_IS_BORINGSSL */
13139 OPENSSL_VERSION_TEXT
13140 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013141 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013142 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013143#endif
13144 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013145#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013146 "no (library version too old)"
13147#elif defined(OPENSSL_NO_TLSEXT)
13148 "no (disabled via OPENSSL_NO_TLSEXT)"
13149#else
13150 "yes"
13151#endif
13152 "", ptr);
13153
13154 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13155#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13156 "yes"
13157#else
13158#ifdef OPENSSL_NO_TLSEXT
13159 "no (because of OPENSSL_NO_TLSEXT)"
13160#else
13161 "no (version might be too old, 0.9.8f min needed)"
13162#endif
13163#endif
13164 "", ptr);
13165
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013166 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13167 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13168 if (methodVersions[i].option)
13169 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013170
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013171 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013172}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013173
Willy Tarreau80713382018-11-26 10:19:54 +010013174INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013175
Emeric Brun46591952012-05-18 15:47:34 +020013176
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013177#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013178void ssl_free_engines(void) {
13179 struct ssl_engine_list *wl, *wlb;
13180 /* free up engine list */
13181 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13182 ENGINE_finish(wl->e);
13183 ENGINE_free(wl->e);
13184 LIST_DEL(&wl->list);
13185 free(wl);
13186 }
13187}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013188#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013189
Remi Gacogned3a23c32015-05-28 16:39:47 +020013190#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013191void ssl_free_dh(void) {
13192 if (local_dh_1024) {
13193 DH_free(local_dh_1024);
13194 local_dh_1024 = NULL;
13195 }
13196 if (local_dh_2048) {
13197 DH_free(local_dh_2048);
13198 local_dh_2048 = NULL;
13199 }
13200 if (local_dh_4096) {
13201 DH_free(local_dh_4096);
13202 local_dh_4096 = NULL;
13203 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013204 if (global_dh) {
13205 DH_free(global_dh);
13206 global_dh = NULL;
13207 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013208}
13209#endif
13210
13211__attribute__((destructor))
13212static void __ssl_sock_deinit(void)
13213{
13214#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013215 if (ssl_ctx_lru_tree) {
13216 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013217 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013218 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013219#endif
13220
Willy Tarreau5db847a2019-05-09 14:13:35 +020013221#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013222 ERR_remove_state(0);
13223 ERR_free_strings();
13224
13225 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013226#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013227
Willy Tarreau5db847a2019-05-09 14:13:35 +020013228#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013229 CRYPTO_cleanup_all_ex_data();
13230#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013231 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013232}
13233
13234
Emeric Brun46591952012-05-18 15:47:34 +020013235/*
13236 * Local variables:
13237 * c-indent-level: 8
13238 * c-basic-offset: 8
13239 * End:
13240 */