blob: 215dcc00f0ce674873c5a248c1a83fa65e9b1849 [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
3808/* allocate and duplicate a ckch_store
3809 * Return a new ckch_store or NULL */
3810static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3811{
3812 struct ckch_store *dst;
3813 int pathlen;
3814
3815 pathlen = strlen(src->path);
3816 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3817 if (!dst)
3818 return NULL;
3819 /* copy previous key */
3820 memcpy(dst->path, src->path, pathlen + 1);
3821 dst->multi = src->multi;
3822 LIST_INIT(&dst->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +02003823 LIST_INIT(&dst->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003824
3825 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3826 if (!dst->ckch)
3827 goto error;
3828
3829#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3830 if (src->multi) {
3831 int n;
3832
3833 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3834 if (&src->ckch[n]) {
3835 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3836 goto error;
3837 }
3838 }
3839 } else
3840#endif
3841 {
3842 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3843 goto error;
3844 }
3845
3846 return dst;
3847
3848error:
William Lallemandba1c33f2020-04-08 17:55:45 +02003849 ckch_store_free(dst);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003850
3851 return NULL;
3852}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003853
William Lallemand36b84632019-07-18 19:28:17 +02003854/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003855 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003856 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003857static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003858{
3859 struct ebmb_node *eb;
3860
William Lallemande3af8fb2019-10-08 11:36:53 +02003861 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003862 if (!eb)
3863 return NULL;
3864
William Lallemande3af8fb2019-10-08 11:36:53 +02003865 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003866}
3867
3868/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003869 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003870 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003871static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003872{
William Lallemande3af8fb2019-10-08 11:36:53 +02003873 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003874
William Lallemande3af8fb2019-10-08 11:36:53 +02003875 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3876 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003877 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3878 goto end;
3879 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003880 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003881
William Lallemande3af8fb2019-10-08 11:36:53 +02003882 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003883 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3884 goto end;
3885 }
3886
William Lallemand9117de92019-10-04 00:29:42 +02003887 LIST_INIT(&ckchs->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +02003888 LIST_INIT(&ckchs->crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02003889
William Lallemand36b84632019-07-18 19:28:17 +02003890 if (!multi) {
3891
William Lallemand96a9c972019-10-17 11:56:17 +02003892 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003893 goto end;
3894
William Lallemande3af8fb2019-10-08 11:36:53 +02003895 /* insert into the ckchs tree */
3896 memcpy(ckchs->path, path, strlen(path) + 1);
3897 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003898 } else {
3899 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003900#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3901 char fp[MAXPATHLEN+1] = {0};
3902 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003903
3904 /* Load all possible certs and keys */
3905 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3906 struct stat buf;
3907 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3908 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003909 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003910 goto end;
3911 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003912 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003913 }
3914 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003915#endif
William Lallemand36b84632019-07-18 19:28:17 +02003916
3917 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003918 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003919 goto end;
3920 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003921 /* insert into the ckchs tree */
3922 memcpy(ckchs->path, path, strlen(path) + 1);
3923 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003924 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003925 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003926
William Lallemand36b84632019-07-18 19:28:17 +02003927end:
William Lallemandba1c33f2020-04-08 17:55:45 +02003928 ckch_store_free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003929
3930 return NULL;
3931}
3932
William Lallemandc4ecddf2019-07-31 16:50:08 +02003933#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3934
William Lallemand36b84632019-07-18 19:28:17 +02003935/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003936 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003937 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003938 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3939 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003940 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003941 *
Emeric Brun054563d2019-10-17 13:16:58 +02003942 * Returns a bitfield containing the flags:
3943 * ERR_FATAL in any fatal error case
3944 * ERR_ALERT if the reason of the error is available in err
3945 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003946 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003947 */
Emeric Brun054563d2019-10-17 13:16:58 +02003948static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3949 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3950 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003951{
William Lallemand36b84632019-07-18 19:28:17 +02003952 int i = 0, n = 0;
3953 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003954 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003955 struct ebmb_node *node;
3956 struct ebmb_node *next;
3957 /* Array of SSL_CTX pointers corresponding to each possible combo
3958 * of keytypes
3959 */
3960 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003961 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003962 X509_NAME *xname = NULL;
3963 char *str = NULL;
3964#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3965 STACK_OF(GENERAL_NAME) *names = NULL;
3966#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003967 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003968
Emeric Brun054563d2019-10-17 13:16:58 +02003969 *ckchi = NULL;
3970
William Lallemande3af8fb2019-10-08 11:36:53 +02003971 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003972 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3973 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003974 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003975 }
3976
3977 ckch_inst = ckch_inst_new();
3978 if (!ckch_inst) {
3979 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3980 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003981 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003982 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003983 }
3984
William Lallemande3af8fb2019-10-08 11:36:53 +02003985 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003986
yanbzhu08ce6ab2015-12-02 13:01:29 -05003987 /* Process each ckch and update keytypes for each CN/SAN
3988 * for example, if CN/SAN www.a.com is associated with
3989 * certs with keytype 0 and 2, then at the end of the loop,
3990 * www.a.com will have:
3991 * keyindex = 0 | 1 | 4 = 5
3992 */
3993 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003994 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003995
3996 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3997 continue;
3998
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003999 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004000 for (i = 0; i < fcount; i++) {
4001 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4002 if (ret < 0) {
4003 memprintf(err, "%sunable to allocate SSL context.\n",
4004 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004005 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004006 goto end;
4007 }
4008 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004009 } else {
4010 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4011 * so the line that contains logic is marked via comments
4012 */
4013 xname = X509_get_subject_name(certs_and_keys[n].cert);
4014 i = -1;
4015 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4016 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004017 ASN1_STRING *value;
4018 value = X509_NAME_ENTRY_get_data(entry);
4019 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004020 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004021 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004022
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004023 OPENSSL_free(str);
4024 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004025 if (ret < 0) {
4026 memprintf(err, "%sunable to allocate SSL context.\n",
4027 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004028 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004029 goto end;
4030 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004031 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004032 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004033
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004034 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004035#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004036 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4037 if (names) {
4038 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4039 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004040
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004041 if (name->type == GEN_DNS) {
4042 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4043 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004044 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004045
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004046 OPENSSL_free(str);
4047 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004048 if (ret < 0) {
4049 memprintf(err, "%sunable to allocate SSL context.\n",
4050 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004051 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004052 goto end;
4053 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004054 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004055 }
4056 }
4057 }
4058 }
4059#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4060 }
4061
4062 /* If no files found, return error */
4063 if (eb_is_empty(&sni_keytypes_map)) {
4064 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4065 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004066 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004067 goto end;
4068 }
4069
4070 /* We now have a map of CN/SAN to keytypes that are loaded in
4071 * Iterate through the map to create the SSL_CTX's (if needed)
4072 * and add each CTX to the SNI tree
4073 *
4074 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004075 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004076 * combination is denoted by the key in the map. Each key
4077 * has a value between 1 and 2^n - 1. Conveniently, the array
4078 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4079 * entry in the array to correspond to the unique combo (key)
4080 * associated with i. This unique key combo (i) will be associated
4081 * with combos[i-1]
4082 */
4083
4084 node = ebmb_first(&sni_keytypes_map);
4085 while (node) {
4086 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004087 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004088 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004089
4090 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4091 i = container_of(node, struct sni_keytype, name)->keytypes;
4092 cur_ctx = key_combos[i-1].ctx;
4093
4094 if (cur_ctx == NULL) {
4095 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004096 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004097 if (cur_ctx == NULL) {
4098 memprintf(err, "%sunable to allocate SSL context.\n",
4099 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004100 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004101 goto end;
4102 }
4103
yanbzhube2774d2015-12-10 15:07:30 -05004104 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004105 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4106 if (i & (1<<n)) {
4107 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004108 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004109 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4110 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004111 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004112 }
4113 }
4114
yanbzhu08ce6ab2015-12-02 13:01:29 -05004115 /* Update key_combos */
4116 key_combos[i-1].ctx = cur_ctx;
4117 }
4118
4119 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004120
William Lallemand1d29c742019-10-04 00:53:29 +02004121 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 +02004122 kinfo, str, key_combos[i-1].order);
4123 if (key_combos[i-1].order < 0) {
4124 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004125 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004126 goto end;
4127 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004128 node = ebmb_next(node);
4129 }
4130
4131
4132 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4133 if (!bind_conf->default_ctx) {
4134 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4135 if (key_combos[i].ctx) {
4136 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004137 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004138 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004139 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004140 break;
4141 }
4142 }
4143 }
4144
William Lallemand614ca0d2019-10-07 13:52:11 +02004145 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004146 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004147 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004148
yanbzhu08ce6ab2015-12-02 13:01:29 -05004149end:
4150
4151 if (names)
4152 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4153
yanbzhu08ce6ab2015-12-02 13:01:29 -05004154 node = ebmb_first(&sni_keytypes_map);
4155 while (node) {
4156 next = ebmb_next(node);
4157 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004158 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004159 node = next;
4160 }
4161
William Lallemand02e19a52020-04-08 16:11:26 +02004162 /* we need to free the ctx since we incremented the refcount where it's used */
4163 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4164 if (key_combos[i].ctx)
4165 SSL_CTX_free(key_combos[i].ctx);
4166 }
4167
Emeric Brun054563d2019-10-17 13:16:58 +02004168 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004169 if (ckch_inst->is_default) {
4170 SSL_CTX_free(bind_conf->default_ctx);
4171 bind_conf->default_ctx = NULL;
4172 }
4173
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004174 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004175 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004176 }
4177
Emeric Brun054563d2019-10-17 13:16:58 +02004178 *ckchi = ckch_inst;
4179 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004180}
4181#else
4182/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004183static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4184 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4185 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004186{
4187 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4188 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004189 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004190}
4191
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004192#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004193
William Lallemand614ca0d2019-10-07 13:52:11 +02004194/*
4195 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004196 *
4197 * Returns a bitfield containing the flags:
4198 * ERR_FATAL in any fatal error case
4199 * ERR_ALERT if the reason of the error is available in err
4200 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004201 */
Emeric Brun054563d2019-10-17 13:16:58 +02004202static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4203 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004204{
William Lallemandc9402072019-05-15 15:33:54 +02004205 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004206 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004207 int order = 0;
4208 X509_NAME *xname;
4209 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004210 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004211 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004212#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4213 STACK_OF(GENERAL_NAME) *names;
4214#endif
William Lallemand36b84632019-07-18 19:28:17 +02004215 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004216 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004217 int errcode = 0;
4218
4219 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004220
William Lallemande3af8fb2019-10-08 11:36:53 +02004221 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004222 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004223
William Lallemande3af8fb2019-10-08 11:36:53 +02004224 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004225
William Lallemandc9402072019-05-15 15:33:54 +02004226 ctx = SSL_CTX_new(SSLv23_server_method());
4227 if (!ctx) {
4228 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4229 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004230 errcode |= ERR_ALERT | ERR_FATAL;
4231 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004232 }
4233
Emeric Bruna96b5822019-10-17 13:25:14 +02004234 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4235 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004236 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004237
4238 ckch_inst = ckch_inst_new();
4239 if (!ckch_inst) {
4240 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4241 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004242 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004243 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004244 }
4245
William Lallemand36b84632019-07-18 19:28:17 +02004246 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004247 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004248 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004249 switch(EVP_PKEY_base_id(pkey)) {
4250 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004251 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004252 break;
4253 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004254 kinfo.sig = TLSEXT_signature_ecdsa;
4255 break;
4256 case EVP_PKEY_DSA:
4257 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004258 break;
4259 }
4260 EVP_PKEY_free(pkey);
4261 }
4262
Emeric Brun50bcecc2013-04-22 13:05:23 +02004263 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004264 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004265 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 +02004266 if (order < 0) {
4267 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004268 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004269 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004270 }
4271 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004272 }
4273 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004274#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004275 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004276 if (names) {
4277 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4278 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4279 if (name->type == GEN_DNS) {
4280 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004281 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004282 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004283 if (order < 0) {
4284 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004285 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004286 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004287 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004288 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004289 }
4290 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004291 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004292 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004293#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004294 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004295 i = -1;
4296 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4297 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004298 ASN1_STRING *value;
4299
4300 value = X509_NAME_ENTRY_get_data(entry);
4301 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004302 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004303 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004304 if (order < 0) {
4305 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004306 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004307 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004308 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004309 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004310 }
4311 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004312 /* we must not free the SSL_CTX anymore below, since it's already in
4313 * the tree, so it will be discovered and cleaned in time.
4314 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004315
Emeric Brunfc0421f2012-09-07 17:30:07 +02004316#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004317 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004318 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4319 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;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004322 }
4323#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004324 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004325 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004326 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004327 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004328 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004329 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004330
William Lallemand9117de92019-10-04 00:29:42 +02004331 /* everything succeed, the ckch instance can be used */
4332 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004333 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004334 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004335
William Lallemand02e19a52020-04-08 16:11:26 +02004336 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4337
Emeric Brun054563d2019-10-17 13:16:58 +02004338 *ckchi = ckch_inst;
4339 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004340
4341error:
4342 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004343 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004344 if (ckch_inst->is_default)
4345 SSL_CTX_free(ctx);
4346
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004347 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004348 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004349 }
William Lallemandd9199372019-10-04 15:37:05 +02004350 SSL_CTX_free(ctx);
4351
Emeric Brun054563d2019-10-17 13:16:58 +02004352 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004353}
4354
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004355/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004356static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4357 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004358 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004359{
Emeric Brun054563d2019-10-17 13:16:58 +02004360 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004361
4362 /* we found the ckchs in the tree, we can use it directly */
4363 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004364 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 +02004365 else
William Lallemand24bde432020-03-09 16:48:43 +01004366 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 +02004367
Emeric Brun054563d2019-10-17 13:16:58 +02004368 if (errcode & ERR_CODE)
4369 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004370
William Lallemand24bde432020-03-09 16:48:43 +01004371 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004372
4373 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004374 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004375 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004376}
4377
William Lallemand6be66ec2020-03-06 22:26:32 +01004378
William Lallemand4c68bba2020-03-30 18:45:10 +02004379
4380
4381/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4382 * done once. Zero is returned if the operation fails. No error is returned
4383 * if the random is said as not implemented, because we expect that openssl
4384 * will use another method once needed.
4385 */
4386static int ssl_initialize_random()
4387{
4388 unsigned char random;
4389 static int random_initialized = 0;
4390
4391 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4392 random_initialized = 1;
4393
4394 return random_initialized;
4395}
4396
4397/* release ssl bind conf */
4398void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
4399{
4400 if (conf) {
4401#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4402 free(conf->npn_str);
4403 conf->npn_str = NULL;
4404#endif
4405#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4406 free(conf->alpn_str);
4407 conf->alpn_str = NULL;
4408#endif
4409 free(conf->ca_file);
4410 conf->ca_file = NULL;
4411 free(conf->ca_verify_file);
4412 conf->ca_verify_file = NULL;
4413 free(conf->crl_file);
4414 conf->crl_file = NULL;
4415 free(conf->ciphers);
4416 conf->ciphers = NULL;
4417#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
4418 free(conf->ciphersuites);
4419 conf->ciphersuites = NULL;
4420#endif
4421 free(conf->curves);
4422 conf->curves = NULL;
4423 free(conf->ecdhe);
4424 conf->ecdhe = NULL;
4425 }
4426}
4427/* free sni filters */
4428static void crtlist_free_filters(char **args)
4429{
4430 int i;
4431
4432 if (!args)
4433 return;
4434
4435 for (i = 0; args[i]; i++)
4436 free(args[i]);
4437
4438 free(args);
4439}
4440
4441/* Alloc and duplicate a char ** array */
4442static char **crtlist_dup_filters(char **args, int fcount)
4443{
4444 char **dst;
4445 int i;
4446
4447 if (fcount == 0)
4448 return NULL;
4449
4450 dst = calloc(fcount + 1, sizeof(*dst));
4451 if (!dst)
4452 return NULL;
4453
4454 for (i = 0; i < fcount; i++) {
4455 dst[i] = strdup(args[i]);
4456 if (!dst[i])
4457 goto error;
4458 }
4459 return dst;
4460
4461error:
4462 crtlist_free_filters(dst);
4463 return NULL;
4464}
4465
4466
4467/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
4468static void crtlist_free(struct crtlist *crtlist)
4469{
4470 struct crtlist_entry *entry, *s_entry;
4471
4472 if (crtlist == NULL)
4473 return;
4474
4475 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
4476 ebpt_delete(&entry->node);
4477 LIST_DEL(&entry->by_crtlist);
4478 crtlist_free_filters(entry->filters);
4479 ssl_sock_free_ssl_conf(entry->ssl_conf);
4480 free(entry->ssl_conf);
4481 free(entry);
4482 }
4483 ebmb_delete(&crtlist->node);
4484 free(crtlist);
4485}
4486
William Lallemand6be66ec2020-03-06 22:26:32 +01004487/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4488 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4489 *
4490 * This function tries to open and store certificate files.
4491 */
4492static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004493{
William Lallemand6be66ec2020-03-06 22:26:32 +01004494 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004495 struct dirent **de_list;
4496 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004497 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004498 char *end;
4499 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004500 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004501 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004502#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004503 int is_bundle;
4504 int j;
4505#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004506
William Lallemand6be66ec2020-03-06 22:26:32 +01004507 dir = malloc(sizeof(*dir) + strlen(path) + 1);
4508 if (dir == NULL) {
4509 memprintf(err, "not enough memory");
4510 return ERR_ALERT | ERR_FATAL;
4511 }
4512 memcpy(dir->node.key, path, strlen(path) + 1);
4513 dir->entries = EB_ROOT_UNIQUE; /* it's a directory, files are unique */
William Lallemand79d31ec2020-03-25 15:10:49 +01004514 dir->bind_conf = NULL;
William Lallemandc69f02d2020-04-06 19:07:03 +02004515 dir->linecount = 0;
William Lallemand83918e22020-03-16 17:21:51 +01004516 LIST_INIT(&dir->ord_entries);
William Lallemand6be66ec2020-03-06 22:26:32 +01004517
William Lallemand2d232c22020-03-06 22:12:35 +01004518 n = scandir(path, &de_list, 0, alphasort);
4519 if (n < 0) {
4520 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4521 err && *err ? *err : "", path, strerror(errno));
4522 cfgerr |= ERR_ALERT | ERR_FATAL;
4523 }
4524 else {
4525 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004526 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004527 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004528
William Lallemand2d232c22020-03-06 22:12:35 +01004529 end = strrchr(de->d_name, '.');
4530 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4531 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004532
William Lallemanda64593c2020-03-17 20:11:41 +01004533 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4534 if (stat(fp, &buf) != 0) {
4535 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4536 err && *err ? *err : "", fp, strerror(errno));
4537 cfgerr |= ERR_ALERT | ERR_FATAL;
4538 goto ignore_entry;
4539 }
4540 if (!S_ISREG(buf.st_mode))
4541 goto ignore_entry;
4542
William Lallemand6be66ec2020-03-06 22:26:32 +01004543 entry = malloc(sizeof(*entry));
4544 if (entry == NULL) {
4545 memprintf(err, "not enough memory '%s'", fp);
4546 cfgerr |= ERR_ALERT | ERR_FATAL;
4547 goto ignore_entry;
4548 }
4549
William Lallemand83918e22020-03-16 17:21:51 +01004550 /* directories don't use ssl_conf and filters */
William Lallemandc69f02d2020-04-06 19:07:03 +02004551 entry->linenum = 0;
William Lallemand83918e22020-03-16 17:21:51 +01004552 entry->fcount = 0;
4553 entry->filters = NULL;
4554 entry->ssl_conf = NULL;
William Lallemandc2e3b722020-03-31 14:31:41 +02004555 LIST_INIT(&entry->ckch_inst);
William Lallemand83918e22020-03-16 17:21:51 +01004556
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004557#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004558 is_bundle = 0;
4559 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004560
William Lallemand2d232c22020-03-06 22:12:35 +01004561 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4562 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4563 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4564 is_bundle = 1;
4565 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004566 }
William Lallemand2d232c22020-03-06 22:12:35 +01004567 }
yanbzhu63ea8462015-12-09 13:35:14 -05004568
William Lallemand2d232c22020-03-06 22:12:35 +01004569 if (is_bundle) {
4570 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004571
William Lallemand2d232c22020-03-06 22:12:35 +01004572 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004573
William Lallemand2d232c22020-03-06 22:12:35 +01004574 /* increment i and free de until we get to a non-bundle cert
4575 * Note here that we look at de_list[i + 1] before freeing de
4576 * this is important since ignore_entry will free de. This also
4577 * guarantees that de->d_name continues to hold the same prefix.
4578 */
4579 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4580 free(de);
4581 i++;
4582 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004583 }
William Lallemand2d232c22020-03-06 22:12:35 +01004584
4585 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004586 ckchs = ckchs_lookup(fp);
4587 if (ckchs == NULL)
4588 ckchs = ckchs_load_cert_file(fp, 1, err);
4589 if (ckchs == NULL) {
4590 free(de);
4591 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004592 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004593 goto end;
4594 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004595 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004596 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004597 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004598 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004599 ebpt_insert(&dir->entries, &entry->node);
4600
William Lallemand2d232c22020-03-06 22:12:35 +01004601 /* Successfully processed the bundle */
4602 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004603 }
William Lallemand2d232c22020-03-06 22:12:35 +01004604 }
yanbzhu63ea8462015-12-09 13:35:14 -05004605
4606#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004607 ckchs = ckchs_lookup(fp);
4608 if (ckchs == NULL)
4609 ckchs = ckchs_load_cert_file(fp, 0, err);
4610 if (ckchs == NULL) {
4611 free(de);
4612 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004613 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004614 goto end;
4615 }
4616 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004617 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004618 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004619 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004620 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004621
yanbzhu08ce6ab2015-12-02 13:01:29 -05004622ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004623 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004624 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004625end:
William Lallemand2d232c22020-03-06 22:12:35 +01004626 free(de_list);
4627 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004628
4629 if (cfgerr & ERR_CODE) {
4630 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004631 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004632 } else {
4633 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004634 }
William Lallemand2d232c22020-03-06 22:12:35 +01004635 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004636
William Lallemand2d232c22020-03-06 22:12:35 +01004637}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004638
William Lallemand97b08102020-04-01 10:14:00 +02004639/*
4640 * Read a single crt-list line. /!\ alter the <line> string.
4641 * Fill <crt_path> and <crtlist_entry>
4642 * <crtlist_entry> must be alloc and free by the caller
4643 * <crtlist_entry->ssl_conf> is alloc by the function
4644 * <crtlist_entry->filters> is alloc by the function
4645 * <crt_path> is a ptr in <line>
4646 * Return an error code
4647 */
4648static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4649{
4650 int cfgerr = 0;
4651 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4652 char *end;
4653 char *args[MAX_CRT_ARGS + 1];
4654 struct ssl_bind_conf *ssl_conf = NULL;
4655
4656 if (!line || !crt_path || !entry)
4657 return ERR_ALERT | ERR_FATAL;
4658
4659 end = line + strlen(line);
4660 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4661 /* Check if we reached the limit and the last char is not \n.
4662 * Watch out for the last line without the terminating '\n'!
4663 */
4664 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4665 linenum, file, CRT_LINESIZE-1);
4666 cfgerr |= ERR_ALERT | ERR_FATAL;
4667 goto error;
4668 }
4669 arg = 0;
4670 newarg = 1;
4671 while (*line) {
4672 if (isspace((unsigned char)*line)) {
4673 newarg = 1;
4674 *line = 0;
4675 } else if (*line == '[') {
4676 if (ssl_b) {
4677 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4678 cfgerr |= ERR_ALERT | ERR_FATAL;
4679 goto error;
4680 }
4681 if (!arg) {
4682 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4683 cfgerr |= ERR_ALERT | ERR_FATAL;
4684 goto error;
4685 }
4686 ssl_b = arg;
4687 newarg = 1;
4688 *line = 0;
4689 } else if (*line == ']') {
4690 if (ssl_e) {
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 (!ssl_b) {
4696 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4697 cfgerr |= ERR_ALERT | ERR_FATAL;
4698 goto error;
4699 }
4700 ssl_e = arg;
4701 newarg = 1;
4702 *line = 0;
4703 } else if (newarg) {
4704 if (arg == MAX_CRT_ARGS) {
4705 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4706 cfgerr |= ERR_ALERT | ERR_FATAL;
4707 goto error;
4708 }
4709 newarg = 0;
4710 args[arg++] = line;
4711 }
4712 line++;
4713 }
4714 args[arg++] = line;
4715
4716 /* empty line */
4717 if (!*args[0]) {
4718 cfgerr |= ERR_NONE;
4719 goto error;
4720 }
4721
4722 *crt_path = args[0];
4723
4724 ssl_conf = calloc(1, sizeof *ssl_conf);
4725 if (!ssl_conf) {
4726 memprintf(err, "not enough memory!");
4727 cfgerr |= ERR_ALERT | ERR_FATAL;
4728 goto error;
4729 }
4730 cur_arg = ssl_b ? ssl_b : 1;
4731 while (cur_arg < ssl_e) {
4732 newarg = 0;
4733 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4734 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4735 newarg = 1;
4736 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4737 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4738 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4739 args[cur_arg], linenum, file);
4740 cfgerr |= ERR_ALERT | ERR_FATAL;
4741 goto error;
4742 }
4743 cur_arg += 1 + ssl_bind_kws[i].skip;
4744 break;
4745 }
4746 }
4747 if (!cfgerr && !newarg) {
4748 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4749 args[cur_arg], linenum, file);
4750 cfgerr |= ERR_ALERT | ERR_FATAL;
4751 goto error;
4752 }
4753 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004754 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004755 entry->ssl_conf = ssl_conf;
4756 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4757 entry->fcount = arg - cur_arg - 1;
4758
4759 return cfgerr;
4760
4761error:
4762 crtlist_free_filters(entry->filters);
4763 entry->filters = NULL;
4764 ssl_sock_free_ssl_conf(entry->ssl_conf);
4765 free(entry->ssl_conf);
4766 entry->ssl_conf = NULL;
4767 return cfgerr;
4768}
4769
William Lallemand2954c472020-03-06 21:54:13 +01004770/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4771 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4772 *
4773 * This function tries to open and store certificate files.
4774 */
4775static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4776{
4777 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004778 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004779 char thisline[CRT_LINESIZE];
4780 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004781 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004782 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004783 int linenum = 0;
4784 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004785
Willy Tarreauad1731d2013-04-02 17:35:58 +02004786 if ((f = fopen(file, "r")) == NULL) {
4787 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004788 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004789 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004790
William Lallemand2954c472020-03-06 21:54:13 +01004791 newlist = malloc(sizeof(*newlist) + strlen(file) + 1);
4792 if (newlist == NULL) {
4793 memprintf(err, "Not enough memory!");
4794 cfgerr |= ERR_ALERT | ERR_FATAL;
4795 goto error;
4796 }
4797 memcpy(newlist->node.key, file, strlen(file) + 1);
4798 newlist->entries = EB_ROOT;
William Lallemand79d31ec2020-03-25 15:10:49 +01004799 newlist->bind_conf = NULL;
William Lallemand97b08102020-04-01 10:14:00 +02004800 newlist->node.node.leaf_p = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004801 LIST_INIT(&newlist->ord_entries);
4802
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004803 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004804 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004805 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004806 char *crt_path;
4807 struct ssl_bind_conf *ssl_conf = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004808 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004809
4810 linenum++;
4811 end = line + strlen(line);
4812 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4813 /* Check if we reached the limit and the last char is not \n.
4814 * Watch out for the last line without the terminating '\n'!
4815 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004816 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4817 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004818 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004819 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004820 }
William Lallemand97b08102020-04-01 10:14:00 +02004821
4822 if (*line == '#' || *line == '\n' || *line == '\r')
4823 continue;
4824
4825 entry = malloc(sizeof(*entry));
4826 if (entry == NULL) {
4827 memprintf(err, "Not enough memory!");
4828 cfgerr |= ERR_ALERT | ERR_FATAL;
4829 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004830 }
William Lallemand97b08102020-04-01 10:14:00 +02004831 entry->fcount = 0;
4832 entry->filters = NULL;
4833 entry->ssl_conf = NULL;
4834 entry->node.key = NULL;
4835 LIST_INIT(&entry->by_crtlist);
4836
4837 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4838 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004839 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004840 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004841
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004842 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004843 if (!crt_path || !*crt_path) {
4844 free(entry);
4845 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004846 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004847 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004848
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004849 if (*crt_path != '/' && global_ssl.crt_base) {
4850 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4851 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4852 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004853 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004854 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004855 }
4856 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4857 crt_path = path;
4858 }
4859
William Lallemand2954c472020-03-06 21:54:13 +01004860 /* Look for a ckch_store or create one */
4861 ckchs = ckchs_lookup(crt_path);
4862 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004863 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004864 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004865 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004866 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004867 }
William Lallemand909086e2020-03-17 16:53:27 +01004868 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004869 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004870
William Lallemand97b08102020-04-01 10:14:00 +02004871 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004872 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004873
William Lallemand2954c472020-03-06 21:54:13 +01004874 entry->node.key = ckchs;
4875 entry->ssl_conf = ssl_conf;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004876 entry->crtlist = newlist;
William Lallemandc2e3b722020-03-31 14:31:41 +02004877 LIST_INIT(&entry->ckch_inst);
William Lallemand2954c472020-03-06 21:54:13 +01004878 ebpt_insert(&newlist->entries, &entry->node);
4879 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004880 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004881
4882 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004883 }
William Lallemand2954c472020-03-06 21:54:13 +01004884 if (cfgerr & ERR_CODE)
4885 goto error;
4886
William Lallemandc69f02d2020-04-06 19:07:03 +02004887 newlist->linecount = linenum;
4888
William Lallemand2954c472020-03-06 21:54:13 +01004889 fclose(f);
4890 *crtlist = newlist;
4891
4892 return cfgerr;
4893error:
William Lallemand97b08102020-04-01 10:14:00 +02004894 if (entry) {
4895 crtlist_free_filters(entry->filters);
4896 entry->filters = NULL;
4897 ssl_sock_free_ssl_conf(entry->ssl_conf);
4898 free(entry->ssl_conf);
4899 entry->ssl_conf = NULL;
4900 free(entry);
4901 }
4902
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004903 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004904 crtlist_free(newlist);
4905 return cfgerr;
4906}
4907
4908/* Load a crt-list file, this is done in 2 parts:
4909 * - store the content of the file in a crtlist structure with crtlist_entry structures
4910 * - generate the instances by iterating on entries in the crtlist struct
4911 *
4912 * Nothing is locked there, this function is used in the configuration parser.
4913 *
4914 * Returns a set of ERR_* flags possibly with an error in <err>.
4915 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004916int 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 +01004917{
4918 struct crtlist *crtlist = NULL;
4919 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004920 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004921 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004922 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004923 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004924
William Lallemand79d31ec2020-03-25 15:10:49 +01004925 bind_conf_node = malloc(sizeof(*bind_conf_node));
4926 if (!bind_conf_node) {
4927 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4928 cfgerr |= ERR_FATAL | ERR_ALERT;
4929 goto error;
4930 }
4931 bind_conf_node->next = NULL;
4932 bind_conf_node->bind_conf = bind_conf;
4933
William Lallemand41ca9302020-04-08 13:15:18 +02004934 /* strip trailing slashes, including first one */
4935 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4936 *end = 0;
4937
William Lallemand2954c472020-03-06 21:54:13 +01004938 /* look for an existing crtlist or create one */
4939 eb = ebst_lookup(&crtlists_tree, file);
4940 if (eb) {
4941 crtlist = ebmb_entry(eb, struct crtlist, node);
4942 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004943 /* load a crt-list OR a directory */
4944 if (dir)
4945 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4946 else
4947 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4948
William Lallemand2954c472020-03-06 21:54:13 +01004949 if (!(cfgerr & ERR_CODE))
4950 ebst_insert(&crtlists_tree, &crtlist->node);
4951 }
4952
4953 if (cfgerr & ERR_CODE) {
4954 cfgerr |= ERR_FATAL | ERR_ALERT;
4955 goto error;
4956 }
4957
4958 /* generates ckch instance from the crtlist_entry */
4959 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4960 struct ckch_store *store;
4961 struct ckch_inst *ckch_inst = NULL;
4962
4963 store = entry->node.key;
4964 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
4965 if (cfgerr & ERR_CODE) {
4966 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
4967 goto error;
4968 }
William Lallemand49398312020-03-30 17:01:33 +02004969 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02004970 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01004971 }
William Lallemand2954c472020-03-06 21:54:13 +01004972
William Lallemand79d31ec2020-03-25 15:10:49 +01004973 /* add the bind_conf to the list */
4974 bind_conf_node->next = crtlist->bind_conf;
4975 crtlist->bind_conf = bind_conf_node;
4976
William Lallemand2954c472020-03-06 21:54:13 +01004977 return cfgerr;
4978error:
4979 {
William Lallemand49398312020-03-30 17:01:33 +02004980 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01004981 struct ckch_inst *inst, *s_inst;
4982
William Lallemand49398312020-03-30 17:01:33 +02004983 lastentry = entry; /* which entry we tried to generate last */
4984 if (lastentry) {
4985 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4986 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
4987 break;
4988
4989 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01004990
William Lallemand49398312020-03-30 17:01:33 +02004991 /* this was not generated for this bind_conf, skip */
4992 if (inst->bind_conf != bind_conf)
4993 continue;
4994
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004995 /* free the sni_ctx and instance */
4996 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02004997 }
William Lallemand2954c472020-03-06 21:54:13 +01004998 }
William Lallemand2954c472020-03-06 21:54:13 +01004999 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005000 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005001 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005002 return cfgerr;
5003}
5004
William Lallemand06b22a82020-03-16 14:45:55 +01005005/* Returns a set of ERR_* flags possibly with an error in <err>. */
5006int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5007{
5008 struct stat buf;
5009 char fp[MAXPATHLEN+1];
5010 int cfgerr = 0;
5011 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005012 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005013
5014 if ((ckchs = ckchs_lookup(path))) {
5015 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005016 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005017 }
5018 if (stat(path, &buf) == 0) {
5019 if (S_ISDIR(buf.st_mode) == 0) {
5020 ckchs = ckchs_load_cert_file(path, 0, err);
5021 if (!ckchs)
5022 return ERR_ALERT | ERR_FATAL;
5023
William Lallemand24bde432020-03-09 16:48:43 +01005024 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005025 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005026 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005027 }
5028 } else {
5029 /* stat failed, could be a bundle */
5030 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5031 /* try to load a bundle if it is permitted */
5032 ckchs = ckchs_load_cert_file(path, 1, err);
5033 if (!ckchs)
5034 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005035 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005036 } else {
5037 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5038 err && *err ? *err : "", fp, strerror(errno));
5039 cfgerr |= ERR_ALERT | ERR_FATAL;
5040 }
5041 }
5042
5043 return cfgerr;
5044}
5045
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005046/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005047static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005048ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005049{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005050 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005051 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005052 SSL_OP_ALL | /* all known workarounds for bugs */
5053 SSL_OP_NO_SSLv2 |
5054 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005055 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005056 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005057 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005058 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005059 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005060 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005061 SSL_MODE_ENABLE_PARTIAL_WRITE |
5062 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005063 SSL_MODE_RELEASE_BUFFERS |
5064 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005065 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005066 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005067 int flags = MC_SSL_O_ALL;
5068 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005069
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005070 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005071 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005072
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005073 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005074 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5075 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5076 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005077 else
5078 flags = conf_ssl_methods->flags;
5079
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005080 min = conf_ssl_methods->min;
5081 max = conf_ssl_methods->max;
5082 /* start with TLSv10 to remove SSLv3 per default */
5083 if (!min && (!max || max >= CONF_TLSV10))
5084 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005085 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005086 if (min)
5087 flags |= (methodVersions[min].flag - 1);
5088 if (max)
5089 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005090 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005091 min = max = CONF_TLSV_NONE;
5092 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005093 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005094 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005095 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005096 if (min) {
5097 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005098 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5099 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5100 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5101 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005102 hole = 0;
5103 }
5104 max = i;
5105 }
5106 else {
5107 min = max = i;
5108 }
5109 }
5110 else {
5111 if (min)
5112 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005113 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005114 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005115 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5116 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005117 cfgerr += 1;
5118 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005119 /* save real min/max in bind_conf */
5120 conf_ssl_methods->min = min;
5121 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005122
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005123#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005124 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005125 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005126 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005127 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005128 else
5129 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5130 if (flags & methodVersions[i].flag)
5131 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005132#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005133 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005134 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5135 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005136#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005137
5138 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5139 options |= SSL_OP_NO_TICKET;
5140 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5141 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005142
5143#ifdef SSL_OP_NO_RENEGOTIATION
5144 options |= SSL_OP_NO_RENEGOTIATION;
5145#endif
5146
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005147 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005148
Willy Tarreau5db847a2019-05-09 14:13:35 +02005149#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005150 if (global_ssl.async)
5151 mode |= SSL_MODE_ASYNC;
5152#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005153 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005154 if (global_ssl.life_time)
5155 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005156
5157#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5158#ifdef OPENSSL_IS_BORINGSSL
5159 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5160 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005161#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005162 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005163 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005164 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5165 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005166#else
5167 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005168#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005169 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005170#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005171 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005172}
5173
William Lallemand4f45bb92017-10-30 20:08:51 +01005174
5175static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5176{
5177 if (first == block) {
5178 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5179 if (first->len > 0)
5180 sh_ssl_sess_tree_delete(sh_ssl_sess);
5181 }
5182}
5183
5184/* return first block from sh_ssl_sess */
5185static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5186{
5187 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5188
5189}
5190
5191/* store a session into the cache
5192 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5193 * data: asn1 encoded session
5194 * data_len: asn1 encoded session length
5195 * Returns 1 id session was stored (else 0)
5196 */
5197static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5198{
5199 struct shared_block *first;
5200 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5201
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005202 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005203 if (!first) {
5204 /* Could not retrieve enough free blocks to store that session */
5205 return 0;
5206 }
5207
5208 /* STORE the key in the first elem */
5209 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5210 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5211 first->len = sizeof(struct sh_ssl_sess_hdr);
5212
5213 /* it returns the already existing node
5214 or current node if none, never returns null */
5215 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5216 if (oldsh_ssl_sess != sh_ssl_sess) {
5217 /* NOTE: Row couldn't be in use because we lock read & write function */
5218 /* release the reserved row */
5219 shctx_row_dec_hot(ssl_shctx, first);
5220 /* replace the previous session already in the tree */
5221 sh_ssl_sess = oldsh_ssl_sess;
5222 /* ignore the previous session data, only use the header */
5223 first = sh_ssl_sess_first_block(sh_ssl_sess);
5224 shctx_row_inc_hot(ssl_shctx, first);
5225 first->len = sizeof(struct sh_ssl_sess_hdr);
5226 }
5227
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005228 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005229 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005230 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005231 }
5232
5233 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005234
5235 return 1;
5236}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005237
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005238/* SSL callback used when a new session is created while connecting to a server */
5239static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5240{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005241 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005242 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005243
Willy Tarreau07d94e42018-09-20 10:57:52 +02005244 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005245
Olivier Houcharde6060c52017-11-16 17:42:52 +01005246 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5247 int len;
5248 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005249
Olivier Houcharde6060c52017-11-16 17:42:52 +01005250 len = i2d_SSL_SESSION(sess, NULL);
5251 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5252 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5253 } else {
5254 free(s->ssl_ctx.reused_sess[tid].ptr);
5255 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5256 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5257 }
5258 if (s->ssl_ctx.reused_sess[tid].ptr) {
5259 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5260 &ptr);
5261 }
5262 } else {
5263 free(s->ssl_ctx.reused_sess[tid].ptr);
5264 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5265 }
5266
5267 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005268}
5269
Olivier Houcharde6060c52017-11-16 17:42:52 +01005270
William Lallemanded0b5ad2017-10-30 19:36:36 +01005271/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005272int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005273{
5274 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5275 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5276 unsigned char *p;
5277 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005278 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005279 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005280
5281 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005282 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005283 * note: SSL_SESSION_set1_id is using
5284 * a memcpy so we need to use a different pointer
5285 * than sid_data or sid_ctx_data to avoid valgrind
5286 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005287 */
5288
5289 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005290
5291 /* copy value in an other buffer */
5292 memcpy(encid, sid_data, sid_length);
5293
5294 /* pad with 0 */
5295 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5296 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5297
5298 /* force length to zero to avoid ASN1 encoding */
5299 SSL_SESSION_set1_id(sess, encid, 0);
5300
5301 /* force length to zero to avoid ASN1 encoding */
5302 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005303
5304 /* check if buffer is large enough for the ASN1 encoded session */
5305 data_len = i2d_SSL_SESSION(sess, NULL);
5306 if (data_len > SHSESS_MAX_DATA_LEN)
5307 goto err;
5308
5309 p = encsess;
5310
5311 /* process ASN1 session encoding before the lock */
5312 i2d_SSL_SESSION(sess, &p);
5313
William Lallemanded0b5ad2017-10-30 19:36:36 +01005314
William Lallemanda3c77cf2017-10-30 23:44:40 +01005315 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005316 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005317 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005318 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005319err:
5320 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005321 SSL_SESSION_set1_id(sess, encid, sid_length);
5322 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005323
5324 return 0; /* do not increment session reference count */
5325}
5326
5327/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005328SSL_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 +01005329{
William Lallemand4f45bb92017-10-30 20:08:51 +01005330 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005331 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5332 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005333 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005334 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005335
5336 global.shctx_lookups++;
5337
5338 /* allow the session to be freed automatically by openssl */
5339 *do_copy = 0;
5340
5341 /* tree key is zeros padded sessionid */
5342 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5343 memcpy(tmpkey, key, key_len);
5344 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5345 key = tmpkey;
5346 }
5347
5348 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005349 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005350
5351 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005352 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5353 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005354 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005355 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005356 global.shctx_misses++;
5357 return NULL;
5358 }
5359
William Lallemand4f45bb92017-10-30 20:08:51 +01005360 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5361 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005362
William Lallemand4f45bb92017-10-30 20:08:51 +01005363 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 +01005364
William Lallemanda3c77cf2017-10-30 23:44:40 +01005365 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005366
5367 /* decode ASN1 session */
5368 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005369 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005370 /* Reset session id and session id contenxt */
5371 if (sess) {
5372 SSL_SESSION_set1_id(sess, key, key_len);
5373 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5374 }
5375
5376 return sess;
5377}
5378
William Lallemand4f45bb92017-10-30 20:08:51 +01005379
William Lallemanded0b5ad2017-10-30 19:36:36 +01005380/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005381void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005382{
William Lallemand4f45bb92017-10-30 20:08:51 +01005383 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005384 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5385 unsigned int sid_length;
5386 const unsigned char *sid_data;
5387 (void)ctx;
5388
5389 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5390 /* tree key is zeros padded sessionid */
5391 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5392 memcpy(tmpkey, sid_data, sid_length);
5393 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5394 sid_data = tmpkey;
5395 }
5396
William Lallemanda3c77cf2017-10-30 23:44:40 +01005397 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005398
5399 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005400 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5401 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005402 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005403 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005404 }
5405
5406 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005407 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005408}
5409
5410/* Set session cache mode to server and disable openssl internal cache.
5411 * Set shared cache callbacks on an ssl context.
5412 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005413void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005414{
5415 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5416
5417 if (!ssl_shctx) {
5418 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5419 return;
5420 }
5421
5422 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5423 SSL_SESS_CACHE_NO_INTERNAL |
5424 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5425
5426 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005427 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5428 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5429 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005430}
5431
William Lallemand8b453912019-11-21 15:48:10 +01005432/*
5433 * This function applies the SSL configuration on a SSL_CTX
5434 * It returns an error code and fills the <err> buffer
5435 */
5436int 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 +01005437{
5438 struct proxy *curproxy = bind_conf->frontend;
5439 int cfgerr = 0;
5440 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005441 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005442 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005443#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005444 const char *conf_ciphersuites;
5445#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005446 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005447
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005448 if (ssl_conf) {
5449 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5450 int i, min, max;
5451 int flags = MC_SSL_O_ALL;
5452
5453 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005454 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5455 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005456 if (min)
5457 flags |= (methodVersions[min].flag - 1);
5458 if (max)
5459 flags |= ~((methodVersions[max].flag << 1) - 1);
5460 min = max = CONF_TLSV_NONE;
5461 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5462 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5463 if (min)
5464 max = i;
5465 else
5466 min = max = i;
5467 }
5468 /* save real min/max */
5469 conf_ssl_methods->min = min;
5470 conf_ssl_methods->max = max;
5471 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005472 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5473 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005474 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005475 }
5476 }
5477
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005478 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005479 case SSL_SOCK_VERIFY_NONE:
5480 verify = SSL_VERIFY_NONE;
5481 break;
5482 case SSL_SOCK_VERIFY_OPTIONAL:
5483 verify = SSL_VERIFY_PEER;
5484 break;
5485 case SSL_SOCK_VERIFY_REQUIRED:
5486 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5487 break;
5488 }
5489 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5490 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005491 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 +01005492 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 +01005493 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 +01005494 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005495 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005496 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005497 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005498 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005499 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005500 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005501 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5502 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5503 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5504 cfgerr |= ERR_ALERT | ERR_FATAL;
5505 }
5506 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005507 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005508 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 +02005509 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005510 }
Emeric Brun850efd52014-01-29 12:24:34 +01005511 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005512 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5513 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005514 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005515 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005516#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005517 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005518 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5519
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005520 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005521 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5522 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005523 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005524 }
Emeric Brun561e5742012-10-02 15:20:55 +02005525 else {
5526 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5527 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005528 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005529#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005530 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005531 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005532#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005533 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005534 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005535 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5536 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005537 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005538 }
5539 }
5540#endif
5541
William Lallemand4f45bb92017-10-30 20:08:51 +01005542 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005543 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5544 if (conf_ciphers &&
5545 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005546 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5547 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005548 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005549 }
5550
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005551#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005552 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5553 if (conf_ciphersuites &&
5554 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005555 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5556 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005557 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005558 }
5559#endif
5560
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005561#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005562 /* If tune.ssl.default-dh-param has not been set,
5563 neither has ssl-default-dh-file and no static DH
5564 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005565 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005566 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005567 (ssl_dh_ptr_index == -1 ||
5568 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005569 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5570 const SSL_CIPHER * cipher = NULL;
5571 char cipher_description[128];
5572 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5573 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5574 which is not ephemeral DH. */
5575 const char dhe_description[] = " Kx=DH ";
5576 const char dhe_export_description[] = " Kx=DH(";
5577 int idx = 0;
5578 int dhe_found = 0;
5579 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005580
Remi Gacogne23d5d372014-10-10 17:04:26 +02005581 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005582
Remi Gacogne23d5d372014-10-10 17:04:26 +02005583 if (ssl) {
5584 ciphers = SSL_get_ciphers(ssl);
5585
5586 if (ciphers) {
5587 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5588 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5589 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5590 if (strstr(cipher_description, dhe_description) != NULL ||
5591 strstr(cipher_description, dhe_export_description) != NULL) {
5592 dhe_found = 1;
5593 break;
5594 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005595 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005596 }
5597 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005598 SSL_free(ssl);
5599 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005600 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005601
Lukas Tribus90132722014-08-18 00:56:33 +02005602 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005603 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",
5604 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005605 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005606 }
5607
Willy Tarreauef934602016-12-22 23:12:01 +01005608 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005609 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005610
Willy Tarreauef934602016-12-22 23:12:01 +01005611 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005612 if (local_dh_1024 == NULL) {
5613 local_dh_1024 = ssl_get_dh_1024();
5614 }
Willy Tarreauef934602016-12-22 23:12:01 +01005615 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005616 if (local_dh_2048 == NULL) {
5617 local_dh_2048 = ssl_get_dh_2048();
5618 }
Willy Tarreauef934602016-12-22 23:12:01 +01005619 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005620 if (local_dh_4096 == NULL) {
5621 local_dh_4096 = ssl_get_dh_4096();
5622 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005623 }
5624 }
5625 }
5626#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005627
Emeric Brunfc0421f2012-09-07 17:30:07 +02005628 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005629#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005630 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005631#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005632
Bernard Spil13c53f82018-02-15 13:34:58 +01005633#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005634 ssl_conf_cur = NULL;
5635 if (ssl_conf && ssl_conf->npn_str)
5636 ssl_conf_cur = ssl_conf;
5637 else if (bind_conf->ssl_conf.npn_str)
5638 ssl_conf_cur = &bind_conf->ssl_conf;
5639 if (ssl_conf_cur)
5640 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005641#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005642#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005643 ssl_conf_cur = NULL;
5644 if (ssl_conf && ssl_conf->alpn_str)
5645 ssl_conf_cur = ssl_conf;
5646 else if (bind_conf->ssl_conf.alpn_str)
5647 ssl_conf_cur = &bind_conf->ssl_conf;
5648 if (ssl_conf_cur)
5649 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005650#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005651#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005652 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5653 if (conf_curves) {
5654 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005655 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5656 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005657 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005658 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005659 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005660 }
5661#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005662#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005663 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005664 int i;
5665 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005666#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005667 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005668 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5669 NULL);
5670
5671 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005672 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005673 return cfgerr;
5674 }
5675#else
5676 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5677 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5678 ECDHE_DEFAULT_CURVE);
5679#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005680
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005681 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005682 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005683 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5684 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005685 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005686 }
5687 else {
5688 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5689 EC_KEY_free(ecdh);
5690 }
5691 }
5692#endif
5693
Emeric Brunfc0421f2012-09-07 17:30:07 +02005694 return cfgerr;
5695}
5696
Evan Broderbe554312013-06-27 00:05:25 -07005697static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5698{
5699 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5700 size_t prefixlen, suffixlen;
5701
5702 /* Trivial case */
5703 if (strcmp(pattern, hostname) == 0)
5704 return 1;
5705
Evan Broderbe554312013-06-27 00:05:25 -07005706 /* The rest of this logic is based on RFC 6125, section 6.4.3
5707 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5708
Emeric Bruna848dae2013-10-08 11:27:28 +02005709 pattern_wildcard = NULL;
5710 pattern_left_label_end = pattern;
5711 while (*pattern_left_label_end != '.') {
5712 switch (*pattern_left_label_end) {
5713 case 0:
5714 /* End of label not found */
5715 return 0;
5716 case '*':
5717 /* If there is more than one wildcards */
5718 if (pattern_wildcard)
5719 return 0;
5720 pattern_wildcard = pattern_left_label_end;
5721 break;
5722 }
5723 pattern_left_label_end++;
5724 }
5725
5726 /* If it's not trivial and there is no wildcard, it can't
5727 * match */
5728 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005729 return 0;
5730
5731 /* Make sure all labels match except the leftmost */
5732 hostname_left_label_end = strchr(hostname, '.');
5733 if (!hostname_left_label_end
5734 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5735 return 0;
5736
5737 /* Make sure the leftmost label of the hostname is long enough
5738 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005739 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005740 return 0;
5741
5742 /* Finally compare the string on either side of the
5743 * wildcard */
5744 prefixlen = pattern_wildcard - pattern;
5745 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005746 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5747 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005748 return 0;
5749
5750 return 1;
5751}
5752
5753static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5754{
5755 SSL *ssl;
5756 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005757 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005758 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005759 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005760
5761 int depth;
5762 X509 *cert;
5763 STACK_OF(GENERAL_NAME) *alt_names;
5764 int i;
5765 X509_NAME *cert_subject;
5766 char *str;
5767
5768 if (ok == 0)
5769 return ok;
5770
5771 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005772 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005773 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005774
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005775 /* We're checking if the provided hostnames match the desired one. The
5776 * desired hostname comes from the SNI we presented if any, or if not
5777 * provided then it may have been explicitly stated using a "verifyhost"
5778 * directive. If neither is set, we don't care about the name so the
5779 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005780 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005781 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005782 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005783 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005784 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005785 if (!servername)
5786 return ok;
5787 }
Evan Broderbe554312013-06-27 00:05:25 -07005788
5789 /* We only need to verify the CN on the actual server cert,
5790 * not the indirect CAs */
5791 depth = X509_STORE_CTX_get_error_depth(ctx);
5792 if (depth != 0)
5793 return ok;
5794
5795 /* At this point, the cert is *not* OK unless we can find a
5796 * hostname match */
5797 ok = 0;
5798
5799 cert = X509_STORE_CTX_get_current_cert(ctx);
5800 /* It seems like this might happen if verify peer isn't set */
5801 if (!cert)
5802 return ok;
5803
5804 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5805 if (alt_names) {
5806 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5807 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5808 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005809#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005810 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5811#else
Evan Broderbe554312013-06-27 00:05:25 -07005812 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005813#endif
Evan Broderbe554312013-06-27 00:05:25 -07005814 ok = ssl_sock_srv_hostcheck(str, servername);
5815 OPENSSL_free(str);
5816 }
5817 }
5818 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005819 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005820 }
5821
5822 cert_subject = X509_get_subject_name(cert);
5823 i = -1;
5824 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5825 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005826 ASN1_STRING *value;
5827 value = X509_NAME_ENTRY_get_data(entry);
5828 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005829 ok = ssl_sock_srv_hostcheck(str, servername);
5830 OPENSSL_free(str);
5831 }
5832 }
5833
Willy Tarreau71d058c2017-07-26 20:09:56 +02005834 /* report the mismatch and indicate if SNI was used or not */
5835 if (!ok && !conn->err_code)
5836 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005837 return ok;
5838}
5839
Emeric Brun94324a42012-10-11 14:00:19 +02005840/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005841int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005842{
Willy Tarreau03209342016-12-22 17:08:28 +01005843 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005844 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005845 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005846 SSL_OP_ALL | /* all known workarounds for bugs */
5847 SSL_OP_NO_SSLv2 |
5848 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005849 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005850 SSL_MODE_ENABLE_PARTIAL_WRITE |
5851 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005852 SSL_MODE_RELEASE_BUFFERS |
5853 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005854 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005855 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005856 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005857 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005858 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005859
Thierry Fournier383085f2013-01-24 14:15:43 +01005860 /* Make sure openssl opens /dev/urandom before the chroot */
5861 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005862 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005863 cfgerr++;
5864 }
5865
Willy Tarreaufce03112015-01-15 21:32:40 +01005866 /* Automatic memory computations need to know we use SSL there */
5867 global.ssl_used_backend = 1;
5868
5869 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005870 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005871 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005872 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5873 curproxy->id, srv->id,
5874 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005875 cfgerr++;
5876 return cfgerr;
5877 }
5878 }
Emeric Brun94324a42012-10-11 14:00:19 +02005879 if (srv->use_ssl)
5880 srv->xprt = &ssl_sock;
5881 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005882 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005883
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005884 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005885 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005886 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5887 proxy_type_str(curproxy), curproxy->id,
5888 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005889 cfgerr++;
5890 return cfgerr;
5891 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005892
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005893 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005894 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5895 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5896 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005897 else
5898 flags = conf_ssl_methods->flags;
5899
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005900 /* Real min and max should be determinate with configuration and openssl's capabilities */
5901 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005902 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005903 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005904 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005905
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005906 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005907 min = max = CONF_TLSV_NONE;
5908 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005909 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005910 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005911 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005912 if (min) {
5913 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005914 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5915 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5916 proxy_type_str(curproxy), curproxy->id, srv->id,
5917 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005918 hole = 0;
5919 }
5920 max = i;
5921 }
5922 else {
5923 min = max = i;
5924 }
5925 }
5926 else {
5927 if (min)
5928 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005929 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005930 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005931 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5932 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005933 cfgerr += 1;
5934 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005935
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005936#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005937 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005938 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005939 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005940 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005941 else
5942 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5943 if (flags & methodVersions[i].flag)
5944 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005945#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005946 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005947 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5948 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005949#endif
5950
5951 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5952 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005953 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005954
Willy Tarreau5db847a2019-05-09 14:13:35 +02005955#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005956 if (global_ssl.async)
5957 mode |= SSL_MODE_ASYNC;
5958#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005959 SSL_CTX_set_mode(ctx, mode);
5960 srv->ssl_ctx.ctx = ctx;
5961
Emeric Bruna7aa3092012-10-26 12:58:00 +02005962 if (srv->ssl_ctx.client_crt) {
5963 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 +01005964 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5965 proxy_type_str(curproxy), curproxy->id,
5966 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005967 cfgerr++;
5968 }
5969 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 +01005970 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5971 proxy_type_str(curproxy), curproxy->id,
5972 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005973 cfgerr++;
5974 }
5975 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005976 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5977 proxy_type_str(curproxy), curproxy->id,
5978 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005979 cfgerr++;
5980 }
5981 }
Emeric Brun94324a42012-10-11 14:00:19 +02005982
Emeric Brun850efd52014-01-29 12:24:34 +01005983 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5984 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005985 switch (srv->ssl_ctx.verify) {
5986 case SSL_SOCK_VERIFY_NONE:
5987 verify = SSL_VERIFY_NONE;
5988 break;
5989 case SSL_SOCK_VERIFY_REQUIRED:
5990 verify = SSL_VERIFY_PEER;
5991 break;
5992 }
Evan Broderbe554312013-06-27 00:05:25 -07005993 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005994 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005995 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005996 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005997 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005998 /* set CAfile to verify */
5999 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6000 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006001 curproxy->id, srv->id,
6002 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006003 cfgerr++;
6004 }
6005 }
Emeric Brun850efd52014-01-29 12:24:34 +01006006 else {
6007 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006008 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",
6009 curproxy->id, srv->id,
6010 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006011 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006012 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6013 curproxy->id, srv->id,
6014 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006015 cfgerr++;
6016 }
Emeric Brunef42d922012-10-11 16:11:36 +02006017#ifdef X509_V_FLAG_CRL_CHECK
6018 if (srv->ssl_ctx.crl_file) {
6019 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6020
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006021 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006022 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6023 curproxy->id, srv->id,
6024 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006025 cfgerr++;
6026 }
6027 else {
6028 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6029 }
6030 }
6031#endif
6032 }
6033
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006034 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6035 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6036 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006037 if (srv->ssl_ctx.ciphers &&
6038 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006039 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6040 curproxy->id, srv->id,
6041 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006042 cfgerr++;
6043 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006044
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006045#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006046 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006047 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006048 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6049 curproxy->id, srv->id,
6050 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6051 cfgerr++;
6052 }
6053#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006054#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6055 if (srv->ssl_ctx.npn_str)
6056 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6057#endif
6058#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6059 if (srv->ssl_ctx.alpn_str)
6060 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6061#endif
6062
Emeric Brun94324a42012-10-11 14:00:19 +02006063
6064 return cfgerr;
6065}
6066
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006067/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006068 * be NULL, in which case nothing is done. Returns the number of errors
6069 * encountered.
6070 */
Willy Tarreau03209342016-12-22 17:08:28 +01006071int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006072{
6073 struct ebmb_node *node;
6074 struct sni_ctx *sni;
6075 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006076 int errcode = 0;
6077 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006078
Willy Tarreaufce03112015-01-15 21:32:40 +01006079 /* Automatic memory computations need to know we use SSL there */
6080 global.ssl_used_frontend = 1;
6081
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006082 /* Make sure openssl opens /dev/urandom before the chroot */
6083 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006084 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006085 err++;
6086 }
6087 /* Create initial_ctx used to start the ssl connection before do switchctx */
6088 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006089 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006090 /* It should not be necessary to call this function, but it's
6091 necessary first to check and move all initialisation related
6092 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006093 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006094 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006095 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006096 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006097
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006098 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006099 while (node) {
6100 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006101 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6102 /* only initialize the CTX on its first occurrence and
6103 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006104 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006105 node = ebmb_next(node);
6106 }
6107
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006108 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006109 while (node) {
6110 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006111 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006112 /* only initialize the CTX on its first occurrence and
6113 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006114 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6115 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006116 node = ebmb_next(node);
6117 }
William Lallemand8b453912019-11-21 15:48:10 +01006118
6119 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006120 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006121 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006122 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006123 err++;
6124 }
6125
6126 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006127 return err;
6128}
6129
Willy Tarreau55d37912016-12-21 23:38:39 +01006130/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6131 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6132 * alerts are directly emitted since the rest of the stack does it below.
6133 */
6134int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6135{
6136 struct proxy *px = bind_conf->frontend;
6137 int alloc_ctx;
6138 int err;
6139
6140 if (!bind_conf->is_ssl) {
6141 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006142 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6143 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006144 }
6145 return 0;
6146 }
6147 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006148 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006149 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6150 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006151 }
6152 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006153 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6154 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006155 return -1;
6156 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006157 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006158 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006159 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006160 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006161 sizeof(*sh_ssl_sess_tree),
6162 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006163 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006164 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6165 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");
6166 else
6167 ha_alert("Unable to allocate SSL session cache.\n");
6168 return -1;
6169 }
6170 /* free block callback */
6171 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6172 /* init the root tree within the extra space */
6173 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6174 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006175 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006176 err = 0;
6177 /* initialize all certificate contexts */
6178 err += ssl_sock_prepare_all_ctx(bind_conf);
6179
6180 /* initialize CA variables if the certificates generation is enabled */
6181 err += ssl_sock_load_ca(bind_conf);
6182
6183 return -err;
6184}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006185
6186/* release ssl context allocated for servers. */
6187void ssl_sock_free_srv_ctx(struct server *srv)
6188{
Olivier Houchardc7566002018-11-20 23:33:50 +01006189#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6190 if (srv->ssl_ctx.alpn_str)
6191 free(srv->ssl_ctx.alpn_str);
6192#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006193#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006194 if (srv->ssl_ctx.npn_str)
6195 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006196#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006197 if (srv->ssl_ctx.ctx)
6198 SSL_CTX_free(srv->ssl_ctx.ctx);
6199}
6200
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006201/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006202 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6203 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006204void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006205{
6206 struct ebmb_node *node, *back;
6207 struct sni_ctx *sni;
6208
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006209 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006210 while (node) {
6211 sni = ebmb_entry(node, struct sni_ctx, name);
6212 back = ebmb_next(node);
6213 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006214 SSL_CTX_free(sni->ctx);
6215 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006216 ssl_sock_free_ssl_conf(sni->conf);
6217 free(sni->conf);
6218 sni->conf = NULL;
6219 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006220 free(sni);
6221 node = back;
6222 }
6223
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006224 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006225 while (node) {
6226 sni = ebmb_entry(node, struct sni_ctx, name);
6227 back = ebmb_next(node);
6228 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006229 SSL_CTX_free(sni->ctx);
6230 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006231 ssl_sock_free_ssl_conf(sni->conf);
6232 free(sni->conf);
6233 sni->conf = NULL;
6234 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006235 free(sni);
6236 node = back;
6237 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006238 SSL_CTX_free(bind_conf->initial_ctx);
6239 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006240 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006241 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006242 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006243}
6244
Willy Tarreau795cdab2016-12-22 17:30:54 +01006245/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6246void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6247{
6248 ssl_sock_free_ca(bind_conf);
6249 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006250 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006251 free(bind_conf->ca_sign_file);
6252 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006253 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006254 free(bind_conf->keys_ref->filename);
6255 free(bind_conf->keys_ref->tlskeys);
6256 LIST_DEL(&bind_conf->keys_ref->list);
6257 free(bind_conf->keys_ref);
6258 }
6259 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006260 bind_conf->ca_sign_pass = NULL;
6261 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006262}
6263
Christopher Faulet31af49d2015-06-09 17:29:50 +02006264/* Load CA cert file and private key used to generate certificates */
6265int
Willy Tarreau03209342016-12-22 17:08:28 +01006266ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006267{
Willy Tarreau03209342016-12-22 17:08:28 +01006268 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006269 FILE *fp;
6270 X509 *cacert = NULL;
6271 EVP_PKEY *capkey = NULL;
6272 int err = 0;
6273
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006274 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006275 return err;
6276
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006277#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006278 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006279 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006280 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006281 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006282 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006283#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006284
Christopher Faulet31af49d2015-06-09 17:29:50 +02006285 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006286 ha_alert("Proxy '%s': cannot enable certificate generation, "
6287 "no CA certificate File configured at [%s:%d].\n",
6288 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006289 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006290 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006291
6292 /* read in the CA certificate */
6293 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006294 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6295 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006296 goto load_error;
6297 }
6298 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006299 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6300 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006301 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006302 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006303 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006304 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006305 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6306 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006307 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006308 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006309
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006310 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006311 bind_conf->ca_sign_cert = cacert;
6312 bind_conf->ca_sign_pkey = capkey;
6313 return err;
6314
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006315 read_error:
6316 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006317 if (capkey) EVP_PKEY_free(capkey);
6318 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006319 load_error:
6320 bind_conf->generate_certs = 0;
6321 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006322 return err;
6323}
6324
6325/* Release CA cert and private key used to generate certificated */
6326void
6327ssl_sock_free_ca(struct bind_conf *bind_conf)
6328{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006329 if (bind_conf->ca_sign_pkey)
6330 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6331 if (bind_conf->ca_sign_cert)
6332 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006333 bind_conf->ca_sign_pkey = NULL;
6334 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006335}
6336
Emeric Brun46591952012-05-18 15:47:34 +02006337/*
6338 * This function is called if SSL * context is not yet allocated. The function
6339 * is designed to be called before any other data-layer operation and sets the
6340 * handshake flag on the connection. It is safe to call it multiple times.
6341 * It returns 0 on success and -1 in error case.
6342 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006343static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006344{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006345 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006346 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006347 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006348 return 0;
6349
Willy Tarreau3c728722014-01-23 13:50:42 +01006350 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006351 return 0;
6352
Olivier Houchard66ab4982019-02-26 18:37:15 +01006353 ctx = pool_alloc(ssl_sock_ctx_pool);
6354 if (!ctx) {
6355 conn->err_code = CO_ER_SSL_NO_MEM;
6356 return -1;
6357 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006358 ctx->wait_event.tasklet = tasklet_new();
6359 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006360 conn->err_code = CO_ER_SSL_NO_MEM;
6361 pool_free(ssl_sock_ctx_pool, ctx);
6362 return -1;
6363 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006364 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6365 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006366 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006367 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006368 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006369 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006370 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006371 ctx->xprt_st = 0;
6372 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006373
6374 /* Only work with sockets for now, this should be adapted when we'll
6375 * add QUIC support.
6376 */
6377 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006378 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006379 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6380 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006381 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006382
Willy Tarreau20879a02012-12-03 16:32:10 +01006383 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6384 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006385 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006386 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006387
Emeric Brun46591952012-05-18 15:47:34 +02006388 /* If it is in client mode initiate SSL session
6389 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006390 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006391 int may_retry = 1;
6392
6393 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006394 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006395 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6396 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006397 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006398 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006399 goto retry_connect;
6400 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006401 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006402 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006403 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006404 ctx->bio = BIO_new(ha_meth);
6405 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006406 SSL_free(ctx->ssl);
6407 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006408 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006409 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006410 goto retry_connect;
6411 }
Emeric Brun55476152014-11-12 17:35:37 +01006412 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006413 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006414 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006415 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006416 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006417
Evan Broderbe554312013-06-27 00:05:25 -07006418 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006419 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6420 SSL_free(ctx->ssl);
6421 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006422 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006423 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006424 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006425 goto retry_connect;
6426 }
Emeric Brun55476152014-11-12 17:35:37 +01006427 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006428 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006429 }
6430
Olivier Houchard66ab4982019-02-26 18:37:15 +01006431 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006432 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6433 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6434 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 +01006435 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006436 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006437 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6438 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006439 } else if (sess) {
6440 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006441 }
6442 }
Evan Broderbe554312013-06-27 00:05:25 -07006443
Emeric Brun46591952012-05-18 15:47:34 +02006444 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006445 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006446
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006447 _HA_ATOMIC_ADD(&sslconns, 1);
6448 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006449 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006450 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006451 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006452 return 0;
6453 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006454 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006455 int may_retry = 1;
6456
6457 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006458 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006459 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6460 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006461 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006462 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006463 goto retry_accept;
6464 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006465 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006466 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006467 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006468 ctx->bio = BIO_new(ha_meth);
6469 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006470 SSL_free(ctx->ssl);
6471 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006472 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006473 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006474 goto retry_accept;
6475 }
Emeric Brun55476152014-11-12 17:35:37 +01006476 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006477 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006478 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006479 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006480 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006481
Emeric Brune1f38db2012-09-03 20:36:47 +02006482 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006483 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6484 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 }
6493
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006494#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6495 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6496 b_alloc(&ctx->early_buf);
6497 SSL_set_max_early_data(ctx->ssl,
6498 /* Only allow early data if we managed to allocate
6499 * a buffer.
6500 */
6501 (!b_is_null(&ctx->early_buf)) ?
6502 global.tune.bufsize - global.tune.maxrewrite : 0);
6503 }
6504#endif
6505
Olivier Houchard66ab4982019-02-26 18:37:15 +01006506 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006507
Emeric Brun46591952012-05-18 15:47:34 +02006508 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006509 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006510#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006511 conn->flags |= CO_FL_EARLY_SSL_HS;
6512#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006513
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006514 _HA_ATOMIC_ADD(&sslconns, 1);
6515 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006516 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006517 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006518 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006519 return 0;
6520 }
6521 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006522 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006523err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006524 if (ctx && ctx->wait_event.tasklet)
6525 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006526 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006527 return -1;
6528}
6529
6530
6531/* This is the callback which is used when an SSL handshake is pending. It
6532 * updates the FD status if it wants some polling before being called again.
6533 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6534 * otherwise it returns non-zero and removes itself from the connection's
6535 * flags (the bit is provided in <flag> by the caller).
6536 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006537static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006538{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006539 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006540 int ret;
6541
Willy Tarreau3c728722014-01-23 13:50:42 +01006542 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006543 return 0;
6544
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006545 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006546 goto out_error;
6547
Willy Tarreau5db847a2019-05-09 14:13:35 +02006548#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006549 /*
6550 * Check if we have early data. If we do, we have to read them
6551 * before SSL_do_handshake() is called, And there's no way to
6552 * detect early data, except to try to read them
6553 */
6554 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006555 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006556
Olivier Houchard54907bb2019-12-19 15:02:39 +01006557 while (1) {
6558 ret = SSL_read_early_data(ctx->ssl,
6559 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6560 &read_data);
6561 if (ret == SSL_READ_EARLY_DATA_ERROR)
6562 goto check_error;
6563 if (read_data > 0) {
6564 conn->flags |= CO_FL_EARLY_DATA;
6565 b_add(&ctx->early_buf, read_data);
6566 }
6567 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6568 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6569 if (!b_data(&ctx->early_buf))
6570 b_free(&ctx->early_buf);
6571 break;
6572 }
6573 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006574 }
6575#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006576 /* If we use SSL_do_handshake to process a reneg initiated by
6577 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6578 * Usually SSL_write and SSL_read are used and process implicitly
6579 * the reneg handshake.
6580 * Here we use SSL_peek as a workaround for reneg.
6581 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006582 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006583 char c;
6584
Olivier Houchard66ab4982019-02-26 18:37:15 +01006585 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006586 if (ret <= 0) {
6587 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006588 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006589
Emeric Brun674b7432012-11-08 19:21:55 +01006590 if (ret == SSL_ERROR_WANT_WRITE) {
6591 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006592 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006593 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006594 return 0;
6595 }
6596 else if (ret == SSL_ERROR_WANT_READ) {
6597 /* handshake may have been completed but we have
6598 * no more data to read.
6599 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006600 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006601 ret = 1;
6602 goto reneg_ok;
6603 }
6604 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006605 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006606 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006607 return 0;
6608 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006609#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006610 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006611 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006612 return 0;
6613 }
6614#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006615 else if (ret == SSL_ERROR_SYSCALL) {
6616 /* if errno is null, then connection was successfully established */
6617 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6618 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006619 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006620#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6621 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006622 conn->err_code = CO_ER_SSL_HANDSHAKE;
6623#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006624 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006625#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006626 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006627 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006628 empty_handshake = state == TLS_ST_BEFORE;
6629#else
Lukas Tribus49799162019-07-08 14:29:15 +02006630 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6631 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006632#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006633 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006634 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006635 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006636 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6637 else
6638 conn->err_code = CO_ER_SSL_EMPTY;
6639 }
6640 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006641 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006642 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6643 else
6644 conn->err_code = CO_ER_SSL_ABORT;
6645 }
6646 }
6647 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006648 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006649 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006650 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006651 conn->err_code = CO_ER_SSL_HANDSHAKE;
6652 }
Lukas Tribus49799162019-07-08 14:29:15 +02006653#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006654 }
Emeric Brun674b7432012-11-08 19:21:55 +01006655 goto out_error;
6656 }
6657 else {
6658 /* Fail on all other handshake errors */
6659 /* Note: OpenSSL may leave unread bytes in the socket's
6660 * buffer, causing an RST to be emitted upon close() on
6661 * TCP sockets. We first try to drain possibly pending
6662 * data to avoid this as much as possible.
6663 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006664 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006665 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006666 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006667 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006668 goto out_error;
6669 }
6670 }
6671 /* read some data: consider handshake completed */
6672 goto reneg_ok;
6673 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006674 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006675check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006676 if (ret != 1) {
6677 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006678 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006679
6680 if (ret == SSL_ERROR_WANT_WRITE) {
6681 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006682 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006683 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006684 return 0;
6685 }
6686 else if (ret == SSL_ERROR_WANT_READ) {
6687 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006688 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006689 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6690 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006691 return 0;
6692 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006693#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006694 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006695 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006696 return 0;
6697 }
6698#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006699 else if (ret == SSL_ERROR_SYSCALL) {
6700 /* if errno is null, then connection was successfully established */
6701 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6702 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006703 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006704#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6705 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006706 conn->err_code = CO_ER_SSL_HANDSHAKE;
6707#else
6708 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006709#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006710 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006711 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006712 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006713#else
Lukas Tribus49799162019-07-08 14:29:15 +02006714 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6715 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006716#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006717 if (empty_handshake) {
6718 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006719 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006720 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6721 else
6722 conn->err_code = CO_ER_SSL_EMPTY;
6723 }
6724 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006725 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006726 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6727 else
6728 conn->err_code = CO_ER_SSL_ABORT;
6729 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006730 }
6731 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006732 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006733 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6734 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006735 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006736 }
Lukas Tribus49799162019-07-08 14:29:15 +02006737#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006738 }
Willy Tarreau89230192012-09-28 20:22:13 +02006739 goto out_error;
6740 }
Emeric Brun46591952012-05-18 15:47:34 +02006741 else {
6742 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006743 /* Note: OpenSSL may leave unread bytes in the socket's
6744 * buffer, causing an RST to be emitted upon close() on
6745 * TCP sockets. We first try to drain possibly pending
6746 * data to avoid this as much as possible.
6747 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006748 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006749 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006750 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006751 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006752 goto out_error;
6753 }
6754 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006755#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006756 else {
6757 /*
6758 * If the server refused the early data, we have to send a
6759 * 425 to the client, as we no longer have the data to sent
6760 * them again.
6761 */
6762 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006763 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006764 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6765 goto out_error;
6766 }
6767 }
6768 }
6769#endif
6770
Emeric Brun46591952012-05-18 15:47:34 +02006771
Emeric Brun674b7432012-11-08 19:21:55 +01006772reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006773
Willy Tarreau5db847a2019-05-09 14:13:35 +02006774#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006775 /* ASYNC engine API doesn't support moving read/write
6776 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006777 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006778 */
6779 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006780 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006781#endif
Emeric Brun46591952012-05-18 15:47:34 +02006782 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006783 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006784 if (objt_server(conn->target)) {
6785 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6786 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6787 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006788 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006789 else {
6790 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6791 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6792 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6793 }
Emeric Brun46591952012-05-18 15:47:34 +02006794 }
6795
6796 /* The connection is now established at both layers, it's time to leave */
6797 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6798 return 1;
6799
6800 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006801 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006802 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006803 ERR_clear_error();
6804
Emeric Brun9fa89732012-10-04 17:09:56 +02006805 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006806 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6807 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6808 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006809 }
6810
Emeric Brun46591952012-05-18 15:47:34 +02006811 /* Fail on all other handshake errors */
6812 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006813 if (!conn->err_code)
6814 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006815 return 0;
6816}
6817
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006818/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6819 * event subscriber <es> is not allowed to change from a previous call as long
6820 * as at least one event is still subscribed. The <event_type> must only be a
6821 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6822 * unless the transport layer was already released.
6823 */
6824static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006825{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006826 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006827
Olivier Houchard0ff28652019-06-24 18:57:39 +02006828 if (!ctx)
6829 return -1;
6830
Willy Tarreau113d52b2020-01-10 09:20:26 +01006831 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006832 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006833
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006834 ctx->subs = es;
6835 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006836
6837 /* we may have to subscribe to lower layers for new events */
6838 event_type &= ~ctx->wait_event.events;
6839 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6840 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006841 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006842}
6843
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006844/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6845 * The <es> pointer is not allowed to differ from the one passed to the
6846 * subscribe() call. It always returns zero.
6847 */
6848static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006849{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006850 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006851
Willy Tarreau113d52b2020-01-10 09:20:26 +01006852 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006853 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006854
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006855 es->events &= ~event_type;
6856 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006857 ctx->subs = NULL;
6858
6859 /* If we subscribed, and we're not doing the handshake,
6860 * then we subscribed because the upper layer asked for it,
6861 * as the upper layer is no longer interested, we can
6862 * unsubscribe too.
6863 */
6864 event_type &= ctx->wait_event.events;
6865 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6866 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006867
6868 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006869}
6870
Olivier Houchard2e055482019-05-27 19:50:12 +02006871/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6872 * Returns 0 on success, and non-zero on failure.
6873 */
6874static 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)
6875{
6876 struct ssl_sock_ctx *ctx = xprt_ctx;
6877
6878 if (oldxprt_ops != NULL)
6879 *oldxprt_ops = ctx->xprt;
6880 if (oldxprt_ctx != NULL)
6881 *oldxprt_ctx = ctx->xprt_ctx;
6882 ctx->xprt = toadd_ops;
6883 ctx->xprt_ctx = toadd_ctx;
6884 return 0;
6885}
6886
Olivier Houchard5149b592019-05-23 17:47:36 +02006887/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6888 * return 0, otherwise just call the remove_xprt method from the underlying
6889 * XPRT.
6890 */
6891static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6892{
6893 struct ssl_sock_ctx *ctx = xprt_ctx;
6894
6895 if (ctx->xprt_ctx == toremove_ctx) {
6896 ctx->xprt_ctx = newctx;
6897 ctx->xprt = newops;
6898 return 0;
6899 }
6900 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6901}
6902
Olivier Houchardea8dd942019-05-20 14:02:16 +02006903static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6904{
6905 struct ssl_sock_ctx *ctx = context;
6906
6907 /* First if we're doing an handshake, try that */
6908 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6909 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6910 /* If we had an error, or the handshake is done and I/O is available,
6911 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006912 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006913 * we can't be sure conn_fd_handler() will be called again.
6914 */
6915 if ((ctx->conn->flags & CO_FL_ERROR) ||
6916 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6917 int ret = 0;
6918 int woke = 0;
6919
6920 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006921 if (ctx->subs) {
6922 tasklet_wakeup(ctx->subs->tasklet);
6923 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006924 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006925 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006926 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006927
Olivier Houchardea8dd942019-05-20 14:02:16 +02006928 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006929 * upper layers know. If we have no mux, create it,
6930 * and once we have a mux, call its wake method if we didn't
6931 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006932 */
6933 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006934 if (!ctx->conn->mux)
6935 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006936 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6937 ctx->conn->mux->wake(ctx->conn);
6938 return NULL;
6939 }
6940 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006941#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6942 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006943 else if (b_data(&ctx->early_buf) && ctx->subs &&
6944 ctx->subs->events & SUB_RETRY_RECV) {
6945 tasklet_wakeup(ctx->subs->tasklet);
6946 ctx->subs->events &= ~SUB_RETRY_RECV;
6947 if (!ctx->subs->events)
6948 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006949 }
6950#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006951 return NULL;
6952}
6953
Emeric Brun46591952012-05-18 15:47:34 +02006954/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006955 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006956 * buffer wraps, in which case a second call may be performed. The connection's
6957 * flags are updated with whatever special event is detected (error, read0,
6958 * empty). The caller is responsible for taking care of those events and
6959 * avoiding the call if inappropriate. The function does not call the
6960 * connection's polling update function, so the caller is responsible for this.
6961 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006962static 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 +02006963{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006964 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006965 ssize_t ret;
6966 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006967
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006968 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006969 goto out_error;
6970
Olivier Houchard54907bb2019-12-19 15:02:39 +01006971#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6972 if (b_data(&ctx->early_buf)) {
6973 try = b_contig_space(buf);
6974 if (try > b_data(&ctx->early_buf))
6975 try = b_data(&ctx->early_buf);
6976 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6977 b_add(buf, try);
6978 b_del(&ctx->early_buf, try);
6979 if (b_data(&ctx->early_buf) == 0)
6980 b_free(&ctx->early_buf);
6981 return try;
6982 }
6983#endif
6984
Willy Tarreau911db9b2020-01-23 16:27:54 +01006985 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006986 /* a handshake was requested */
6987 return 0;
6988
Emeric Brun46591952012-05-18 15:47:34 +02006989 /* read the largest possible block. For this, we perform only one call
6990 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6991 * in which case we accept to do it once again. A new attempt is made on
6992 * EINTR too.
6993 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006994 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006995
Willy Tarreau591d4452018-06-15 17:21:00 +02006996 try = b_contig_space(buf);
6997 if (!try)
6998 break;
6999
Willy Tarreauabf08d92014-01-14 11:31:27 +01007000 if (try > count)
7001 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007002
Olivier Houchard66ab4982019-02-26 18:37:15 +01007003 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007004
Emeric Brune1f38db2012-09-03 20:36:47 +02007005 if (conn->flags & CO_FL_ERROR) {
7006 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007007 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007008 }
Emeric Brun46591952012-05-18 15:47:34 +02007009 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007010 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007011 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007012 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007013 }
Emeric Brun46591952012-05-18 15:47:34 +02007014 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007015 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007016 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007017 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007018 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007019 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007020#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007021 /* Async mode can be re-enabled, because we're leaving data state.*/
7022 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007023 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007024#endif
Emeric Brun46591952012-05-18 15:47:34 +02007025 break;
7026 }
7027 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007028 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007029 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7030 SUB_RETRY_RECV,
7031 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007032 /* handshake is running, and it may need to re-enable read */
7033 conn->flags |= CO_FL_SSL_WAIT_HS;
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 Brun282a76a2012-11-08 18:02:56 +01007039 break;
7040 }
Emeric Brun46591952012-05-18 15:47:34 +02007041 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007042 } else if (ret == SSL_ERROR_ZERO_RETURN)
7043 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007044 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7045 * stack before shutting down the connection for
7046 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007047 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7048 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007049 /* otherwise it's a real error */
7050 goto out_error;
7051 }
7052 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007053 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007054 return done;
7055
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007056 clear_ssl_error:
7057 /* Clear openssl global errors stack */
7058 ssl_sock_dump_errors(conn);
7059 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007060 read0:
7061 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007062 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007063
Emeric Brun46591952012-05-18 15:47:34 +02007064 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007065 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007066 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007067 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007068 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007069 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007070}
7071
7072
Willy Tarreau787db9a2018-06-14 18:31:46 +02007073/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7074 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7075 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007076 * Only one call to send() is performed, unless the buffer wraps, in which case
7077 * a second call may be performed. The connection's flags are updated with
7078 * whatever special event is detected (error, empty). The caller is responsible
7079 * for taking care of those events and avoiding the call if inappropriate. The
7080 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007081 * is responsible for this. The buffer's output is not adjusted, it's up to the
7082 * caller to take care of this. It's up to the caller to update the buffer's
7083 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007084 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007085static 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 +02007086{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007087 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007088 ssize_t ret;
7089 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007090
7091 done = 0;
7092
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007093 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007094 goto out_error;
7095
Willy Tarreau911db9b2020-01-23 16:27:54 +01007096 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007097 /* a handshake was requested */
7098 return 0;
7099
7100 /* send the largest possible block. For this we perform only one call
7101 * to send() unless the buffer wraps and we exactly fill the first hunk,
7102 * in which case we accept to do it once again.
7103 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007104 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007105#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007106 size_t written_data;
7107#endif
7108
Willy Tarreau787db9a2018-06-14 18:31:46 +02007109 try = b_contig_data(buf, done);
7110 if (try > count)
7111 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007112
Willy Tarreau7bed9452014-02-02 02:00:24 +01007113 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007114 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007115 global_ssl.max_record && try > global_ssl.max_record) {
7116 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007117 }
7118 else {
7119 /* we need to keep the information about the fact that
7120 * we're not limiting the upcoming send(), because if it
7121 * fails, we'll have to retry with at least as many data.
7122 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007123 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007124 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007125
Willy Tarreau5db847a2019-05-09 14:13:35 +02007126#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007127 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007128 unsigned int max_early;
7129
Olivier Houchard522eea72017-11-03 16:27:47 +01007130 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007131 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007132 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007133 if (SSL_get0_session(ctx->ssl))
7134 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007135 else
7136 max_early = 0;
7137 }
7138
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007139 if (try + ctx->sent_early_data > max_early) {
7140 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007141 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007142 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007143 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007144 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007145 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007146 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007147 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007148 if (ret == 1) {
7149 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007150 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007151 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007152 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007153 /* Initiate the handshake, now */
7154 tasklet_wakeup(ctx->wait_event.tasklet);
7155 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007156
Olivier Houchardc2aae742017-09-22 18:26:28 +02007157 }
7158
7159 } else
7160#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007161 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007162
Emeric Brune1f38db2012-09-03 20:36:47 +02007163 if (conn->flags & CO_FL_ERROR) {
7164 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007165 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007166 }
Emeric Brun46591952012-05-18 15:47:34 +02007167 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007168 /* A send succeeded, so we can consider ourself connected */
7169 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007170 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007171 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007172 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007173 }
7174 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007175 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007176
Emeric Brun46591952012-05-18 15:47:34 +02007177 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007178 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007179 /* handshake is running, and it may need to re-enable write */
7180 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007181 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007182#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007183 /* Async mode can be re-enabled, because we're leaving data state.*/
7184 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007185 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007186#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007187 break;
7188 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007189
Emeric Brun46591952012-05-18 15:47:34 +02007190 break;
7191 }
7192 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007193 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007194 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007195 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7196 SUB_RETRY_RECV,
7197 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007198#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007199 /* Async mode can be re-enabled, because we're leaving data state.*/
7200 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007201 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007202#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007203 break;
7204 }
Emeric Brun46591952012-05-18 15:47:34 +02007205 goto out_error;
7206 }
7207 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007208 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007209 return done;
7210
7211 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007212 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007213 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007214 ERR_clear_error();
7215
Emeric Brun46591952012-05-18 15:47:34 +02007216 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007217 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007218}
7219
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007220static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007221
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007222 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007223
Olivier Houchardea8dd942019-05-20 14:02:16 +02007224
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007225 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007226 if (ctx->wait_event.events != 0)
7227 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7228 ctx->wait_event.events,
7229 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007230 if (ctx->subs) {
7231 ctx->subs->events = 0;
7232 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007233 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007234
Olivier Houchard692c1d02019-05-23 18:41:47 +02007235 if (ctx->xprt->close)
7236 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007237#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007238 if (global_ssl.async) {
7239 OSSL_ASYNC_FD all_fd[32], afd;
7240 size_t num_all_fds = 0;
7241 int i;
7242
Olivier Houchard66ab4982019-02-26 18:37:15 +01007243 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007244 if (num_all_fds > 32) {
7245 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7246 return;
7247 }
7248
Olivier Houchard66ab4982019-02-26 18:37:15 +01007249 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007250
7251 /* If an async job is pending, we must try to
7252 to catch the end using polling before calling
7253 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007254 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007255 for (i=0 ; i < num_all_fds ; i++) {
7256 /* switch on an handler designed to
7257 * handle the SSL_free
7258 */
7259 afd = all_fd[i];
7260 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007261 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007262 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007263 /* To ensure that the fd cache won't be used
7264 * and we'll catch a real RD event.
7265 */
7266 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007267 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007268 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007269 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007270 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007271 return;
7272 }
Emeric Brun3854e012017-05-17 20:42:48 +02007273 /* Else we can remove the fds from the fdtab
7274 * and call SSL_free.
7275 * note: we do a fd_remove and not a delete
7276 * because the fd is owned by the engine.
7277 * the engine is responsible to close
7278 */
7279 for (i=0 ; i < num_all_fds ; i++)
7280 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007281 }
7282#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007283 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007284 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007285 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007286 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007287 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007288 }
Emeric Brun46591952012-05-18 15:47:34 +02007289}
7290
7291/* This function tries to perform a clean shutdown on an SSL connection, and in
7292 * any case, flags the connection as reusable if no handshake was in progress.
7293 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007294static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007295{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007296 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007297
Willy Tarreau911db9b2020-01-23 16:27:54 +01007298 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007299 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007300 if (!clean)
7301 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007302 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007303 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007304 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007305 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007306 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007307 ERR_clear_error();
7308 }
Emeric Brun46591952012-05-18 15:47:34 +02007309}
7310
William Lallemandd4f946c2019-12-05 10:26:40 +01007311/* fill a buffer with the algorithm and size of a public key */
7312static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007313{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007314 int bits = 0;
7315 int sig = TLSEXT_signature_anonymous;
7316 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007317 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007318
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007319 pkey = X509_get_pubkey(crt);
7320 if (pkey) {
7321 bits = EVP_PKEY_bits(pkey);
7322 switch(EVP_PKEY_base_id(pkey)) {
7323 case EVP_PKEY_RSA:
7324 sig = TLSEXT_signature_rsa;
7325 break;
7326 case EVP_PKEY_EC:
7327 sig = TLSEXT_signature_ecdsa;
7328 break;
7329 case EVP_PKEY_DSA:
7330 sig = TLSEXT_signature_dsa;
7331 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007332 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007333 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007334 }
7335
7336 switch(sig) {
7337 case TLSEXT_signature_rsa:
7338 len = chunk_printf(out, "RSA%d", bits);
7339 break;
7340 case TLSEXT_signature_ecdsa:
7341 len = chunk_printf(out, "EC%d", bits);
7342 break;
7343 case TLSEXT_signature_dsa:
7344 len = chunk_printf(out, "DSA%d", bits);
7345 break;
7346 default:
7347 return 0;
7348 }
7349 if (len < 0)
7350 return 0;
7351 return 1;
7352}
7353
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007354/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007355int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7356{
7357 struct ssl_sock_ctx *ctx;
7358 X509 *crt;
7359
7360 if (!ssl_sock_is_ssl(conn))
7361 return 0;
7362
7363 ctx = conn->xprt_ctx;
7364
7365 crt = SSL_get_certificate(ctx->ssl);
7366 if (!crt)
7367 return 0;
7368
7369 return cert_get_pkey_algo(crt, out);
7370}
7371
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007372/* used for ppv2 cert signature (can be used for logging) */
7373const char *ssl_sock_get_cert_sig(struct connection *conn)
7374{
Christopher Faulet82004142019-09-10 10:12:03 +02007375 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007376
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007377 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7378 X509 *crt;
7379
7380 if (!ssl_sock_is_ssl(conn))
7381 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007382 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007383 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007384 if (!crt)
7385 return NULL;
7386 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7387 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7388}
7389
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007390/* used for ppv2 authority */
7391const char *ssl_sock_get_sni(struct connection *conn)
7392{
7393#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007394 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007395
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007396 if (!ssl_sock_is_ssl(conn))
7397 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007398 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007399 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007400#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007401 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007402#endif
7403}
7404
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007405/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007406const char *ssl_sock_get_cipher_name(struct connection *conn)
7407{
Christopher Faulet82004142019-09-10 10:12:03 +02007408 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007409
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007410 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007411 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_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007414}
7415
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007416/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007417const char *ssl_sock_get_proto_version(struct connection *conn)
7418{
Christopher Faulet82004142019-09-10 10:12:03 +02007419 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007420
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007421 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007422 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007423 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007424 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007425}
7426
Willy Tarreau8d598402012-10-22 17:58:39 +02007427/* Extract a serial from a cert, and copy it to a chunk.
7428 * Returns 1 if serial is found and copied, 0 if no serial found and
7429 * -1 if output is not large enough.
7430 */
7431static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007432ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007433{
7434 ASN1_INTEGER *serial;
7435
7436 serial = X509_get_serialNumber(crt);
7437 if (!serial)
7438 return 0;
7439
7440 if (out->size < serial->length)
7441 return -1;
7442
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007443 memcpy(out->area, serial->data, serial->length);
7444 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007445 return 1;
7446}
7447
Emeric Brun43e79582014-10-29 19:03:26 +01007448/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007449 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7450 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007451 */
7452static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007453ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007454{
7455 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007456 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007457
7458 len =i2d_X509(crt, NULL);
7459 if (len <= 0)
7460 return 1;
7461
7462 if (out->size < len)
7463 return -1;
7464
7465 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007466 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007467 return 1;
7468}
7469
Emeric Brunce5ad802012-10-22 14:11:22 +02007470
Willy Tarreau83061a82018-07-13 11:56:34 +02007471/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007472 * Returns 1 if serial is found and copied, 0 if no valid time found
7473 * and -1 if output is not large enough.
7474 */
7475static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007476ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007477{
7478 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7479 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7480
7481 if (gentm->length < 12)
7482 return 0;
7483 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7484 return 0;
7485 if (out->size < gentm->length-2)
7486 return -1;
7487
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007488 memcpy(out->area, gentm->data+2, gentm->length-2);
7489 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007490 return 1;
7491 }
7492 else if (tm->type == V_ASN1_UTCTIME) {
7493 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7494
7495 if (utctm->length < 10)
7496 return 0;
7497 if (utctm->data[0] >= 0x35)
7498 return 0;
7499 if (out->size < utctm->length)
7500 return -1;
7501
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007502 memcpy(out->area, utctm->data, utctm->length);
7503 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007504 return 1;
7505 }
7506
7507 return 0;
7508}
7509
Emeric Brun87855892012-10-17 17:39:35 +02007510/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7511 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7512 */
7513static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007514ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7515 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007516{
7517 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007518 ASN1_OBJECT *obj;
7519 ASN1_STRING *data;
7520 const unsigned char *data_ptr;
7521 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007522 int i, j, n;
7523 int cur = 0;
7524 const char *s;
7525 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007526 int name_count;
7527
7528 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007529
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007530 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007531 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007532 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007533 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007534 else
7535 j = i;
7536
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007537 ne = X509_NAME_get_entry(a, j);
7538 obj = X509_NAME_ENTRY_get_object(ne);
7539 data = X509_NAME_ENTRY_get_data(ne);
7540 data_ptr = ASN1_STRING_get0_data(data);
7541 data_len = ASN1_STRING_length(data);
7542 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007543 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007544 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007545 s = tmp;
7546 }
7547
7548 if (chunk_strcasecmp(entry, s) != 0)
7549 continue;
7550
7551 if (pos < 0)
7552 cur--;
7553 else
7554 cur++;
7555
7556 if (cur != pos)
7557 continue;
7558
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007559 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007560 return -1;
7561
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007562 memcpy(out->area, data_ptr, data_len);
7563 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007564 return 1;
7565 }
7566
7567 return 0;
7568
William Lallemandd4f946c2019-12-05 10:26:40 +01007569}
7570
7571/*
7572 * Extract and format the DNS SAN extensions and copy result into a chuink
7573 * Return 0;
7574 */
7575#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7576static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7577{
7578 int i;
7579 char *str;
7580 STACK_OF(GENERAL_NAME) *names = NULL;
7581
7582 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7583 if (names) {
7584 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7585 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7586 if (i > 0)
7587 chunk_appendf(out, ", ");
7588 if (name->type == GEN_DNS) {
7589 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7590 chunk_appendf(out, "DNS:%s", str);
7591 OPENSSL_free(str);
7592 }
7593 }
7594 }
7595 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7596 }
7597 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007598}
William Lallemandd4f946c2019-12-05 10:26:40 +01007599#endif
Emeric Brun87855892012-10-17 17:39:35 +02007600
Elliot Otchet71f82972020-01-15 08:12:14 -05007601/*
7602 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7603 * Currently supports rfc2253 for returning LDAP V3 DNs.
7604 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7605 */
7606static int
7607ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7608{
7609 BIO *bio = NULL;
7610 int ret = 0;
7611 int data_len = 0;
7612
7613 if (chunk_strcmp(format, "rfc2253") == 0) {
7614 bio = BIO_new(BIO_s_mem());
7615 if (bio == NULL)
7616 goto out;
7617
7618 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7619 goto out;
7620
7621 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7622 goto out;
7623
7624 out->data = data_len;
7625
7626 ret = 1;
7627 }
7628out:
7629 if (bio)
7630 BIO_free(bio);
7631 return ret;
7632}
7633
Emeric Brun87855892012-10-17 17:39:35 +02007634/* Extract and format full DN from a X509_NAME and copy result into a chunk
7635 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7636 */
7637static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007638ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007639{
7640 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007641 ASN1_OBJECT *obj;
7642 ASN1_STRING *data;
7643 const unsigned char *data_ptr;
7644 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007645 int i, n, ln;
7646 int l = 0;
7647 const char *s;
7648 char *p;
7649 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007650 int name_count;
7651
7652
7653 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007654
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007655 out->data = 0;
7656 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007657 for (i = 0; i < name_count; i++) {
7658 ne = X509_NAME_get_entry(a, i);
7659 obj = X509_NAME_ENTRY_get_object(ne);
7660 data = X509_NAME_ENTRY_get_data(ne);
7661 data_ptr = ASN1_STRING_get0_data(data);
7662 data_len = ASN1_STRING_length(data);
7663 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007664 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007665 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007666 s = tmp;
7667 }
7668 ln = strlen(s);
7669
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007670 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007671 if (l > out->size)
7672 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007673 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007674
7675 *(p++)='/';
7676 memcpy(p, s, ln);
7677 p += ln;
7678 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007679 memcpy(p, data_ptr, data_len);
7680 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007681 }
7682
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007683 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007684 return 0;
7685
7686 return 1;
7687}
7688
Olivier Houchardab28a322018-12-21 19:45:40 +01007689void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7690{
7691#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007692 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007693
Olivier Houcharde488ea82019-06-28 14:10:33 +02007694 if (!ssl_sock_is_ssl(conn))
7695 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007696 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007697 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007698#endif
7699}
7700
Willy Tarreau119a4082016-12-22 21:58:38 +01007701/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7702 * to disable SNI.
7703 */
Willy Tarreau63076412015-07-10 11:33:32 +02007704void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7705{
7706#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007707 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007708
Willy Tarreau119a4082016-12-22 21:58:38 +01007709 char *prev_name;
7710
Willy Tarreau63076412015-07-10 11:33:32 +02007711 if (!ssl_sock_is_ssl(conn))
7712 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007713 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007714
Willy Tarreau119a4082016-12-22 21:58:38 +01007715 /* if the SNI changes, we must destroy the reusable context so that a
7716 * new connection will present a new SNI. As an optimization we could
7717 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7718 * server.
7719 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007720 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007721 if ((!prev_name && hostname) ||
7722 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007723 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007724
Olivier Houchard66ab4982019-02-26 18:37:15 +01007725 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007726#endif
7727}
7728
Emeric Brun0abf8362014-06-24 18:26:41 +02007729/* Extract peer certificate's common name into the chunk dest
7730 * Returns
7731 * the len of the extracted common name
7732 * or 0 if no CN found in DN
7733 * or -1 on error case (i.e. no peer certificate)
7734 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007735int ssl_sock_get_remote_common_name(struct connection *conn,
7736 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007737{
Christopher Faulet82004142019-09-10 10:12:03 +02007738 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007739 X509 *crt = NULL;
7740 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007741 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007742 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007743 .area = (char *)&find_cn,
7744 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007745 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007746 int result = -1;
David Safb76832014-05-08 23:42:08 -04007747
7748 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007749 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007750 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007751
7752 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007753 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007754 if (!crt)
7755 goto out;
7756
7757 name = X509_get_subject_name(crt);
7758 if (!name)
7759 goto out;
David Safb76832014-05-08 23:42:08 -04007760
Emeric Brun0abf8362014-06-24 18:26:41 +02007761 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7762out:
David Safb76832014-05-08 23:42:08 -04007763 if (crt)
7764 X509_free(crt);
7765
7766 return result;
7767}
7768
Dave McCowan328fb582014-07-30 10:39:13 -04007769/* returns 1 if client passed a certificate for this session, 0 if not */
7770int ssl_sock_get_cert_used_sess(struct connection *conn)
7771{
Christopher Faulet82004142019-09-10 10:12:03 +02007772 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007773 X509 *crt = NULL;
7774
7775 if (!ssl_sock_is_ssl(conn))
7776 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007777 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007778
7779 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007780 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007781 if (!crt)
7782 return 0;
7783
7784 X509_free(crt);
7785 return 1;
7786}
7787
7788/* returns 1 if client passed a certificate for this connection, 0 if not */
7789int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007790{
Christopher Faulet82004142019-09-10 10:12:03 +02007791 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007792
David Safb76832014-05-08 23:42:08 -04007793 if (!ssl_sock_is_ssl(conn))
7794 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007795 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007796 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007797}
7798
7799/* returns result from SSL verify */
7800unsigned int ssl_sock_get_verify_result(struct connection *conn)
7801{
Christopher Faulet82004142019-09-10 10:12:03 +02007802 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007803
David Safb76832014-05-08 23:42:08 -04007804 if (!ssl_sock_is_ssl(conn))
7805 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007806 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007807 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007808}
7809
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007810/* Returns the application layer protocol name in <str> and <len> when known.
7811 * Zero is returned if the protocol name was not found, otherwise non-zero is
7812 * returned. The string is allocated in the SSL context and doesn't have to be
7813 * freed by the caller. NPN is also checked if available since older versions
7814 * of openssl (1.0.1) which are more common in field only support this one.
7815 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007816static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007817{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007818#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7819 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007820 struct ssl_sock_ctx *ctx = xprt_ctx;
7821 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007822 return 0;
7823
7824 *str = NULL;
7825
7826#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007827 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007828 if (*str)
7829 return 1;
7830#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007831#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007832 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007833 if (*str)
7834 return 1;
7835#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007836#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007837 return 0;
7838}
7839
Willy Tarreau7875d092012-09-10 08:20:03 +02007840/***** Below are some sample fetching functions for ACL/patterns *****/
7841
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007842static int
7843smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7844{
7845 struct connection *conn;
7846
7847 conn = objt_conn(smp->sess->origin);
7848 if (!conn || conn->xprt != &ssl_sock)
7849 return 0;
7850
7851 smp->flags = 0;
7852 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007853#ifdef OPENSSL_IS_BORINGSSL
7854 {
7855 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7856 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7857 SSL_early_data_accepted(ctx->ssl));
7858 }
7859#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007860 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007861 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007862#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007863 return 1;
7864}
7865
Emeric Brune64aef12012-09-21 13:15:06 +02007866/* boolean, returns true if client cert was present */
7867static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007868smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007869{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007870 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007871 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007872
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007873 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007874 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007875 return 0;
7876
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007877 ctx = conn->xprt_ctx;
7878
Willy Tarreau911db9b2020-01-23 16:27:54 +01007879 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007880 smp->flags |= SMP_F_MAY_CHANGE;
7881 return 0;
7882 }
7883
7884 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007885 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007886 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007887
7888 return 1;
7889}
7890
Emeric Brun43e79582014-10-29 19:03:26 +01007891/* binary, returns a certificate in a binary chunk (der/raw).
7892 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7893 * should be use.
7894 */
7895static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007896smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007897{
7898 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7899 X509 *crt = NULL;
7900 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007901 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007902 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007903 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007904
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007905 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007906 if (!conn || conn->xprt != &ssl_sock)
7907 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007908 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007909
Willy Tarreau911db9b2020-01-23 16:27:54 +01007910 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007911 smp->flags |= SMP_F_MAY_CHANGE;
7912 return 0;
7913 }
7914
7915 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007916 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007917 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007918 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007919
7920 if (!crt)
7921 goto out;
7922
7923 smp_trash = get_trash_chunk();
7924 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7925 goto out;
7926
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007927 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007928 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007929 ret = 1;
7930out:
7931 /* SSL_get_peer_certificate, it increase X509 * ref count */
7932 if (cert_peer && crt)
7933 X509_free(crt);
7934 return ret;
7935}
7936
Emeric Brunba841a12014-04-30 17:05:08 +02007937/* binary, returns serial of certificate in a binary chunk.
7938 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7939 * should be use.
7940 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007941static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007942smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007943{
Emeric Brunba841a12014-04-30 17:05:08 +02007944 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007945 X509 *crt = NULL;
7946 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007947 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007948 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007949 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007950
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007951 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007952 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007953 return 0;
7954
Olivier Houchard66ab4982019-02-26 18:37:15 +01007955 ctx = conn->xprt_ctx;
7956
Willy Tarreau911db9b2020-01-23 16:27:54 +01007957 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007958 smp->flags |= SMP_F_MAY_CHANGE;
7959 return 0;
7960 }
7961
Emeric Brunba841a12014-04-30 17:05:08 +02007962 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007963 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007964 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007965 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007966
Willy Tarreau8d598402012-10-22 17:58:39 +02007967 if (!crt)
7968 goto out;
7969
Willy Tarreau47ca5452012-12-23 20:22:19 +01007970 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007971 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7972 goto out;
7973
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007974 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007975 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007976 ret = 1;
7977out:
Emeric Brunba841a12014-04-30 17:05:08 +02007978 /* SSL_get_peer_certificate, it increase X509 * ref count */
7979 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007980 X509_free(crt);
7981 return ret;
7982}
Emeric Brune64aef12012-09-21 13:15:06 +02007983
Emeric Brunba841a12014-04-30 17:05:08 +02007984/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7985 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7986 * should be use.
7987 */
James Votha051b4a2013-05-14 20:37:59 +02007988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007989smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007990{
Emeric Brunba841a12014-04-30 17:05:08 +02007991 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007992 X509 *crt = NULL;
7993 const EVP_MD *digest;
7994 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01007995 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007996 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007997 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007998 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007999
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008000 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008001 if (!conn || conn->xprt != &ssl_sock)
8002 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008003 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008004
Willy Tarreau911db9b2020-01-23 16:27:54 +01008005 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008006 smp->flags |= SMP_F_MAY_CHANGE;
8007 return 0;
8008 }
8009
Emeric Brunba841a12014-04-30 17:05:08 +02008010 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008011 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008012 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008013 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008014 if (!crt)
8015 goto out;
8016
8017 smp_trash = get_trash_chunk();
8018 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008019 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8020 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008021 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008022 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008023 ret = 1;
8024out:
Emeric Brunba841a12014-04-30 17:05:08 +02008025 /* SSL_get_peer_certificate, it increase X509 * ref count */
8026 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008027 X509_free(crt);
8028 return ret;
8029}
8030
Emeric Brunba841a12014-04-30 17:05:08 +02008031/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8032 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8033 * should be use.
8034 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008036smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008037{
Emeric Brunba841a12014-04-30 17:05:08 +02008038 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008039 X509 *crt = NULL;
8040 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008041 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008042 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008043 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008044
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008045 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008046 if (!conn || conn->xprt != &ssl_sock)
8047 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008048 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008049
Willy Tarreau911db9b2020-01-23 16:27:54 +01008050 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008051 smp->flags |= SMP_F_MAY_CHANGE;
8052 return 0;
8053 }
8054
Emeric Brunba841a12014-04-30 17:05:08 +02008055 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008056 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008057 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008058 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008059 if (!crt)
8060 goto out;
8061
Willy Tarreau47ca5452012-12-23 20:22:19 +01008062 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008063 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008064 goto out;
8065
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008066 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008067 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008068 ret = 1;
8069out:
Emeric Brunba841a12014-04-30 17:05:08 +02008070 /* SSL_get_peer_certificate, it increase X509 * ref count */
8071 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008072 X509_free(crt);
8073 return ret;
8074}
8075
Emeric Brunba841a12014-04-30 17:05:08 +02008076/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8077 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8078 * should be use.
8079 */
Emeric Brun87855892012-10-17 17:39:35 +02008080static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008081smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008082{
Emeric Brunba841a12014-04-30 17:05:08 +02008083 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008084 X509 *crt = NULL;
8085 X509_NAME *name;
8086 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008087 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008088 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008089 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008090
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008091 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008092 if (!conn || conn->xprt != &ssl_sock)
8093 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008094 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008095
Willy Tarreau911db9b2020-01-23 16:27:54 +01008096 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008097 smp->flags |= SMP_F_MAY_CHANGE;
8098 return 0;
8099 }
8100
Emeric Brunba841a12014-04-30 17:05:08 +02008101 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008102 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008103 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008104 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008105 if (!crt)
8106 goto out;
8107
8108 name = X509_get_issuer_name(crt);
8109 if (!name)
8110 goto out;
8111
Willy Tarreau47ca5452012-12-23 20:22:19 +01008112 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008113 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008114 int pos = 1;
8115
8116 if (args[1].type == ARGT_SINT)
8117 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008118
8119 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8120 goto out;
8121 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008122 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8123 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8124 goto out;
8125 }
Emeric Brun87855892012-10-17 17:39:35 +02008126 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8127 goto out;
8128
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008129 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008130 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008131 ret = 1;
8132out:
Emeric Brunba841a12014-04-30 17:05:08 +02008133 /* SSL_get_peer_certificate, it increase X509 * ref count */
8134 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008135 X509_free(crt);
8136 return ret;
8137}
8138
Emeric Brunba841a12014-04-30 17:05:08 +02008139/* string, returns notbefore date in ASN1_UTCTIME format.
8140 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8141 * should be use.
8142 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008143static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008144smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008145{
Emeric Brunba841a12014-04-30 17:05:08 +02008146 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008147 X509 *crt = NULL;
8148 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008149 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008150 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008151 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008152
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008153 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008154 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008155 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008156 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008157
Willy Tarreau911db9b2020-01-23 16:27:54 +01008158 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008159 smp->flags |= SMP_F_MAY_CHANGE;
8160 return 0;
8161 }
8162
Emeric Brunba841a12014-04-30 17:05:08 +02008163 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008164 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008165 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008166 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008167 if (!crt)
8168 goto out;
8169
Willy Tarreau47ca5452012-12-23 20:22:19 +01008170 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008171 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008172 goto out;
8173
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008174 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008175 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008176 ret = 1;
8177out:
Emeric Brunba841a12014-04-30 17:05:08 +02008178 /* SSL_get_peer_certificate, it increase X509 * ref count */
8179 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008180 X509_free(crt);
8181 return ret;
8182}
8183
Emeric Brunba841a12014-04-30 17:05:08 +02008184/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8185 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8186 * should be use.
8187 */
Emeric Brun87855892012-10-17 17:39:35 +02008188static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008189smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008190{
Emeric Brunba841a12014-04-30 17:05:08 +02008191 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008192 X509 *crt = NULL;
8193 X509_NAME *name;
8194 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008195 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008196 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008197 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008198
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008199 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008200 if (!conn || conn->xprt != &ssl_sock)
8201 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008202 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008203
Willy Tarreau911db9b2020-01-23 16:27:54 +01008204 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008205 smp->flags |= SMP_F_MAY_CHANGE;
8206 return 0;
8207 }
8208
Emeric Brunba841a12014-04-30 17:05:08 +02008209 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008210 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008211 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008212 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008213 if (!crt)
8214 goto out;
8215
8216 name = X509_get_subject_name(crt);
8217 if (!name)
8218 goto out;
8219
Willy Tarreau47ca5452012-12-23 20:22:19 +01008220 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008221 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008222 int pos = 1;
8223
8224 if (args[1].type == ARGT_SINT)
8225 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008226
8227 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8228 goto out;
8229 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008230 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8231 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8232 goto out;
8233 }
Emeric Brun87855892012-10-17 17:39:35 +02008234 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8235 goto out;
8236
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008237 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008238 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008239 ret = 1;
8240out:
Emeric Brunba841a12014-04-30 17:05:08 +02008241 /* SSL_get_peer_certificate, it increase X509 * ref count */
8242 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008243 X509_free(crt);
8244 return ret;
8245}
Emeric Brun9143d372012-12-20 15:44:16 +01008246
8247/* integer, returns true if current session use a client certificate */
8248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008249smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008250{
8251 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008252 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008253 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008254
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008255 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008256 if (!conn || conn->xprt != &ssl_sock)
8257 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008258 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008259
Willy Tarreau911db9b2020-01-23 16:27:54 +01008260 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008261 smp->flags |= SMP_F_MAY_CHANGE;
8262 return 0;
8263 }
8264
8265 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008266 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008267 if (crt) {
8268 X509_free(crt);
8269 }
8270
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008271 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008272 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008273 return 1;
8274}
8275
Emeric Brunba841a12014-04-30 17:05:08 +02008276/* integer, returns the certificate version
8277 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8278 * should be use.
8279 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008280static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008281smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008282{
Emeric Brunba841a12014-04-30 17:05:08 +02008283 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008284 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008285 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008286 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008287
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008288 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008289 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008290 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008291 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008292
Willy Tarreau911db9b2020-01-23 16:27:54 +01008293 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008294 smp->flags |= SMP_F_MAY_CHANGE;
8295 return 0;
8296 }
8297
Emeric Brunba841a12014-04-30 17:05:08 +02008298 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008299 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008300 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008301 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008302 if (!crt)
8303 return 0;
8304
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008305 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008306 /* SSL_get_peer_certificate increase X509 * ref count */
8307 if (cert_peer)
8308 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008309 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008310
8311 return 1;
8312}
8313
Emeric Brunba841a12014-04-30 17:05:08 +02008314/* string, returns the certificate's signature algorithm.
8315 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8316 * should be use.
8317 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008318static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008319smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008320{
Emeric Brunba841a12014-04-30 17:05:08 +02008321 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008322 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008323 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008324 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008325 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008326 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008327
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008328 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008329 if (!conn || conn->xprt != &ssl_sock)
8330 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008331 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008332
Willy Tarreau911db9b2020-01-23 16:27:54 +01008333 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008334 smp->flags |= SMP_F_MAY_CHANGE;
8335 return 0;
8336 }
8337
Emeric Brunba841a12014-04-30 17:05:08 +02008338 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008339 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008340 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008341 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008342 if (!crt)
8343 return 0;
8344
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008345 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8346 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008347
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008348 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8349 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008350 /* SSL_get_peer_certificate increase X509 * ref count */
8351 if (cert_peer)
8352 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008353 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008354 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008355
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008356 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008357 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008358 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008359 /* SSL_get_peer_certificate increase X509 * ref count */
8360 if (cert_peer)
8361 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008362
8363 return 1;
8364}
8365
Emeric Brunba841a12014-04-30 17:05:08 +02008366/* string, returns the certificate's key algorithm.
8367 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8368 * should be use.
8369 */
Emeric Brun521a0112012-10-22 12:22:55 +02008370static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008371smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008372{
Emeric Brunba841a12014-04-30 17:05:08 +02008373 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008374 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008375 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008376 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008377 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008378 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008379
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008380 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008381 if (!conn || conn->xprt != &ssl_sock)
8382 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008383 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008384
Willy Tarreau911db9b2020-01-23 16:27:54 +01008385 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008386 smp->flags |= SMP_F_MAY_CHANGE;
8387 return 0;
8388 }
8389
Emeric Brunba841a12014-04-30 17:05:08 +02008390 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008391 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008392 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008393 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008394 if (!crt)
8395 return 0;
8396
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008397 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8398 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008399
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008400 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8401 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008402 /* SSL_get_peer_certificate increase X509 * ref count */
8403 if (cert_peer)
8404 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008405 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008406 }
Emeric Brun521a0112012-10-22 12:22:55 +02008407
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008408 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008409 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008410 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008411 if (cert_peer)
8412 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008413
8414 return 1;
8415}
8416
Emeric Brun645ae792014-04-30 14:21:06 +02008417/* boolean, returns true if front conn. transport layer is SSL.
8418 * This function is also usable on backend conn if the fetch keyword 5th
8419 * char is 'b'.
8420 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008421static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008422smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008423{
Emeric Bruneb8def92018-02-19 15:59:48 +01008424 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8425 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008426
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008427 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008428 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008429 return 1;
8430}
8431
Emeric Brun2525b6b2012-10-18 15:59:43 +02008432/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008433static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008434smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008435{
8436#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008437 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008438 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008439
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008440 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008441 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008442 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008443 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008444 return 1;
8445#else
8446 return 0;
8447#endif
8448}
8449
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008450/* boolean, returns true if client session has been resumed.
8451 * This function is also usable on backend conn if the fetch keyword 5th
8452 * char is 'b'.
8453 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008454static int
8455smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8456{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008457 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8458 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008459 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008460
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008461
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008462 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008463 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008464 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008465 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008466 return 1;
8467}
8468
Emeric Brun645ae792014-04-30 14:21:06 +02008469/* string, returns the used cipher if front conn. transport layer is SSL.
8470 * This function is also usable on backend conn if the fetch keyword 5th
8471 * char is 'b'.
8472 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008473static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008474smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008475{
Emeric Bruneb8def92018-02-19 15:59:48 +01008476 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8477 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008478 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008479
Willy Tarreaube508f12016-03-10 11:47:01 +01008480 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008481 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008482 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008483 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008484
Olivier Houchard66ab4982019-02-26 18:37:15 +01008485 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008486 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008487 return 0;
8488
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008489 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008490 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008491 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008492
8493 return 1;
8494}
8495
Emeric Brun645ae792014-04-30 14:21:06 +02008496/* integer, returns the algoritm's keysize if front conn. transport layer
8497 * is SSL.
8498 * This function is also usable on backend conn if the fetch keyword 5th
8499 * char is 'b'.
8500 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008501static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008502smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008503{
Emeric Bruneb8def92018-02-19 15:59:48 +01008504 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8505 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008506 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008507 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008508
Emeric Brun589fcad2012-10-16 14:13:26 +02008509 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008510 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008511 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008512 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008513
Olivier Houchard66ab4982019-02-26 18:37:15 +01008514 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008515 return 0;
8516
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008517 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008518 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008519
8520 return 1;
8521}
8522
Emeric Brun645ae792014-04-30 14:21:06 +02008523/* integer, returns the used keysize if front conn. transport layer is SSL.
8524 * This function is also usable on backend conn if the fetch keyword 5th
8525 * char is 'b'.
8526 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008527static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008528smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008529{
Emeric Bruneb8def92018-02-19 15:59:48 +01008530 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8531 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008532 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008533
Emeric Brun589fcad2012-10-16 14:13:26 +02008534 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008535 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8536 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008537 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008538
Olivier Houchard66ab4982019-02-26 18:37:15 +01008539 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008540 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008541 return 0;
8542
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008543 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008544
8545 return 1;
8546}
8547
Bernard Spil13c53f82018-02-15 13:34:58 +01008548#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008549static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008550smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008551{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008552 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008553 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008554 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008555
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008556 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008557 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008558
Olivier Houchard6b77f492018-11-22 18:18:29 +01008559 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8560 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008561 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8562 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008563 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008564
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008565 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008566 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008567 (const unsigned char **)&smp->data.u.str.area,
8568 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008569
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008570 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008571 return 0;
8572
Willy Tarreau105599c2020-02-25 08:59:23 +01008573 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008574 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008575}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008576#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008577
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008578#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008579static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008580smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008581{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008582 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008583 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008584 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008585
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008586 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008587 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008588
Olivier Houchard6b77f492018-11-22 18:18:29 +01008589 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8590 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8591
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008592 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008593 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008594 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008595
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008596 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008597 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008598 (const unsigned char **)&smp->data.u.str.area,
8599 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008600
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008601 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008602 return 0;
8603
Willy Tarreau105599c2020-02-25 08:59:23 +01008604 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008605 return 1;
8606}
8607#endif
8608
Emeric Brun645ae792014-04-30 14:21:06 +02008609/* string, returns the used protocol if front conn. transport layer is SSL.
8610 * This function is also usable on backend conn if the fetch keyword 5th
8611 * char is 'b'.
8612 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008613static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008614smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008615{
Emeric Bruneb8def92018-02-19 15:59:48 +01008616 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8617 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008618 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008619
Emeric Brun589fcad2012-10-16 14:13:26 +02008620 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008621 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8622 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008623 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008624
Olivier Houchard66ab4982019-02-26 18:37:15 +01008625 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008626 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008627 return 0;
8628
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008629 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008630 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008631 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008632
8633 return 1;
8634}
8635
Willy Tarreau87b09662015-04-03 00:22:06 +02008636/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008637 * This function is also usable on backend conn if the fetch keyword 5th
8638 * char is 'b'.
8639 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008640#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008641static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008642smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008643{
Emeric Bruneb8def92018-02-19 15:59:48 +01008644 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8645 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008646 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008647 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008648 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008649
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008650 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008651 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008652
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008653 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8654 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008655 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008656
Olivier Houchard66ab4982019-02-26 18:37:15 +01008657 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008658 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008659 return 0;
8660
Willy Tarreau105599c2020-02-25 08:59:23 +01008661 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008662 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008663 return 0;
8664
Willy Tarreau105599c2020-02-25 08:59:23 +01008665 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008666 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008667}
Patrick Hemmer41966772018-04-28 19:15:48 -04008668#endif
8669
Emeric Brunfe68f682012-10-16 14:59:28 +02008670
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008671#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008672static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008673smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8674{
8675 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8676 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8677 struct buffer *data;
8678 struct ssl_sock_ctx *ctx;
8679
8680 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8681 return 0;
8682 ctx = conn->xprt_ctx;
8683
8684 data = get_trash_chunk();
8685 if (kw[7] == 'c')
8686 data->data = SSL_get_client_random(ctx->ssl,
8687 (unsigned char *) data->area,
8688 data->size);
8689 else
8690 data->data = SSL_get_server_random(ctx->ssl,
8691 (unsigned char *) data->area,
8692 data->size);
8693 if (!data->data)
8694 return 0;
8695
8696 smp->flags = 0;
8697 smp->data.type = SMP_T_BIN;
8698 smp->data.u.str = *data;
8699
8700 return 1;
8701}
8702
8703static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008704smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8705{
8706 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8707 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8708 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008709 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008710 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008711
8712 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8713 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008714 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008715
Olivier Houchard66ab4982019-02-26 18:37:15 +01008716 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008717 if (!ssl_sess)
8718 return 0;
8719
8720 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008721 data->data = SSL_SESSION_get_master_key(ssl_sess,
8722 (unsigned char *) data->area,
8723 data->size);
8724 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008725 return 0;
8726
8727 smp->flags = 0;
8728 smp->data.type = SMP_T_BIN;
8729 smp->data.u.str = *data;
8730
8731 return 1;
8732}
8733#endif
8734
Patrick Hemmer41966772018-04-28 19:15:48 -04008735#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008736static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008737smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008738{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008739 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008740 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008741
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008742 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008743 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008744
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008745 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008746 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8747 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008748 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008749
Olivier Houchard66ab4982019-02-26 18:37:15 +01008750 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008751 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008752 return 0;
8753
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008754 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008755 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008756}
Patrick Hemmer41966772018-04-28 19:15:48 -04008757#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008758
David Sc1ad52e2014-04-08 18:48:47 -04008759static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008760smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8761{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008762 struct connection *conn;
8763 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008764 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008765
8766 conn = objt_conn(smp->sess->origin);
8767 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8768 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008769 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008770
Olivier Houchard66ab4982019-02-26 18:37:15 +01008771 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008772 if (!capture)
8773 return 0;
8774
8775 smp->flags = SMP_F_CONST;
8776 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008777 smp->data.u.str.area = capture->ciphersuite;
8778 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008779 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008780}
8781
8782static int
8783smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8784{
Willy Tarreau83061a82018-07-13 11:56:34 +02008785 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008786
8787 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8788 return 0;
8789
8790 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008791 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008792 smp->data.type = SMP_T_BIN;
8793 smp->data.u.str = *data;
8794 return 1;
8795}
8796
8797static int
8798smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8799{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008800 struct connection *conn;
8801 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008802 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008803
8804 conn = objt_conn(smp->sess->origin);
8805 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8806 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008807 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008808
Olivier Houchard66ab4982019-02-26 18:37:15 +01008809 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008810 if (!capture)
8811 return 0;
8812
8813 smp->data.type = SMP_T_SINT;
8814 smp->data.u.sint = capture->xxh64;
8815 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008816}
8817
8818static int
8819smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8820{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008821#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008822 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008823 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008824
8825 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8826 return 0;
8827
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008828 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008829 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008830 const char *str;
8831 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008832 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008833 uint16_t id = (bin[0] << 8) | bin[1];
8834#if defined(OPENSSL_IS_BORINGSSL)
8835 cipher = SSL_get_cipher_by_value(id);
8836#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008837 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008838 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8839 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008840#endif
8841 str = SSL_CIPHER_get_name(cipher);
8842 if (!str || strcmp(str, "(NONE)") == 0)
8843 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008844 else
8845 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8846 }
8847 smp->data.type = SMP_T_STR;
8848 smp->data.u.str = *data;
8849 return 1;
8850#else
8851 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8852#endif
8853}
8854
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008855#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008856static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008857smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008858{
Emeric Bruneb8def92018-02-19 15:59:48 +01008859 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8860 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008861 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008862 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008863 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008864
8865 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008866 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8867 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008868 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008869
Willy Tarreau911db9b2020-01-23 16:27:54 +01008870 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008871 smp->flags |= SMP_F_MAY_CHANGE;
8872 return 0;
8873 }
8874
8875 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008876 if (!SSL_session_reused(ctx->ssl))
8877 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008878 finished_trash->area,
8879 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008880 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008881 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008882 finished_trash->area,
8883 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008884
8885 if (!finished_len)
8886 return 0;
8887
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008888 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008889 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008890 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008891
8892 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008893}
Patrick Hemmer41966772018-04-28 19:15:48 -04008894#endif
David Sc1ad52e2014-04-08 18:48:47 -04008895
Emeric Brun2525b6b2012-10-18 15:59:43 +02008896/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008897static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008898smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008899{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008900 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008901 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008902
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008903 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008904 if (!conn || conn->xprt != &ssl_sock)
8905 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008906 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008907
Willy Tarreau911db9b2020-01-23 16:27:54 +01008908 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008909 smp->flags = SMP_F_MAY_CHANGE;
8910 return 0;
8911 }
8912
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008913 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008914 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008915 smp->flags = 0;
8916
8917 return 1;
8918}
8919
Emeric Brun2525b6b2012-10-18 15:59:43 +02008920/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008921static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008922smp_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 +02008923{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008924 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008925 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008926
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008927 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008928 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008929 return 0;
8930
Willy Tarreau911db9b2020-01-23 16:27:54 +01008931 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008932 smp->flags = SMP_F_MAY_CHANGE;
8933 return 0;
8934 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008935 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008936
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008937 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008938 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008939 smp->flags = 0;
8940
8941 return 1;
8942}
8943
Emeric Brun2525b6b2012-10-18 15:59:43 +02008944/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008945static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008946smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008947{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008948 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008949 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008950
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008951 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008952 if (!conn || conn->xprt != &ssl_sock)
8953 return 0;
8954
Willy Tarreau911db9b2020-01-23 16:27:54 +01008955 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008956 smp->flags = SMP_F_MAY_CHANGE;
8957 return 0;
8958 }
8959
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008960 ctx = conn->xprt_ctx;
8961
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008962 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008963 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008964 smp->flags = 0;
8965
8966 return 1;
8967}
8968
Emeric Brun2525b6b2012-10-18 15:59:43 +02008969/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008970static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008971smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008972{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008973 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008974 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008975
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008976 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008977 if (!conn || conn->xprt != &ssl_sock)
8978 return 0;
8979
Willy Tarreau911db9b2020-01-23 16:27:54 +01008980 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008981 smp->flags = SMP_F_MAY_CHANGE;
8982 return 0;
8983 }
8984
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008985 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008986 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008987 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008988
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008989 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008990 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008991 smp->flags = 0;
8992
8993 return 1;
8994}
8995
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008996/* for ca-file and ca-verify-file */
8997static 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 +02008998{
8999 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009000 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009001 return ERR_ALERT | ERR_FATAL;
9002 }
9003
Willy Tarreauef934602016-12-22 23:12:01 +01009004 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009005 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009006 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009007 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009008
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009009 if (!ssl_store_load_locations_file(*ca_file_p)) {
9010 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009011 return ERR_ALERT | ERR_FATAL;
9012 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009013 return 0;
9014}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009015
9016/* parse the "ca-file" bind keyword */
9017static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9018{
9019 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9020}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009021static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9022{
9023 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9024}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009025
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009026/* parse the "ca-verify-file" bind keyword */
9027static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9028{
9029 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9030}
9031static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9032{
9033 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9034}
9035
Christopher Faulet31af49d2015-06-09 17:29:50 +02009036/* parse the "ca-sign-file" bind keyword */
9037static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9038{
9039 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009040 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009041 return ERR_ALERT | ERR_FATAL;
9042 }
9043
Willy Tarreauef934602016-12-22 23:12:01 +01009044 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9045 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009046 else
9047 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9048
9049 return 0;
9050}
9051
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009052/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009053static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9054{
9055 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009056 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009057 return ERR_ALERT | ERR_FATAL;
9058 }
9059 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9060 return 0;
9061}
9062
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009063/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009064static 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 +02009065{
9066 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009067 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009068 return ERR_ALERT | ERR_FATAL;
9069 }
9070
Emeric Brun76d88952012-10-05 15:47:31 +02009071 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009072 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009073 return 0;
9074}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009075static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9076{
9077 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9078}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009079
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009080#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009081/* parse the "ciphersuites" bind keyword */
9082static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9083{
9084 if (!*args[cur_arg + 1]) {
9085 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9086 return ERR_ALERT | ERR_FATAL;
9087 }
9088
9089 free(conf->ciphersuites);
9090 conf->ciphersuites = strdup(args[cur_arg + 1]);
9091 return 0;
9092}
9093static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9094{
9095 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9096}
9097#endif
9098
Willy Tarreaubbc91962019-10-16 16:42:19 +02009099/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009100static 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 +02009101{
Willy Tarreau38011032013-08-13 16:59:39 +02009102 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009103
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009104 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009105 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009106 return ERR_ALERT | ERR_FATAL;
9107 }
9108
Willy Tarreauef934602016-12-22 23:12:01 +01009109 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9110 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009111 memprintf(err, "'%s' : path too long", args[cur_arg]);
9112 return ERR_ALERT | ERR_FATAL;
9113 }
Willy Tarreauef934602016-12-22 23:12:01 +01009114 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009115 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009116 }
9117
Willy Tarreaubbc91962019-10-16 16:42:19 +02009118 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009119}
9120
Willy Tarreaubbc91962019-10-16 16:42:19 +02009121/* 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 +01009122static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9123{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009124 int err_code;
9125
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009126 if (!*args[cur_arg + 1]) {
9127 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9128 return ERR_ALERT | ERR_FATAL;
9129 }
9130
William Lallemand6be66ec2020-03-06 22:26:32 +01009131 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009132 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009133 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009134
Willy Tarreaubbc91962019-10-16 16:42:19 +02009135 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009136}
9137
Emeric Brunfb510ea2012-10-05 12:00:26 +02009138/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009139static 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 +02009140{
Emeric Brun051cdab2012-10-02 19:25:50 +02009141#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009142 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009143 return ERR_ALERT | ERR_FATAL;
9144#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009145 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009146 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009147 return ERR_ALERT | ERR_FATAL;
9148 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009149
Willy Tarreauef934602016-12-22 23:12:01 +01009150 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9151 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009152 else
9153 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009154
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009155 if (!ssl_store_load_locations_file(conf->crl_file)) {
9156 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9157 return ERR_ALERT | ERR_FATAL;
9158 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009159 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009160#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009161}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009162static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9163{
9164 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9165}
Emeric Brun2b58d042012-09-20 17:10:03 +02009166
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009167/* parse the "curves" bind keyword keyword */
9168static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9169{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009170#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009171 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009172 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009173 return ERR_ALERT | ERR_FATAL;
9174 }
9175 conf->curves = strdup(args[cur_arg + 1]);
9176 return 0;
9177#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009178 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009179 return ERR_ALERT | ERR_FATAL;
9180#endif
9181}
9182static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9183{
9184 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9185}
9186
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009187/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009188static 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 +02009189{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009190#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009191 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009192 return ERR_ALERT | ERR_FATAL;
9193#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009194 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 +02009195 return ERR_ALERT | ERR_FATAL;
9196#else
9197 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009198 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009199 return ERR_ALERT | ERR_FATAL;
9200 }
9201
9202 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009203
9204 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009205#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009206}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009207static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9208{
9209 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9210}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009211
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009212/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009213static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9214{
9215 int code;
9216 char *p = args[cur_arg + 1];
9217 unsigned long long *ignerr = &conf->crt_ignerr;
9218
9219 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009220 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009221 return ERR_ALERT | ERR_FATAL;
9222 }
9223
9224 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9225 ignerr = &conf->ca_ignerr;
9226
9227 if (strcmp(p, "all") == 0) {
9228 *ignerr = ~0ULL;
9229 return 0;
9230 }
9231
9232 while (p) {
9233 code = atoi(p);
9234 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009235 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9236 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009237 return ERR_ALERT | ERR_FATAL;
9238 }
9239 *ignerr |= 1ULL << code;
9240 p = strchr(p, ',');
9241 if (p)
9242 p++;
9243 }
9244
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009245 return 0;
9246}
9247
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009248/* parse tls_method_options "no-xxx" and "force-xxx" */
9249static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009250{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009251 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009252 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009253 p = strchr(arg, '-');
9254 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009255 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009256 p++;
9257 if (!strcmp(p, "sslv3"))
9258 v = CONF_SSLV3;
9259 else if (!strcmp(p, "tlsv10"))
9260 v = CONF_TLSV10;
9261 else if (!strcmp(p, "tlsv11"))
9262 v = CONF_TLSV11;
9263 else if (!strcmp(p, "tlsv12"))
9264 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009265 else if (!strcmp(p, "tlsv13"))
9266 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009267 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009268 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009269 if (!strncmp(arg, "no-", 3))
9270 methods->flags |= methodVersions[v].flag;
9271 else if (!strncmp(arg, "force-", 6))
9272 methods->min = methods->max = v;
9273 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009274 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009275 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009276 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009277 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009278 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009279}
9280
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009281static 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 +02009282{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009283 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009284}
9285
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009286static 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 +02009287{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009288 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9289}
9290
9291/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9292static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9293{
9294 uint16_t i, v = 0;
9295 char *argv = args[cur_arg + 1];
9296 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009297 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009298 return ERR_ALERT | ERR_FATAL;
9299 }
9300 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9301 if (!strcmp(argv, methodVersions[i].name))
9302 v = i;
9303 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009304 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009305 return ERR_ALERT | ERR_FATAL;
9306 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009307 if (!strcmp("ssl-min-ver", args[cur_arg]))
9308 methods->min = v;
9309 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9310 methods->max = v;
9311 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009312 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009313 return ERR_ALERT | ERR_FATAL;
9314 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009315 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009316}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009317
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009318static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9319{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009320#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009321 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 +02009322#endif
9323 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9324}
9325
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009326static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9327{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009328 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009329}
9330
9331static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9332{
9333 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9334}
9335
Emeric Brun2d0c4822012-10-02 13:45:20 +02009336/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009337static 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 +02009338{
Emeric Brun89675492012-10-05 13:48:26 +02009339 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009340 return 0;
9341}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009342
Olivier Houchardc2aae742017-09-22 18:26:28 +02009343/* parse the "allow-0rtt" bind keyword */
9344static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9345{
9346 conf->early_data = 1;
9347 return 0;
9348}
9349
9350static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9351{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009352 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009353 return 0;
9354}
9355
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009356/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009357static 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 +02009358{
Bernard Spil13c53f82018-02-15 13:34:58 +01009359#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009360 char *p1, *p2;
9361
9362 if (!*args[cur_arg + 1]) {
9363 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9364 return ERR_ALERT | ERR_FATAL;
9365 }
9366
9367 free(conf->npn_str);
9368
Willy Tarreau3724da12016-02-12 17:11:12 +01009369 /* the NPN string is built as a suite of (<len> <name>)*,
9370 * so we reuse each comma to store the next <len> and need
9371 * one more for the end of the string.
9372 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009373 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009374 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009375 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9376
9377 /* replace commas with the name length */
9378 p1 = conf->npn_str;
9379 p2 = p1 + 1;
9380 while (1) {
9381 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9382 if (!p2)
9383 p2 = p1 + 1 + strlen(p1 + 1);
9384
9385 if (p2 - (p1 + 1) > 255) {
9386 *p2 = '\0';
9387 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9388 return ERR_ALERT | ERR_FATAL;
9389 }
9390
9391 *p1 = p2 - (p1 + 1);
9392 p1 = p2;
9393
9394 if (!*p2)
9395 break;
9396
9397 *(p2++) = '\0';
9398 }
9399 return 0;
9400#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009401 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009402 return ERR_ALERT | ERR_FATAL;
9403#endif
9404}
9405
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009406static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9407{
9408 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9409}
9410
Willy Tarreauab861d32013-04-02 02:30:41 +02009411/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009412static 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 +02009413{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009414#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009415 char *p1, *p2;
9416
9417 if (!*args[cur_arg + 1]) {
9418 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9419 return ERR_ALERT | ERR_FATAL;
9420 }
9421
9422 free(conf->alpn_str);
9423
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009424 /* the ALPN string is built as a suite of (<len> <name>)*,
9425 * so we reuse each comma to store the next <len> and need
9426 * one more for the end of the string.
9427 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009428 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009429 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009430 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9431
9432 /* replace commas with the name length */
9433 p1 = conf->alpn_str;
9434 p2 = p1 + 1;
9435 while (1) {
9436 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9437 if (!p2)
9438 p2 = p1 + 1 + strlen(p1 + 1);
9439
9440 if (p2 - (p1 + 1) > 255) {
9441 *p2 = '\0';
9442 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9443 return ERR_ALERT | ERR_FATAL;
9444 }
9445
9446 *p1 = p2 - (p1 + 1);
9447 p1 = p2;
9448
9449 if (!*p2)
9450 break;
9451
9452 *(p2++) = '\0';
9453 }
9454 return 0;
9455#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009456 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009457 return ERR_ALERT | ERR_FATAL;
9458#endif
9459}
9460
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009461static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9462{
9463 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9464}
9465
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009466/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009467static 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 +02009468{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009469 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009470 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009471
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009472 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9473 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009474#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009475 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9476 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9477#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009478 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009479 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9480 if (!conf->ssl_conf.ssl_methods.min)
9481 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9482 if (!conf->ssl_conf.ssl_methods.max)
9483 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009484
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009485 return 0;
9486}
9487
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009488/* parse the "prefer-client-ciphers" bind keyword */
9489static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9490{
9491 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9492 return 0;
9493}
9494
Christopher Faulet31af49d2015-06-09 17:29:50 +02009495/* parse the "generate-certificates" bind keyword */
9496static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9497{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009498#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009499 conf->generate_certs = 1;
9500#else
9501 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9502 err && *err ? *err : "");
9503#endif
9504 return 0;
9505}
9506
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009507/* parse the "strict-sni" bind keyword */
9508static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9509{
9510 conf->strict_sni = 1;
9511 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009512}
9513
9514/* parse the "tls-ticket-keys" bind keyword */
9515static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9516{
9517#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009518 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009519 int i = 0;
9520 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009521 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009522
9523 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009524 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009525 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009526 }
9527
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009528 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009529 if (keys_ref) {
9530 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009531 conf->keys_ref = keys_ref;
9532 return 0;
9533 }
9534
Christopher Faulete566f3d2019-10-21 09:55:49 +02009535 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009536 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009537 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009538 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009539 }
9540
Emeric Brun9e754772019-01-10 17:51:55 +01009541 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009542 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009543 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009544 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009545 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009546
9547 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009548 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009549 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009550 }
9551
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009552 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009553 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009554 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009555 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009556 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009557
Emeric Brun9e754772019-01-10 17:51:55 +01009558 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009559 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9560 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009561 int dec_size;
9562
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009563 /* Strip newline characters from the end */
9564 if(thisline[len - 1] == '\n')
9565 thisline[--len] = 0;
9566
9567 if(thisline[len - 1] == '\r')
9568 thisline[--len] = 0;
9569
Emeric Brun9e754772019-01-10 17:51:55 +01009570 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9571 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009572 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009573 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009574 }
Emeric Brun9e754772019-01-10 17:51:55 +01009575 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9576 keys_ref->key_size_bits = 128;
9577 }
9578 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9579 keys_ref->key_size_bits = 256;
9580 }
9581 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9582 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9583 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009584 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009585 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009586 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009587 i++;
9588 }
9589
9590 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009591 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 +02009592 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009593 }
9594
9595 fclose(f);
9596
9597 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009598 i -= 2;
9599 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009600 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009601 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009602 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009603 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009604
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009605 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9606
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009607 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009608
9609 fail:
9610 if (f)
9611 fclose(f);
9612 if (keys_ref) {
9613 free(keys_ref->filename);
9614 free(keys_ref->tlskeys);
9615 free(keys_ref);
9616 }
9617 return ERR_ALERT | ERR_FATAL;
9618
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009619#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009620 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009621 return ERR_ALERT | ERR_FATAL;
9622#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009623}
9624
Emeric Brund94b3fe2012-09-20 18:23:56 +02009625/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009626static 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 +02009627{
9628 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009629 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009630 return ERR_ALERT | ERR_FATAL;
9631 }
9632
9633 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009634 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009635 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009636 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009637 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009638 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009639 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009640 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9641 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009642 return ERR_ALERT | ERR_FATAL;
9643 }
9644
9645 return 0;
9646}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009647static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9648{
9649 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9650}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009651
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009652/* parse the "no-ca-names" bind keyword */
9653static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9654{
9655 conf->no_ca_names = 1;
9656 return 0;
9657}
9658static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9659{
9660 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9661}
9662
Willy Tarreau92faadf2012-10-10 23:04:25 +02009663/************** "server" keywords ****************/
9664
Olivier Houchardc7566002018-11-20 23:33:50 +01009665/* parse the "npn" bind keyword */
9666static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9667{
9668#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9669 char *p1, *p2;
9670
9671 if (!*args[*cur_arg + 1]) {
9672 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9673 return ERR_ALERT | ERR_FATAL;
9674 }
9675
9676 free(newsrv->ssl_ctx.npn_str);
9677
9678 /* the NPN string is built as a suite of (<len> <name>)*,
9679 * so we reuse each comma to store the next <len> and need
9680 * one more for the end of the string.
9681 */
9682 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9683 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9684 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9685 newsrv->ssl_ctx.npn_len);
9686
9687 /* replace commas with the name length */
9688 p1 = newsrv->ssl_ctx.npn_str;
9689 p2 = p1 + 1;
9690 while (1) {
9691 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9692 newsrv->ssl_ctx.npn_len - (p1 + 1));
9693 if (!p2)
9694 p2 = p1 + 1 + strlen(p1 + 1);
9695
9696 if (p2 - (p1 + 1) > 255) {
9697 *p2 = '\0';
9698 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9699 return ERR_ALERT | ERR_FATAL;
9700 }
9701
9702 *p1 = p2 - (p1 + 1);
9703 p1 = p2;
9704
9705 if (!*p2)
9706 break;
9707
9708 *(p2++) = '\0';
9709 }
9710 return 0;
9711#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009712 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009713 return ERR_ALERT | ERR_FATAL;
9714#endif
9715}
9716
Olivier Houchard92150142018-12-21 19:47:01 +01009717/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009718static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9719{
9720#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9721 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009722 char **alpn_str;
9723 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009724
Olivier Houchard92150142018-12-21 19:47:01 +01009725 if (*args[*cur_arg] == 'c') {
9726 alpn_str = &newsrv->check.alpn_str;
9727 alpn_len = &newsrv->check.alpn_len;
9728 } else {
9729 alpn_str = &newsrv->ssl_ctx.alpn_str;
9730 alpn_len = &newsrv->ssl_ctx.alpn_len;
9731
9732 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009733 if (!*args[*cur_arg + 1]) {
9734 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9735 return ERR_ALERT | ERR_FATAL;
9736 }
9737
Olivier Houchard92150142018-12-21 19:47:01 +01009738 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009739
9740 /* the ALPN string is built as a suite of (<len> <name>)*,
9741 * so we reuse each comma to store the next <len> and need
9742 * one more for the end of the string.
9743 */
Olivier Houchard92150142018-12-21 19:47:01 +01009744 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9745 *alpn_str = calloc(1, *alpn_len + 1);
9746 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009747
9748 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009749 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009750 p2 = p1 + 1;
9751 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009752 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009753 if (!p2)
9754 p2 = p1 + 1 + strlen(p1 + 1);
9755
9756 if (p2 - (p1 + 1) > 255) {
9757 *p2 = '\0';
9758 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9759 return ERR_ALERT | ERR_FATAL;
9760 }
9761
9762 *p1 = p2 - (p1 + 1);
9763 p1 = p2;
9764
9765 if (!*p2)
9766 break;
9767
9768 *(p2++) = '\0';
9769 }
9770 return 0;
9771#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009772 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009773 return ERR_ALERT | ERR_FATAL;
9774#endif
9775}
9776
Emeric Brunef42d922012-10-11 16:11:36 +02009777/* parse the "ca-file" server keyword */
9778static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9779{
9780 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009781 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009782 return ERR_ALERT | ERR_FATAL;
9783 }
9784
Willy Tarreauef934602016-12-22 23:12:01 +01009785 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9786 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009787 else
9788 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9789
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009790 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9791 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9792 return ERR_ALERT | ERR_FATAL;
9793 }
Emeric Brunef42d922012-10-11 16:11:36 +02009794 return 0;
9795}
9796
Olivier Houchard9130a962017-10-17 17:33:43 +02009797/* parse the "check-sni" server keyword */
9798static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9799{
9800 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009801 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009802 return ERR_ALERT | ERR_FATAL;
9803 }
9804
9805 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9806 if (!newsrv->check.sni) {
9807 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9808 return ERR_ALERT | ERR_FATAL;
9809 }
9810 return 0;
9811
9812}
9813
Willy Tarreau92faadf2012-10-10 23:04:25 +02009814/* parse the "check-ssl" server keyword */
9815static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9816{
9817 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009818 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9819 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009820#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009821 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9822 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9823#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009824 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009825 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9826 if (!newsrv->ssl_ctx.methods.min)
9827 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9828 if (!newsrv->ssl_ctx.methods.max)
9829 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9830
Willy Tarreau92faadf2012-10-10 23:04:25 +02009831 return 0;
9832}
9833
9834/* parse the "ciphers" server keyword */
9835static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9836{
9837 if (!*args[*cur_arg + 1]) {
9838 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9839 return ERR_ALERT | ERR_FATAL;
9840 }
9841
9842 free(newsrv->ssl_ctx.ciphers);
9843 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9844 return 0;
9845}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009846
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009847#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009848/* parse the "ciphersuites" server keyword */
9849static int srv_parse_ciphersuites(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.ciphersuites);
9857 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9858 return 0;
9859}
9860#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009861
Emeric Brunef42d922012-10-11 16:11:36 +02009862/* parse the "crl-file" server keyword */
9863static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9864{
9865#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009866 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009867 return ERR_ALERT | ERR_FATAL;
9868#else
9869 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009870 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009871 return ERR_ALERT | ERR_FATAL;
9872 }
9873
Willy Tarreauef934602016-12-22 23:12:01 +01009874 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9875 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009876 else
9877 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9878
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009879 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9880 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9881 return ERR_ALERT | ERR_FATAL;
9882 }
Emeric Brunef42d922012-10-11 16:11:36 +02009883 return 0;
9884#endif
9885}
9886
Emeric Bruna7aa3092012-10-26 12:58:00 +02009887/* parse the "crt" server keyword */
9888static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9889{
9890 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009891 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009892 return ERR_ALERT | ERR_FATAL;
9893 }
9894
Willy Tarreauef934602016-12-22 23:12:01 +01009895 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009896 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009897 else
9898 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9899
9900 return 0;
9901}
Emeric Brunef42d922012-10-11 16:11:36 +02009902
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009903/* parse the "no-check-ssl" server keyword */
9904static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9905{
9906 newsrv->check.use_ssl = 0;
9907 free(newsrv->ssl_ctx.ciphers);
9908 newsrv->ssl_ctx.ciphers = NULL;
9909 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9910 return 0;
9911}
9912
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009913/* parse the "no-send-proxy-v2-ssl" server keyword */
9914static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9915{
9916 newsrv->pp_opts &= ~SRV_PP_V2;
9917 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9918 return 0;
9919}
9920
9921/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9922static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9923{
9924 newsrv->pp_opts &= ~SRV_PP_V2;
9925 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9926 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9927 return 0;
9928}
9929
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009930/* parse the "no-ssl" server keyword */
9931static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9932{
9933 newsrv->use_ssl = 0;
9934 free(newsrv->ssl_ctx.ciphers);
9935 newsrv->ssl_ctx.ciphers = NULL;
9936 return 0;
9937}
9938
Olivier Houchard522eea72017-11-03 16:27:47 +01009939/* parse the "allow-0rtt" server keyword */
9940static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9941{
9942 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9943 return 0;
9944}
9945
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009946/* parse the "no-ssl-reuse" server keyword */
9947static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9948{
9949 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9950 return 0;
9951}
9952
Emeric Brunf9c5c472012-10-11 15:28:34 +02009953/* parse the "no-tls-tickets" server keyword */
9954static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9955{
9956 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9957 return 0;
9958}
David Safb76832014-05-08 23:42:08 -04009959/* parse the "send-proxy-v2-ssl" server keyword */
9960static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9961{
9962 newsrv->pp_opts |= SRV_PP_V2;
9963 newsrv->pp_opts |= SRV_PP_V2_SSL;
9964 return 0;
9965}
9966
9967/* parse the "send-proxy-v2-ssl-cn" server keyword */
9968static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9969{
9970 newsrv->pp_opts |= SRV_PP_V2;
9971 newsrv->pp_opts |= SRV_PP_V2_SSL;
9972 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9973 return 0;
9974}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009975
Willy Tarreau732eac42015-07-09 11:40:25 +02009976/* parse the "sni" server keyword */
9977static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9978{
9979#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9980 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9981 return ERR_ALERT | ERR_FATAL;
9982#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009983 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009984
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009985 arg = args[*cur_arg + 1];
9986 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009987 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9988 return ERR_ALERT | ERR_FATAL;
9989 }
9990
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009991 free(newsrv->sni_expr);
9992 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009993
Willy Tarreau732eac42015-07-09 11:40:25 +02009994 return 0;
9995#endif
9996}
9997
Willy Tarreau92faadf2012-10-10 23:04:25 +02009998/* parse the "ssl" server keyword */
9999static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10000{
10001 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010002 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10003 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010004#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010005 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10006 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10007#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +020010008 return 0;
10009}
10010
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010011/* parse the "ssl-reuse" server keyword */
10012static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10013{
10014 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10015 return 0;
10016}
10017
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010018/* parse the "tls-tickets" server keyword */
10019static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10020{
10021 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10022 return 0;
10023}
10024
Emeric Brunef42d922012-10-11 16:11:36 +020010025/* parse the "verify" server keyword */
10026static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10027{
10028 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010029 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010030 return ERR_ALERT | ERR_FATAL;
10031 }
10032
10033 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010034 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010035 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010036 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010037 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010038 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10039 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010040 return ERR_ALERT | ERR_FATAL;
10041 }
10042
Evan Broderbe554312013-06-27 00:05:25 -070010043 return 0;
10044}
10045
10046/* parse the "verifyhost" server keyword */
10047static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10048{
10049 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010050 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010051 return ERR_ALERT | ERR_FATAL;
10052 }
10053
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010054 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010055 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10056
Emeric Brunef42d922012-10-11 16:11:36 +020010057 return 0;
10058}
10059
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010060/* parse the "ssl-default-bind-options" keyword in global section */
10061static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10062 struct proxy *defpx, const char *file, int line,
10063 char **err) {
10064 int i = 1;
10065
10066 if (*(args[i]) == 0) {
10067 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10068 return -1;
10069 }
10070 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010071 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010072 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010073 else if (!strcmp(args[i], "prefer-client-ciphers"))
10074 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010075 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10076 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10077 i++;
10078 else {
10079 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10080 return -1;
10081 }
10082 }
10083 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010084 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10085 return -1;
10086 }
10087 i++;
10088 }
10089 return 0;
10090}
10091
10092/* parse the "ssl-default-server-options" keyword in global section */
10093static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10094 struct proxy *defpx, const char *file, int line,
10095 char **err) {
10096 int i = 1;
10097
10098 if (*(args[i]) == 0) {
10099 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10100 return -1;
10101 }
10102 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010103 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010104 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010105 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10106 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10107 i++;
10108 else {
10109 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10110 return -1;
10111 }
10112 }
10113 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010114 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10115 return -1;
10116 }
10117 i++;
10118 }
10119 return 0;
10120}
10121
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010122/* parse the "ca-base" / "crt-base" keywords in global section.
10123 * Returns <0 on alert, >0 on warning, 0 on success.
10124 */
10125static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10126 struct proxy *defpx, const char *file, int line,
10127 char **err)
10128{
10129 char **target;
10130
Willy Tarreauef934602016-12-22 23:12:01 +010010131 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010132
10133 if (too_many_args(1, args, err, NULL))
10134 return -1;
10135
10136 if (*target) {
10137 memprintf(err, "'%s' already specified.", args[0]);
10138 return -1;
10139 }
10140
10141 if (*(args[1]) == 0) {
10142 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10143 return -1;
10144 }
10145 *target = strdup(args[1]);
10146 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010147}
10148
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010149/* "issuers-chain-path" load chain certificate in global */
10150static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10151{
10152 X509 *ca;
10153 X509_NAME *name = NULL;
10154 ASN1_OCTET_STRING *skid = NULL;
10155 STACK_OF(X509) *chain = NULL;
10156 struct issuer_chain *issuer;
10157 struct eb64_node *node;
10158 char *path;
10159 u64 key;
10160 int ret = 0;
10161
10162 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10163 if (chain == NULL) {
10164 chain = sk_X509_new_null();
10165 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10166 name = X509_get_subject_name(ca);
10167 }
10168 if (!sk_X509_push(chain, ca)) {
10169 X509_free(ca);
10170 goto end;
10171 }
10172 }
10173 if (!chain) {
10174 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10175 goto end;
10176 }
10177 if (!skid) {
10178 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10179 goto end;
10180 }
10181 if (!name) {
10182 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10183 goto end;
10184 }
10185 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010186 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010187 issuer = container_of(node, typeof(*issuer), node);
10188 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10189 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10190 goto end;
10191 }
10192 }
10193 issuer = calloc(1, sizeof *issuer);
10194 path = strdup(fp);
10195 if (!issuer || !path) {
10196 free(issuer);
10197 free(path);
10198 goto end;
10199 }
10200 issuer->node.key = key;
10201 issuer->path = path;
10202 issuer->chain = chain;
10203 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010204 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010205 ret = 1;
10206 end:
10207 if (skid)
10208 ASN1_OCTET_STRING_free(skid);
10209 if (chain)
10210 sk_X509_pop_free(chain, X509_free);
10211 return ret;
10212}
10213
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010214static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010215{
10216 AUTHORITY_KEYID *akid;
10217 struct issuer_chain *issuer = NULL;
10218
10219 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10220 if (akid) {
10221 struct eb64_node *node;
10222 u64 hk;
10223 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10224 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10225 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10226 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10227 issuer = ti;
10228 break;
10229 }
10230 }
10231 AUTHORITY_KEYID_free(akid);
10232 }
10233 return issuer;
10234}
10235
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010236static void ssl_free_global_issuers(void)
10237{
10238 struct eb64_node *node, *back;
10239 struct issuer_chain *issuer;
10240
William Lallemande0f3fd52020-02-25 14:53:06 +010010241 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010242 while (node) {
10243 issuer = container_of(node, typeof(*issuer), node);
10244 back = eb64_next(node);
10245 eb64_delete(node);
10246 free(issuer->path);
10247 sk_X509_pop_free(issuer->chain, X509_free);
10248 free(issuer);
10249 node = back;
10250 }
10251}
10252
10253static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10254 struct proxy *defpx, const char *file, int line,
10255 char **err)
10256{
10257 char *path;
10258 struct dirent **de_list;
10259 int i, n;
10260 struct stat buf;
10261 char *end;
10262 char fp[MAXPATHLEN+1];
10263
10264 if (too_many_args(1, args, err, NULL))
10265 return -1;
10266
10267 path = args[1];
10268 if (*path == 0 || stat(path, &buf)) {
10269 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10270 err && *err ? *err : "", args[0]);
10271 return -1;
10272 }
10273 if (S_ISDIR(buf.st_mode) == 0) {
10274 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10275 err && *err ? *err : "", args[0], path);
10276 return -1;
10277 }
10278
10279 /* strip trailing slashes, including first one */
10280 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10281 *end = 0;
10282 /* path already parsed? */
10283 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10284 return 0;
10285 /* overwrite old issuers_chain_path */
10286 free(global_ssl.issuers_chain_path);
10287 global_ssl.issuers_chain_path = strdup(path);
10288 ssl_free_global_issuers();
10289
10290 n = scandir(path, &de_list, 0, alphasort);
10291 if (n < 0) {
10292 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10293 err && *err ? *err : "", args[0], path, strerror(errno));
10294 return -1;
10295 }
10296 for (i = 0; i < n; i++) {
10297 struct dirent *de = de_list[i];
10298 BIO *in = NULL;
10299 char *warn = NULL;
10300
10301 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10302 free(de);
10303 if (stat(fp, &buf) != 0) {
10304 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10305 goto next;
10306 }
10307 if (!S_ISREG(buf.st_mode))
10308 goto next;
10309
10310 in = BIO_new(BIO_s_file());
10311 if (in == NULL)
10312 goto next;
10313 if (BIO_read_filename(in, fp) <= 0)
10314 goto next;
10315 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10316 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010317 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010318 free(warn);
10319 warn = NULL;
10320 }
10321 next:
10322 if (in)
10323 BIO_free(in);
10324 }
10325 free(de_list);
10326
10327 return 0;
10328}
10329
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010330/* parse the "ssl-mode-async" keyword in global section.
10331 * Returns <0 on alert, >0 on warning, 0 on success.
10332 */
10333static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10334 struct proxy *defpx, const char *file, int line,
10335 char **err)
10336{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010337#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010338 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010339 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010340 return 0;
10341#else
10342 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10343 return -1;
10344#endif
10345}
10346
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010347#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010348static int ssl_check_async_engine_count(void) {
10349 int err_code = 0;
10350
Emeric Brun3854e012017-05-17 20:42:48 +020010351 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010352 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010353 err_code = ERR_ABORT;
10354 }
10355 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010356}
10357
Grant Zhang872f9c22017-01-21 01:10:18 +000010358/* parse the "ssl-engine" keyword in global section.
10359 * Returns <0 on alert, >0 on warning, 0 on success.
10360 */
10361static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10362 struct proxy *defpx, const char *file, int line,
10363 char **err)
10364{
10365 char *algo;
10366 int ret = -1;
10367
10368 if (*(args[1]) == 0) {
10369 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10370 return ret;
10371 }
10372
10373 if (*(args[2]) == 0) {
10374 /* if no list of algorithms is given, it defaults to ALL */
10375 algo = strdup("ALL");
10376 goto add_engine;
10377 }
10378
10379 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10380 if (strcmp(args[2], "algo") != 0) {
10381 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10382 return ret;
10383 }
10384
10385 if (*(args[3]) == 0) {
10386 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10387 return ret;
10388 }
10389 algo = strdup(args[3]);
10390
10391add_engine:
10392 if (ssl_init_single_engine(args[1], algo)==0) {
10393 openssl_engines_initialized++;
10394 ret = 0;
10395 }
10396 free(algo);
10397 return ret;
10398}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010399#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010400
Willy Tarreauf22e9682016-12-21 23:23:19 +010010401/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10402 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10403 */
10404static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10405 struct proxy *defpx, const char *file, int line,
10406 char **err)
10407{
10408 char **target;
10409
Willy Tarreauef934602016-12-22 23:12:01 +010010410 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010411
10412 if (too_many_args(1, args, err, NULL))
10413 return -1;
10414
10415 if (*(args[1]) == 0) {
10416 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10417 return -1;
10418 }
10419
10420 free(*target);
10421 *target = strdup(args[1]);
10422 return 0;
10423}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010424
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010425#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010426/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10427 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10428 */
10429static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10430 struct proxy *defpx, const char *file, int line,
10431 char **err)
10432{
10433 char **target;
10434
10435 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10436
10437 if (too_many_args(1, args, err, NULL))
10438 return -1;
10439
10440 if (*(args[1]) == 0) {
10441 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10442 return -1;
10443 }
10444
10445 free(*target);
10446 *target = strdup(args[1]);
10447 return 0;
10448}
10449#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010450
Willy Tarreau9ceda382016-12-21 23:13:03 +010010451/* parse various global tune.ssl settings consisting in positive integers.
10452 * Returns <0 on alert, >0 on warning, 0 on success.
10453 */
10454static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10455 struct proxy *defpx, const char *file, int line,
10456 char **err)
10457{
10458 int *target;
10459
10460 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10461 target = &global.tune.sslcachesize;
10462 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010463 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010464 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010465 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010466 else if (strcmp(args[0], "maxsslconn") == 0)
10467 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010468 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10469 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010470 else {
10471 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10472 return -1;
10473 }
10474
10475 if (too_many_args(1, args, err, NULL))
10476 return -1;
10477
10478 if (*(args[1]) == 0) {
10479 memprintf(err, "'%s' expects an integer argument.", args[0]);
10480 return -1;
10481 }
10482
10483 *target = atoi(args[1]);
10484 if (*target < 0) {
10485 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10486 return -1;
10487 }
10488 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010489}
10490
10491static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10492 struct proxy *defpx, const char *file, int line,
10493 char **err)
10494{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010495 int ret;
10496
10497 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10498 if (ret != 0)
10499 return ret;
10500
Willy Tarreaubafbe012017-11-24 17:34:44 +010010501 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010502 memprintf(err, "'%s' is already configured.", args[0]);
10503 return -1;
10504 }
10505
Willy Tarreaubafbe012017-11-24 17:34:44 +010010506 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10507 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010508 memprintf(err, "Out of memory error.");
10509 return -1;
10510 }
10511 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010512}
10513
10514/* parse "ssl.force-private-cache".
10515 * Returns <0 on alert, >0 on warning, 0 on success.
10516 */
10517static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10518 struct proxy *defpx, const char *file, int line,
10519 char **err)
10520{
10521 if (too_many_args(0, args, err, NULL))
10522 return -1;
10523
Willy Tarreauef934602016-12-22 23:12:01 +010010524 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010525 return 0;
10526}
10527
10528/* parse "ssl.lifetime".
10529 * Returns <0 on alert, >0 on warning, 0 on success.
10530 */
10531static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10532 struct proxy *defpx, const char *file, int line,
10533 char **err)
10534{
10535 const char *res;
10536
10537 if (too_many_args(1, args, err, NULL))
10538 return -1;
10539
10540 if (*(args[1]) == 0) {
10541 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10542 return -1;
10543 }
10544
Willy Tarreauef934602016-12-22 23:12:01 +010010545 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010546 if (res == PARSE_TIME_OVER) {
10547 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10548 args[1], args[0]);
10549 return -1;
10550 }
10551 else if (res == PARSE_TIME_UNDER) {
10552 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10553 args[1], args[0]);
10554 return -1;
10555 }
10556 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010557 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10558 return -1;
10559 }
10560 return 0;
10561}
10562
10563#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010564/* parse "ssl-dh-param-file".
10565 * Returns <0 on alert, >0 on warning, 0 on success.
10566 */
10567static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10568 struct proxy *defpx, const char *file, int line,
10569 char **err)
10570{
10571 if (too_many_args(1, args, err, NULL))
10572 return -1;
10573
10574 if (*(args[1]) == 0) {
10575 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10576 return -1;
10577 }
10578
10579 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10580 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10581 return -1;
10582 }
10583 return 0;
10584}
10585
Willy Tarreau9ceda382016-12-21 23:13:03 +010010586/* parse "ssl.default-dh-param".
10587 * Returns <0 on alert, >0 on warning, 0 on success.
10588 */
10589static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10590 struct proxy *defpx, const char *file, int line,
10591 char **err)
10592{
10593 if (too_many_args(1, args, err, NULL))
10594 return -1;
10595
10596 if (*(args[1]) == 0) {
10597 memprintf(err, "'%s' expects an integer argument.", args[0]);
10598 return -1;
10599 }
10600
Willy Tarreauef934602016-12-22 23:12:01 +010010601 global_ssl.default_dh_param = atoi(args[1]);
10602 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010603 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10604 return -1;
10605 }
10606 return 0;
10607}
10608#endif
10609
William Lallemand3af48e72020-02-03 17:15:52 +010010610
10611/*
10612 * parse "ssl-load-extra-files".
10613 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10614 */
10615static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10616 struct proxy *defpx, const char *file, int line,
10617 char **err)
10618{
10619 int i;
10620 int gf = SSL_GF_NONE;
10621
10622 if (*(args[1]) == 0)
10623 goto err_arg;
10624
10625 for (i = 1; *args[i]; i++) {
10626
10627 if (!strcmp("bundle", args[i])) {
10628 gf |= SSL_GF_BUNDLE;
10629
10630 } else if (!strcmp("sctl", args[i])) {
10631 gf |= SSL_GF_SCTL;
10632
10633 } else if (!strcmp("ocsp", args[i])){
10634 gf |= SSL_GF_OCSP;
10635
10636 } else if (!strcmp("issuer", args[i])){
10637 gf |= SSL_GF_OCSP_ISSUER;
10638
William Lallemand4c5adbf2020-02-24 14:23:22 +010010639 } else if (!strcmp("key", args[i])) {
10640 gf |= SSL_GF_KEY;
10641
William Lallemand3af48e72020-02-03 17:15:52 +010010642 } else if (!strcmp("none", args[i])) {
10643 if (gf != SSL_GF_NONE)
10644 goto err_alone;
10645 gf = SSL_GF_NONE;
10646 i++;
10647 break;
10648
10649 } else if (!strcmp("all", args[i])) {
10650 if (gf != SSL_GF_NONE)
10651 goto err_alone;
10652 gf = SSL_GF_ALL;
10653 i++;
10654 break;
10655 } else {
10656 goto err_arg;
10657 }
10658 }
10659 /* break from loop but there are still arguments */
10660 if (*args[i])
10661 goto err_alone;
10662
10663 global_ssl.extra_files = gf;
10664
10665 return 0;
10666
10667err_alone:
10668 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10669 return -1;
10670
10671err_arg:
10672 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10673 return -1;
10674}
10675
Willy Tarreau9ceda382016-12-21 23:13:03 +010010676
William Lallemand32af2032016-10-29 18:09:35 +020010677/* This function is used with TLS ticket keys management. It permits to browse
10678 * each reference. The variable <getnext> must contain the current node,
10679 * <end> point to the root node.
10680 */
10681#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10682static inline
10683struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10684{
10685 struct tls_keys_ref *ref = getnext;
10686
10687 while (1) {
10688
10689 /* Get next list entry. */
10690 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10691
10692 /* If the entry is the last of the list, return NULL. */
10693 if (&ref->list == end)
10694 return NULL;
10695
10696 return ref;
10697 }
10698}
10699
10700static inline
10701struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10702{
10703 int id;
10704 char *error;
10705
10706 /* If the reference starts by a '#', this is numeric id. */
10707 if (reference[0] == '#') {
10708 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10709 id = strtol(reference + 1, &error, 10);
10710 if (*error != '\0')
10711 return NULL;
10712
10713 /* Perform the unique id lookup. */
10714 return tlskeys_ref_lookupid(id);
10715 }
10716
10717 /* Perform the string lookup. */
10718 return tlskeys_ref_lookup(reference);
10719}
10720#endif
10721
10722
10723#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10724
10725static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10726
10727static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10728 return cli_io_handler_tlskeys_files(appctx);
10729}
10730
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010731/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10732 * (next index to be dumped), and cli.p0 (next key reference).
10733 */
William Lallemand32af2032016-10-29 18:09:35 +020010734static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10735
10736 struct stream_interface *si = appctx->owner;
10737
10738 switch (appctx->st2) {
10739 case STAT_ST_INIT:
10740 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010741 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010742 * later and restart at the state "STAT_ST_INIT".
10743 */
10744 chunk_reset(&trash);
10745
10746 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10747 chunk_appendf(&trash, "# id secret\n");
10748 else
10749 chunk_appendf(&trash, "# id (file)\n");
10750
Willy Tarreau06d80a92017-10-19 14:32:15 +020010751 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010752 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010753 return 0;
10754 }
10755
William Lallemand32af2032016-10-29 18:09:35 +020010756 /* Now, we start the browsing of the references lists.
10757 * Note that the following call to LIST_ELEM return bad pointer. The only
10758 * available field of this pointer is <list>. It is used with the function
10759 * tlskeys_list_get_next() for retruning the first available entry
10760 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010761 if (appctx->ctx.cli.p0 == NULL) {
10762 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10763 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010764 }
10765
10766 appctx->st2 = STAT_ST_LIST;
10767 /* fall through */
10768
10769 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010770 while (appctx->ctx.cli.p0) {
10771 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010772
10773 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010774 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010775 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010776
10777 if (appctx->ctx.cli.i1 == 0)
10778 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10779
William Lallemand32af2032016-10-29 18:09:35 +020010780 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010781 int head;
10782
10783 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10784 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010785 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010786 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010787
10788 chunk_reset(t2);
10789 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010790 if (ref->key_size_bits == 128) {
10791 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10792 sizeof(struct tls_sess_key_128),
10793 t2->area, t2->size);
10794 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10795 t2->area);
10796 }
10797 else if (ref->key_size_bits == 256) {
10798 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10799 sizeof(struct tls_sess_key_256),
10800 t2->area, t2->size);
10801 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10802 t2->area);
10803 }
10804 else {
10805 /* This case should never happen */
10806 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10807 }
William Lallemand32af2032016-10-29 18:09:35 +020010808
Willy Tarreau06d80a92017-10-19 14:32:15 +020010809 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010810 /* let's try again later from this stream. We add ourselves into
10811 * this stream's users so that it can remove us upon termination.
10812 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010813 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010814 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010815 return 0;
10816 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010817 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010818 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010819 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010820 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010821 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010822 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010823 /* let's try again later from this stream. We add ourselves into
10824 * this stream's users so that it can remove us upon termination.
10825 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010826 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010827 return 0;
10828 }
10829
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010830 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010831 break;
10832
10833 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010834 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010835 }
10836
10837 appctx->st2 = STAT_ST_FIN;
10838 /* fall through */
10839
10840 default:
10841 appctx->st2 = STAT_ST_FIN;
10842 return 1;
10843 }
10844 return 0;
10845}
10846
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010847/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010848static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010849{
William Lallemand32af2032016-10-29 18:09:35 +020010850 /* no parameter, shows only file list */
10851 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010852 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010853 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010854 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010855 }
10856
10857 if (args[2][0] == '*') {
10858 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010859 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010860 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010861 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010862 if (!appctx->ctx.cli.p0)
10863 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010864 }
William Lallemand32af2032016-10-29 18:09:35 +020010865 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010866 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010867}
10868
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010869static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010870{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010871 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010872 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010873
William Lallemand32af2032016-10-29 18:09:35 +020010874 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010875 if (!*args[3] || !*args[4])
10876 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 +020010877
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010878 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010879 if (!ref)
10880 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010881
Willy Tarreau1c913e42018-08-22 05:26:57 +020010882 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010883 if (ret < 0)
10884 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010885
Willy Tarreau1c913e42018-08-22 05:26:57 +020010886 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010887 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10888 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010889
Willy Tarreau9d008692019-08-09 11:21:01 +020010890 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010891}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010892#endif
William Lallemand32af2032016-10-29 18:09:35 +020010893
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010894/*
10895 * Take an ssl_bind_conf structure and append the configuration line used to
10896 * create it in the buffer
10897 */
10898static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10899{
10900 int space = 0;
10901
10902 if (conf == NULL)
10903 return;
10904
10905 chunk_appendf(buf, " [");
10906#ifdef OPENSSL_NPN_NEGOTIATED
10907 if (conf->npn_str) {
10908 int len = conf->npn_len;
10909 char *ptr = conf->npn_str;
10910 int comma = 0;
10911
10912 if (space) chunk_appendf(buf, " ");
10913 chunk_appendf(buf, "npn ");
10914 while (len) {
10915 unsigned short size;
10916
10917 size = *ptr;
10918 ptr++;
10919 if (comma)
10920 chunk_memcat(buf, ",", 1);
10921 chunk_memcat(buf, ptr, size);
10922 ptr += size;
10923 len -= size + 1;
10924 comma = 1;
10925 }
10926 chunk_memcat(buf, "", 1); /* finish with a \0 */
10927 space++;
10928 }
10929#endif
10930#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10931 if (conf->alpn_str) {
10932 int len = conf->alpn_len;
10933 char *ptr = conf->alpn_str;
10934 int comma = 0;
10935
10936 if (space) chunk_appendf(buf, " ");
10937 chunk_appendf(buf, "alpn ");
10938 while (len) {
10939 unsigned short size;
10940
10941 size = *ptr;
10942 ptr++;
10943 if (comma)
10944 chunk_memcat(buf, ",", 1);
10945 chunk_memcat(buf, ptr, size);
10946 ptr += size;
10947 len -= size + 1;
10948 comma = 1;
10949 }
10950 chunk_memcat(buf, "", 1); /* finish with a \0 */
10951 space++;
10952 }
10953#endif
10954 /* verify */
10955 {
10956 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
10957 if (space) chunk_appendf(buf, " ");
10958 chunk_appendf(buf, "verify none");
10959 space++;
10960 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
10961 if (space) chunk_appendf(buf, " ");
10962 chunk_appendf(buf, "verify optional");
10963 space++;
10964 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
10965 if (space) chunk_appendf(buf, " ");
10966 chunk_appendf(buf, "verify required");
10967 space++;
10968 }
10969 }
10970
10971 if (conf->no_ca_names) {
10972 if (space) chunk_appendf(buf, " ");
10973 chunk_appendf(buf, "no-ca-names");
10974 space++;
10975 }
10976
10977 if (conf->early_data) {
10978 if (space) chunk_appendf(buf, " ");
10979 chunk_appendf(buf, "allow-0rtt");
10980 space++;
10981 }
10982 if (conf->ca_file) {
10983 if (space) chunk_appendf(buf, " ");
10984 chunk_appendf(buf, "ca-file %s", conf->ca_file);
10985 space++;
10986 }
10987 if (conf->crl_file) {
10988 if (space) chunk_appendf(buf, " ");
10989 chunk_appendf(buf, "crl-file %s", conf->crl_file);
10990 space++;
10991 }
10992 if (conf->ciphers) {
10993 if (space) chunk_appendf(buf, " ");
10994 chunk_appendf(buf, "ciphers %s", conf->ciphers);
10995 space++;
10996 }
10997#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
10998 if (conf->ciphersuites) {
10999 if (space) chunk_appendf(buf, " ");
11000 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11001 space++;
11002 }
11003#endif
11004 if (conf->curves) {
11005 if (space) chunk_appendf(buf, " ");
11006 chunk_appendf(buf, "curves %s", conf->curves);
11007 space++;
11008 }
11009 if (conf->ecdhe) {
11010 if (space) chunk_appendf(buf, " ");
11011 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11012 space++;
11013 }
11014
11015 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11016 /* XXX: this part need to be revamp so we don't dump the default settings */
11017 if (conf->ssl_methods.min) {
11018 if (space) chunk_appendf(buf, " ");
11019 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11020 space++;
11021 }
11022
11023 if (conf->ssl_methods.max) {
11024 if (space) chunk_appendf(buf, " ");
11025 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11026 space++;
11027 }
11028
William Lallemand58a52222020-04-02 18:11:47 +020011029 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011030
11031 return;
11032}
11033
11034/* dump a list of filters */
11035static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11036{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011037 int i;
11038
11039 if (!entry->fcount)
11040 return;
11041
11042 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011043 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011044 }
11045 return;
11046}
11047
11048/* CLI IO handler for '(show|dump) ssl crt-list' */
11049static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11050{
11051 struct buffer *trash = alloc_trash_chunk();
11052 struct stream_interface *si = appctx->owner;
11053 struct ebmb_node *lnode;
11054
11055 if (trash == NULL)
11056 return 1;
11057
11058 /* dump the list of crt-lists */
11059 lnode = appctx->ctx.cli.p1;
11060 if (lnode == NULL)
11061 lnode = ebmb_first(&crtlists_tree);
11062 while (lnode) {
11063 chunk_appendf(trash, "%s\n", lnode->key);
11064 if (ci_putchk(si_ic(si), trash) == -1) {
11065 si_rx_room_blk(si);
11066 goto yield;
11067 }
11068 lnode = ebmb_next(lnode);
11069 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011070 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011071 return 1;
11072yield:
11073 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011074 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011075 return 0;
11076}
11077
11078/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11079static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11080{
11081 struct buffer *trash = alloc_trash_chunk();
11082 struct crtlist *crtlist;
11083 struct stream_interface *si = appctx->owner;
11084 struct crtlist_entry *entry;
11085
11086 if (trash == NULL)
11087 return 1;
11088
11089 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11090
11091 entry = appctx->ctx.cli.p1;
11092 if (entry == NULL) {
11093 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11094 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11095 if (ci_putchk(si_ic(si), trash) == -1) {
11096 si_rx_room_blk(si);
11097 goto yield;
11098 }
11099 }
11100
11101 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11102 struct ckch_store *store;
11103 const char *filename;
11104
11105 store = entry->node.key;
11106 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011107 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011108 if (appctx->ctx.cli.i0 == 's') /* show */
11109 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011110 dump_crtlist_sslconf(trash, entry->ssl_conf);
11111 dump_crtlist_filters(trash, entry);
11112 chunk_appendf(trash, "\n");
11113
11114 if (ci_putchk(si_ic(si), trash) == -1) {
11115 si_rx_room_blk(si);
11116 goto yield;
11117 }
11118 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011119 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011120 return 1;
11121yield:
11122 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011123 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011124 return 0;
11125}
11126
11127/* CLI argument parser for '(show|dump) ssl crt-list' */
11128static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11129{
11130 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011131 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011132 int mode;
11133
11134 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11135 return 1;
11136
11137 appctx->ctx.cli.p0 = NULL;
11138 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011139
William Lallemandc69f02d2020-04-06 19:07:03 +020011140 if (*args[3] && !strcmp(args[3], "-n")) {
11141 mode = 's';
11142 filename = args[4];
11143 } else {
11144 mode = 'd';
11145 filename = args[3];
11146 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011147
William Lallemandc69f02d2020-04-06 19:07:03 +020011148 if (mode == 's' && !*args[4])
11149 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11150
11151 if (filename && *filename) {
11152 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011153 if (lnode == NULL)
11154 return cli_err(appctx, "didn't find the specified filename\n");
11155
11156 appctx->ctx.cli.p0 = lnode;
11157 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11158 }
11159 appctx->ctx.cli.i0 = mode;
11160
11161 return 0;
11162}
William Lallemand44b35322019-10-17 16:28:40 +020011163
William Lallemande67c80b2020-03-25 14:42:37 +010011164/* release function of the "add ssl crt-list' command, free things and unlock
11165 the spinlock */
11166static void cli_release_add_crtlist(struct appctx *appctx)
11167{
11168 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11169
11170 if (appctx->st2 != SETCERT_ST_FIN) {
11171 struct ckch_inst *inst, *inst_s;
11172 /* upon error free the ckch_inst and everything inside */
11173 ebpt_delete(&entry->node);
11174 LIST_DEL(&entry->by_crtlist);
11175 LIST_DEL(&entry->by_ckch_store);
11176
11177 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +020011178 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011179 }
11180 crtlist_free_filters(entry->filters);
11181 ssl_sock_free_ssl_conf(entry->ssl_conf);
11182 free(entry->ssl_conf);
11183 free(entry);
11184 }
11185
11186 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11187}
11188
11189
11190/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11191 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11192 *
11193 * The logic is the same as the "commit ssl cert" command but without the
11194 * freeing of the old structures, because there are none.
11195 */
11196static int cli_io_handler_add_crtlist(struct appctx *appctx)
11197{
11198 struct bind_conf_list *bind_conf_node;
11199 struct stream_interface *si = appctx->owner;
11200 struct crtlist *crtlist = appctx->ctx.cli.p0;
11201 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11202 struct ckch_store *store = entry->node.key;
11203 struct buffer *trash = alloc_trash_chunk();
11204 struct ckch_inst *new_inst;
11205 char *err = NULL;
11206 int i = 0;
11207 int errcode = 0;
11208
11209 if (trash == NULL)
11210 goto error;
11211
11212 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11213 * created.
11214 */
11215 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11216 goto error;
11217
11218 while (1) {
11219 switch (appctx->st2) {
11220 case SETCERT_ST_INIT:
11221 /* This state just print the update message */
11222 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11223 if (ci_putchk(si_ic(si), trash) == -1) {
11224 si_rx_room_blk(si);
11225 goto yield;
11226 }
11227 appctx->st2 = SETCERT_ST_GEN;
11228 /* fallthrough */
11229 case SETCERT_ST_GEN:
11230 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11231 if (bind_conf_node == NULL)
11232 bind_conf_node = crtlist->bind_conf;
11233 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11234 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11235 struct sni_ctx *sni;
11236
11237 /* yield every 10 generations */
11238 if (i > 10) {
11239 appctx->ctx.cli.p2 = bind_conf_node;
11240 goto yield;
11241 }
11242
11243 /* we don't support multi-cert bundles, only simple ones */
11244 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11245 if (errcode & ERR_CODE)
11246 goto error;
11247
11248 /* we need to initialize the SSL_CTX generated */
11249 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11250 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11251 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11252 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11253 if (errcode & ERR_CODE)
11254 goto error;
11255 }
11256 }
11257 /* display one dot for each new instance */
11258 chunk_appendf(trash, ".");
11259 i++;
11260 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11261 }
11262 appctx->st2 = SETCERT_ST_INSERT;
11263 /* fallthrough */
11264 case SETCERT_ST_INSERT:
11265 /* insert SNIs in bind_conf */
11266 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11267 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11268 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11269 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11270 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011271 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011272 appctx->st2 = SETCERT_ST_FIN;
11273 goto end;
11274 }
11275 }
11276
11277end:
11278 chunk_appendf(trash, "\n");
11279 if (errcode & ERR_WARN)
11280 chunk_appendf(trash, "%s", err);
11281 chunk_appendf(trash, "Success!\n");
11282 if (ci_putchk(si_ic(si), trash) == -1)
11283 si_rx_room_blk(si);
11284 free_trash_chunk(trash);
11285 /* success: call the release function and don't come back */
11286 return 1;
11287yield:
11288 /* store the state */
11289 if (ci_putchk(si_ic(si), trash) == -1)
11290 si_rx_room_blk(si);
11291 free_trash_chunk(trash);
11292 si_rx_endp_more(si); /* let's come back later */
11293 return 0; /* should come back */
11294
11295error:
11296 /* spin unlock and free are done in the release function */
11297 if (trash) {
11298 chunk_appendf(trash, "\n%sFailed!\n", err);
11299 if (ci_putchk(si_ic(si), trash) == -1)
11300 si_rx_room_blk(si);
11301 free_trash_chunk(trash);
11302 }
11303 /* error: call the release function and don't come back */
11304 return 1;
11305}
11306
11307
11308/*
11309 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011310 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011311 */
11312static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11313{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011314 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011315 struct ckch_store *store;
11316 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011317 char path[MAXPATHLEN+1];
11318 char *crtlist_path;
11319 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011320 struct ebmb_node *eb;
11321 struct ebpt_node *inserted;
11322 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011323 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011324
11325 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11326 return 1;
11327
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011328 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011329 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11330
11331 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011332
11333 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11334 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11335
11336 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11337 if (!eb) {
11338 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11339 goto error;
11340 }
11341 crtlist = ebmb_entry(eb, struct crtlist, node);
11342
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011343 entry = malloc(sizeof(*entry));
11344 if (entry == NULL) {
11345 memprintf(&err, "Not enough memory!");
11346 goto error;
11347 }
William Lallemanda690fed2020-04-02 12:02:54 +020011348 entry->fcount = 0;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011349 entry->filters = NULL;
11350 entry->ssl_conf = NULL;
11351
11352 if (payload) {
11353 char *lf;
11354
11355 lf = strrchr(payload, '\n');
11356 if (lf) {
11357 memprintf(&err, "only one line of payload is supported!");
11358 goto error;
11359 }
11360 /* cert_path is filled here */
11361 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11362 if (cfgerr & ERR_CODE)
11363 goto error;
11364 } else {
11365 cert_path = args[4];
11366 }
11367
11368 if (!cert_path) {
11369 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11370 cfgerr |= ERR_ALERT | ERR_FATAL;
11371 goto error;
11372 }
11373
11374 if (*cert_path != '/' && global_ssl.crt_base) {
11375 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11376 memprintf(&err, "'%s' : path too long", cert_path);
11377 cfgerr |= ERR_ALERT | ERR_FATAL;
11378 goto error;
11379 }
11380 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11381 cert_path = path;
11382 }
11383
William Lallemande67c80b2020-03-25 14:42:37 +010011384 store = ckchs_lookup(cert_path);
11385 if (store == NULL) {
11386 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11387 goto error;
11388 }
William Lallemand36ccc392020-04-08 10:57:24 +020011389 if (store->multi) {
11390 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);
11391 goto error;
11392 }
William Lallemande67c80b2020-03-25 14:42:37 +010011393 if (store->ckch == NULL || store->ckch->cert == NULL) {
11394 memprintf(&err, "certificate '%s' is empty!", cert_path);
11395 goto error;
11396 }
11397
William Lallemande67c80b2020-03-25 14:42:37 +010011398 /* check if it's possible to insert this new crtlist_entry */
11399 entry->node.key = store;
11400 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11401 if (inserted != &entry->node) {
11402 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011403 goto error;
11404 }
11405
11406 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
11407 LIST_INIT(&entry->ckch_inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011408 entry->crtlist = crtlist;
11409 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11410
11411 appctx->st2 = SETCERT_ST_INIT;
11412 appctx->ctx.cli.p0 = crtlist;
11413 appctx->ctx.cli.p1 = entry;
11414
11415 /* unlock is done in the release handler */
11416 return 0;
11417
11418error:
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011419 if (entry) {
11420 crtlist_free_filters(entry->filters);
11421 ssl_sock_free_ssl_conf(entry->ssl_conf);
11422 free(entry->ssl_conf);
11423 free(entry);
11424 }
William Lallemande67c80b2020-03-25 14:42:37 +010011425 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11426 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11427 return cli_dynerr(appctx, err);
11428}
11429
William Lallemand0a9b9412020-04-06 17:43:05 +020011430/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11431static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11432{
11433 struct ckch_store *store;
11434 char *err = NULL;
11435 char *crtlist_path, *cert_path;
11436 struct ebmb_node *ebmb;
11437 struct ebpt_node *ebpt;
11438 struct crtlist *crtlist;
11439 struct crtlist_entry *entry = NULL;
11440 struct ckch_inst *inst, *inst_s;
11441 int linenum = 0;
11442 char *colons;
11443
11444 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11445 return 1;
11446
11447 if (!*args[3] || !*args[4])
11448 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11449
William Lallemand463b5242020-04-08 10:30:44 +020011450 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11451 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11452
William Lallemand0a9b9412020-04-06 17:43:05 +020011453 crtlist_path = args[3];
11454 cert_path = args[4];
11455
11456 colons = strchr(cert_path, ':');
11457 if (colons) {
11458 char *endptr;
11459
11460 linenum = strtol(colons + 1, &endptr, 10);
11461 if (colons + 1 == endptr || *endptr != '\0') {
11462 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11463 goto error;
11464 }
11465 *colons = '\0';
11466 }
11467 /* look for crtlist */
11468 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11469 if (!ebmb) {
11470 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11471 goto error;
11472 }
11473 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11474
11475 /* look for store */
11476 store = ckchs_lookup(cert_path);
11477 if (store == NULL) {
11478 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11479 goto error;
11480 }
William Lallemand36ccc392020-04-08 10:57:24 +020011481 if (store->multi) {
11482 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);
11483 goto error;
11484 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011485 if (store->ckch == NULL || store->ckch->cert == NULL) {
11486 memprintf(&err, "certificate '%s' is empty!", cert_path);
11487 goto error;
11488 }
11489
11490 ebpt = ebpt_lookup(&crtlist->entries, store);
11491 if (!ebpt) {
11492 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11493 goto error;
11494 }
11495
11496 /* list the line number of entries for errors in err, and select the right ebpt */
11497 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11498 struct crtlist_entry *tmp;
11499
11500 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11501 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11502
11503 /* select the entry we wanted */
11504 if (linenum == 0 || tmp->linenum == linenum) {
11505 if (!entry)
11506 entry = tmp;
11507 }
11508 }
11509
11510 /* we didn't found the specified entry */
11511 if (!entry) {
11512 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);
11513 goto error;
11514 }
11515
11516 /* we didn't specified a line number but there were several entries */
11517 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11518 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11519 goto error;
11520 }
11521
11522 /* upon error free the ckch_inst and everything inside */
11523
11524 ebpt_delete(&entry->node);
11525 LIST_DEL(&entry->by_crtlist);
11526 LIST_DEL(&entry->by_ckch_store);
11527
11528 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11529 struct sni_ctx *sni, *sni_s;
11530
11531 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11532 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11533 ebmb_delete(&sni->name);
11534 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011535 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011536 free(sni);
11537 }
11538 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11539 LIST_DEL(&inst->by_ckchs);
11540 free(inst);
11541 }
11542
11543 crtlist_free_filters(entry->filters);
11544 ssl_sock_free_ssl_conf(entry->ssl_conf);
11545 free(entry->ssl_conf);
11546 free(entry);
11547
11548 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11549 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11550 return cli_dynmsg(appctx, LOG_NOTICE, err);
11551
11552error:
11553 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11554 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11555 return cli_dynerr(appctx, err);
11556}
William Lallemande67c80b2020-03-25 14:42:37 +010011557
William Lallemand44b35322019-10-17 16:28:40 +020011558/* Type of SSL payloads that can be updated over the CLI */
11559
11560enum {
11561 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011562 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011563#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011564 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011565#endif
William Lallemand44b35322019-10-17 16:28:40 +020011566 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011567#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011568 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011569#endif
William Lallemand44b35322019-10-17 16:28:40 +020011570 CERT_TYPE_MAX,
11571};
11572
11573struct {
11574 const char *ext;
11575 int type;
11576 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11577 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011578} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011579 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011580 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011581#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011582 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011583#endif
11584#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011585 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011586#endif
William Lallemand44b35322019-10-17 16:28:40 +020011587 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011588 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011589};
11590
William Lallemand8f840d72019-10-23 10:53:05 +020011591
William Lallemandd4f946c2019-12-05 10:26:40 +010011592/* release function of the `show ssl cert' command */
11593static void cli_release_show_cert(struct appctx *appctx)
11594{
11595 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11596}
11597
11598/* IO handler of "show ssl cert <filename>" */
11599static int cli_io_handler_show_cert(struct appctx *appctx)
11600{
11601 struct buffer *trash = alloc_trash_chunk();
11602 struct ebmb_node *node;
11603 struct stream_interface *si = appctx->owner;
11604 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011605
11606 if (trash == NULL)
11607 return 1;
11608
11609 if (!appctx->ctx.ssl.old_ckchs) {
11610 if (ckchs_transaction.old_ckchs) {
11611 ckchs = ckchs_transaction.old_ckchs;
11612 chunk_appendf(trash, "# transaction\n");
11613 if (!ckchs->multi) {
11614 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011615#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011616 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011617 int n;
11618
William Lallemandd4f946c2019-12-05 10:26:40 +010011619 chunk_appendf(trash, "*%s:", ckchs->path);
11620 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11621 if (ckchs->ckch[n].cert)
11622 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11623 }
11624 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011625#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011626 }
11627 }
11628 }
11629
11630 if (!appctx->ctx.cli.p0) {
11631 chunk_appendf(trash, "# filename\n");
11632 node = ebmb_first(&ckchs_tree);
11633 } else {
11634 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11635 }
11636 while (node) {
11637 ckchs = ebmb_entry(node, struct ckch_store, node);
11638 if (!ckchs->multi) {
11639 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011640#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011641 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011642 int n;
11643
William Lallemandd4f946c2019-12-05 10:26:40 +010011644 chunk_appendf(trash, "%s:", ckchs->path);
11645 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11646 if (ckchs->ckch[n].cert)
11647 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11648 }
11649 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011650#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011651 }
11652
11653 node = ebmb_next(node);
11654 if (ci_putchk(si_ic(si), trash) == -1) {
11655 si_rx_room_blk(si);
11656 goto yield;
11657 }
11658 }
11659
11660 appctx->ctx.cli.p0 = NULL;
11661 free_trash_chunk(trash);
11662 return 1;
11663yield:
11664
11665 free_trash_chunk(trash);
11666 appctx->ctx.cli.p0 = ckchs;
11667 return 0; /* should come back */
11668}
11669
11670/* IO handler of the details "show ssl cert <filename>" */
11671static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11672{
11673 struct stream_interface *si = appctx->owner;
11674 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11675 struct buffer *out = alloc_trash_chunk();
11676 struct buffer *tmp = alloc_trash_chunk();
11677 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011678 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011679 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011680 int write = -1;
11681 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011682 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011683
11684 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011685 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011686
11687 if (!ckchs->multi) {
11688 chunk_appendf(out, "Filename: ");
11689 if (ckchs == ckchs_transaction.new_ckchs)
11690 chunk_appendf(out, "*");
11691 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011692
William Lallemand59c16fc2020-03-19 20:26:02 +010011693 chunk_appendf(out, "Status: ");
11694 if (ckchs->ckch->cert == NULL)
11695 chunk_appendf(out, "Empty\n");
11696 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11697 chunk_appendf(out, "Unused\n");
11698 else
11699 chunk_appendf(out, "Used\n");
11700
William Lallemandea987ed2020-03-19 16:48:33 +010011701 if (ckchs->ckch->cert == NULL)
11702 goto end;
11703
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011704 chain = ckchs->ckch->chain;
11705 if (chain == NULL) {
11706 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011707 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011708 if (issuer) {
11709 chain = issuer->chain;
11710 chunk_appendf(out, "Chain Filename: ");
11711 chunk_appendf(out, "%s\n", issuer->path);
11712 }
11713 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011714 chunk_appendf(out, "Serial: ");
11715 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11716 goto end;
11717 dump_binary(out, tmp->area, tmp->data);
11718 chunk_appendf(out, "\n");
11719
11720 chunk_appendf(out, "notBefore: ");
11721 chunk_reset(tmp);
11722 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11723 goto end;
11724 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11725 goto end;
11726 write = BIO_read(bio, tmp->area, tmp->size-1);
11727 tmp->area[write] = '\0';
11728 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011729 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011730 chunk_appendf(out, "%s\n", tmp->area);
11731
11732 chunk_appendf(out, "notAfter: ");
11733 chunk_reset(tmp);
11734 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11735 goto end;
11736 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11737 goto end;
11738 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11739 goto end;
11740 tmp->area[write] = '\0';
11741 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011742 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011743 chunk_appendf(out, "%s\n", tmp->area);
11744
William Lallemandd4f946c2019-12-05 10:26:40 +010011745#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11746 chunk_appendf(out, "Subject Alternative Name: ");
11747 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11748 goto end;
11749 *(out->area + out->data) = '\0';
11750 chunk_appendf(out, "\n");
11751#endif
11752 chunk_reset(tmp);
11753 chunk_appendf(out, "Algorithm: ");
11754 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11755 goto end;
11756 chunk_appendf(out, "%s\n", tmp->area);
11757
11758 chunk_reset(tmp);
11759 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011760 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011761 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011762 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011763 dump_binary(out, tmp->area, tmp->data);
11764 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011765
William Lallemanda90e5932020-02-25 14:07:58 +010011766 chunk_appendf(out, "Subject: ");
11767 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11768 goto end;
11769 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11770 goto end;
11771 *(tmp->area + tmp->data) = '\0';
11772 chunk_appendf(out, "%s\n", tmp->area);
11773
11774 chunk_appendf(out, "Issuer: ");
11775 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11776 goto end;
11777 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11778 goto end;
11779 *(tmp->area + tmp->data) = '\0';
11780 chunk_appendf(out, "%s\n", tmp->area);
11781
William Lallemand35f4a9d2020-02-25 11:56:32 +010011782 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011783 for (i = 0; i < sk_X509_num(chain); i++) {
11784 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011785
William Lallemandbb7288a2020-02-25 14:04:33 +010011786 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011787 if ((name = X509_get_subject_name(ca)) == NULL)
11788 goto end;
11789 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11790 goto end;
11791 *(tmp->area + tmp->data) = '\0';
11792 chunk_appendf(out, "%s\n", tmp->area);
11793
William Lallemandbb7288a2020-02-25 14:04:33 +010011794 chunk_appendf(out, "Chain Issuer: ");
11795 if ((name = X509_get_issuer_name(ca)) == NULL)
11796 goto end;
11797 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11798 goto end;
11799 *(tmp->area + tmp->data) = '\0';
11800 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011801 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011802 }
11803
William Lallemandea987ed2020-03-19 16:48:33 +010011804end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011805 if (ci_putchk(si_ic(si), out) == -1) {
11806 si_rx_room_blk(si);
11807 goto yield;
11808 }
11809
William Lallemand18eeb8e2020-03-20 14:42:36 +010011810end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011811 if (bio)
11812 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011813 free_trash_chunk(tmp);
11814 free_trash_chunk(out);
11815 return 1;
11816yield:
11817 free_trash_chunk(tmp);
11818 free_trash_chunk(out);
11819 return 0; /* should come back */
11820}
11821
11822/* parsing function for 'show ssl cert [certfile]' */
11823static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11824{
11825 struct ckch_store *ckchs;
11826
11827 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11828 return cli_err(appctx, "Can't allocate memory!\n");
11829
11830 /* The operations on the CKCH architecture are locked so we can
11831 * manipulate ckch_store and ckch_inst */
11832 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11833 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11834
11835 /* check if there is a certificate to lookup */
11836 if (*args[3]) {
11837 if (*args[3] == '*') {
11838 if (!ckchs_transaction.new_ckchs)
11839 goto error;
11840
11841 ckchs = ckchs_transaction.new_ckchs;
11842
11843 if (strcmp(args[3] + 1, ckchs->path))
11844 goto error;
11845
11846 } else {
11847 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11848 goto error;
11849
11850 }
11851
11852 if (ckchs->multi)
11853 goto error;
11854
11855 appctx->ctx.cli.p0 = ckchs;
11856 /* use the IO handler that shows details */
11857 appctx->io_handler = cli_io_handler_show_cert_detail;
11858 }
11859
11860 return 0;
11861
11862error:
11863 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11864 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11865}
11866
William Lallemand430413e2019-10-28 14:30:47 +010011867/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011868static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011869{
11870 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011871
William Lallemand430413e2019-10-28 14:30:47 +010011872 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011873
William Lallemand430413e2019-10-28 14:30:47 +010011874 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011875 /* 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 +010011876 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011877
William Lallemandbeea2a42019-10-30 17:45:33 +010011878 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020011879 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011880 }
11881}
11882
11883
11884/*
11885 * This function tries to create the new ckch_inst and their SNIs
11886 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011887static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011888{
11889 struct stream_interface *si = appctx->owner;
11890 int y = 0;
11891 char *err = NULL;
11892 int errcode = 0;
11893 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11894 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011895 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011896 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011897 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011898
William Lallemand33cc76f2019-10-31 11:43:45 +010011899 if (trash == NULL)
11900 goto error;
11901
William Lallemand8f840d72019-10-23 10:53:05 +020011902 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11903 goto error;
11904
William Lallemand430413e2019-10-28 14:30:47 +010011905 while (1) {
11906 switch (appctx->st2) {
11907 case SETCERT_ST_INIT:
11908 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011909 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011910 if (ci_putchk(si_ic(si), trash) == -1) {
11911 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011912 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011913 }
11914 appctx->st2 = SETCERT_ST_GEN;
11915 /* fallthrough */
11916 case SETCERT_ST_GEN:
11917 /*
11918 * This state generates the ckch instances with their
11919 * sni_ctxs and SSL_CTX.
11920 *
William Lallemand430413e2019-10-28 14:30:47 +010011921 * Since the SSL_CTX generation can be CPU consumer, we
11922 * yield every 10 instances.
11923 */
William Lallemand8f840d72019-10-23 10:53:05 +020011924
William Lallemandbeea2a42019-10-30 17:45:33 +010011925 old_ckchs = appctx->ctx.ssl.old_ckchs;
11926 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011927
William Lallemandbeea2a42019-10-30 17:45:33 +010011928 if (!new_ckchs)
11929 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020011930
William Lallemandbeea2a42019-10-30 17:45:33 +010011931 /* get the next ckchi to regenerate */
11932 ckchi = appctx->ctx.ssl.next_ckchi;
11933 /* we didn't start yet, set it to the first elem */
11934 if (ckchi == NULL)
11935 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011936
William Lallemandbeea2a42019-10-30 17:45:33 +010011937 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
11938 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
11939 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010011940 char **sni_filter = NULL;
11941 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020011942
William Lallemandbeea2a42019-10-30 17:45:33 +010011943 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
11944 if (y >= 10) {
11945 /* save the next ckchi to compute */
11946 appctx->ctx.ssl.next_ckchi = ckchi;
11947 goto yield;
11948 }
William Lallemandcaa16192020-04-08 16:29:15 +020011949
11950 if (ckchi->crtlist_entry) {
11951 sni_filter = ckchi->crtlist_entry->filters;
11952 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010011953 }
William Lallemand38df1c82019-12-04 15:39:35 +010011954
William Lallemandbeea2a42019-10-30 17:45:33 +010011955 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010011956 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 +010011957 else
William Lallemand38df1c82019-12-04 15:39:35 +010011958 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
11959
William Lallemandbeea2a42019-10-30 17:45:33 +010011960 if (errcode & ERR_CODE)
11961 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020011962
William Lallemand21724f02019-11-04 17:56:13 +010011963 /* if the previous ckchi was used as the default */
11964 if (ckchi->is_default)
11965 new_inst->is_default = 1;
11966
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011967 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010011968 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11969 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011970 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 +010011971 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
11972 if (errcode & ERR_CODE)
11973 goto error;
11974 }
11975 }
11976
11977
William Lallemandbeea2a42019-10-30 17:45:33 +010011978 /* display one dot per new instance */
11979 chunk_appendf(trash, ".");
11980 /* link the new ckch_inst to the duplicate */
11981 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
11982 y++;
11983 }
William Lallemand430413e2019-10-28 14:30:47 +010011984 appctx->st2 = SETCERT_ST_INSERT;
11985 /* fallthrough */
11986 case SETCERT_ST_INSERT:
11987 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020011988
William Lallemandbeea2a42019-10-30 17:45:33 +010011989 old_ckchs = appctx->ctx.ssl.old_ckchs;
11990 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011991
William Lallemandbeea2a42019-10-30 17:45:33 +010011992 if (!new_ckchs)
11993 continue;
William Lallemand430413e2019-10-28 14:30:47 +010011994
William Lallemand90afe902020-03-30 19:29:45 +020011995 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
11996 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
11997 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
11998 ebpt_delete(&entry->node);
11999 /* change the ptr and reinsert the node */
12000 entry->node.key = new_ckchs;
12001 ebpt_insert(&entry->crtlist->entries, &entry->node);
12002 }
12003
William Lallemand21724f02019-11-04 17:56:13 +010012004 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012005 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12006 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12007 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12008 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12009 }
William Lallemand8f840d72019-10-23 10:53:05 +020012010
William Lallemandbeea2a42019-10-30 17:45:33 +010012011 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12012 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemandd5e93772020-04-09 17:12:16 +020012013 struct bind_conf *bind_conf = ckchi->bind_conf;
12014
12015 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020012016 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020012017 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010012018 }
William Lallemand8f840d72019-10-23 10:53:05 +020012019
William Lallemandbeea2a42019-10-30 17:45:33 +010012020 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020012021 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010012022 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012023 appctx->st2 = SETCERT_ST_FIN;
12024 /* fallthrough */
12025 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012026 /* we achieved the transaction, we can set everything to NULL */
12027 free(ckchs_transaction.path);
12028 ckchs_transaction.path = NULL;
12029 ckchs_transaction.new_ckchs = NULL;
12030 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012031 goto end;
12032 }
William Lallemand8f840d72019-10-23 10:53:05 +020012033 }
William Lallemand430413e2019-10-28 14:30:47 +010012034end:
William Lallemand8f840d72019-10-23 10:53:05 +020012035
William Lallemanded442432019-11-21 16:41:07 +010012036 chunk_appendf(trash, "\n");
12037 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012038 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012039 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012040 if (ci_putchk(si_ic(si), trash) == -1)
12041 si_rx_room_blk(si);
12042 free_trash_chunk(trash);
12043 /* success: call the release function and don't come back */
12044 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012045yield:
12046 /* store the state */
12047 if (ci_putchk(si_ic(si), trash) == -1)
12048 si_rx_room_blk(si);
12049 free_trash_chunk(trash);
12050 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012051 return 0; /* should come back */
12052
12053error:
12054 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012055 if (trash) {
12056 chunk_appendf(trash, "\n%sFailed!\n", err);
12057 if (ci_putchk(si_ic(si), trash) == -1)
12058 si_rx_room_blk(si);
12059 free_trash_chunk(trash);
12060 }
William Lallemand430413e2019-10-28 14:30:47 +010012061 /* error: call the release function and don't come back */
12062 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012063}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012064
12065/*
12066 * Parsing function of 'commit ssl cert'
12067 */
12068static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12069{
12070 char *err = NULL;
12071
William Lallemand230662a2019-12-03 13:32:54 +010012072 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12073 return 1;
12074
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012075 if (!*args[3])
12076 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12077
12078 /* The operations on the CKCH architecture are locked so we can
12079 * manipulate ckch_store and ckch_inst */
12080 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12081 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12082
12083 if (!ckchs_transaction.path) {
12084 memprintf(&err, "No ongoing transaction! !\n");
12085 goto error;
12086 }
12087
12088 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12089 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12090 goto error;
12091 }
12092
William Lallemand4c5adbf2020-02-24 14:23:22 +010012093#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12094 if (ckchs_transaction.new_ckchs->multi) {
12095 int n;
12096
12097 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12098 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)) {
12099 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12100 goto error;
12101 }
12102 }
12103 } else
12104#endif
12105 {
12106 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12107 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12108 goto error;
12109 }
12110 }
12111
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012112 /* init the appctx structure */
12113 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012114 appctx->ctx.ssl.next_ckchi = NULL;
12115 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12116 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12117
12118 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12119 return 0;
12120
12121error:
12122
12123 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12124 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12125
12126 return cli_dynerr(appctx, err);
12127}
12128
William Lallemand8f840d72019-10-23 10:53:05 +020012129/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012130 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012131 */
William Lallemand150bfa82019-09-19 17:12:49 +020012132static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12133{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012134 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012135 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012136 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012137 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012138 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012139 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012140 char *end;
12141 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012142 struct cert_key_and_chain *ckch;
12143 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012144
William Lallemand230662a2019-12-03 13:32:54 +010012145 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12146 return 1;
12147
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012148 if ((buf = alloc_trash_chunk()) == NULL)
12149 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012150
12151 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012152 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012153
12154 /* The operations on the CKCH architecture are locked so we can
12155 * manipulate ckch_store and ckch_inst */
12156 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12157 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12158
William Lallemand8f840d72019-10-23 10:53:05 +020012159 if (!chunk_strcpy(buf, args[3])) {
12160 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12161 errcode |= ERR_ALERT | ERR_FATAL;
12162 goto end;
12163 }
12164
William Lallemand44b35322019-10-17 16:28:40 +020012165 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012166 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012167 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012168 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12169 *end = '\0';
12170 type = cert_exts[i].type;
12171 break;
12172 }
12173 }
12174
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012175 appctx->ctx.ssl.old_ckchs = NULL;
12176 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012177
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012178 /* if there is an ongoing transaction */
12179 if (ckchs_transaction.path) {
12180 /* 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 +020012181#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012182 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012183 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012184 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012185
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012186 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012187 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012188 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012189 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012190 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12191 bundle = j; /* keep the type of certificate so we insert it at the right place */
12192 *end = '\0'; /* it's a bundle let's end the string*/
12193 break;
12194 }
William Lallemand150bfa82019-09-19 17:12:49 +020012195 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012196 if (bundle < 0) {
12197 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);
12198 errcode |= ERR_ALERT | ERR_FATAL;
12199 goto end;
12200 }
12201 }
12202#endif
12203
12204 /* if there is an ongoing transaction, check if this is the same file */
12205 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12206 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12207 errcode |= ERR_ALERT | ERR_FATAL;
12208 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012209 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012210
12211 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12212
12213 } else {
12214 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12215
12216 /* lookup for the certificate in the tree:
12217 * check if this is used as a bundle AND as a unique certificate */
12218 for (i = 0; i < 2; i++) {
12219
12220 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12221 /* only the bundle name is in the tree and you should
12222 * never update a bundle name, only a filename */
12223 if (bundle < 0 && find_ckchs[i]->multi) {
12224 /* we tried to look for a non-bundle and we found a bundle */
12225 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12226 err ? err : "", args[3], args[3]);
12227 errcode |= ERR_ALERT | ERR_FATAL;
12228 goto end;
12229 }
William Lallemand3246d942019-11-04 14:02:11 +010012230 /* If we want a bundle but this is not a bundle
12231 * example: When you try to update <file>.rsa, but
12232 * <file> is a regular file */
12233 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12234 find_ckchs[i] = NULL;
12235 break;
12236 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012237 }
12238#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12239 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012240 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012241 int j;
12242
12243 /* check if it was used in a bundle by removing the
12244 * .dsa/.rsa/.ecdsa at the end of the filename */
12245 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012246 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012247 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12248 bundle = j; /* keep the type of certificate so we insert it at the right place */
12249 *end = '\0'; /* it's a bundle let's end the string*/
12250 break;
12251 }
12252 }
William Lallemand37031b82019-11-04 13:38:53 +010012253 if (bundle < 0) /* we didn't find a bundle extension */
12254 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012255 }
William Lallemand963b2e72019-10-14 11:38:36 +020012256#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012257 /* bundles are not supported here, so we don't need to lookup again */
12258 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012259#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012260 }
12261
12262 if (find_ckchs[0] && find_ckchs[1]) {
12263 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",
12264 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12265 errcode |= ERR_ALERT | ERR_FATAL;
12266 goto end;
12267 }
12268
12269 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012270 }
12271
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012272 if (!appctx->ctx.ssl.old_ckchs) {
12273 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012274 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012275 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012276 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012277 }
12278
William Lallemand8a7fdf02019-11-04 10:59:32 +010012279 if (!appctx->ctx.ssl.path) {
12280 /* this is a new transaction, set the path of the transaction */
12281 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12282 if (!appctx->ctx.ssl.path) {
12283 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12284 errcode |= ERR_ALERT | ERR_FATAL;
12285 goto end;
12286 }
12287 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012288
12289 old_ckchs = appctx->ctx.ssl.old_ckchs;
12290
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012291 /* duplicate the ckch store */
12292 new_ckchs = ckchs_dup(old_ckchs);
12293 if (!new_ckchs) {
12294 memprintf(&err, "%sCannot allocate memory!\n",
12295 err ? err : "");
12296 errcode |= ERR_ALERT | ERR_FATAL;
12297 goto end;
12298 }
12299
12300 if (!new_ckchs->multi)
12301 ckch = new_ckchs->ckch;
12302 else
12303 ckch = &new_ckchs->ckch[bundle];
12304
12305 /* appply the change on the duplicate */
12306 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12307 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12308 errcode |= ERR_ALERT | ERR_FATAL;
12309 goto end;
12310 }
12311
12312 appctx->ctx.ssl.new_ckchs = new_ckchs;
12313
12314 /* we succeed, we can save the ckchs in the transaction */
12315
12316 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012317 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012318 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12319 ckchs_transaction.path = appctx->ctx.ssl.path;
12320 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12321 } else {
12322 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12323
12324 }
12325
12326 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020012327 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012328
12329 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12330
12331
William Lallemand8f840d72019-10-23 10:53:05 +020012332 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012333
William Lallemand8f840d72019-10-23 10:53:05 +020012334end:
12335 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012336
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012337 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012338
William Lallemandba1c33f2020-04-08 17:55:45 +020012339 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012340 appctx->ctx.ssl.new_ckchs = NULL;
12341
12342 appctx->ctx.ssl.old_ckchs = NULL;
12343
12344 free(appctx->ctx.ssl.path);
12345 appctx->ctx.ssl.path = NULL;
12346
12347 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012348 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012349 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012350
12351 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12352 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012353 }
William Lallemand8f840d72019-10-23 10:53:05 +020012354 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012355}
12356
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012357/* parsing function of 'abort ssl cert' */
12358static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12359{
12360 char *err = NULL;
12361
William Lallemand230662a2019-12-03 13:32:54 +010012362 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12363 return 1;
12364
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012365 if (!*args[3])
12366 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12367
12368 /* The operations on the CKCH architecture are locked so we can
12369 * manipulate ckch_store and ckch_inst */
12370 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12371 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12372
12373 if (!ckchs_transaction.path) {
12374 memprintf(&err, "No ongoing transaction!\n");
12375 goto error;
12376 }
12377
12378 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12379 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12380 goto error;
12381 }
12382
12383 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020012384 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012385 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020012386 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012387 ckchs_transaction.old_ckchs = NULL;
12388 free(ckchs_transaction.path);
12389 ckchs_transaction.path = NULL;
12390
12391 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12392
12393 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12394 return cli_dynmsg(appctx, LOG_NOTICE, err);
12395
12396error:
12397 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12398
12399 return cli_dynerr(appctx, err);
12400}
12401
William Lallemandea987ed2020-03-19 16:48:33 +010012402/* parsing function of 'new ssl cert' */
12403static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12404{
12405 struct ckch_store *store;
12406 char *err = NULL;
12407 char *path;
12408
12409 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12410 return 1;
12411
12412 if (!*args[3])
12413 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12414
12415 path = args[3];
12416
12417 /* The operations on the CKCH architecture are locked so we can
12418 * manipulate ckch_store and ckch_inst */
12419 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12420 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12421
12422 store = ckchs_lookup(path);
12423 if (store != NULL) {
12424 memprintf(&err, "Certificate '%s' already exists!\n", path);
12425 store = NULL; /* we don't want to free it */
12426 goto error;
12427 }
12428 store = calloc(1, sizeof(*store) + strlen(path) + 1);
12429 if (!store) {
12430 memprintf(&err, "unable to allocate memory.\n");
12431 goto error;
12432 }
12433 store->ckch = calloc(1, sizeof(*store->ckch));
12434 if (!store->ckch) {
12435 memprintf(&err, "unable to allocate memory.\n");
12436 goto error;
12437 }
12438 /* we won't create any instance */
12439 LIST_INIT(&store->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +020012440 LIST_INIT(&store->crtlist_entry);
William Lallemandea987ed2020-03-19 16:48:33 +010012441
12442 /* we won't support multi-certificate bundle here */
12443 store->multi = 0;
12444
12445 /* insert into the ckchs tree */
12446 memcpy(store->path, path, strlen(path) + 1);
12447 ebst_insert(&ckchs_tree, &store->node);
12448 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12449
12450 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12451 return cli_dynmsg(appctx, LOG_NOTICE, err);
12452error:
12453 free(store);
12454 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12455 return cli_dynerr(appctx, err);
12456}
12457
William Lallemand419e6342020-04-08 12:05:39 +020012458/* parsing function of 'del ssl cert' */
12459static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12460{
12461 struct ckch_store *store;
12462 char *err = NULL;
12463 char *filename;
12464
12465 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12466 return 1;
12467
12468 if (!*args[3])
12469 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12470
12471 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12472 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12473
12474 filename = args[3];
12475
12476 store = ckchs_lookup(filename);
12477 if (store == NULL) {
12478 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12479 goto error;
12480 }
12481 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12482 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12483 goto error;
12484 }
12485
12486 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020012487 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020012488
12489 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12490
12491 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12492 return cli_dynmsg(appctx, LOG_NOTICE, err);
12493
12494error:
12495 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12496 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12497 return cli_dynerr(appctx, err);
12498}
12499
12500
12501
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012502static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012503{
12504#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12505 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012506 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012507
12508 if (!payload)
12509 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012510
12511 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012512 if (!*payload)
12513 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012514
12515 /* remove \r and \n from the payload */
12516 for (i = 0, j = 0; payload[i]; i++) {
12517 if (payload[i] == '\r' || payload[i] == '\n')
12518 continue;
12519 payload[j++] = payload[i];
12520 }
12521 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012522
Willy Tarreau1c913e42018-08-22 05:26:57 +020012523 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012524 if (ret < 0)
12525 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012526
Willy Tarreau1c913e42018-08-22 05:26:57 +020012527 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012528 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012529 if (err)
12530 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12531 else
12532 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012533 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012534
12535 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012536#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012537 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 +020012538#endif
12539
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012540}
12541
Willy Tarreau86a394e2019-05-09 14:15:32 +020012542#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012543static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12544{
12545 switch (arg->type) {
12546 case ARGT_STR:
12547 smp->data.type = SMP_T_STR;
12548 smp->data.u.str = arg->data.str;
12549 return 1;
12550 case ARGT_VAR:
12551 if (!vars_get_by_desc(&arg->data.var, smp))
12552 return 0;
12553 if (!sample_casts[smp->data.type][SMP_T_STR])
12554 return 0;
12555 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12556 return 0;
12557 return 1;
12558 default:
12559 return 0;
12560 }
12561}
12562
12563static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12564 const char *file, int line, char **err)
12565{
12566 switch(args[0].data.sint) {
12567 case 128:
12568 case 192:
12569 case 256:
12570 break;
12571 default:
12572 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12573 return 0;
12574 }
12575 /* Try to decode a variable. */
12576 vars_check_arg(&args[1], NULL);
12577 vars_check_arg(&args[2], NULL);
12578 vars_check_arg(&args[3], NULL);
12579 return 1;
12580}
12581
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012582/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012583static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12584{
12585 struct sample nonce, key, aead_tag;
12586 struct buffer *smp_trash, *smp_trash_alloc;
12587 EVP_CIPHER_CTX *ctx;
12588 int dec_size, ret;
12589
12590 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12591 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12592 return 0;
12593
12594 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12595 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12596 return 0;
12597
12598 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12599 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12600 return 0;
12601
12602 smp_trash = get_trash_chunk();
12603 smp_trash_alloc = alloc_trash_chunk();
12604 if (!smp_trash_alloc)
12605 return 0;
12606
12607 ctx = EVP_CIPHER_CTX_new();
12608
12609 if (!ctx)
12610 goto err;
12611
12612 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12613 if (dec_size < 0)
12614 goto err;
12615 smp_trash->data = dec_size;
12616
12617 /* Set cipher type and mode */
12618 switch(arg_p[0].data.sint) {
12619 case 128:
12620 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12621 break;
12622 case 192:
12623 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12624 break;
12625 case 256:
12626 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12627 break;
12628 }
12629
12630 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12631
12632 /* Initialise IV */
12633 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12634 goto err;
12635
12636 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12637 if (dec_size < 0)
12638 goto err;
12639 smp_trash->data = dec_size;
12640
12641 /* Initialise key */
12642 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12643 goto err;
12644
12645 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12646 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12647 goto err;
12648
12649 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12650 if (dec_size < 0)
12651 goto err;
12652 smp_trash_alloc->data = dec_size;
12653 dec_size = smp_trash->data;
12654
12655 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12656 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12657
12658 if (ret <= 0)
12659 goto err;
12660
12661 smp->data.u.str.data = dec_size + smp_trash->data;
12662 smp->data.u.str.area = smp_trash->area;
12663 smp->data.type = SMP_T_BIN;
12664 smp->flags &= ~SMP_F_CONST;
12665 free_trash_chunk(smp_trash_alloc);
12666 return 1;
12667
12668err:
12669 free_trash_chunk(smp_trash_alloc);
12670 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012671}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012672# endif
William Lallemand32af2032016-10-29 18:09:35 +020012673
Elliot Otchet71f82972020-01-15 08:12:14 -050012674/* Argument validation functions */
12675
12676/* This function is used to validate the arguments passed to any "x_dn" ssl
12677 * keywords. These keywords support specifying a third parameter that must be
12678 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12679 */
12680int val_dnfmt(struct arg *arg, char **err_msg)
12681{
12682 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12683 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12684 return 0;
12685 }
12686 return 1;
12687}
12688
William Lallemand32af2032016-10-29 18:09:35 +020012689/* register cli keywords */
12690static struct cli_kw_list cli_kws = {{ },{
12691#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12692 { { "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 +020012693 { { "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 +020012694#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012695 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012696 { { "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 +010012697 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12698 { { "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 +010012699 { { "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 +020012700 { { "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 +010012701 { { "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 +010012702 { { "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 +020012703 { { "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 +020012704 { { "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 +020012705 { { NULL }, NULL, NULL, NULL }
12706}};
12707
Willy Tarreau0108d902018-11-25 19:14:37 +010012708INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012709
Willy Tarreau7875d092012-09-10 08:20:03 +020012710/* Note: must not be declared <const> as its list will be overwritten.
12711 * Please take care of keeping this list alphabetically sorted.
12712 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012713static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012714 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012715 { "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 +010012716#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012717 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012718#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012719 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012720#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12721 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12722#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012723 { "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 +020012724 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012725 { "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 +020012726 { "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 +020012727#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012728 { "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 -040012729#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012730#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012731 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12732 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012733 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12734#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012735 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12736 { "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 +010012737 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012738 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012739 { "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 +020012740 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12741 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12742 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12743 { "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 -050012744 { "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 +020012745 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12746 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012747 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012748 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12749 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012750 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012751 { "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 +020012752 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12753 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12754 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12755 { "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 -050012756 { "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 +020012757 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012758 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012759 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012760 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012761 { "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 +010012762 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012763 { "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 +020012764 { "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 +010012765 { "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 +020012766 { "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 +010012767#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012768 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012769#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012770#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012771 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012772#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012773 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012774#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012775 { "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 -040012776#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012777 { "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 +020012778#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012779 { "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 -040012780#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012781#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012782 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12783 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012784 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12785#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012786#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012787 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012788#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012789 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12790 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12791 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12792 { "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 +020012793 { NULL, NULL, 0, 0, 0 },
12794}};
12795
Willy Tarreau0108d902018-11-25 19:14:37 +010012796INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12797
Willy Tarreau7875d092012-09-10 08:20:03 +020012798/* Note: must not be declared <const> as its list will be overwritten.
12799 * Please take care of keeping this list alphabetically sorted.
12800 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012801static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012802 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12803 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012804 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012805}};
12806
Willy Tarreau0108d902018-11-25 19:14:37 +010012807INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12808
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012809/* Note: must not be declared <const> as its list will be overwritten.
12810 * Please take care of keeping this list alphabetically sorted, doing so helps
12811 * all code contributors.
12812 * Optional keywords are also declared with a NULL ->parse() function so that
12813 * the config parser can report an appropriate error when a known keyword was
12814 * not enabled.
12815 */
William Lallemand557823f2020-04-01 17:42:47 +020012816
12817/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12818 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012819static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012820 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012821 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012822 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12823 { "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 +010012824 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012825#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012826 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12827#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012828 { "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 +010012829 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012830 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012831 { "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 +010012832 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012833 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12834 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012835 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12836 { NULL, NULL, 0 },
12837};
12838
Willy Tarreau0108d902018-11-25 19:14:37 +010012839/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12840
Willy Tarreau51fb7652012-09-18 18:24:39 +020012841static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012842 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012843 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012844 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12845 { "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 +020012846 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12847 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12848 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12849 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012850#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012851 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12852#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012853 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012854 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012855 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012856 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12857 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12858 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12859 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12860 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12861 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12862 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012863 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012864 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012865 { "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 +020012866 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12867 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12868 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12869 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012870 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012871 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12872 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012873 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12874 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012875 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12876 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12877 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12878 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12879 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012880 { NULL, NULL, 0 },
12881}};
Emeric Brun46591952012-05-18 15:47:34 +020012882
Willy Tarreau0108d902018-11-25 19:14:37 +010012883INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12884
Willy Tarreau92faadf2012-10-10 23:04:25 +020012885/* Note: must not be declared <const> as its list will be overwritten.
12886 * Please take care of keeping this list alphabetically sorted, doing so helps
12887 * all code contributors.
12888 * Optional keywords are also declared with a NULL ->parse() function so that
12889 * the config parser can report an appropriate error when a known keyword was
12890 * not enabled.
12891 */
12892static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012893 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012894 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012895 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012896 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012897 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012898 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12899 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012900#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012901 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12902#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012903 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12904 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12905 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12906 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12907 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12908 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12909 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12910 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12911 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12912 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12913 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12914 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12915 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12916 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12917 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12918 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12919 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12920 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012921 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012922 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12923 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12924 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12925 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12926 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12927 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12928 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12929 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12930 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12931 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012932 { NULL, NULL, 0, 0 },
12933}};
12934
Willy Tarreau0108d902018-11-25 19:14:37 +010012935INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12936
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012937static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012938 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12939 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012940 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012941 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012942 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12943 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012944#ifndef OPENSSL_NO_DH
12945 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12946#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012947 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012948#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012949 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012950#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010012951 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12952#ifndef OPENSSL_NO_DH
12953 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12954#endif
12955 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12956 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12957 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12958 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012959 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012960 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
12961 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012962#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012963 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
12964 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
12965#endif
William Lallemand3af48e72020-02-03 17:15:52 +010012966 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012967 { 0, NULL, NULL },
12968}};
12969
Willy Tarreau0108d902018-11-25 19:14:37 +010012970INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
12971
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012972/* Note: must not be declared <const> as its list will be overwritten */
12973static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020012974#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012975 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
12976#endif
12977 { NULL, NULL, 0, 0, 0 },
12978}};
12979
12980INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
12981
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020012982/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010012983static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020012984 .snd_buf = ssl_sock_from_buf,
12985 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010012986 .subscribe = ssl_subscribe,
12987 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020012988 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020012989 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020012990 .rcv_pipe = NULL,
12991 .snd_pipe = NULL,
12992 .shutr = NULL,
12993 .shutw = ssl_sock_shutw,
12994 .close = ssl_sock_close,
12995 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010012996 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010012997 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010012998 .prepare_srv = ssl_sock_prepare_srv_ctx,
12999 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013000 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013001 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013002};
13003
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013004enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13005 struct session *sess, struct stream *s, int flags)
13006{
13007 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013008 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013009
13010 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013011 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013012
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013013 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013014 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013015 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013016 s->req.flags |= CF_READ_NULL;
13017 return ACT_RET_YIELD;
13018 }
13019 }
13020 return (ACT_RET_CONT);
13021}
13022
13023static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13024{
13025 rule->action_ptr = ssl_action_wait_for_hs;
13026
13027 return ACT_RET_PRS_OK;
13028}
13029
13030static struct action_kw_list http_req_actions = {ILH, {
13031 { "wait-for-handshake", ssl_parse_wait_for_hs },
13032 { /* END */ }
13033}};
13034
Willy Tarreau0108d902018-11-25 19:14:37 +010013035INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13036
Willy Tarreau5db847a2019-05-09 14:13:35 +020013037#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013038
13039static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13040{
13041 if (ptr) {
13042 chunk_destroy(ptr);
13043 free(ptr);
13044 }
13045}
13046
13047#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013048static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13049{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013050 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013051}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013052
Emeric Brun46591952012-05-18 15:47:34 +020013053__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013054static void __ssl_sock_init(void)
13055{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013056#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013057 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013058 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013059#endif
Emeric Brun46591952012-05-18 15:47:34 +020013060
Willy Tarreauef934602016-12-22 23:12:01 +010013061 if (global_ssl.listen_default_ciphers)
13062 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13063 if (global_ssl.connect_default_ciphers)
13064 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013065#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013066 if (global_ssl.listen_default_ciphersuites)
13067 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13068 if (global_ssl.connect_default_ciphersuites)
13069 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13070#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013071
Willy Tarreau13e14102016-12-22 20:25:26 +010013072 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013073#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013074 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013075#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013076#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013077 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013078 n = sk_SSL_COMP_num(cm);
13079 while (n--) {
13080 (void) sk_SSL_COMP_pop(cm);
13081 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013082#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013083
Willy Tarreau5db847a2019-05-09 14:13:35 +020013084#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013085 ssl_locking_init();
13086#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013087#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013088 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13089#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013090 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013091 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 +020013092#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013093 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013094 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013095#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013096#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13097 hap_register_post_check(tlskeys_finalize_config);
13098#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013099
13100 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13101 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13102
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013103 hap_register_post_deinit(ssl_free_global_issuers);
13104
Willy Tarreau80713382018-11-26 10:19:54 +010013105#ifndef OPENSSL_NO_DH
13106 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13107 hap_register_post_deinit(ssl_free_dh);
13108#endif
13109#ifndef OPENSSL_NO_ENGINE
13110 hap_register_post_deinit(ssl_free_engines);
13111#endif
13112 /* Load SSL string for the verbose & debug mode. */
13113 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013114 ha_meth = BIO_meth_new(0x666, "ha methods");
13115 BIO_meth_set_write(ha_meth, ha_ssl_write);
13116 BIO_meth_set_read(ha_meth, ha_ssl_read);
13117 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13118 BIO_meth_set_create(ha_meth, ha_ssl_new);
13119 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13120 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13121 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013122
13123 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013124}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013125
Willy Tarreau80713382018-11-26 10:19:54 +010013126/* Compute and register the version string */
13127static void ssl_register_build_options()
13128{
13129 char *ptr = NULL;
13130 int i;
13131
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013132 memprintf(&ptr, "Built with OpenSSL version : "
13133#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013134 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013135#else /* OPENSSL_IS_BORINGSSL */
13136 OPENSSL_VERSION_TEXT
13137 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013138 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013139 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013140#endif
13141 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013142#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013143 "no (library version too old)"
13144#elif defined(OPENSSL_NO_TLSEXT)
13145 "no (disabled via OPENSSL_NO_TLSEXT)"
13146#else
13147 "yes"
13148#endif
13149 "", ptr);
13150
13151 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13152#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13153 "yes"
13154#else
13155#ifdef OPENSSL_NO_TLSEXT
13156 "no (because of OPENSSL_NO_TLSEXT)"
13157#else
13158 "no (version might be too old, 0.9.8f min needed)"
13159#endif
13160#endif
13161 "", ptr);
13162
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013163 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13164 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13165 if (methodVersions[i].option)
13166 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013167
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013168 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013169}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013170
Willy Tarreau80713382018-11-26 10:19:54 +010013171INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013172
Emeric Brun46591952012-05-18 15:47:34 +020013173
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013174#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013175void ssl_free_engines(void) {
13176 struct ssl_engine_list *wl, *wlb;
13177 /* free up engine list */
13178 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13179 ENGINE_finish(wl->e);
13180 ENGINE_free(wl->e);
13181 LIST_DEL(&wl->list);
13182 free(wl);
13183 }
13184}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013185#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013186
Remi Gacogned3a23c32015-05-28 16:39:47 +020013187#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013188void ssl_free_dh(void) {
13189 if (local_dh_1024) {
13190 DH_free(local_dh_1024);
13191 local_dh_1024 = NULL;
13192 }
13193 if (local_dh_2048) {
13194 DH_free(local_dh_2048);
13195 local_dh_2048 = NULL;
13196 }
13197 if (local_dh_4096) {
13198 DH_free(local_dh_4096);
13199 local_dh_4096 = NULL;
13200 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013201 if (global_dh) {
13202 DH_free(global_dh);
13203 global_dh = NULL;
13204 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013205}
13206#endif
13207
13208__attribute__((destructor))
13209static void __ssl_sock_deinit(void)
13210{
13211#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013212 if (ssl_ctx_lru_tree) {
13213 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013214 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013215 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013216#endif
13217
Willy Tarreau5db847a2019-05-09 14:13:35 +020013218#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013219 ERR_remove_state(0);
13220 ERR_free_strings();
13221
13222 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013223#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013224
Willy Tarreau5db847a2019-05-09 14:13:35 +020013225#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013226 CRYPTO_cleanup_all_ex_data();
13227#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013228 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013229}
13230
13231
Emeric Brun46591952012-05-18 15:47:34 +020013232/*
13233 * Local variables:
13234 * c-indent-level: 8
13235 * c-basic-offset: 8
13236 * End:
13237 */