blob: b1fdb09c534c716f135a6751c93f5dbd33c5fd57 [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
William Lallemand2954c472020-03-06 21:54:13 +010060#include <ebpttree.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020061#include <ebsttree.h>
62
William Lallemand32af2032016-10-29 18:09:35 +020063#include <types/applet.h>
64#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020065#include <types/global.h>
66#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020067#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020068
Willy Tarreau7875d092012-09-10 08:20:03 +020069#include <proto/acl.h>
70#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020071#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020072#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020073#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020074#include <proto/fd.h>
75#include <proto/freq_ctr.h>
76#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020077#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020078#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010079#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020080#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020081#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020082#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020083#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020084#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020085#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020086#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020087#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020088#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020089#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010090#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020091
Willy Tarreau9356dac2019-05-10 09:22:53 +020092/* ***** READ THIS before adding code here! *****
93 *
94 * Due to API incompatibilities between multiple OpenSSL versions and their
95 * derivatives, it's often tempting to add macros to (re-)define certain
96 * symbols. Please do not do this here, and do it in common/openssl-compat.h
97 * exclusively so that the whole code consistently uses the same macros.
98 *
99 * Whenever possible if a macro is missing in certain versions, it's better
100 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
101 */
102
Willy Tarreau518cedd2014-02-17 15:43:01 +0100103/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200104#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100105#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100106#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200107#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
108
Emeric Brunf282a812012-09-21 15:27:54 +0200109/* bits 0xFFFF0000 are reserved to store verify errors */
110
111/* Verify errors macros */
112#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
113#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
114#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
115
116#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
117#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
118#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200119
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200120/* ssl_methods flags for ssl options */
121#define MC_SSL_O_ALL 0x0000
122#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
123#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
124#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
125#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200126#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200127
William Lallemand3af48e72020-02-03 17:15:52 +0100128/* file to guess during file loading */
129#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
130#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
131#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
132#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
133#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
William Lallemand4c5adbf2020-02-24 14:23:22 +0100134#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
William Lallemand3af48e72020-02-03 17:15:52 +0100135
William Lallemand4c5adbf2020-02-24 14:23:22 +0100136#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
William Lallemand3af48e72020-02-03 17:15:52 +0100137
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200138/* ssl_methods versions */
139enum {
140 CONF_TLSV_NONE = 0,
141 CONF_TLSV_MIN = 1,
142 CONF_SSLV3 = 1,
143 CONF_TLSV10 = 2,
144 CONF_TLSV11 = 3,
145 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200146 CONF_TLSV13 = 5,
147 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200148};
149
Emeric Brun850efd52014-01-29 12:24:34 +0100150/* server and bind verify method, it uses a global value as default */
151enum {
152 SSL_SOCK_VERIFY_DEFAULT = 0,
153 SSL_SOCK_VERIFY_REQUIRED = 1,
154 SSL_SOCK_VERIFY_OPTIONAL = 2,
155 SSL_SOCK_VERIFY_NONE = 3,
156};
157
Willy Tarreau71b734c2014-01-28 15:19:44 +0100158int sslconns = 0;
159int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100160static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100161int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200162
William Lallemande0f3fd52020-02-25 14:53:06 +0100163static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +0100164static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert);
William Lallemande0f3fd52020-02-25 14:53:06 +0100165
Willy Tarreauef934602016-12-22 23:12:01 +0100166static struct {
167 char *crt_base; /* base directory path for certificates */
168 char *ca_base; /* base directory path for CAs and CRLs */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100169 char *issuers_chain_path; /* from "issuers-chain-path" */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100170
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000171 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100172
173 char *listen_default_ciphers;
174 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200175#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200176 char *listen_default_ciphersuites;
177 char *connect_default_ciphersuites;
178#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100179 int listen_default_ssloptions;
180 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200181 struct tls_version_filter listen_default_sslmethods;
182 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100183
184 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
185 unsigned int life_time; /* SSL session lifetime in seconds */
186 unsigned int max_record; /* SSL max record size */
187 unsigned int default_dh_param; /* SSL maximum DH parameter size */
188 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100189 int capture_cipherlist; /* Size of the cipherlist buffer. */
William Lallemand3af48e72020-02-03 17:15:52 +0100190 int extra_files; /* which files not defined in the configuration file are we looking for */
Willy Tarreauef934602016-12-22 23:12:01 +0100191} global_ssl = {
192#ifdef LISTEN_DEFAULT_CIPHERS
193 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
194#endif
195#ifdef CONNECT_DEFAULT_CIPHERS
196 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
197#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200198#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200199#ifdef LISTEN_DEFAULT_CIPHERSUITES
200 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
201#endif
202#ifdef CONNECT_DEFAULT_CIPHERSUITES
203 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
204#endif
205#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100206 .listen_default_ssloptions = BC_SSL_O_NONE,
207 .connect_default_ssloptions = SRV_SSL_O_NONE,
208
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200209 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
210 .listen_default_sslmethods.min = CONF_TLSV_NONE,
211 .listen_default_sslmethods.max = CONF_TLSV_NONE,
212 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
213 .connect_default_sslmethods.min = CONF_TLSV_NONE,
214 .connect_default_sslmethods.max = CONF_TLSV_NONE,
215
Willy Tarreauef934602016-12-22 23:12:01 +0100216#ifdef DEFAULT_SSL_MAX_RECORD
217 .max_record = DEFAULT_SSL_MAX_RECORD,
218#endif
219 .default_dh_param = SSL_DEFAULT_DH_PARAM,
220 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100221 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100222 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100223};
224
Olivier Houcharda8955d52019-04-07 22:00:38 +0200225static BIO_METHOD *ha_meth;
226
Olivier Houchard66ab4982019-02-26 18:37:15 +0100227struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200228 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100229 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200230 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200231 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100232 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200233 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100234 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100235 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100236 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100237 int sent_early_data; /* Amount of early data we sent so far */
238
Olivier Houchard66ab4982019-02-26 18:37:15 +0100239};
240
241DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
242
Olivier Houchardea8dd942019-05-20 14:02:16 +0200243static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200244static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200245
Olivier Houcharda8955d52019-04-07 22:00:38 +0200246/* Methods to implement OpenSSL BIO */
247static int ha_ssl_write(BIO *h, const char *buf, int num)
248{
249 struct buffer tmpbuf;
250 struct ssl_sock_ctx *ctx;
251 int ret;
252
253 ctx = BIO_get_data(h);
254 tmpbuf.size = num;
255 tmpbuf.area = (void *)(uintptr_t)buf;
256 tmpbuf.data = num;
257 tmpbuf.head = 0;
258 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200259 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200260 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200261 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200262 } else if (ret == 0)
263 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200264 return ret;
265}
266
267static int ha_ssl_gets(BIO *h, char *buf, int size)
268{
269
270 return 0;
271}
272
273static int ha_ssl_puts(BIO *h, const char *str)
274{
275
276 return ha_ssl_write(h, str, strlen(str));
277}
278
279static int ha_ssl_read(BIO *h, char *buf, int size)
280{
281 struct buffer tmpbuf;
282 struct ssl_sock_ctx *ctx;
283 int ret;
284
285 ctx = BIO_get_data(h);
286 tmpbuf.size = size;
287 tmpbuf.area = buf;
288 tmpbuf.data = 0;
289 tmpbuf.head = 0;
290 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200291 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200292 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200293 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200294 } else if (ret == 0)
295 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200296
297 return ret;
298}
299
300static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
301{
302 int ret = 0;
303 switch (cmd) {
304 case BIO_CTRL_DUP:
305 case BIO_CTRL_FLUSH:
306 ret = 1;
307 break;
308 }
309 return ret;
310}
311
312static int ha_ssl_new(BIO *h)
313{
314 BIO_set_init(h, 1);
315 BIO_set_data(h, NULL);
316 BIO_clear_flags(h, ~0);
317 return 1;
318}
319
320static int ha_ssl_free(BIO *data)
321{
322
323 return 1;
324}
325
326
Willy Tarreau5db847a2019-05-09 14:13:35 +0200327#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100328
Emeric Brun821bb9b2017-06-15 16:37:39 +0200329static HA_RWLOCK_T *ssl_rwlocks;
330
331
332unsigned long ssl_id_function(void)
333{
334 return (unsigned long)tid;
335}
336
337void ssl_locking_function(int mode, int n, const char * file, int line)
338{
339 if (mode & CRYPTO_LOCK) {
340 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200342 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200344 }
345 else {
346 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200348 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100349 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200350 }
351}
352
353static int ssl_locking_init(void)
354{
355 int i;
356
357 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
358 if (!ssl_rwlocks)
359 return -1;
360
361 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100362 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200363
364 CRYPTO_set_id_callback(ssl_id_function);
365 CRYPTO_set_locking_callback(ssl_locking_function);
366
367 return 0;
368}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100369
Emeric Brun821bb9b2017-06-15 16:37:39 +0200370#endif
371
William Lallemand150bfa82019-09-19 17:12:49 +0200372__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200373
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100374/* Uncommitted CKCH transaction */
375
376static struct {
377 struct ckch_store *new_ckchs;
378 struct ckch_store *old_ckchs;
379 char *path;
380} ckchs_transaction;
381
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200382/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100383 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200384 */
385struct cafile_entry {
386 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200387 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200388 struct ebmb_node node;
389 char path[0];
390};
391
392static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
393
394static X509_STORE* ssl_store_get0_locations_file(char *path)
395{
396 struct ebmb_node *eb;
397
398 eb = ebst_lookup(&cafile_tree, path);
399 if (eb) {
400 struct cafile_entry *ca_e;
401 ca_e = ebmb_entry(eb, struct cafile_entry, node);
402 return ca_e->ca_store;
403 }
404 return NULL;
405}
406
407static int ssl_store_load_locations_file(char *path)
408{
409 if (ssl_store_get0_locations_file(path) == NULL) {
410 struct cafile_entry *ca_e;
411 X509_STORE *store = X509_STORE_new();
412 if (X509_STORE_load_locations(store, path, NULL)) {
413 int pathlen;
414 pathlen = strlen(path);
415 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
416 if (ca_e) {
417 memcpy(ca_e->path, path, pathlen + 1);
418 ca_e->ca_store = store;
419 ebst_insert(&cafile_tree, &ca_e->node);
420 return 1;
421 }
422 }
423 X509_STORE_free(store);
424 return 0;
425 }
426 return 1;
427}
428
429/* mimic what X509_STORE_load_locations do with store_ctx */
430static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
431{
432 X509_STORE *store;
433 store = ssl_store_get0_locations_file(path);
434 if (store_ctx && store) {
435 int i;
436 X509_OBJECT *obj;
437 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
438 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
439 obj = sk_X509_OBJECT_value(objs, i);
440 switch (X509_OBJECT_get_type(obj)) {
441 case X509_LU_X509:
442 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
443 break;
444 case X509_LU_CRL:
445 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
446 break;
447 default:
448 break;
449 }
450 }
451 return 1;
452 }
453 return 0;
454}
455
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500456/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200457static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
458{
459 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
460 return ssl_set_cert_crl_file(store_ctx, path);
461}
462
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200463/*
464 Extract CA_list from CA_file already in tree.
465 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
466 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
467*/
468static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
469{
470 struct ebmb_node *eb;
471 struct cafile_entry *ca_e;
472
473 eb = ebst_lookup(&cafile_tree, path);
474 if (!eb)
475 return NULL;
476 ca_e = ebmb_entry(eb, struct cafile_entry, node);
477
478 if (ca_e->ca_list == NULL) {
479 int i;
480 unsigned long key;
481 struct eb_root ca_name_tree = EB_ROOT;
482 struct eb64_node *node, *back;
483 struct {
484 struct eb64_node node;
485 X509_NAME *xname;
486 } *ca_name;
487 STACK_OF(X509_OBJECT) *objs;
488 STACK_OF(X509_NAME) *skn;
489 X509 *x;
490 X509_NAME *xn;
491
492 skn = sk_X509_NAME_new_null();
493 /* take x509 from cafile_tree */
494 objs = X509_STORE_get0_objects(ca_e->ca_store);
495 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
496 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
497 if (!x)
498 continue;
499 xn = X509_get_subject_name(x);
500 if (!xn)
501 continue;
502 /* Check for duplicates. */
503 key = X509_NAME_hash(xn);
504 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
505 node && ca_name == NULL;
506 node = eb64_next(node)) {
507 ca_name = container_of(node, typeof(*ca_name), node);
508 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
509 ca_name = NULL;
510 }
511 /* find a duplicate */
512 if (ca_name)
513 continue;
514 ca_name = calloc(1, sizeof *ca_name);
515 xn = X509_NAME_dup(xn);
516 if (!ca_name ||
517 !xn ||
518 !sk_X509_NAME_push(skn, xn)) {
519 free(ca_name);
520 X509_NAME_free(xn);
521 sk_X509_NAME_pop_free(skn, X509_NAME_free);
522 sk_X509_NAME_free(skn);
523 skn = NULL;
524 break;
525 }
526 ca_name->node.key = key;
527 ca_name->xname = xn;
528 eb64_insert(&ca_name_tree, &ca_name->node);
529 }
530 ca_e->ca_list = skn;
531 /* remove temporary ca_name tree */
532 node = eb64_first(&ca_name_tree);
533 while (node) {
534 ca_name = container_of(node, typeof(*ca_name), node);
535 back = eb64_next(node);
536 eb64_delete(node);
537 free(ca_name);
538 node = back;
539 }
540 }
541 return ca_e->ca_list;
542}
543
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100544/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100545struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100546 unsigned long long int xxh64;
547 unsigned char ciphersuite_len;
548 char ciphersuite[0];
549};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100550struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100551static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200552static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100553
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200554#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
555struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
556#endif
557
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200558#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000559static unsigned int openssl_engines_initialized;
560struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
561struct ssl_engine_list {
562 struct list list;
563 ENGINE *e;
564};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200565#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000566
Remi Gacogne8de54152014-07-15 11:36:40 +0200567#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200568static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200569static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200570static DH *local_dh_1024 = NULL;
571static DH *local_dh_2048 = NULL;
572static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100573static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200574#endif /* OPENSSL_NO_DH */
575
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100576#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200577/* X509V3 Extensions that will be added on generated certificates */
578#define X509V3_EXT_SIZE 5
579static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
580 "basicConstraints",
581 "nsComment",
582 "subjectKeyIdentifier",
583 "authorityKeyIdentifier",
584 "keyUsage",
585};
586static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
587 "CA:FALSE",
588 "\"OpenSSL Generated Certificate\"",
589 "hash",
590 "keyid,issuer:always",
591 "nonRepudiation,digitalSignature,keyEncipherment"
592};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200593/* LRU cache to store generated certificate */
594static struct lru64_head *ssl_ctx_lru_tree = NULL;
595static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200596static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100597__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200598
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200599#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
600
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100601static struct ssl_bind_kw ssl_bind_kws[];
602
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200603#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500604/* The order here matters for picking a default context,
605 * keep the most common keytype at the bottom of the list
606 */
607const char *SSL_SOCK_KEYTYPE_NAMES[] = {
608 "dsa",
609 "ecdsa",
610 "rsa"
611};
612#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100613#else
614#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500615#endif
616
William Lallemandc3cd35f2017-11-28 11:04:43 +0100617static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100618static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
619
620#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
621
622#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
623 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
624
625#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
626 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200627
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100628/*
629 * This function gives the detail of the SSL error. It is used only
630 * if the debug mode and the verbose mode are activated. It dump all
631 * the SSL error until the stack was empty.
632 */
633static forceinline void ssl_sock_dump_errors(struct connection *conn)
634{
635 unsigned long ret;
636
637 if (unlikely(global.mode & MODE_DEBUG)) {
638 while(1) {
639 ret = ERR_get_error();
640 if (ret == 0)
641 return;
642 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200643 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100644 ERR_func_error_string(ret), ERR_reason_error_string(ret));
645 }
646 }
647}
648
yanbzhube2774d2015-12-10 15:07:30 -0500649
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200650#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000651static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
652{
653 int err_code = ERR_ABORT;
654 ENGINE *engine;
655 struct ssl_engine_list *el;
656
657 /* grab the structural reference to the engine */
658 engine = ENGINE_by_id(engine_id);
659 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100660 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000661 goto fail_get;
662 }
663
664 if (!ENGINE_init(engine)) {
665 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100666 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000667 goto fail_init;
668 }
669
670 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100671 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000672 goto fail_set_method;
673 }
674
675 el = calloc(1, sizeof(*el));
676 el->e = engine;
677 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100678 nb_engines++;
679 if (global_ssl.async)
680 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000681 return 0;
682
683fail_set_method:
684 /* release the functional reference from ENGINE_init() */
685 ENGINE_finish(engine);
686
687fail_init:
688 /* release the structural reference from ENGINE_by_id() */
689 ENGINE_free(engine);
690
691fail_get:
692 return err_code;
693}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200694#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000695
Willy Tarreau5db847a2019-05-09 14:13:35 +0200696#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200697/*
698 * openssl async fd handler
699 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200700void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000701{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200702 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000703
Emeric Brun3854e012017-05-17 20:42:48 +0200704 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000705 * to poll this fd until it is requested
706 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000707 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000708 fd_cant_recv(fd);
709
710 /* crypto engine is available, let's notify the associated
711 * connection that it can pursue its processing.
712 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200713 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000714}
715
Emeric Brun3854e012017-05-17 20:42:48 +0200716/*
717 * openssl async delayed SSL_free handler
718 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200719void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000720{
721 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200722 OSSL_ASYNC_FD all_fd[32];
723 size_t num_all_fds = 0;
724 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000725
Emeric Brun3854e012017-05-17 20:42:48 +0200726 /* We suppose that the async job for a same SSL *
727 * are serialized. So if we are awake it is
728 * because the running job has just finished
729 * and we can remove all async fds safely
730 */
731 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
732 if (num_all_fds > 32) {
733 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
734 return;
735 }
736
737 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
738 for (i=0 ; i < num_all_fds ; i++)
739 fd_remove(all_fd[i]);
740
741 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000742 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100743 _HA_ATOMIC_SUB(&sslconns, 1);
744 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000745}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000746/*
Emeric Brun3854e012017-05-17 20:42:48 +0200747 * function used to manage a returned SSL_ERROR_WANT_ASYNC
748 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000749 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200750static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000751{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100752 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200753 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200754 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000755 size_t num_add_fds = 0;
756 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200757 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000758
759 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
760 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200761 if (num_add_fds > 32 || num_del_fds > 32) {
762 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000763 return;
764 }
765
Emeric Brun3854e012017-05-17 20:42:48 +0200766 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000767
Emeric Brun3854e012017-05-17 20:42:48 +0200768 /* We remove unused fds from the fdtab */
769 for (i=0 ; i < num_del_fds ; i++)
770 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000771
Emeric Brun3854e012017-05-17 20:42:48 +0200772 /* We add new fds to the fdtab */
773 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200774 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000775 }
776
Emeric Brun3854e012017-05-17 20:42:48 +0200777 num_add_fds = 0;
778 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
779 if (num_add_fds > 32) {
780 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
781 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000782 }
Emeric Brun3854e012017-05-17 20:42:48 +0200783
784 /* We activate the polling for all known async fds */
785 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000786 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200787 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000788 /* To ensure that the fd cache won't be used
789 * We'll prefer to catch a real RD event
790 * because handling an EAGAIN on this fd will
791 * result in a context switch and also
792 * some engines uses a fd in blocking mode.
793 */
794 fd_cant_recv(add_fd[i]);
795 }
Emeric Brun3854e012017-05-17 20:42:48 +0200796
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000797}
798#endif
799
William Lallemand104a7a62019-10-14 14:14:59 +0200800#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200801/*
802 * This function returns the number of seconds elapsed
803 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
804 * date presented un ASN1_GENERALIZEDTIME.
805 *
806 * In parsing error case, it returns -1.
807 */
808static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
809{
810 long epoch;
811 char *p, *end;
812 const unsigned short month_offset[12] = {
813 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
814 };
815 int year, month;
816
817 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
818
819 p = (char *)d->data;
820 end = p + d->length;
821
822 if (end - p < 4) return -1;
823 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
824 p += 4;
825 if (end - p < 2) return -1;
826 month = 10 * (p[0] - '0') + p[1] - '0';
827 if (month < 1 || month > 12) return -1;
828 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
829 We consider leap years and the current month (<marsh or not) */
830 epoch = ( ((year - 1970) * 365)
831 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
832 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
833 + month_offset[month-1]
834 ) * 24 * 60 * 60;
835 p += 2;
836 if (end - p < 2) return -1;
837 /* Add the number of seconds of completed days of current month */
838 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
839 p += 2;
840 if (end - p < 2) return -1;
841 /* Add the completed hours of the current day */
842 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
843 p += 2;
844 if (end - p < 2) return -1;
845 /* Add the completed minutes of the current hour */
846 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
847 p += 2;
848 if (p == end) return -1;
849 /* Test if there is available seconds */
850 if (p[0] < '0' || p[0] > '9')
851 goto nosec;
852 if (end - p < 2) return -1;
853 /* Add the seconds of the current minute */
854 epoch += 10 * (p[0] - '0') + p[1] - '0';
855 p += 2;
856 if (p == end) return -1;
857 /* Ignore seconds float part if present */
858 if (p[0] == '.') {
859 do {
860 if (++p == end) return -1;
861 } while (p[0] >= '0' && p[0] <= '9');
862 }
863
864nosec:
865 if (p[0] == 'Z') {
866 if (end - p != 1) return -1;
867 return epoch;
868 }
869 else if (p[0] == '+') {
870 if (end - p != 5) return -1;
871 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700872 return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200873 }
874 else if (p[0] == '-') {
875 if (end - p != 5) return -1;
876 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700877 return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200878 }
879
880 return -1;
881}
882
William Lallemand104a7a62019-10-14 14:14:59 +0200883/*
884 * struct alignment works here such that the key.key is the same as key_data
885 * Do not change the placement of key_data
886 */
887struct certificate_ocsp {
888 struct ebmb_node key;
889 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
890 struct buffer response;
891 long expire;
892};
893
894struct ocsp_cbk_arg {
895 int is_single;
896 int single_kt;
897 union {
898 struct certificate_ocsp *s_ocsp;
899 /*
900 * m_ocsp will have multiple entries dependent on key type
901 * Entry 0 - DSA
902 * Entry 1 - ECDSA
903 * Entry 2 - RSA
904 */
905 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
906 };
907};
908
Emeric Brun1d3865b2014-06-20 15:37:32 +0200909static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200910
911/* This function starts to check if the OCSP response (in DER format) contained
912 * in chunk 'ocsp_response' is valid (else exits on error).
913 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
914 * contained in the OCSP Response and exits on error if no match.
915 * If it's a valid OCSP Response:
916 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
917 * pointed by 'ocsp'.
918 * If 'ocsp' is NULL, the function looks up into the OCSP response's
919 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
920 * from the response) and exits on error if not found. Finally, If an OCSP response is
921 * already present in the container, it will be overwritten.
922 *
923 * Note: OCSP response containing more than one OCSP Single response is not
924 * considered valid.
925 *
926 * Returns 0 on success, 1 in error case.
927 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200928static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
929 struct certificate_ocsp *ocsp,
930 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200931{
932 OCSP_RESPONSE *resp;
933 OCSP_BASICRESP *bs = NULL;
934 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200935 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200936 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200937 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200938 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200939 int reason;
940 int ret = 1;
941
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200942 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
943 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200944 if (!resp) {
945 memprintf(err, "Unable to parse OCSP response");
946 goto out;
947 }
948
949 rc = OCSP_response_status(resp);
950 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
951 memprintf(err, "OCSP response status not successful");
952 goto out;
953 }
954
955 bs = OCSP_response_get1_basic(resp);
956 if (!bs) {
957 memprintf(err, "Failed to get basic response from OCSP Response");
958 goto out;
959 }
960
961 count_sr = OCSP_resp_count(bs);
962 if (count_sr > 1) {
963 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
964 goto out;
965 }
966
967 sr = OCSP_resp_get0(bs, 0);
968 if (!sr) {
969 memprintf(err, "Failed to get OCSP single response");
970 goto out;
971 }
972
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200973 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
974
Emeric Brun4147b2e2014-06-16 18:36:30 +0200975 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200976 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200977 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200978 goto out;
979 }
980
Emeric Brun13a6b482014-06-20 15:44:34 +0200981 if (!nextupd) {
982 memprintf(err, "OCSP single response: missing nextupdate");
983 goto out;
984 }
985
Emeric Brunc8b27b62014-06-19 14:16:17 +0200986 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200987 if (!rc) {
988 memprintf(err, "OCSP single response: no longer valid.");
989 goto out;
990 }
991
992 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200993 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200994 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
995 goto out;
996 }
997 }
998
999 if (!ocsp) {
1000 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
1001 unsigned char *p;
1002
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001003 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001004 if (!rc) {
1005 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
1006 goto out;
1007 }
1008
1009 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
1010 memprintf(err, "OCSP single response: Certificate ID too long");
1011 goto out;
1012 }
1013
1014 p = key;
1015 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001016 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001017 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1018 if (!ocsp) {
1019 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1020 goto out;
1021 }
1022 }
1023
1024 /* According to comments on "chunk_dup", the
1025 previous chunk buffer will be freed */
1026 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1027 memprintf(err, "OCSP response: Memory allocation error");
1028 goto out;
1029 }
1030
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001031 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1032
Emeric Brun4147b2e2014-06-16 18:36:30 +02001033 ret = 0;
1034out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001035 ERR_clear_error();
1036
Emeric Brun4147b2e2014-06-16 18:36:30 +02001037 if (bs)
1038 OCSP_BASICRESP_free(bs);
1039
1040 if (resp)
1041 OCSP_RESPONSE_free(resp);
1042
1043 return ret;
1044}
1045/*
1046 * External function use to update the OCSP response in the OCSP response's
1047 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1048 * to update in DER format.
1049 *
1050 * Returns 0 on success, 1 in error case.
1051 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001052int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001053{
1054 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1055}
1056
William Lallemand4a660132019-10-14 14:51:41 +02001057#endif
1058
1059#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001060/*
1061 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001062 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001063 *
1064 * Returns 0 on success, 1 in error case.
1065 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001066static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001067{
1068 int fd = -1;
1069 int r = 0;
1070 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001071 struct buffer *ocsp_response;
1072 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001073
William Lallemand3b5f3602019-10-16 18:05:05 +02001074 if (buf) {
1075 int i, j;
1076 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001077
William Lallemand3b5f3602019-10-16 18:05:05 +02001078 /* remove \r and \n from the payload */
1079 for (i = 0, j = 0; buf[i]; i++) {
1080 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001081 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001082 buf[j++] = buf[i];
1083 }
1084 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001085
William Lallemand3b5f3602019-10-16 18:05:05 +02001086 ret = base64dec(buf, j, trash.area, trash.size);
1087 if (ret < 0) {
1088 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001089 goto end;
1090 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001091 trash.data = ret;
1092 src = &trash;
1093 } else {
1094 fd = open(ocsp_path, O_RDONLY);
1095 if (fd == -1) {
1096 memprintf(err, "Error opening OCSP response file");
1097 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001098 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001099
1100 trash.data = 0;
1101 while (trash.data < trash.size) {
1102 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1103 if (r < 0) {
1104 if (errno == EINTR)
1105 continue;
1106
1107 memprintf(err, "Error reading OCSP response from file");
1108 goto end;
1109 }
1110 else if (r == 0) {
1111 break;
1112 }
1113 trash.data += r;
1114 }
1115 close(fd);
1116 fd = -1;
1117 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001118 }
1119
William Lallemand3b5f3602019-10-16 18:05:05 +02001120 ocsp_response = calloc(1, sizeof(*ocsp_response));
1121 if (!chunk_dup(ocsp_response, src)) {
1122 free(ocsp_response);
1123 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001124 goto end;
1125 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001126 /* no error, fill ckch with new context, old context must be free */
1127 if (ckch->ocsp_response) {
1128 free(ckch->ocsp_response->area);
1129 ckch->ocsp_response->area = NULL;
1130 free(ckch->ocsp_response);
1131 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001132 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001133 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001134end:
1135 if (fd != -1)
1136 close(fd);
1137
1138 return ret;
1139}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001140#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001141
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001142#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1143static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc)
1144{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001145 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001146 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001147 struct connection *conn;
1148 int head;
1149 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001150 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001151
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001152 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001153 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001154 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1155
1156 keys = ref->tlskeys;
1157 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001158
1159 if (enc) {
1160 memcpy(key_name, keys[head].name, 16);
1161
1162 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001163 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001164
Emeric Brun9e754772019-01-10 17:51:55 +01001165 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001166
Emeric Brun9e754772019-01-10 17:51:55 +01001167 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1168 goto end;
1169
Willy Tarreau9356dac2019-05-10 09:22:53 +02001170 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001171 ret = 1;
1172 }
1173 else if (ref->key_size_bits == 256 ) {
1174
1175 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1176 goto end;
1177
Willy Tarreau9356dac2019-05-10 09:22:53 +02001178 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001179 ret = 1;
1180 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001181 } else {
1182 for (i = 0; i < TLS_TICKETS_NO; i++) {
1183 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1184 goto found;
1185 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001186 ret = 0;
1187 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001188
Christopher Faulet16f45c82018-02-16 11:23:49 +01001189 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001190 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001191 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001192 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1193 goto end;
1194 /* 2 for key renewal, 1 if current key is still valid */
1195 ret = i ? 2 : 1;
1196 }
1197 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001198 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001199 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1200 goto end;
1201 /* 2 for key renewal, 1 if current key is still valid */
1202 ret = i ? 2 : 1;
1203 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001204 }
Emeric Brun9e754772019-01-10 17:51:55 +01001205
Christopher Faulet16f45c82018-02-16 11:23:49 +01001206 end:
1207 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1208 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001209}
1210
1211struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1212{
1213 struct tls_keys_ref *ref;
1214
1215 list_for_each_entry(ref, &tlskeys_reference, list)
1216 if (ref->filename && strcmp(filename, ref->filename) == 0)
1217 return ref;
1218 return NULL;
1219}
1220
1221struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1222{
1223 struct tls_keys_ref *ref;
1224
1225 list_for_each_entry(ref, &tlskeys_reference, list)
1226 if (ref->unique_id == unique_id)
1227 return ref;
1228 return NULL;
1229}
1230
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001231/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001232 * match existing ones, this function returns -1
1233 * else it returns 0 on success.
1234 */
1235int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001236 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001237{
Emeric Brun9e754772019-01-10 17:51:55 +01001238 if (ref->key_size_bits == 128) {
1239 if (tlskey->data != sizeof(struct tls_sess_key_128))
1240 return -1;
1241 }
1242 else if (ref->key_size_bits == 256) {
1243 if (tlskey->data != sizeof(struct tls_sess_key_256))
1244 return -1;
1245 }
1246 else
1247 return -1;
1248
Christopher Faulet16f45c82018-02-16 11:23:49 +01001249 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001250 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1251 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001252 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1253 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001254
1255 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001256}
1257
Willy Tarreau83061a82018-07-13 11:56:34 +02001258int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001259{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001260 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1261
1262 if(!ref) {
1263 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1264 return 1;
1265 }
Emeric Brun9e754772019-01-10 17:51:55 +01001266 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1267 memprintf(err, "Invalid key size");
1268 return 1;
1269 }
1270
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001271 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001272}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001273
1274/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001275 * automatic ids. It's called just after the basic checks. It returns
1276 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001277 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001278static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001279{
1280 int i = 0;
1281 struct tls_keys_ref *ref, *ref2, *ref3;
1282 struct list tkr = LIST_HEAD_INIT(tkr);
1283
1284 list_for_each_entry(ref, &tlskeys_reference, list) {
1285 if (ref->unique_id == -1) {
1286 /* Look for the first free id. */
1287 while (1) {
1288 list_for_each_entry(ref2, &tlskeys_reference, list) {
1289 if (ref2->unique_id == i) {
1290 i++;
1291 break;
1292 }
1293 }
1294 if (&ref2->list == &tlskeys_reference)
1295 break;
1296 }
1297
1298 /* Uses the unique id and increment it for the next entry. */
1299 ref->unique_id = i;
1300 i++;
1301 }
1302 }
1303
1304 /* This sort the reference list by id. */
1305 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1306 LIST_DEL(&ref->list);
1307 list_for_each_entry(ref3, &tkr, list) {
1308 if (ref->unique_id < ref3->unique_id) {
1309 LIST_ADDQ(&ref3->list, &ref->list);
1310 break;
1311 }
1312 }
1313 if (&ref3->list == &tkr)
1314 LIST_ADDQ(&tkr, &ref->list);
1315 }
1316
1317 /* swap root */
1318 LIST_ADD(&tkr, &tlskeys_reference);
1319 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001320 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001321}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001322#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1323
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001324#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001325int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1326{
1327 switch (evp_keytype) {
1328 case EVP_PKEY_RSA:
1329 return 2;
1330 case EVP_PKEY_DSA:
1331 return 0;
1332 case EVP_PKEY_EC:
1333 return 1;
1334 }
1335
1336 return -1;
1337}
1338
Emeric Brun4147b2e2014-06-16 18:36:30 +02001339/*
1340 * Callback used to set OCSP status extension content in server hello.
1341 */
1342int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1343{
yanbzhube2774d2015-12-10 15:07:30 -05001344 struct certificate_ocsp *ocsp;
1345 struct ocsp_cbk_arg *ocsp_arg;
1346 char *ssl_buf;
1347 EVP_PKEY *ssl_pkey;
1348 int key_type;
1349 int index;
1350
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001351 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001352
1353 ssl_pkey = SSL_get_privatekey(ssl);
1354 if (!ssl_pkey)
1355 return SSL_TLSEXT_ERR_NOACK;
1356
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001357 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001358
1359 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1360 ocsp = ocsp_arg->s_ocsp;
1361 else {
1362 /* For multiple certs per context, we have to find the correct OCSP response based on
1363 * the certificate type
1364 */
1365 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1366
1367 if (index < 0)
1368 return SSL_TLSEXT_ERR_NOACK;
1369
1370 ocsp = ocsp_arg->m_ocsp[index];
1371
1372 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001373
1374 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 !ocsp->response.area ||
1376 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001377 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001378 return SSL_TLSEXT_ERR_NOACK;
1379
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001380 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001381 if (!ssl_buf)
1382 return SSL_TLSEXT_ERR_NOACK;
1383
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001384 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1385 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001386
1387 return SSL_TLSEXT_ERR_OK;
1388}
1389
William Lallemand4a660132019-10-14 14:51:41 +02001390#endif
1391
1392#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001393/*
1394 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001395 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1396 * status extension, the issuer's certificate is mandatory. It should be
1397 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001398 *
William Lallemand246c0242019-10-11 08:59:13 +02001399 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1400 * OCSP response. If file is empty or content is not a valid OCSP response,
1401 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1402 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001403 *
1404 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001405 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001406 */
William Lallemand4a660132019-10-14 14:51:41 +02001407#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001408static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001409{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001410 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001411 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001412 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001413 struct certificate_ocsp *ocsp = NULL, *iocsp;
1414 char *warn = NULL;
1415 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001416 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001417
Emeric Brun4147b2e2014-06-16 18:36:30 +02001418
William Lallemand246c0242019-10-11 08:59:13 +02001419 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001420 if (!x)
1421 goto out;
1422
William Lallemand246c0242019-10-11 08:59:13 +02001423 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001424 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1425 if (chain) {
1426 /* check if one of the certificate of the chain is the issuer */
1427 for (i = 0; i < sk_X509_num(chain); i++) {
1428 X509 *ti = sk_X509_value(chain, i);
1429 if (X509_check_issued(ti, x) == X509_V_OK) {
1430 issuer = ti;
1431 break;
1432 }
1433 }
1434 }
William Lallemand246c0242019-10-11 08:59:13 +02001435 if (!issuer)
1436 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001437
1438 cid = OCSP_cert_to_id(0, x, issuer);
1439 if (!cid)
1440 goto out;
1441
1442 i = i2d_OCSP_CERTID(cid, NULL);
1443 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1444 goto out;
1445
Vincent Bernat02779b62016-04-03 13:48:43 +02001446 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001447 if (!ocsp)
1448 goto out;
1449
1450 p = ocsp->key_data;
1451 i2d_OCSP_CERTID(cid, &p);
1452
1453 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1454 if (iocsp == ocsp)
1455 ocsp = NULL;
1456
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001457#ifndef SSL_CTX_get_tlsext_status_cb
1458# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1459 *cb = (void (*) (void))ctx->tlsext_status_cb;
1460#endif
1461 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1462
1463 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001464 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001465 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001466
1467 cb_arg->is_single = 1;
1468 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001469
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001470 pkey = X509_get_pubkey(x);
1471 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1472 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001473
1474 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1475 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1476 } else {
1477 /*
1478 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1479 * Update that cb_arg with the new cert's staple
1480 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001481 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001482 struct certificate_ocsp *tmp_ocsp;
1483 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001484 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001485 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001486
1487#ifdef SSL_CTX_get_tlsext_status_arg
1488 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1489#else
1490 cb_arg = ctx->tlsext_status_arg;
1491#endif
yanbzhube2774d2015-12-10 15:07:30 -05001492
1493 /*
1494 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1495 * the order of operations below matter, take care when changing it
1496 */
1497 tmp_ocsp = cb_arg->s_ocsp;
1498 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1499 cb_arg->s_ocsp = NULL;
1500 cb_arg->m_ocsp[index] = tmp_ocsp;
1501 cb_arg->is_single = 0;
1502 cb_arg->single_kt = 0;
1503
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001504 pkey = X509_get_pubkey(x);
1505 key_type = EVP_PKEY_base_id(pkey);
1506 EVP_PKEY_free(pkey);
1507
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001508 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001509 if (index >= 0 && !cb_arg->m_ocsp[index])
1510 cb_arg->m_ocsp[index] = iocsp;
1511
1512 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001513
1514 ret = 0;
1515
1516 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001517 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001518 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001519 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001520 }
1521
1522out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001523 if (cid)
1524 OCSP_CERTID_free(cid);
1525
1526 if (ocsp)
1527 free(ocsp);
1528
1529 if (warn)
1530 free(warn);
1531
Emeric Brun4147b2e2014-06-16 18:36:30 +02001532 return ret;
1533}
William Lallemand4a660132019-10-14 14:51:41 +02001534#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001535static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001536{
William Lallemand4a660132019-10-14 14:51:41 +02001537 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001538}
1539#endif
1540
William Lallemand4a660132019-10-14 14:51:41 +02001541#endif
1542
1543
Willy Tarreau5db847a2019-05-09 14:13:35 +02001544#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001545
1546#define CT_EXTENSION_TYPE 18
1547
1548static int sctl_ex_index = -1;
1549
1550/*
1551 * Try to parse Signed Certificate Timestamp List structure. This function
1552 * makes only basic test if the data seems like SCTL. No signature validation
1553 * is performed.
1554 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001555static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001556{
1557 int ret = 1;
1558 int len, pos, sct_len;
1559 unsigned char *data;
1560
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001561 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001562 goto out;
1563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001565 len = (data[0] << 8) | data[1];
1566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001568 goto out;
1569
1570 data = data + 2;
1571 pos = 0;
1572 while (pos < len) {
1573 if (len - pos < 2)
1574 goto out;
1575
1576 sct_len = (data[pos] << 8) | data[pos + 1];
1577 if (pos + sct_len + 2 > len)
1578 goto out;
1579
1580 pos += sct_len + 2;
1581 }
1582
1583 ret = 0;
1584
1585out:
1586 return ret;
1587}
1588
William Lallemand0dfae6c2019-10-16 18:06:58 +02001589/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1590 * It fills the ckch->sctl buffer
1591 * return 0 on success or != 0 on failure */
1592static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001593{
1594 int fd = -1;
1595 int r = 0;
1596 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001597 struct buffer tmp;
1598 struct buffer *src;
1599 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001600
William Lallemand0dfae6c2019-10-16 18:06:58 +02001601 if (buf) {
1602 tmp.area = buf;
1603 tmp.data = strlen(buf);
1604 tmp.size = tmp.data + 1;
1605 src = &tmp;
1606 } else {
1607 fd = open(sctl_path, O_RDONLY);
1608 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001609 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001610
1611 trash.data = 0;
1612 while (trash.data < trash.size) {
1613 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1614 if (r < 0) {
1615 if (errno == EINTR)
1616 continue;
1617 goto end;
1618 }
1619 else if (r == 0) {
1620 break;
1621 }
1622 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001623 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001624 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001625 }
1626
William Lallemand0dfae6c2019-10-16 18:06:58 +02001627 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001628 if (ret)
1629 goto end;
1630
William Lallemand0dfae6c2019-10-16 18:06:58 +02001631 sctl = calloc(1, sizeof(*sctl));
1632 if (!chunk_dup(sctl, src)) {
1633 free(sctl);
1634 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001635 goto end;
1636 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001637 /* no error, fill ckch with new context, old context must be free */
1638 if (ckch->sctl) {
1639 free(ckch->sctl->area);
1640 ckch->sctl->area = NULL;
1641 free(ckch->sctl);
1642 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001643 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001644 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001645end:
1646 if (fd != -1)
1647 close(fd);
1648
1649 return ret;
1650}
1651
1652int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1653{
Willy Tarreau83061a82018-07-13 11:56:34 +02001654 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001655
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001656 *out = (unsigned char *) sctl->area;
1657 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001658
1659 return 1;
1660}
1661
1662int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1663{
1664 return 1;
1665}
1666
William Lallemanda17f4112019-10-10 15:16:44 +02001667static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001668{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001669 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001670
William Lallemanda17f4112019-10-10 15:16:44 +02001671 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL))
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001672 goto out;
1673
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001674 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1675
1676 ret = 0;
1677
1678out:
1679 return ret;
1680}
1681
1682#endif
1683
Emeric Brune1f38db2012-09-03 20:36:47 +02001684void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1685{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001686 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001687 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001688 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001689 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001690
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001691#ifndef SSL_OP_NO_RENEGOTIATION
1692 /* Please note that BoringSSL defines this macro to zero so don't
1693 * change this to #if and do not assign a default value to this macro!
1694 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001695 if (where & SSL_CB_HANDSHAKE_START) {
1696 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001697 if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001698 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001699 conn->err_code = CO_ER_SSL_RENEG;
1700 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001701 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001702#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001703
1704 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001705 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001706 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001707 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001708 consider that the buffering was activated,
1709 so we rise the output buffer size from 4k
1710 to 16k */
1711 write_bio = SSL_get_wbio(ssl);
1712 if (write_bio != SSL_get_rbio(ssl)) {
1713 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001714 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001715 }
1716 }
1717 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001718}
1719
Emeric Brune64aef12012-09-21 13:15:06 +02001720/* Callback is called for each certificate of the chain during a verify
1721 ok is set to 1 if preverify detect no error on current certificate.
1722 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001723int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001724{
1725 SSL *ssl;
1726 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001727 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001728 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001729
1730 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001731 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001732
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001733 ctx = conn->xprt_ctx;
1734
1735 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001736
Emeric Brun81c00f02012-09-21 14:31:21 +02001737 if (ok) /* no errors */
1738 return ok;
1739
1740 depth = X509_STORE_CTX_get_error_depth(x_store);
1741 err = X509_STORE_CTX_get_error(x_store);
1742
1743 /* check if CA error needs to be ignored */
1744 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001745 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1746 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1747 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001748 }
1749
Willy Tarreau731248f2020-02-04 14:02:02 +01001750 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001751 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001752 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001753 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001754 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001755
Willy Tarreau20879a02012-12-03 16:32:10 +01001756 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001757 return 0;
1758 }
1759
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001760 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1761 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001762
Emeric Brun81c00f02012-09-21 14:31:21 +02001763 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001764 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001765 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001766 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001767 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001768 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001769
Willy Tarreau20879a02012-12-03 16:32:10 +01001770 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001771 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001772}
1773
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001774static inline
1775void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001776 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001777{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001778 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001779 unsigned char *msg;
1780 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001781 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001782
1783 /* This function is called for "from client" and "to server"
1784 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001785 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001786 */
1787
1788 /* "write_p" is set to 0 is the bytes are received messages,
1789 * otherwise it is set to 1.
1790 */
1791 if (write_p != 0)
1792 return;
1793
1794 /* content_type contains the type of message received or sent
1795 * according with the SSL/TLS protocol spec. This message is
1796 * encoded with one byte. The value 256 (two bytes) is used
1797 * for designing the SSL/TLS record layer. According with the
1798 * rfc6101, the expected message (other than 256) are:
1799 * - change_cipher_spec(20)
1800 * - alert(21)
1801 * - handshake(22)
1802 * - application_data(23)
1803 * - (255)
1804 * We are interessed by the handshake and specially the client
1805 * hello.
1806 */
1807 if (content_type != 22)
1808 return;
1809
1810 /* The message length is at least 4 bytes, containing the
1811 * message type and the message length.
1812 */
1813 if (len < 4)
1814 return;
1815
1816 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001817 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001818 * - hello_request(0)
1819 * - client_hello(1)
1820 * - server_hello(2)
1821 * - certificate(11)
1822 * - server_key_exchange (12)
1823 * - certificate_request(13)
1824 * - server_hello_done(14)
1825 * We are interested by the client hello.
1826 */
1827 msg = (unsigned char *)buf;
1828 if (msg[0] != 1)
1829 return;
1830
1831 /* Next three bytes are the length of the message. The total length
1832 * must be this decoded length + 4. If the length given as argument
1833 * is not the same, we abort the protocol dissector.
1834 */
1835 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1836 if (len < rec_len + 4)
1837 return;
1838 msg += 4;
1839 end = msg + rec_len;
1840 if (end < msg)
1841 return;
1842
1843 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1844 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001845 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1846 */
1847 msg += 1 + 1 + 4 + 28;
1848 if (msg > end)
1849 return;
1850
1851 /* Next, is session id:
1852 * if present, we have to jump by length + 1 for the size information
1853 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001854 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001855 if (msg[0] > 0)
1856 msg += msg[0];
1857 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001858 if (msg > end)
1859 return;
1860
1861 /* Next two bytes are the ciphersuite length. */
1862 if (msg + 2 > end)
1863 return;
1864 rec_len = (msg[0] << 8) + msg[1];
1865 msg += 2;
1866 if (msg + rec_len > end || msg + rec_len < msg)
1867 return;
1868
Willy Tarreaubafbe012017-11-24 17:34:44 +01001869 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001870 if (!capture)
1871 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001872 /* Compute the xxh64 of the ciphersuite. */
1873 capture->xxh64 = XXH64(msg, rec_len, 0);
1874
1875 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001876 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1877 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001878 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001879
1880 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001881}
1882
Emeric Brun29f037d2014-04-25 19:05:36 +02001883/* Callback is called for ssl protocol analyse */
1884void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1885{
Emeric Brun29f037d2014-04-25 19:05:36 +02001886#ifdef TLS1_RT_HEARTBEAT
1887 /* test heartbeat received (write_p is set to 0
1888 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001889 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001890 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001891 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001892 const unsigned char *p = buf;
1893 unsigned int payload;
1894
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001895 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001896
1897 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1898 if (*p != TLS1_HB_REQUEST)
1899 return;
1900
Willy Tarreauaeed6722014-04-25 23:59:58 +02001901 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001902 goto kill_it;
1903
1904 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001905 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001906 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001907 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001908 /* We have a clear heartbleed attack (CVE-2014-0160), the
1909 * advertised payload is larger than the advertised packet
1910 * length, so we have garbage in the buffer between the
1911 * payload and the end of the buffer (p+len). We can't know
1912 * if the SSL stack is patched, and we don't know if we can
1913 * safely wipe out the area between p+3+len and payload.
1914 * So instead, we prevent the response from being sent by
1915 * setting the max_send_fragment to 0 and we report an SSL
1916 * error, which will kill this connection. It will be reported
1917 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001918 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1919 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001920 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001921 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1922 return;
1923 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001924#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001925 if (global_ssl.capture_cipherlist > 0)
1926 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001927}
1928
Bernard Spil13c53f82018-02-15 13:34:58 +01001929#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001930static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1931 const unsigned char *in, unsigned int inlen,
1932 void *arg)
1933{
1934 struct server *srv = arg;
1935
1936 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1937 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1938 return SSL_TLSEXT_ERR_OK;
1939 return SSL_TLSEXT_ERR_NOACK;
1940}
1941#endif
1942
1943#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001944/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001945 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001946 */
1947static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1948 unsigned int *len, void *arg)
1949{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001950 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001951
1952 *data = (const unsigned char *)conf->npn_str;
1953 *len = conf->npn_len;
1954 return SSL_TLSEXT_ERR_OK;
1955}
1956#endif
1957
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001958#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001959/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001960 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001961 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001962static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1963 unsigned char *outlen,
1964 const unsigned char *server,
1965 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001966{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001967 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001968
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001969 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1970 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1971 return SSL_TLSEXT_ERR_NOACK;
1972 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001973 return SSL_TLSEXT_ERR_OK;
1974}
1975#endif
1976
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001977#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001978#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001979
Christopher Faulet30548802015-06-11 13:39:32 +02001980/* Create a X509 certificate with the specified servername and serial. This
1981 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001982static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001983ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001984{
Christopher Faulet7969a332015-10-09 11:15:03 +02001985 X509 *cacert = bind_conf->ca_sign_cert;
1986 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001987 SSL_CTX *ssl_ctx = NULL;
1988 X509 *newcrt = NULL;
1989 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001990 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001991 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001992 X509_NAME *name;
1993 const EVP_MD *digest;
1994 X509V3_CTX ctx;
1995 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001996 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001997
Christopher Faulet48a83322017-07-28 16:56:09 +02001998 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001999#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002000 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
2001#else
2002 tmp_ssl = SSL_new(bind_conf->default_ctx);
2003 if (tmp_ssl)
2004 pkey = SSL_get_privatekey(tmp_ssl);
2005#endif
2006 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002007 goto mkcert_error;
2008
2009 /* Create the certificate */
2010 if (!(newcrt = X509_new()))
2011 goto mkcert_error;
2012
2013 /* Set version number for the certificate (X509v3) and the serial
2014 * number */
2015 if (X509_set_version(newcrt, 2L) != 1)
2016 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002017 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002018
2019 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002020 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2021 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002022 goto mkcert_error;
2023
2024 /* set public key in the certificate */
2025 if (X509_set_pubkey(newcrt, pkey) != 1)
2026 goto mkcert_error;
2027
2028 /* Set issuer name from the CA */
2029 if (!(name = X509_get_subject_name(cacert)))
2030 goto mkcert_error;
2031 if (X509_set_issuer_name(newcrt, name) != 1)
2032 goto mkcert_error;
2033
2034 /* Set the subject name using the same, but the CN */
2035 name = X509_NAME_dup(name);
2036 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2037 (const unsigned char *)servername,
2038 -1, -1, 0) != 1) {
2039 X509_NAME_free(name);
2040 goto mkcert_error;
2041 }
2042 if (X509_set_subject_name(newcrt, name) != 1) {
2043 X509_NAME_free(name);
2044 goto mkcert_error;
2045 }
2046 X509_NAME_free(name);
2047
2048 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002049 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002050 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2051 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2052 X509_EXTENSION *ext;
2053
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002054 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002055 goto mkcert_error;
2056 if (!X509_add_ext(newcrt, ext, -1)) {
2057 X509_EXTENSION_free(ext);
2058 goto mkcert_error;
2059 }
2060 X509_EXTENSION_free(ext);
2061 }
2062
2063 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002064
2065 key_type = EVP_PKEY_base_id(capkey);
2066
2067 if (key_type == EVP_PKEY_DSA)
2068 digest = EVP_sha1();
2069 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002070 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002071 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002072 digest = EVP_sha256();
2073 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002074#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002075 int nid;
2076
2077 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2078 goto mkcert_error;
2079 if (!(digest = EVP_get_digestbynid(nid)))
2080 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002081#else
2082 goto mkcert_error;
2083#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002084 }
2085
Christopher Faulet31af49d2015-06-09 17:29:50 +02002086 if (!(X509_sign(newcrt, capkey, digest)))
2087 goto mkcert_error;
2088
2089 /* Create and set the new SSL_CTX */
2090 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2091 goto mkcert_error;
2092 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2093 goto mkcert_error;
2094 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2095 goto mkcert_error;
2096 if (!SSL_CTX_check_private_key(ssl_ctx))
2097 goto mkcert_error;
2098
2099 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002100
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002101#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002102 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002103#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002104#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2105 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002106 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002107 EC_KEY *ecc;
2108 int nid;
2109
2110 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2111 goto end;
2112 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2113 goto end;
2114 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2115 EC_KEY_free(ecc);
2116 }
2117#endif
2118 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002119 return ssl_ctx;
2120
2121 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002122 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002123 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002124 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2125 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002126 return NULL;
2127}
2128
Christopher Faulet7969a332015-10-09 11:15:03 +02002129SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002130ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002131{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002132 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002133 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002134
Olivier Houchard66ab4982019-02-26 18:37:15 +01002135 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002136}
2137
Christopher Faulet30548802015-06-11 13:39:32 +02002138/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002139 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002140SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002141ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002142{
2143 struct lru64 *lru = NULL;
2144
2145 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002146 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002147 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002148 if (lru && lru->domain) {
2149 if (ssl)
2150 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002151 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002152 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002153 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002154 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002155 }
2156 return NULL;
2157}
2158
Emeric Brun821bb9b2017-06-15 16:37:39 +02002159/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2160 * function is not thread-safe, it should only be used to check if a certificate
2161 * exists in the lru cache (with no warranty it will not be removed by another
2162 * thread). It is kept for backward compatibility. */
2163SSL_CTX *
2164ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2165{
2166 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2167}
2168
Christopher Fauletd2cab922015-07-28 16:03:47 +02002169/* Set a certificate int the LRU cache used to store generated
2170 * certificate. Return 0 on success, otherwise -1 */
2171int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002172ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002173{
2174 struct lru64 *lru = NULL;
2175
2176 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002177 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002178 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002179 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002180 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002181 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002182 }
Christopher Faulet30548802015-06-11 13:39:32 +02002183 if (lru->domain && lru->data)
2184 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002185 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002186 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002187 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002188 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002189 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002190}
2191
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002192/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002193unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002194ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002195{
2196 return XXH32(data, len, ssl_ctx_lru_seed);
2197}
2198
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002199/* Generate a cert and immediately assign it to the SSL session so that the cert's
2200 * refcount is maintained regardless of the cert's presence in the LRU cache.
2201 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002202static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002203ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002204{
2205 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002206 SSL_CTX *ssl_ctx = NULL;
2207 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002208 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002209
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002210 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002211 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002212 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002213 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002214 if (lru && lru->domain)
2215 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002216 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002217 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002218 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002219 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002220 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002221 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002222 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002223 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002224 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002225 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002226 SSL_set_SSL_CTX(ssl, ssl_ctx);
2227 /* No LRU cache, this CTX will be released as soon as the session dies */
2228 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002229 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002230 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002231 return 0;
2232}
2233static int
2234ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2235{
2236 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002237 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002238
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002239 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002240 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002241 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002242 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002243 }
2244 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002245}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002246#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002247
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002248#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002249typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2250
2251static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002252{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002253#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002254 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002255 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2256#endif
2257}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002258static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2259 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002260 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2261}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002262static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002263#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002264 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002265 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2266#endif
2267}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002268static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002269#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002270 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002271 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2272#endif
2273}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002274/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002275static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2276/* Unusable in this context. */
2277static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2278static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2279static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2280static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2281static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002282#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002283typedef enum { SET_MIN, SET_MAX } set_context_func;
2284
2285static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2286 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002287 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2288}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002289static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2290 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2291 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2292}
2293static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2294 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002295 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2296}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002297static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2298 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2299 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2300}
2301static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2302 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002303 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2304}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002305static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2306 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2307 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2308}
2309static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2310 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002311 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2312}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002313static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2314 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2315 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2316}
2317static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002318#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002319 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002320 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2321#endif
2322}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002323static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2324#if SSL_OP_NO_TLSv1_3
2325 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2326 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002327#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002328}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002329#endif
2330static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2331static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002332
2333static struct {
2334 int option;
2335 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002336 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2337 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002338 const char *name;
2339} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002340 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2341 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2342 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2343 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2344 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2345 {SSL_OP_NO_TLSv1_3, MC_SSL_O_NO_TLSV13, ctx_set_TLSv13_func, ssl_set_TLSv13_func, "TLSv1.3"}, /* CONF_TLSV13 */
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002346};
2347
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002348static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2349{
2350 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2351 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2352 SSL_set_SSL_CTX(ssl, ctx);
2353}
2354
Willy Tarreau5db847a2019-05-09 14:13:35 +02002355#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356
2357static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2358{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002359 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002360 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002361
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002362 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2363 return SSL_TLSEXT_ERR_OK;
2364 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002365}
2366
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002367#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2369{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002370 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002371#else
2372static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2373{
2374#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002375 struct connection *conn;
2376 struct bind_conf *s;
2377 const uint8_t *extension_data;
2378 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002379 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002380
2381 char *wildp = NULL;
2382 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002383 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002384 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002385 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002386 int i;
2387
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002388 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002389 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002390
Olivier Houchard9679ac92017-10-27 14:58:08 +02002391 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002392 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002393#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002394 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2395 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002396#else
2397 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2398#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002399 /*
2400 * The server_name extension was given too much extensibility when it
2401 * was written, so parsing the normal case is a bit complex.
2402 */
2403 size_t len;
2404 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002405 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002406 /* Extract the length of the supplied list of names. */
2407 len = (*extension_data++) << 8;
2408 len |= *extension_data++;
2409 if (len + 2 != extension_len)
2410 goto abort;
2411 /*
2412 * The list in practice only has a single element, so we only consider
2413 * the first one.
2414 */
2415 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2416 goto abort;
2417 extension_len = len - 1;
2418 /* Now we can finally pull out the byte array with the actual hostname. */
2419 if (extension_len <= 2)
2420 goto abort;
2421 len = (*extension_data++) << 8;
2422 len |= *extension_data++;
2423 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2424 || memchr(extension_data, 0, len) != NULL)
2425 goto abort;
2426 servername = extension_data;
2427 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002428 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002429#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2430 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002431 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002432 }
2433#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002434 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002435 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002436 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002437 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002438 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002439 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002440 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002441 goto abort;
2442 }
2443
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002444 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002445#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002446 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002447#else
2448 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2449#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002450 uint8_t sign;
2451 size_t len;
2452 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002453 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002454 len = (*extension_data++) << 8;
2455 len |= *extension_data++;
2456 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002457 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 if (len % 2 != 0)
2459 goto abort;
2460 for (; len > 0; len -= 2) {
2461 extension_data++; /* hash */
2462 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002463 switch (sign) {
2464 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002465 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002466 break;
2467 case TLSEXT_signature_ecdsa:
2468 has_ecdsa_sig = 1;
2469 break;
2470 default:
2471 continue;
2472 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002473 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002474 break;
2475 }
2476 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002477 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002478 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002479 }
2480 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002481 const SSL_CIPHER *cipher;
2482 size_t len;
2483 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002484 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002485#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002486 len = ctx->cipher_suites_len;
2487 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002488#else
2489 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2490#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002491 if (len % 2 != 0)
2492 goto abort;
2493 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002494#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002495 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002496 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002497#else
2498 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2499#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002500 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002501 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002502 break;
2503 }
2504 }
2505 }
2506
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002507 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002508 trash.area[i] = tolower(servername[i]);
2509 if (!wildp && (trash.area[i] == '.'))
2510 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002511 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002513
William Lallemand150bfa82019-09-19 17:12:49 +02002514 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002515
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002516 for (i = 0; i < 2; i++) {
2517 if (i == 0) /* lookup in full qualified names */
2518 node = ebst_lookup(&s->sni_ctx, trash.area);
2519 else if (i == 1 && wildp) /* lookup in wildcards names */
2520 node = ebst_lookup(&s->sni_w_ctx, wildp);
2521 else
2522 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002523 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002524 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002525 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002526 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002527 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002528 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002529 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002530 break;
2531 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002532 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002533 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002534 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002535 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002536 if (!node_anonymous)
2537 node_anonymous = n;
2538 break;
2539 }
2540 }
2541 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002542 /* select by key_signature priority order */
2543 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2544 : ((has_rsa_sig && node_rsa) ? node_rsa
2545 : (node_anonymous ? node_anonymous
2546 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2547 : node_rsa /* no rsa signature case (far far away) */
2548 )));
2549 if (node) {
2550 /* switch ctx */
2551 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2552 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002553 if (conf) {
2554 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2555 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2556 if (conf->early_data)
2557 allow_early = 1;
2558 }
William Lallemand02010472019-10-18 11:02:19 +02002559 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002560 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002561 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002562 }
William Lallemand150bfa82019-09-19 17:12:49 +02002563
William Lallemand02010472019-10-18 11:02:19 +02002564 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002565#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002566 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002567 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002568 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002569 }
2570#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002571 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002572 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002573 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002574 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002575 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002576 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002577allow_early:
2578#ifdef OPENSSL_IS_BORINGSSL
2579 if (allow_early)
2580 SSL_set_early_data_enabled(ssl, 1);
2581#else
2582 if (!allow_early)
2583 SSL_set_max_early_data(ssl, 0);
2584#endif
2585 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002586 abort:
2587 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2588 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002589#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002590 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002591#else
2592 *al = SSL_AD_UNRECOGNIZED_NAME;
2593 return 0;
2594#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002595}
2596
2597#else /* OPENSSL_IS_BORINGSSL */
2598
Emeric Brunfc0421f2012-09-07 17:30:07 +02002599/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2600 * warning when no match is found, which implies the default (first) cert
2601 * will keep being used.
2602 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002603static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002604{
2605 const char *servername;
2606 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002607 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002608 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002609 int i;
2610 (void)al; /* shut gcc stupid warning */
2611
2612 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002613 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002614#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002615 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2616 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002617#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002618 if (s->strict_sni)
2619 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002620 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002621 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002622 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002623 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002624 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002625
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002626 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002627 if (!servername[i])
2628 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002629 trash.area[i] = tolower(servername[i]);
2630 if (!wildp && (trash.area[i] == '.'))
2631 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002632 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002634
William Lallemand150bfa82019-09-19 17:12:49 +02002635 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002636 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002637 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002638 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2639 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002640 if (!container_of(n, struct sni_ctx, name)->neg) {
2641 node = n;
2642 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002643 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002644 }
2645 if (!node && wildp) {
2646 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002647 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2648 /* lookup a not neg filter */
2649 if (!container_of(n, struct sni_ctx, name)->neg) {
2650 node = n;
2651 break;
2652 }
2653 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002654 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002655 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002656#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002657 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2658 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002659 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002660 return SSL_TLSEXT_ERR_OK;
2661 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002662#endif
William Lallemand21724f02019-11-04 17:56:13 +01002663 if (s->strict_sni) {
2664 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002665 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002666 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002667 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002668 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002669 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002670 }
2671
2672 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002673 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002674 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002675 return SSL_TLSEXT_ERR_OK;
2676}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002677#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002678#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2679
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002680#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002681
2682static DH * ssl_get_dh_1024(void)
2683{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002684 static unsigned char dh1024_p[]={
2685 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2686 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2687 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2688 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2689 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2690 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2691 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2692 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2693 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2694 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2695 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2696 };
2697 static unsigned char dh1024_g[]={
2698 0x02,
2699 };
2700
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002701 BIGNUM *p;
2702 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002703 DH *dh = DH_new();
2704 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002705 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2706 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002707
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002708 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002709 DH_free(dh);
2710 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002711 } else {
2712 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002713 }
2714 }
2715 return dh;
2716}
2717
2718static DH *ssl_get_dh_2048(void)
2719{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002720 static unsigned char dh2048_p[]={
2721 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2722 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2723 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2724 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2725 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2726 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2727 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2728 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2729 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2730 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2731 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2732 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2733 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2734 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2735 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2736 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2737 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2738 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2739 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2740 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2741 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2742 0xB7,0x1F,0x77,0xF3,
2743 };
2744 static unsigned char dh2048_g[]={
2745 0x02,
2746 };
2747
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002748 BIGNUM *p;
2749 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002750 DH *dh = DH_new();
2751 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002752 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2753 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002754
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002755 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002756 DH_free(dh);
2757 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002758 } else {
2759 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002760 }
2761 }
2762 return dh;
2763}
2764
2765static DH *ssl_get_dh_4096(void)
2766{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002767 static unsigned char dh4096_p[]={
2768 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2769 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2770 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2771 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2772 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2773 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2774 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2775 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2776 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2777 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2778 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2779 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2780 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2781 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2782 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2783 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2784 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2785 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2786 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2787 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2788 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2789 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2790 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2791 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2792 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2793 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2794 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2795 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2796 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2797 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2798 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2799 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2800 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2801 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2802 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2803 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2804 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2805 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2806 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2807 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2808 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2809 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2810 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002811 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002812 static unsigned char dh4096_g[]={
2813 0x02,
2814 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002815
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002816 BIGNUM *p;
2817 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002818 DH *dh = DH_new();
2819 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002820 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2821 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002822
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002823 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002824 DH_free(dh);
2825 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002826 } else {
2827 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002828 }
2829 }
2830 return dh;
2831}
2832
2833/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002834 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002835static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2836{
2837 DH *dh = NULL;
2838 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002839 int type;
2840
2841 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002842
2843 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2844 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2845 */
2846 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2847 keylen = EVP_PKEY_bits(pkey);
2848 }
2849
Willy Tarreauef934602016-12-22 23:12:01 +01002850 if (keylen > global_ssl.default_dh_param) {
2851 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002852 }
2853
Remi Gacogned3a341a2015-05-29 16:26:17 +02002854 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002855 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002856 }
2857 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002858 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002859 }
2860 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002861 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002862 }
2863
2864 return dh;
2865}
2866
Remi Gacogne47783ef2015-05-29 15:53:22 +02002867static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002868{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002869 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002870 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002871
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002872 if (in == NULL)
2873 goto end;
2874
Remi Gacogne47783ef2015-05-29 15:53:22 +02002875 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002876 goto end;
2877
Remi Gacogne47783ef2015-05-29 15:53:22 +02002878 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2879
2880end:
2881 if (in)
2882 BIO_free(in);
2883
Emeric Brune1b4ed42018-08-16 15:14:12 +02002884 ERR_clear_error();
2885
Remi Gacogne47783ef2015-05-29 15:53:22 +02002886 return dh;
2887}
2888
2889int ssl_sock_load_global_dh_param_from_file(const char *filename)
2890{
2891 global_dh = ssl_sock_get_dh_from_file(filename);
2892
2893 if (global_dh) {
2894 return 0;
2895 }
2896
2897 return -1;
2898}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002899#endif
2900
William Lallemand9117de92019-10-04 00:29:42 +02002901/* Alloc and init a ckch_inst */
2902static struct ckch_inst *ckch_inst_new()
2903{
2904 struct ckch_inst *ckch_inst;
2905
2906 ckch_inst = calloc(1, sizeof *ckch_inst);
2907 if (ckch_inst)
2908 LIST_INIT(&ckch_inst->sni_ctx);
2909
2910 return ckch_inst;
2911}
2912
2913
2914/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002915static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002916 struct bind_conf *s, struct ssl_bind_conf *conf,
2917 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002918{
2919 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002920 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002921
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002922 if (*name == '!') {
2923 neg = 1;
2924 name++;
2925 }
2926 if (*name == '*') {
2927 wild = 1;
2928 name++;
2929 }
2930 /* !* filter is a nop */
2931 if (neg && wild)
2932 return order;
2933 if (*name) {
2934 int j, len;
2935 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002936 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002937 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002938 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002939 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002940 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002941
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002942 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002943 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002944 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002945 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002946 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002947 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002948 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002949 sc->order = order++;
2950 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002951 sc->wild = wild;
2952 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01002953 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02002954 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002955 }
2956 return order;
2957}
2958
William Lallemand6af03992019-07-23 15:00:54 +02002959/*
William Lallemand1d29c742019-10-04 00:53:29 +02002960 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2961 * This function can't return an error.
2962 *
2963 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2964 */
2965static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2966{
2967
2968 struct sni_ctx *sc0, *sc0b, *sc1;
2969 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002970 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002971
2972 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2973
2974 /* ignore if sc0 was already inserted in a tree */
2975 if (sc0->name.node.leaf_p)
2976 continue;
2977
2978 /* Check for duplicates. */
2979 if (sc0->wild)
2980 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2981 else
2982 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2983
2984 for (; node; node = ebmb_next_dup(node)) {
2985 sc1 = ebmb_entry(node, struct sni_ctx, name);
2986 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2987 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2988 /* it's a duplicate, we should remove and free it */
2989 LIST_DEL(&sc0->by_ckch_inst);
2990 free(sc0);
2991 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002992 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002993 }
2994 }
2995
2996 /* if duplicate, ignore the insertion */
2997 if (!sc0)
2998 continue;
2999
3000 if (sc0->wild)
3001 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3002 else
3003 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003004
3005 /* replace the default_ctx if required with the first ctx */
3006 if (ckch_inst->is_default && !def) {
3007 /* we don't need to free the default_ctx because the refcount was not incremented */
3008 bind_conf->default_ctx = sc0->ctx;
3009 def = 1;
3010 }
William Lallemand1d29c742019-10-04 00:53:29 +02003011 }
3012}
3013
3014/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003015 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003016 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003017struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003018
William Lallemand2954c472020-03-06 21:54:13 +01003019/* tree of crtlist (crt-list/directory) */
3020static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003021
Emeric Brun7a883362019-10-17 13:27:40 +02003022/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003023 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003024 * DH parameter is loaded into the SSL_CTX and if there is no
3025 * DH parameter available in ckchs nor in global, the default
3026 * DH parameters are applied on the SSL_CTX.
3027 * Returns a bitfield containing the flags:
3028 * ERR_FATAL in any fatal error case
3029 * ERR_ALERT if a reason of the error is availabine in err
3030 * ERR_WARN if a warning is available into err
3031 * The value 0 means there is no error nor warning and
3032 * the operation succeed.
3033 */
William Lallemandfa892222019-07-23 16:06:08 +02003034#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003035static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3036 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003037{
Emeric Brun7a883362019-10-17 13:27:40 +02003038 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003039 DH *dh = NULL;
3040
William Lallemanda8c73742019-07-31 18:31:34 +02003041 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003042 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003043 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3044 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3045 err && *err ? *err : "", path);
3046#if defined(SSL_CTX_set_dh_auto)
3047 SSL_CTX_set_dh_auto(ctx, 1);
3048 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3049 err && *err ? *err : "");
3050#else
3051 memprintf(err, "%s, DH ciphers won't be available.\n",
3052 err && *err ? *err : "");
3053#endif
3054 ret |= ERR_WARN;
3055 goto end;
3056 }
William Lallemandfa892222019-07-23 16:06:08 +02003057
3058 if (ssl_dh_ptr_index >= 0) {
3059 /* store a pointer to the DH params to avoid complaining about
3060 ssl-default-dh-param not being set for this SSL_CTX */
3061 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3062 }
3063 }
3064 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003065 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3066 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3067 err && *err ? *err : "", path);
3068#if defined(SSL_CTX_set_dh_auto)
3069 SSL_CTX_set_dh_auto(ctx, 1);
3070 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3071 err && *err ? *err : "");
3072#else
3073 memprintf(err, "%s, DH ciphers won't be available.\n",
3074 err && *err ? *err : "");
3075#endif
3076 ret |= ERR_WARN;
3077 goto end;
3078 }
William Lallemandfa892222019-07-23 16:06:08 +02003079 }
3080 else {
3081 /* Clear openssl global errors stack */
3082 ERR_clear_error();
3083
3084 if (global_ssl.default_dh_param <= 1024) {
3085 /* we are limited to DH parameter of 1024 bits anyway */
3086 if (local_dh_1024 == NULL)
3087 local_dh_1024 = ssl_get_dh_1024();
3088
Emeric Brun7a883362019-10-17 13:27:40 +02003089 if (local_dh_1024 == NULL) {
3090 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3091 err && *err ? *err : "", path);
3092 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003093 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003094 }
William Lallemandfa892222019-07-23 16:06:08 +02003095
Emeric Bruna9363eb2019-10-17 14:53:03 +02003096 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3097 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3098 err && *err ? *err : "", path);
3099#if defined(SSL_CTX_set_dh_auto)
3100 SSL_CTX_set_dh_auto(ctx, 1);
3101 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3102 err && *err ? *err : "");
3103#else
3104 memprintf(err, "%s, DH ciphers won't be available.\n",
3105 err && *err ? *err : "");
3106#endif
3107 ret |= ERR_WARN;
3108 goto end;
3109 }
William Lallemandfa892222019-07-23 16:06:08 +02003110 }
3111 else {
3112 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3113 }
William Lallemandfa892222019-07-23 16:06:08 +02003114 }
3115
3116end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003117 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003118 return ret;
3119}
3120#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003121
yanbzhu488a4d22015-12-01 15:16:07 -05003122/* Frees the contents of a cert_key_and_chain
3123 */
3124static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3125{
yanbzhu488a4d22015-12-01 15:16:07 -05003126 if (!ckch)
3127 return;
3128
3129 /* Free the certificate and set pointer to NULL */
3130 if (ckch->cert)
3131 X509_free(ckch->cert);
3132 ckch->cert = NULL;
3133
3134 /* Free the key and set pointer to NULL */
3135 if (ckch->key)
3136 EVP_PKEY_free(ckch->key);
3137 ckch->key = NULL;
3138
3139 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003140 if (ckch->chain)
3141 sk_X509_pop_free(ckch->chain, X509_free);
3142 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003143
William Lallemand455af502019-10-17 18:04:45 +02003144 if (ckch->dh)
3145 DH_free(ckch->dh);
3146 ckch->dh = NULL;
3147
3148 if (ckch->sctl) {
3149 free(ckch->sctl->area);
3150 ckch->sctl->area = NULL;
3151 free(ckch->sctl);
3152 ckch->sctl = NULL;
3153 }
3154
3155 if (ckch->ocsp_response) {
3156 free(ckch->ocsp_response->area);
3157 ckch->ocsp_response->area = NULL;
3158 free(ckch->ocsp_response);
3159 ckch->ocsp_response = NULL;
3160 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003161
3162 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003163 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003164 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003165}
3166
William Lallemand8d0f8932019-10-17 18:03:58 +02003167/*
3168 *
3169 * This function copy a cert_key_and_chain in memory
3170 *
3171 * It's used to try to apply changes on a ckch before committing them, because
3172 * most of the time it's not possible to revert those changes
3173 *
3174 * Return a the dst or NULL
3175 */
3176static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3177 struct cert_key_and_chain *dst)
3178{
3179 if (src->cert) {
3180 dst->cert = src->cert;
3181 X509_up_ref(src->cert);
3182 }
3183
3184 if (src->key) {
3185 dst->key = src->key;
3186 EVP_PKEY_up_ref(src->key);
3187 }
3188
3189 if (src->chain) {
3190 dst->chain = X509_chain_up_ref(src->chain);
3191 }
3192
3193 if (src->dh) {
3194 DH_up_ref(src->dh);
3195 dst->dh = src->dh;
3196 }
3197
3198 if (src->sctl) {
3199 struct buffer *sctl;
3200
3201 sctl = calloc(1, sizeof(*sctl));
3202 if (!chunk_dup(sctl, src->sctl)) {
3203 free(sctl);
3204 sctl = NULL;
3205 goto error;
3206 }
3207 dst->sctl = sctl;
3208 }
3209
3210 if (src->ocsp_response) {
3211 struct buffer *ocsp_response;
3212
3213 ocsp_response = calloc(1, sizeof(*ocsp_response));
3214 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3215 free(ocsp_response);
3216 ocsp_response = NULL;
3217 goto error;
3218 }
3219 dst->ocsp_response = ocsp_response;
3220 }
3221
3222 if (src->ocsp_issuer) {
3223 X509_up_ref(src->ocsp_issuer);
3224 dst->ocsp_issuer = src->ocsp_issuer;
3225 }
3226
3227 return dst;
3228
3229error:
3230
3231 /* free everything */
3232 ssl_sock_free_cert_key_and_chain_contents(dst);
3233
3234 return NULL;
3235}
3236
3237
yanbzhu488a4d22015-12-01 15:16:07 -05003238/* checks if a key and cert exists in the ckch
3239 */
William Lallemand1633e392019-09-30 12:58:13 +02003240#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003241static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3242{
3243 return (ckch->cert != NULL && ckch->key != NULL);
3244}
William Lallemand1633e392019-09-30 12:58:13 +02003245#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003246
William Lallemandf9568fc2019-10-16 18:27:58 +02003247/*
3248 * return 0 on success or != 0 on failure
3249 */
3250static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3251{
3252 int ret = 1;
3253 BIO *in = NULL;
3254 X509 *issuer;
3255
3256 if (buf) {
3257 /* reading from a buffer */
3258 in = BIO_new_mem_buf(buf, -1);
3259 if (in == NULL) {
3260 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3261 goto end;
3262 }
3263
3264 } else {
3265 /* reading from a file */
3266 in = BIO_new(BIO_s_file());
3267 if (in == NULL)
3268 goto end;
3269
3270 if (BIO_read_filename(in, path) <= 0)
3271 goto end;
3272 }
3273
3274 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3275 if (!issuer) {
3276 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003277 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003278 goto end;
3279 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003280 /* no error, fill ckch with new context, old context must be free */
3281 if (ckch->ocsp_issuer)
3282 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003283 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003284 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003285
3286end:
3287
3288 ERR_clear_error();
3289 if (in)
3290 BIO_free(in);
3291
3292 return ret;
3293}
3294
William Lallemand96a9c972019-10-17 11:56:17 +02003295
3296/*
3297 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003298 * The PEM must contain at least a Certificate,
3299 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003300 *
William Lallemand96a9c972019-10-17 11:56:17 +02003301 * If it failed you should not attempt to use the ckch but free it.
3302 *
3303 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003304 */
William Lallemand96a9c972019-10-17 11:56:17 +02003305static 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 -05003306{
William Lallemandf11365b2019-09-19 14:25:58 +02003307 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003308 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003309 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003310 X509 *cert = NULL;
3311 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003312 DH *dh = NULL;
3313 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003314
3315 if (buf) {
3316 /* reading from a buffer */
3317 in = BIO_new_mem_buf(buf, -1);
3318 if (in == NULL) {
3319 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3320 goto end;
3321 }
yanbzhu488a4d22015-12-01 15:16:07 -05003322
William Lallemand96a9c972019-10-17 11:56:17 +02003323 } else {
3324 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003325 in = BIO_new(BIO_s_file());
3326 if (in == NULL)
3327 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003328
William Lallemandf11365b2019-09-19 14:25:58 +02003329 if (BIO_read_filename(in, path) <= 0)
3330 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003331 }
yanbzhu488a4d22015-12-01 15:16:07 -05003332
yanbzhu488a4d22015-12-01 15:16:07 -05003333 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003334 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003335 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003336
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003337#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003338 /* Seek back to beginning of file */
3339 if (BIO_reset(in) == -1) {
3340 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3341 err && *err ? *err : "", path);
3342 goto end;
3343 }
3344
William Lallemand96a9c972019-10-17 11:56:17 +02003345 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3346 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003347#endif
William Lallemandfa892222019-07-23 16:06:08 +02003348
Willy Tarreaubb137a82016-04-06 19:02:38 +02003349 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003350 if (BIO_reset(in) == -1) {
3351 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3352 err && *err ? *err : "", path);
3353 goto end;
3354 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003355
3356 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003357 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3358 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003359 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003360 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003361 goto end;
3362 }
3363
William Lallemand96a9c972019-10-17 11:56:17 +02003364 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003365 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3366 if (chain == NULL)
3367 chain = sk_X509_new_null();
3368 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003369 X509_free(ca);
3370 goto end;
3371 }
3372 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003373
yanbzhu488a4d22015-12-01 15:16:07 -05003374 ret = ERR_get_error();
3375 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3376 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003377 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003378 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003379 }
3380
William Lallemand75b15f72020-01-23 10:56:05 +01003381 /* once it loaded the PEM, it should remove everything else in the ckch */
3382 if (ckch->ocsp_response) {
3383 free(ckch->ocsp_response->area);
3384 ckch->ocsp_response->area = NULL;
3385 free(ckch->ocsp_response);
3386 ckch->ocsp_response = NULL;
3387 }
3388
3389 if (ckch->sctl) {
3390 free(ckch->sctl->area);
3391 ckch->sctl->area = NULL;
3392 free(ckch->sctl);
3393 ckch->sctl = NULL;
3394 }
3395
3396 if (ckch->ocsp_issuer) {
3397 X509_free(ckch->ocsp_issuer);
3398 ckch->ocsp_issuer = NULL;
3399 }
3400
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003401 /* no error, fill ckch with new context, old context will be free at end: */
3402 SWAP(ckch->key, key);
3403 SWAP(ckch->dh, dh);
3404 SWAP(ckch->cert, cert);
3405 SWAP(ckch->chain, chain);
3406
William Lallemand246c0242019-10-11 08:59:13 +02003407 ret = 0;
3408
William Lallemand96a9c972019-10-17 11:56:17 +02003409end:
William Lallemand246c0242019-10-11 08:59:13 +02003410
3411 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003412 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003413 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003414 if (key)
3415 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003416 if (dh)
3417 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003418 if (cert)
3419 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003420 if (chain)
3421 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003422
William Lallemand96a9c972019-10-17 11:56:17 +02003423 return ret;
3424}
3425
3426/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003427 * Try to load a private key file from a <path> or a buffer <buf>
3428 *
3429 * If it failed you should not attempt to use the ckch but free it.
3430 *
3431 * Return 0 on success or != 0 on failure
3432 */
3433static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3434{
3435 BIO *in = NULL;
3436 int ret = 1;
3437 EVP_PKEY *key = NULL;
3438
3439 if (buf) {
3440 /* reading from a buffer */
3441 in = BIO_new_mem_buf(buf, -1);
3442 if (in == NULL) {
3443 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3444 goto end;
3445 }
3446
3447 } else {
3448 /* reading from a file */
3449 in = BIO_new(BIO_s_file());
3450 if (in == NULL)
3451 goto end;
3452
3453 if (BIO_read_filename(in, path) <= 0)
3454 goto end;
3455 }
3456
3457 /* Read Private Key */
3458 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3459 if (key == NULL) {
3460 memprintf(err, "%sunable to load private key from file '%s'.\n",
3461 err && *err ? *err : "", path);
3462 goto end;
3463 }
3464
3465 ret = 0;
3466
3467 SWAP(ckch->key, key);
3468
3469end:
3470
3471 ERR_clear_error();
3472 if (in)
3473 BIO_free(in);
3474 if (key)
3475 EVP_PKEY_free(key);
3476
3477 return ret;
3478}
3479
3480/*
William Lallemand96a9c972019-10-17 11:56:17 +02003481 * Try to load in a ckch every files related to a ckch.
3482 * (PEM, sctl, ocsp, issuer etc.)
3483 *
3484 * This function is only used to load files during the configuration parsing,
3485 * it is not used with the CLI.
3486 *
3487 * This allows us to carry the contents of the file without having to read the
3488 * file multiple times. The caller must call
3489 * ssl_sock_free_cert_key_and_chain_contents.
3490 *
3491 * returns:
3492 * 0 on Success
3493 * 1 on SSL Failure
3494 */
3495static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3496{
3497 int ret = 1;
3498
3499 /* try to load the PEM */
3500 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3501 goto end;
3502 }
3503
William Lallemand4c5adbf2020-02-24 14:23:22 +01003504 /* try to load an external private key if it wasn't in the PEM */
3505 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3506 char fp[MAXPATHLEN+1];
3507 struct stat st;
3508
3509 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3510 if (stat(fp, &st) == 0) {
3511 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3512 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3513 err && *err ? *err : "", fp);
3514 goto end;
3515 }
3516 }
3517 }
3518
3519 if (ckch->key == NULL) {
3520 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3521 goto end;
3522 }
3523
3524 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3525 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3526 err && *err ? *err : "", path);
3527 goto end;
3528 }
3529
William Lallemanda17f4112019-10-10 15:16:44 +02003530#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3531 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003532 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003533 char fp[MAXPATHLEN+1];
3534 struct stat st;
3535
3536 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3537 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003538 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003539 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003540 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003541 ret = 1;
3542 goto end;
3543 }
3544 }
3545 }
3546#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003547
William Lallemand246c0242019-10-11 08:59:13 +02003548 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003549 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003550 char fp[MAXPATHLEN+1];
3551 struct stat st;
3552
3553 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3554 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003555 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003556 ret = 1;
3557 goto end;
3558 }
3559 }
3560 }
3561
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003562#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003563 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003564 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003565 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003566 struct stat st;
3567 char fp[MAXPATHLEN+1];
3568
3569 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3570 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003571 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003572 ret = 1;
3573 goto end;
3574 }
3575
3576 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003577 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003578 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003579 ret = 1;
3580 goto end;
3581 }
William Lallemand246c0242019-10-11 08:59:13 +02003582 }
3583 }
3584 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003585#endif
William Lallemand246c0242019-10-11 08:59:13 +02003586
yanbzhu488a4d22015-12-01 15:16:07 -05003587 ret = 0;
3588
3589end:
3590
3591 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003592
3593 /* Something went wrong in one of the reads */
3594 if (ret != 0)
3595 ssl_sock_free_cert_key_and_chain_contents(ckch);
3596
3597 return ret;
3598}
3599
3600/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003601 * Returns a bitfield containing the flags:
3602 * ERR_FATAL in any fatal error case
3603 * ERR_ALERT if the reason of the error is available in err
3604 * ERR_WARN if a warning is available into err
3605 * The value 0 means there is no error nor warning and
3606 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003607 */
3608static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3609{
Emeric Bruna96b5822019-10-17 13:25:14 +02003610 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003611 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003612
yanbzhu488a4d22015-12-01 15:16:07 -05003613 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3614 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3615 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003616 errcode |= ERR_ALERT | ERR_FATAL;
3617 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003618 }
3619
3620 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3621 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3622 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003623 errcode |= ERR_ALERT | ERR_FATAL;
3624 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003625 }
3626
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003627 if (ckch->chain) {
3628 find_chain = ckch->chain;
3629 } else {
3630 /* Find Certificate Chain in global */
3631 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003632 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003633 if (issuer)
3634 find_chain = issuer->chain;
3635 }
William Lallemand85888572020-02-27 14:48:35 +01003636
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003637 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003638 if (find_chain) {
3639 int i;
3640 X509 *ca;
3641 for (i = 0; i < sk_X509_num(find_chain); i++) {
3642 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003643 /* skip self issued (Root CA) */
3644 if (!X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca)))
3645 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003646 /*
3647 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3648 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3649 */
3650 X509_up_ref(ca);
3651 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3652 X509_free(ca);
3653 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3654 err && *err ? *err : "", path);
3655 errcode |= ERR_ALERT | ERR_FATAL;
3656 goto end;
3657 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003658 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003659 }
yanbzhu488a4d22015-12-01 15:16:07 -05003660
William Lallemandfa892222019-07-23 16:06:08 +02003661#ifndef OPENSSL_NO_DH
3662 /* store a NULL pointer to indicate we have not yet loaded
3663 a custom DH param file */
3664 if (ssl_dh_ptr_index >= 0) {
3665 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3666 }
3667
Emeric Brun7a883362019-10-17 13:27:40 +02003668 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3669 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003670 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3671 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003672 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003673 }
3674#endif
3675
William Lallemanda17f4112019-10-10 15:16:44 +02003676#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3677 if (sctl_ex_index >= 0 && ckch->sctl) {
3678 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3679 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003680 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003681 errcode |= ERR_ALERT | ERR_FATAL;
3682 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003683 }
3684 }
3685#endif
3686
William Lallemand4a660132019-10-14 14:51:41 +02003687#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003688 /* Load OCSP Info into context */
3689 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003690 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003691 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",
3692 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003693 errcode |= ERR_ALERT | ERR_FATAL;
3694 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003695 }
3696 }
William Lallemand246c0242019-10-11 08:59:13 +02003697#endif
3698
Emeric Bruna96b5822019-10-17 13:25:14 +02003699 end:
3700 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003701}
3702
William Lallemandc4ecddf2019-07-31 16:50:08 +02003703#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003704
William Lallemand28a8fce2019-10-04 17:36:55 +02003705static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003706{
3707 struct sni_keytype *s_kt = NULL;
3708 struct ebmb_node *node;
3709 int i;
3710
3711 for (i = 0; i < trash.size; i++) {
3712 if (!str[i])
3713 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003714 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003715 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003716 trash.area[i] = 0;
3717 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003718 if (!node) {
3719 /* CN not found in tree */
3720 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3721 /* Using memcpy here instead of strncpy.
3722 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3723 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3724 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003725 if (!s_kt)
3726 return -1;
3727
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003728 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003729 s_kt->keytypes = 0;
3730 ebst_insert(sni_keytypes, &s_kt->name);
3731 } else {
3732 /* CN found in tree */
3733 s_kt = container_of(node, struct sni_keytype, name);
3734 }
3735
3736 /* Mark that this CN has the keytype of key_index via keytypes mask */
3737 s_kt->keytypes |= 1<<key_index;
3738
William Lallemand28a8fce2019-10-04 17:36:55 +02003739 return 0;
3740
yanbzhu08ce6ab2015-12-02 13:01:29 -05003741}
3742
William Lallemandc4ecddf2019-07-31 16:50:08 +02003743#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003744/*
3745 * Free a ckch_store and its ckch(s)
3746 * The linked ckch_inst are not free'd
3747 */
3748void ckchs_free(struct ckch_store *ckchs)
3749{
3750 if (!ckchs)
3751 return;
3752
3753#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3754 if (ckchs->multi) {
3755 int n;
3756
3757 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3758 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3759 } else
3760#endif
3761 {
3762 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3763 ckchs->ckch = NULL;
3764 }
3765
3766 free(ckchs);
3767}
3768
3769/* allocate and duplicate a ckch_store
3770 * Return a new ckch_store or NULL */
3771static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3772{
3773 struct ckch_store *dst;
3774 int pathlen;
3775
3776 pathlen = strlen(src->path);
3777 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3778 if (!dst)
3779 return NULL;
3780 /* copy previous key */
3781 memcpy(dst->path, src->path, pathlen + 1);
3782 dst->multi = src->multi;
3783 LIST_INIT(&dst->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +02003784 LIST_INIT(&dst->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003785
3786 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3787 if (!dst->ckch)
3788 goto error;
3789
3790#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3791 if (src->multi) {
3792 int n;
3793
3794 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3795 if (&src->ckch[n]) {
3796 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3797 goto error;
3798 }
3799 }
3800 } else
3801#endif
3802 {
3803 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3804 goto error;
3805 }
3806
3807 return dst;
3808
3809error:
3810 ckchs_free(dst);
3811
3812 return NULL;
3813}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003814
William Lallemand36b84632019-07-18 19:28:17 +02003815/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003816 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003817 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003818static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003819{
3820 struct ebmb_node *eb;
3821
William Lallemande3af8fb2019-10-08 11:36:53 +02003822 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003823 if (!eb)
3824 return NULL;
3825
William Lallemande3af8fb2019-10-08 11:36:53 +02003826 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003827}
3828
3829/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003830 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003831 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003832static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003833{
William Lallemande3af8fb2019-10-08 11:36:53 +02003834 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003835
William Lallemande3af8fb2019-10-08 11:36:53 +02003836 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3837 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003838 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3839 goto end;
3840 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003841 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003842
William Lallemande3af8fb2019-10-08 11:36:53 +02003843 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003844 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3845 goto end;
3846 }
3847
William Lallemand9117de92019-10-04 00:29:42 +02003848 LIST_INIT(&ckchs->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +02003849 LIST_INIT(&ckchs->crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02003850
William Lallemand36b84632019-07-18 19:28:17 +02003851 if (!multi) {
3852
William Lallemand96a9c972019-10-17 11:56:17 +02003853 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003854 goto end;
3855
William Lallemande3af8fb2019-10-08 11:36:53 +02003856 /* insert into the ckchs tree */
3857 memcpy(ckchs->path, path, strlen(path) + 1);
3858 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003859 } else {
3860 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003861#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3862 char fp[MAXPATHLEN+1] = {0};
3863 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003864
3865 /* Load all possible certs and keys */
3866 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3867 struct stat buf;
3868 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3869 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003870 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003871 goto end;
3872 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003873 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003874 }
3875 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003876#endif
William Lallemand36b84632019-07-18 19:28:17 +02003877
3878 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003879 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003880 goto end;
3881 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003882 /* insert into the ckchs tree */
3883 memcpy(ckchs->path, path, strlen(path) + 1);
3884 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003885 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003886 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003887
William Lallemand36b84632019-07-18 19:28:17 +02003888end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003889 if (ckchs) {
3890 free(ckchs->ckch);
3891 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003892 }
3893
William Lallemande3af8fb2019-10-08 11:36:53 +02003894 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003895
3896 return NULL;
3897}
3898
William Lallemand38df1c82019-12-04 15:39:35 +01003899
3900/*
3901 * Free a sni filters array generated by ckch_inst_sni_ctx_to_sni_filters()
3902 */
3903static inline void free_sni_filters(char **sni_filter, int fcount)
3904{
3905 int i;
3906
3907 if (sni_filter) {
3908 for (i = 0; i < fcount; i++) {
3909 if (sni_filter[i]) {
3910 free(sni_filter[i]);
3911 sni_filter[i] = NULL;
3912 }
3913 }
3914 free(sni_filter);
3915 }
3916}
3917
3918/*
3919 * Fill <*sni_filter> with an allocated array of ptr to the existing filters,
3920 * The caller should free <*sni_filter>.
3921 * Fill <*fcount> with the number of filters
3922 * Return an ERR_* code.
3923 */
3924static int ckch_inst_sni_ctx_to_sni_filters(const struct ckch_inst *ckchi, char ***sni_filter, int *fcount, char **err)
3925{
3926 struct sni_ctx *sc0;
3927 int errcode = 0;
3928 int i = 0;
3929 char **tmp_filter;
3930 int tmp_fcount = 0;
3931
3932 list_for_each_entry(sc0, &ckchi->sni_ctx, by_ckch_inst) {
3933 tmp_fcount++;
3934 }
3935
3936 if (!tmp_fcount)
3937 goto end;
3938
Tim Duesterhus8c120252020-03-19 16:12:09 +01003939 tmp_filter = calloc(tmp_fcount, sizeof(*tmp_filter));
William Lallemand38df1c82019-12-04 15:39:35 +01003940 if (!tmp_filter) {
3941 errcode |= ERR_FATAL|ERR_ALERT;
3942 goto error;
3943 }
3944
3945 list_for_each_entry(sc0, &ckchi->sni_ctx, by_ckch_inst) {
3946 size_t len = strlen((char *)sc0->name.key);
3947
3948 /* we need to alloc and copy to insert a '!' or/and a '*' */
3949 tmp_filter[i] = calloc(1, len + sc0->neg + sc0->wild + 1);
3950 if (!tmp_filter[i]) {
3951 errcode |= ERR_FATAL|ERR_ALERT;
3952 goto error;
3953 }
3954
3955 if (sc0->neg)
3956 *tmp_filter[i] = '!';
3957 if (sc0->wild)
3958 *(tmp_filter[i] + sc0->neg) = '*';
3959
3960 memcpy(tmp_filter[i] + sc0->neg + sc0->wild, (char *)sc0->name.key, len + 1);
3961 i++;
3962 }
William Lallemand38df1c82019-12-04 15:39:35 +01003963 *sni_filter = tmp_filter;
Willy Tarreaud04a2a62020-03-05 16:26:12 +01003964end:
William Lallemand38df1c82019-12-04 15:39:35 +01003965 *fcount = tmp_fcount;
3966
3967 return errcode;
3968error:
3969 memprintf(err, "%sUnable to generate filters!",
3970 err && *err ? *err : "");
3971 free_sni_filters(tmp_filter, tmp_fcount);
3972
3973 return errcode;
3974}
3975
3976
William Lallemandc4ecddf2019-07-31 16:50:08 +02003977#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3978
William Lallemand36b84632019-07-18 19:28:17 +02003979/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003980 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003981 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003982 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3983 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003984 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003985 *
Emeric Brun054563d2019-10-17 13:16:58 +02003986 * Returns a bitfield containing the flags:
3987 * ERR_FATAL in any fatal error case
3988 * ERR_ALERT if the reason of the error is available in err
3989 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003990 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003991 */
Emeric Brun054563d2019-10-17 13:16:58 +02003992static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3993 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3994 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003995{
William Lallemand36b84632019-07-18 19:28:17 +02003996 int i = 0, n = 0;
3997 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003998 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003999 struct ebmb_node *node;
4000 struct ebmb_node *next;
4001 /* Array of SSL_CTX pointers corresponding to each possible combo
4002 * of keytypes
4003 */
4004 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02004005 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004006 X509_NAME *xname = NULL;
4007 char *str = NULL;
4008#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4009 STACK_OF(GENERAL_NAME) *names = NULL;
4010#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02004011 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004012
Emeric Brun054563d2019-10-17 13:16:58 +02004013 *ckchi = NULL;
4014
William Lallemande3af8fb2019-10-08 11:36:53 +02004015 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02004016 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4017 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004018 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004019 }
4020
4021 ckch_inst = ckch_inst_new();
4022 if (!ckch_inst) {
4023 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4024 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004025 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004026 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004027 }
4028
William Lallemande3af8fb2019-10-08 11:36:53 +02004029 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004030
yanbzhu08ce6ab2015-12-02 13:01:29 -05004031 /* Process each ckch and update keytypes for each CN/SAN
4032 * for example, if CN/SAN www.a.com is associated with
4033 * certs with keytype 0 and 2, then at the end of the loop,
4034 * www.a.com will have:
4035 * keyindex = 0 | 1 | 4 = 5
4036 */
4037 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004038 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004039
4040 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4041 continue;
4042
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004043 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004044 for (i = 0; i < fcount; i++) {
4045 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4046 if (ret < 0) {
4047 memprintf(err, "%sunable to allocate SSL context.\n",
4048 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004049 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004050 goto end;
4051 }
4052 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004053 } else {
4054 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4055 * so the line that contains logic is marked via comments
4056 */
4057 xname = X509_get_subject_name(certs_and_keys[n].cert);
4058 i = -1;
4059 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4060 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004061 ASN1_STRING *value;
4062 value = X509_NAME_ENTRY_get_data(entry);
4063 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004064 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004065 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004066
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004067 OPENSSL_free(str);
4068 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004069 if (ret < 0) {
4070 memprintf(err, "%sunable to allocate SSL context.\n",
4071 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004072 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004073 goto end;
4074 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004075 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004076 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004077
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004078 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004079#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004080 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4081 if (names) {
4082 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4083 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004084
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004085 if (name->type == GEN_DNS) {
4086 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4087 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004088 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004089
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004090 OPENSSL_free(str);
4091 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004092 if (ret < 0) {
4093 memprintf(err, "%sunable to allocate SSL context.\n",
4094 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004095 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004096 goto end;
4097 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004098 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004099 }
4100 }
4101 }
4102 }
4103#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4104 }
4105
4106 /* If no files found, return error */
4107 if (eb_is_empty(&sni_keytypes_map)) {
4108 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4109 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004110 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004111 goto end;
4112 }
4113
4114 /* We now have a map of CN/SAN to keytypes that are loaded in
4115 * Iterate through the map to create the SSL_CTX's (if needed)
4116 * and add each CTX to the SNI tree
4117 *
4118 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004119 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004120 * combination is denoted by the key in the map. Each key
4121 * has a value between 1 and 2^n - 1. Conveniently, the array
4122 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4123 * entry in the array to correspond to the unique combo (key)
4124 * associated with i. This unique key combo (i) will be associated
4125 * with combos[i-1]
4126 */
4127
4128 node = ebmb_first(&sni_keytypes_map);
4129 while (node) {
4130 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004131 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004132 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004133
4134 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4135 i = container_of(node, struct sni_keytype, name)->keytypes;
4136 cur_ctx = key_combos[i-1].ctx;
4137
4138 if (cur_ctx == NULL) {
4139 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004140 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004141 if (cur_ctx == NULL) {
4142 memprintf(err, "%sunable to allocate SSL context.\n",
4143 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004144 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004145 goto end;
4146 }
4147
yanbzhube2774d2015-12-10 15:07:30 -05004148 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004149 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4150 if (i & (1<<n)) {
4151 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004152 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004153 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4154 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004155 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004156 }
4157 }
4158
yanbzhu08ce6ab2015-12-02 13:01:29 -05004159 /* Update key_combos */
4160 key_combos[i-1].ctx = cur_ctx;
4161 }
4162
4163 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004164
William Lallemand1d29c742019-10-04 00:53:29 +02004165 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 +02004166 kinfo, str, key_combos[i-1].order);
4167 if (key_combos[i-1].order < 0) {
4168 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004169 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004170 goto end;
4171 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004172 node = ebmb_next(node);
4173 }
4174
4175
4176 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4177 if (!bind_conf->default_ctx) {
4178 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4179 if (key_combos[i].ctx) {
4180 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004181 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004182 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004183 break;
4184 }
4185 }
4186 }
4187
William Lallemand614ca0d2019-10-07 13:52:11 +02004188 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004189 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004190 ckch_inst->ckch_store = ckchs;
William Lallemand67630162020-03-09 16:56:39 +01004191 ckch_inst->filters = !!fcount;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004192end:
4193
4194 if (names)
4195 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4196
yanbzhu08ce6ab2015-12-02 13:01:29 -05004197 node = ebmb_first(&sni_keytypes_map);
4198 while (node) {
4199 next = ebmb_next(node);
4200 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004201 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004202 node = next;
4203 }
4204
Emeric Brun054563d2019-10-17 13:16:58 +02004205 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004206 struct sni_ctx *sc0, *sc0b;
4207
4208 /* free the SSL_CTX in case of error */
4209 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4210 if (key_combos[i].ctx)
4211 SSL_CTX_free(key_combos[i].ctx);
4212 }
4213
4214 /* free the sni_ctx in case of error */
4215 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4216
4217 ebmb_delete(&sc0->name);
4218 LIST_DEL(&sc0->by_ckch_inst);
4219 free(sc0);
4220 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004221 free(ckch_inst);
4222 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004223 }
4224
Emeric Brun054563d2019-10-17 13:16:58 +02004225 *ckchi = ckch_inst;
4226 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004227}
4228#else
4229/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004230static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4231 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4232 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004233{
4234 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4235 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004236 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004237}
4238
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004239#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004240
William Lallemand614ca0d2019-10-07 13:52:11 +02004241/*
4242 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004243 *
4244 * Returns a bitfield containing the flags:
4245 * ERR_FATAL in any fatal error case
4246 * ERR_ALERT if the reason of the error is available in err
4247 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004248 */
Emeric Brun054563d2019-10-17 13:16:58 +02004249static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4250 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004251{
William Lallemandc9402072019-05-15 15:33:54 +02004252 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004253 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004254 int order = 0;
4255 X509_NAME *xname;
4256 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004257 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004258 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004259#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4260 STACK_OF(GENERAL_NAME) *names;
4261#endif
William Lallemand36b84632019-07-18 19:28:17 +02004262 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004263 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004264 int errcode = 0;
4265
4266 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004267
William Lallemande3af8fb2019-10-08 11:36:53 +02004268 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004269 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004270
William Lallemande3af8fb2019-10-08 11:36:53 +02004271 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004272
William Lallemandc9402072019-05-15 15:33:54 +02004273 ctx = SSL_CTX_new(SSLv23_server_method());
4274 if (!ctx) {
4275 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4276 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004277 errcode |= ERR_ALERT | ERR_FATAL;
4278 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004279 }
4280
Emeric Bruna96b5822019-10-17 13:25:14 +02004281 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4282 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004283 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004284
4285 ckch_inst = ckch_inst_new();
4286 if (!ckch_inst) {
4287 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4288 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004289 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004290 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004291 }
4292
William Lallemand36b84632019-07-18 19:28:17 +02004293 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004294 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004295 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004296 switch(EVP_PKEY_base_id(pkey)) {
4297 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004298 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004299 break;
4300 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004301 kinfo.sig = TLSEXT_signature_ecdsa;
4302 break;
4303 case EVP_PKEY_DSA:
4304 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004305 break;
4306 }
4307 EVP_PKEY_free(pkey);
4308 }
4309
Emeric Brun50bcecc2013-04-22 13:05:23 +02004310 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004311 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004312 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 +02004313 if (order < 0) {
4314 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004315 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004316 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004317 }
4318 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004319 }
4320 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004321#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004322 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004323 if (names) {
4324 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4325 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4326 if (name->type == GEN_DNS) {
4327 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004328 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004329 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004330 if (order < 0) {
4331 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004332 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004333 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004334 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004335 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004336 }
4337 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004338 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004339 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004340#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004341 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004342 i = -1;
4343 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4344 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004345 ASN1_STRING *value;
4346
4347 value = X509_NAME_ENTRY_get_data(entry);
4348 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004349 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004350 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004351 if (order < 0) {
4352 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004353 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004354 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004355 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004356 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004357 }
4358 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004359 /* we must not free the SSL_CTX anymore below, since it's already in
4360 * the tree, so it will be discovered and cleaned in time.
4361 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004362
Emeric Brunfc0421f2012-09-07 17:30:07 +02004363#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004364 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004365 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4366 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004367 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004368 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004369 }
4370#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004371 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004372 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004373 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004374 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004375 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004376
William Lallemand9117de92019-10-04 00:29:42 +02004377 /* everything succeed, the ckch instance can be used */
4378 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004379 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004380 ckch_inst->ckch_store = ckchs;
William Lallemand67630162020-03-09 16:56:39 +01004381 ckch_inst->filters = !!fcount;
William Lallemand9117de92019-10-04 00:29:42 +02004382
Emeric Brun054563d2019-10-17 13:16:58 +02004383 *ckchi = ckch_inst;
4384 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004385
4386error:
4387 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004388 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004389 struct sni_ctx *sc0, *sc0b;
4390
4391 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4392
4393 ebmb_delete(&sc0->name);
4394 LIST_DEL(&sc0->by_ckch_inst);
4395 free(sc0);
4396 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004397 free(ckch_inst);
4398 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004399 }
4400 /* We only created 1 SSL_CTX so we can free it there */
4401 SSL_CTX_free(ctx);
4402
Emeric Brun054563d2019-10-17 13:16:58 +02004403 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004404}
4405
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004406/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004407static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4408 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004409 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004410{
Emeric Brun054563d2019-10-17 13:16:58 +02004411 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004412
4413 /* we found the ckchs in the tree, we can use it directly */
4414 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004415 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 +02004416 else
William Lallemand24bde432020-03-09 16:48:43 +01004417 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 +02004418
Emeric Brun054563d2019-10-17 13:16:58 +02004419 if (errcode & ERR_CODE)
4420 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004421
William Lallemand24bde432020-03-09 16:48:43 +01004422 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004423
4424 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004425 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004426 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004427}
4428
William Lallemand6be66ec2020-03-06 22:26:32 +01004429
William Lallemand4c68bba2020-03-30 18:45:10 +02004430
4431
4432/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4433 * done once. Zero is returned if the operation fails. No error is returned
4434 * if the random is said as not implemented, because we expect that openssl
4435 * will use another method once needed.
4436 */
4437static int ssl_initialize_random()
4438{
4439 unsigned char random;
4440 static int random_initialized = 0;
4441
4442 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4443 random_initialized = 1;
4444
4445 return random_initialized;
4446}
4447
4448/* release ssl bind conf */
4449void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
4450{
4451 if (conf) {
4452#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4453 free(conf->npn_str);
4454 conf->npn_str = NULL;
4455#endif
4456#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4457 free(conf->alpn_str);
4458 conf->alpn_str = NULL;
4459#endif
4460 free(conf->ca_file);
4461 conf->ca_file = NULL;
4462 free(conf->ca_verify_file);
4463 conf->ca_verify_file = NULL;
4464 free(conf->crl_file);
4465 conf->crl_file = NULL;
4466 free(conf->ciphers);
4467 conf->ciphers = NULL;
4468#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
4469 free(conf->ciphersuites);
4470 conf->ciphersuites = NULL;
4471#endif
4472 free(conf->curves);
4473 conf->curves = NULL;
4474 free(conf->ecdhe);
4475 conf->ecdhe = NULL;
4476 }
4477}
4478/* free sni filters */
4479static void crtlist_free_filters(char **args)
4480{
4481 int i;
4482
4483 if (!args)
4484 return;
4485
4486 for (i = 0; args[i]; i++)
4487 free(args[i]);
4488
4489 free(args);
4490}
4491
4492/* Alloc and duplicate a char ** array */
4493static char **crtlist_dup_filters(char **args, int fcount)
4494{
4495 char **dst;
4496 int i;
4497
4498 if (fcount == 0)
4499 return NULL;
4500
4501 dst = calloc(fcount + 1, sizeof(*dst));
4502 if (!dst)
4503 return NULL;
4504
4505 for (i = 0; i < fcount; i++) {
4506 dst[i] = strdup(args[i]);
4507 if (!dst[i])
4508 goto error;
4509 }
4510 return dst;
4511
4512error:
4513 crtlist_free_filters(dst);
4514 return NULL;
4515}
4516
4517
4518/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
4519static void crtlist_free(struct crtlist *crtlist)
4520{
4521 struct crtlist_entry *entry, *s_entry;
4522
4523 if (crtlist == NULL)
4524 return;
4525
4526 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
4527 ebpt_delete(&entry->node);
4528 LIST_DEL(&entry->by_crtlist);
4529 crtlist_free_filters(entry->filters);
4530 ssl_sock_free_ssl_conf(entry->ssl_conf);
4531 free(entry->ssl_conf);
4532 free(entry);
4533 }
4534 ebmb_delete(&crtlist->node);
4535 free(crtlist);
4536}
4537
William Lallemand6be66ec2020-03-06 22:26:32 +01004538/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4539 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4540 *
4541 * This function tries to open and store certificate files.
4542 */
4543static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004544{
William Lallemand6be66ec2020-03-06 22:26:32 +01004545 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004546 struct dirent **de_list;
4547 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004548 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004549 char *end;
4550 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004551 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004552 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004553#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004554 int is_bundle;
4555 int j;
4556#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004557
William Lallemand2d232c22020-03-06 22:12:35 +01004558 /* strip trailing slashes, including first one */
4559 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4560 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004561
William Lallemand6be66ec2020-03-06 22:26:32 +01004562 dir = malloc(sizeof(*dir) + strlen(path) + 1);
4563 if (dir == NULL) {
4564 memprintf(err, "not enough memory");
4565 return ERR_ALERT | ERR_FATAL;
4566 }
4567 memcpy(dir->node.key, path, strlen(path) + 1);
4568 dir->entries = EB_ROOT_UNIQUE; /* it's a directory, files are unique */
William Lallemand79d31ec2020-03-25 15:10:49 +01004569 dir->bind_conf = NULL;
William Lallemand83918e22020-03-16 17:21:51 +01004570 LIST_INIT(&dir->ord_entries);
William Lallemand6be66ec2020-03-06 22:26:32 +01004571
William Lallemand2d232c22020-03-06 22:12:35 +01004572 n = scandir(path, &de_list, 0, alphasort);
4573 if (n < 0) {
4574 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4575 err && *err ? *err : "", path, strerror(errno));
4576 cfgerr |= ERR_ALERT | ERR_FATAL;
4577 }
4578 else {
4579 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004580 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004581 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004582
William Lallemand2d232c22020-03-06 22:12:35 +01004583 end = strrchr(de->d_name, '.');
4584 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4585 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004586
William Lallemanda64593c2020-03-17 20:11:41 +01004587 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4588 if (stat(fp, &buf) != 0) {
4589 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4590 err && *err ? *err : "", fp, strerror(errno));
4591 cfgerr |= ERR_ALERT | ERR_FATAL;
4592 goto ignore_entry;
4593 }
4594 if (!S_ISREG(buf.st_mode))
4595 goto ignore_entry;
4596
William Lallemand6be66ec2020-03-06 22:26:32 +01004597 entry = malloc(sizeof(*entry));
4598 if (entry == NULL) {
4599 memprintf(err, "not enough memory '%s'", fp);
4600 cfgerr |= ERR_ALERT | ERR_FATAL;
4601 goto ignore_entry;
4602 }
4603
William Lallemand83918e22020-03-16 17:21:51 +01004604 /* directories don't use ssl_conf and filters */
4605 entry->fcount = 0;
4606 entry->filters = NULL;
4607 entry->ssl_conf = NULL;
William Lallemandc2e3b722020-03-31 14:31:41 +02004608 LIST_INIT(&entry->ckch_inst);
William Lallemand83918e22020-03-16 17:21:51 +01004609
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004610#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004611 is_bundle = 0;
4612 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004613
William Lallemand2d232c22020-03-06 22:12:35 +01004614 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4615 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4616 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4617 is_bundle = 1;
4618 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004619 }
William Lallemand2d232c22020-03-06 22:12:35 +01004620 }
yanbzhu63ea8462015-12-09 13:35:14 -05004621
William Lallemand2d232c22020-03-06 22:12:35 +01004622 if (is_bundle) {
4623 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004624
William Lallemand2d232c22020-03-06 22:12:35 +01004625 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004626
William Lallemand2d232c22020-03-06 22:12:35 +01004627 /* increment i and free de until we get to a non-bundle cert
4628 * Note here that we look at de_list[i + 1] before freeing de
4629 * this is important since ignore_entry will free de. This also
4630 * guarantees that de->d_name continues to hold the same prefix.
4631 */
4632 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4633 free(de);
4634 i++;
4635 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004636 }
William Lallemand2d232c22020-03-06 22:12:35 +01004637
4638 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004639 ckchs = ckchs_lookup(fp);
4640 if (ckchs == NULL)
4641 ckchs = ckchs_load_cert_file(fp, 1, err);
4642 if (ckchs == NULL) {
4643 free(de);
4644 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004645 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004646 goto end;
4647 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004648 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004649 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004650 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004651 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004652 ebpt_insert(&dir->entries, &entry->node);
4653
William Lallemand2d232c22020-03-06 22:12:35 +01004654 /* Successfully processed the bundle */
4655 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004656 }
William Lallemand2d232c22020-03-06 22:12:35 +01004657 }
yanbzhu63ea8462015-12-09 13:35:14 -05004658
4659#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004660 ckchs = ckchs_lookup(fp);
4661 if (ckchs == NULL)
4662 ckchs = ckchs_load_cert_file(fp, 0, err);
4663 if (ckchs == NULL) {
4664 free(de);
4665 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004666 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004667 goto end;
4668 }
4669 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004670 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004671 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004672 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004673 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004674
yanbzhu08ce6ab2015-12-02 13:01:29 -05004675ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004676 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004677 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004678end:
William Lallemand2d232c22020-03-06 22:12:35 +01004679 free(de_list);
4680 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004681
4682 if (cfgerr & ERR_CODE) {
4683 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004684 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004685 } else {
4686 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004687 }
William Lallemand2d232c22020-03-06 22:12:35 +01004688 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004689
William Lallemand2d232c22020-03-06 22:12:35 +01004690}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004691
William Lallemand97b08102020-04-01 10:14:00 +02004692/*
4693 * Read a single crt-list line. /!\ alter the <line> string.
4694 * Fill <crt_path> and <crtlist_entry>
4695 * <crtlist_entry> must be alloc and free by the caller
4696 * <crtlist_entry->ssl_conf> is alloc by the function
4697 * <crtlist_entry->filters> is alloc by the function
4698 * <crt_path> is a ptr in <line>
4699 * Return an error code
4700 */
4701static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4702{
4703 int cfgerr = 0;
4704 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4705 char *end;
4706 char *args[MAX_CRT_ARGS + 1];
4707 struct ssl_bind_conf *ssl_conf = NULL;
4708
4709 if (!line || !crt_path || !entry)
4710 return ERR_ALERT | ERR_FATAL;
4711
4712 end = line + strlen(line);
4713 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4714 /* Check if we reached the limit and the last char is not \n.
4715 * Watch out for the last line without the terminating '\n'!
4716 */
4717 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4718 linenum, file, CRT_LINESIZE-1);
4719 cfgerr |= ERR_ALERT | ERR_FATAL;
4720 goto error;
4721 }
4722 arg = 0;
4723 newarg = 1;
4724 while (*line) {
4725 if (isspace((unsigned char)*line)) {
4726 newarg = 1;
4727 *line = 0;
4728 } else if (*line == '[') {
4729 if (ssl_b) {
4730 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4731 cfgerr |= ERR_ALERT | ERR_FATAL;
4732 goto error;
4733 }
4734 if (!arg) {
4735 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4736 cfgerr |= ERR_ALERT | ERR_FATAL;
4737 goto error;
4738 }
4739 ssl_b = arg;
4740 newarg = 1;
4741 *line = 0;
4742 } else if (*line == ']') {
4743 if (ssl_e) {
4744 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4745 cfgerr |= ERR_ALERT | ERR_FATAL;
4746 goto error;
4747 }
4748 if (!ssl_b) {
4749 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4750 cfgerr |= ERR_ALERT | ERR_FATAL;
4751 goto error;
4752 }
4753 ssl_e = arg;
4754 newarg = 1;
4755 *line = 0;
4756 } else if (newarg) {
4757 if (arg == MAX_CRT_ARGS) {
4758 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4759 cfgerr |= ERR_ALERT | ERR_FATAL;
4760 goto error;
4761 }
4762 newarg = 0;
4763 args[arg++] = line;
4764 }
4765 line++;
4766 }
4767 args[arg++] = line;
4768
4769 /* empty line */
4770 if (!*args[0]) {
4771 cfgerr |= ERR_NONE;
4772 goto error;
4773 }
4774
4775 *crt_path = args[0];
4776
4777 ssl_conf = calloc(1, sizeof *ssl_conf);
4778 if (!ssl_conf) {
4779 memprintf(err, "not enough memory!");
4780 cfgerr |= ERR_ALERT | ERR_FATAL;
4781 goto error;
4782 }
4783 cur_arg = ssl_b ? ssl_b : 1;
4784 while (cur_arg < ssl_e) {
4785 newarg = 0;
4786 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4787 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4788 newarg = 1;
4789 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4790 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4791 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4792 args[cur_arg], linenum, file);
4793 cfgerr |= ERR_ALERT | ERR_FATAL;
4794 goto error;
4795 }
4796 cur_arg += 1 + ssl_bind_kws[i].skip;
4797 break;
4798 }
4799 }
4800 if (!cfgerr && !newarg) {
4801 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4802 args[cur_arg], linenum, file);
4803 cfgerr |= ERR_ALERT | ERR_FATAL;
4804 goto error;
4805 }
4806 }
4807
4808 entry->ssl_conf = ssl_conf;
4809 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4810 entry->fcount = arg - cur_arg - 1;
4811
4812 return cfgerr;
4813
4814error:
4815 crtlist_free_filters(entry->filters);
4816 entry->filters = NULL;
4817 ssl_sock_free_ssl_conf(entry->ssl_conf);
4818 free(entry->ssl_conf);
4819 entry->ssl_conf = NULL;
4820 return cfgerr;
4821}
4822
William Lallemand2954c472020-03-06 21:54:13 +01004823/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4824 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4825 *
4826 * This function tries to open and store certificate files.
4827 */
4828static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4829{
4830 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004831 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004832 char thisline[CRT_LINESIZE];
4833 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004834 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004835 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004836 int linenum = 0;
4837 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004838
Willy Tarreauad1731d2013-04-02 17:35:58 +02004839 if ((f = fopen(file, "r")) == NULL) {
4840 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004841 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004842 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004843
William Lallemand2954c472020-03-06 21:54:13 +01004844 newlist = malloc(sizeof(*newlist) + strlen(file) + 1);
4845 if (newlist == NULL) {
4846 memprintf(err, "Not enough memory!");
4847 cfgerr |= ERR_ALERT | ERR_FATAL;
4848 goto error;
4849 }
4850 memcpy(newlist->node.key, file, strlen(file) + 1);
4851 newlist->entries = EB_ROOT;
William Lallemand79d31ec2020-03-25 15:10:49 +01004852 newlist->bind_conf = NULL;
William Lallemand97b08102020-04-01 10:14:00 +02004853 newlist->node.node.leaf_p = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004854 LIST_INIT(&newlist->ord_entries);
4855
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004856 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004857 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004858 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004859 char *crt_path;
4860 struct ssl_bind_conf *ssl_conf = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004861 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004862
4863 linenum++;
4864 end = line + strlen(line);
4865 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4866 /* Check if we reached the limit and the last char is not \n.
4867 * Watch out for the last line without the terminating '\n'!
4868 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004869 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4870 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004871 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004872 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004873 }
William Lallemand97b08102020-04-01 10:14:00 +02004874
4875 if (*line == '#' || *line == '\n' || *line == '\r')
4876 continue;
4877
4878 entry = malloc(sizeof(*entry));
4879 if (entry == NULL) {
4880 memprintf(err, "Not enough memory!");
4881 cfgerr |= ERR_ALERT | ERR_FATAL;
4882 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004883 }
William Lallemand97b08102020-04-01 10:14:00 +02004884 entry->fcount = 0;
4885 entry->filters = NULL;
4886 entry->ssl_conf = NULL;
4887 entry->node.key = NULL;
4888 LIST_INIT(&entry->by_crtlist);
4889
4890 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4891 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004892 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004893 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004894
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004895 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004896 if (!crt_path || !*crt_path) {
4897 free(entry);
4898 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004899 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004900 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004901
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004902 if (*crt_path != '/' && global_ssl.crt_base) {
4903 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4904 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4905 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004906 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004907 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004908 }
4909 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4910 crt_path = path;
4911 }
4912
William Lallemand2954c472020-03-06 21:54:13 +01004913 /* Look for a ckch_store or create one */
4914 ckchs = ckchs_lookup(crt_path);
4915 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004916 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004917 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004918 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004919 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004920 }
William Lallemand909086e2020-03-17 16:53:27 +01004921 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004922 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004923
William Lallemand97b08102020-04-01 10:14:00 +02004924 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004925 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004926
William Lallemand2954c472020-03-06 21:54:13 +01004927 entry->node.key = ckchs;
4928 entry->ssl_conf = ssl_conf;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004929 entry->crtlist = newlist;
William Lallemandc2e3b722020-03-31 14:31:41 +02004930 LIST_INIT(&entry->ckch_inst);
William Lallemand2954c472020-03-06 21:54:13 +01004931 ebpt_insert(&newlist->entries, &entry->node);
4932 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004933 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004934
4935 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004936 }
William Lallemand2954c472020-03-06 21:54:13 +01004937 if (cfgerr & ERR_CODE)
4938 goto error;
4939
4940 fclose(f);
4941 *crtlist = newlist;
4942
4943 return cfgerr;
4944error:
William Lallemand97b08102020-04-01 10:14:00 +02004945 if (entry) {
4946 crtlist_free_filters(entry->filters);
4947 entry->filters = NULL;
4948 ssl_sock_free_ssl_conf(entry->ssl_conf);
4949 free(entry->ssl_conf);
4950 entry->ssl_conf = NULL;
4951 free(entry);
4952 }
4953
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004954 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004955 crtlist_free(newlist);
4956 return cfgerr;
4957}
4958
4959/* Load a crt-list file, this is done in 2 parts:
4960 * - store the content of the file in a crtlist structure with crtlist_entry structures
4961 * - generate the instances by iterating on entries in the crtlist struct
4962 *
4963 * Nothing is locked there, this function is used in the configuration parser.
4964 *
4965 * Returns a set of ERR_* flags possibly with an error in <err>.
4966 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004967int 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 +01004968{
4969 struct crtlist *crtlist = NULL;
4970 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004971 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004972 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004973 int cfgerr = 0;
4974
William Lallemand79d31ec2020-03-25 15:10:49 +01004975 bind_conf_node = malloc(sizeof(*bind_conf_node));
4976 if (!bind_conf_node) {
4977 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4978 cfgerr |= ERR_FATAL | ERR_ALERT;
4979 goto error;
4980 }
4981 bind_conf_node->next = NULL;
4982 bind_conf_node->bind_conf = bind_conf;
4983
William Lallemand2954c472020-03-06 21:54:13 +01004984 /* look for an existing crtlist or create one */
4985 eb = ebst_lookup(&crtlists_tree, file);
4986 if (eb) {
4987 crtlist = ebmb_entry(eb, struct crtlist, node);
4988 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004989 /* load a crt-list OR a directory */
4990 if (dir)
4991 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4992 else
4993 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4994
William Lallemand2954c472020-03-06 21:54:13 +01004995 if (!(cfgerr & ERR_CODE))
4996 ebst_insert(&crtlists_tree, &crtlist->node);
4997 }
4998
4999 if (cfgerr & ERR_CODE) {
5000 cfgerr |= ERR_FATAL | ERR_ALERT;
5001 goto error;
5002 }
5003
5004 /* generates ckch instance from the crtlist_entry */
5005 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5006 struct ckch_store *store;
5007 struct ckch_inst *ckch_inst = NULL;
5008
5009 store = entry->node.key;
5010 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
5011 if (cfgerr & ERR_CODE) {
5012 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
5013 goto error;
5014 }
William Lallemand49398312020-03-30 17:01:33 +02005015 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemand2954c472020-03-06 21:54:13 +01005016 }
William Lallemand2954c472020-03-06 21:54:13 +01005017
William Lallemand79d31ec2020-03-25 15:10:49 +01005018 /* add the bind_conf to the list */
5019 bind_conf_node->next = crtlist->bind_conf;
5020 crtlist->bind_conf = bind_conf_node;
5021
William Lallemand2954c472020-03-06 21:54:13 +01005022 return cfgerr;
5023error:
5024 {
William Lallemand49398312020-03-30 17:01:33 +02005025 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01005026 struct ckch_inst *inst, *s_inst;
5027
William Lallemand49398312020-03-30 17:01:33 +02005028 lastentry = entry; /* which entry we tried to generate last */
5029 if (lastentry) {
5030 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5031 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
5032 break;
5033
5034 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
5035 struct sni_ctx *sni, *s_sni;
William Lallemand2954c472020-03-06 21:54:13 +01005036
William Lallemand49398312020-03-30 17:01:33 +02005037 /* this was not generated for this bind_conf, skip */
5038 if (inst->bind_conf != bind_conf)
5039 continue;
5040
5041 /* free the sni_ctx */
5042 list_for_each_entry_safe(sni, s_sni, &inst->sni_ctx, by_ckch_inst) {
5043 ebmb_delete(&sni->name);
5044 LIST_DEL(&sni->by_ckch_inst);
5045 free(sni);
5046 }
5047 LIST_DEL(&inst->by_crtlist_entry);
5048 free(inst);
5049 }
William Lallemand2954c472020-03-06 21:54:13 +01005050 }
William Lallemand2954c472020-03-06 21:54:13 +01005051 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005052 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005053 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005054 return cfgerr;
5055}
5056
William Lallemand06b22a82020-03-16 14:45:55 +01005057/* Returns a set of ERR_* flags possibly with an error in <err>. */
5058int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5059{
5060 struct stat buf;
5061 char fp[MAXPATHLEN+1];
5062 int cfgerr = 0;
5063 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005064 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005065
5066 if ((ckchs = ckchs_lookup(path))) {
5067 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005068 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005069 }
5070 if (stat(path, &buf) == 0) {
5071 if (S_ISDIR(buf.st_mode) == 0) {
5072 ckchs = ckchs_load_cert_file(path, 0, err);
5073 if (!ckchs)
5074 return ERR_ALERT | ERR_FATAL;
5075
William Lallemand24bde432020-03-09 16:48:43 +01005076 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005077 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005078 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005079 }
5080 } else {
5081 /* stat failed, could be a bundle */
5082 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5083 /* try to load a bundle if it is permitted */
5084 ckchs = ckchs_load_cert_file(path, 1, err);
5085 if (!ckchs)
5086 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005087 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005088 } else {
5089 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5090 err && *err ? *err : "", fp, strerror(errno));
5091 cfgerr |= ERR_ALERT | ERR_FATAL;
5092 }
5093 }
5094
5095 return cfgerr;
5096}
5097
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005098/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005099static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005100ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005101{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005102 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005103 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005104 SSL_OP_ALL | /* all known workarounds for bugs */
5105 SSL_OP_NO_SSLv2 |
5106 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005107 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005108 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005109 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005110 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005111 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005112 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005113 SSL_MODE_ENABLE_PARTIAL_WRITE |
5114 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005115 SSL_MODE_RELEASE_BUFFERS |
5116 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005117 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005118 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005119 int flags = MC_SSL_O_ALL;
5120 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005121
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005122 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005123 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005124
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005125 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005126 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5127 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5128 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005129 else
5130 flags = conf_ssl_methods->flags;
5131
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005132 min = conf_ssl_methods->min;
5133 max = conf_ssl_methods->max;
5134 /* start with TLSv10 to remove SSLv3 per default */
5135 if (!min && (!max || max >= CONF_TLSV10))
5136 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005137 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005138 if (min)
5139 flags |= (methodVersions[min].flag - 1);
5140 if (max)
5141 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005142 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005143 min = max = CONF_TLSV_NONE;
5144 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005145 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005146 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005147 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005148 if (min) {
5149 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005150 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5151 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5152 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5153 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005154 hole = 0;
5155 }
5156 max = i;
5157 }
5158 else {
5159 min = max = i;
5160 }
5161 }
5162 else {
5163 if (min)
5164 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005165 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005166 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005167 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5168 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005169 cfgerr += 1;
5170 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005171 /* save real min/max in bind_conf */
5172 conf_ssl_methods->min = min;
5173 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005174
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005175#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005176 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005177 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005178 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005179 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005180 else
5181 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5182 if (flags & methodVersions[i].flag)
5183 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005184#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005185 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005186 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5187 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005188#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005189
5190 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5191 options |= SSL_OP_NO_TICKET;
5192 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5193 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005194
5195#ifdef SSL_OP_NO_RENEGOTIATION
5196 options |= SSL_OP_NO_RENEGOTIATION;
5197#endif
5198
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005199 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005200
Willy Tarreau5db847a2019-05-09 14:13:35 +02005201#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005202 if (global_ssl.async)
5203 mode |= SSL_MODE_ASYNC;
5204#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005205 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005206 if (global_ssl.life_time)
5207 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005208
5209#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5210#ifdef OPENSSL_IS_BORINGSSL
5211 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5212 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005213#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005214 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005215 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005216 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5217 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005218#else
5219 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005220#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005221 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005222#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005223 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005224}
5225
William Lallemand4f45bb92017-10-30 20:08:51 +01005226
5227static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5228{
5229 if (first == block) {
5230 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5231 if (first->len > 0)
5232 sh_ssl_sess_tree_delete(sh_ssl_sess);
5233 }
5234}
5235
5236/* return first block from sh_ssl_sess */
5237static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5238{
5239 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5240
5241}
5242
5243/* store a session into the cache
5244 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5245 * data: asn1 encoded session
5246 * data_len: asn1 encoded session length
5247 * Returns 1 id session was stored (else 0)
5248 */
5249static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5250{
5251 struct shared_block *first;
5252 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5253
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005254 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005255 if (!first) {
5256 /* Could not retrieve enough free blocks to store that session */
5257 return 0;
5258 }
5259
5260 /* STORE the key in the first elem */
5261 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5262 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5263 first->len = sizeof(struct sh_ssl_sess_hdr);
5264
5265 /* it returns the already existing node
5266 or current node if none, never returns null */
5267 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5268 if (oldsh_ssl_sess != sh_ssl_sess) {
5269 /* NOTE: Row couldn't be in use because we lock read & write function */
5270 /* release the reserved row */
5271 shctx_row_dec_hot(ssl_shctx, first);
5272 /* replace the previous session already in the tree */
5273 sh_ssl_sess = oldsh_ssl_sess;
5274 /* ignore the previous session data, only use the header */
5275 first = sh_ssl_sess_first_block(sh_ssl_sess);
5276 shctx_row_inc_hot(ssl_shctx, first);
5277 first->len = sizeof(struct sh_ssl_sess_hdr);
5278 }
5279
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005280 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005281 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005282 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005283 }
5284
5285 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005286
5287 return 1;
5288}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005289
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005290/* SSL callback used when a new session is created while connecting to a server */
5291static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5292{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005293 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005294 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005295
Willy Tarreau07d94e42018-09-20 10:57:52 +02005296 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005297
Olivier Houcharde6060c52017-11-16 17:42:52 +01005298 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5299 int len;
5300 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005301
Olivier Houcharde6060c52017-11-16 17:42:52 +01005302 len = i2d_SSL_SESSION(sess, NULL);
5303 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5304 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5305 } else {
5306 free(s->ssl_ctx.reused_sess[tid].ptr);
5307 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5308 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5309 }
5310 if (s->ssl_ctx.reused_sess[tid].ptr) {
5311 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5312 &ptr);
5313 }
5314 } else {
5315 free(s->ssl_ctx.reused_sess[tid].ptr);
5316 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5317 }
5318
5319 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005320}
5321
Olivier Houcharde6060c52017-11-16 17:42:52 +01005322
William Lallemanded0b5ad2017-10-30 19:36:36 +01005323/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005324int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005325{
5326 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5327 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5328 unsigned char *p;
5329 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005330 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005331 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005332
5333 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005334 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005335 * note: SSL_SESSION_set1_id is using
5336 * a memcpy so we need to use a different pointer
5337 * than sid_data or sid_ctx_data to avoid valgrind
5338 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005339 */
5340
5341 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005342
5343 /* copy value in an other buffer */
5344 memcpy(encid, sid_data, sid_length);
5345
5346 /* pad with 0 */
5347 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5348 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5349
5350 /* force length to zero to avoid ASN1 encoding */
5351 SSL_SESSION_set1_id(sess, encid, 0);
5352
5353 /* force length to zero to avoid ASN1 encoding */
5354 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005355
5356 /* check if buffer is large enough for the ASN1 encoded session */
5357 data_len = i2d_SSL_SESSION(sess, NULL);
5358 if (data_len > SHSESS_MAX_DATA_LEN)
5359 goto err;
5360
5361 p = encsess;
5362
5363 /* process ASN1 session encoding before the lock */
5364 i2d_SSL_SESSION(sess, &p);
5365
William Lallemanded0b5ad2017-10-30 19:36:36 +01005366
William Lallemanda3c77cf2017-10-30 23:44:40 +01005367 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005368 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005369 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005370 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005371err:
5372 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005373 SSL_SESSION_set1_id(sess, encid, sid_length);
5374 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005375
5376 return 0; /* do not increment session reference count */
5377}
5378
5379/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005380SSL_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 +01005381{
William Lallemand4f45bb92017-10-30 20:08:51 +01005382 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005383 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5384 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005385 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005386 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005387
5388 global.shctx_lookups++;
5389
5390 /* allow the session to be freed automatically by openssl */
5391 *do_copy = 0;
5392
5393 /* tree key is zeros padded sessionid */
5394 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5395 memcpy(tmpkey, key, key_len);
5396 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5397 key = tmpkey;
5398 }
5399
5400 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005401 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005402
5403 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005404 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5405 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005406 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005407 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005408 global.shctx_misses++;
5409 return NULL;
5410 }
5411
William Lallemand4f45bb92017-10-30 20:08:51 +01005412 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5413 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005414
William Lallemand4f45bb92017-10-30 20:08:51 +01005415 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 +01005416
William Lallemanda3c77cf2017-10-30 23:44:40 +01005417 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005418
5419 /* decode ASN1 session */
5420 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005421 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005422 /* Reset session id and session id contenxt */
5423 if (sess) {
5424 SSL_SESSION_set1_id(sess, key, key_len);
5425 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5426 }
5427
5428 return sess;
5429}
5430
William Lallemand4f45bb92017-10-30 20:08:51 +01005431
William Lallemanded0b5ad2017-10-30 19:36:36 +01005432/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005433void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005434{
William Lallemand4f45bb92017-10-30 20:08:51 +01005435 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005436 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5437 unsigned int sid_length;
5438 const unsigned char *sid_data;
5439 (void)ctx;
5440
5441 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5442 /* tree key is zeros padded sessionid */
5443 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5444 memcpy(tmpkey, sid_data, sid_length);
5445 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5446 sid_data = tmpkey;
5447 }
5448
William Lallemanda3c77cf2017-10-30 23:44:40 +01005449 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005450
5451 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005452 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5453 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005454 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005455 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005456 }
5457
5458 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005459 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005460}
5461
5462/* Set session cache mode to server and disable openssl internal cache.
5463 * Set shared cache callbacks on an ssl context.
5464 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005465void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005466{
5467 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5468
5469 if (!ssl_shctx) {
5470 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5471 return;
5472 }
5473
5474 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5475 SSL_SESS_CACHE_NO_INTERNAL |
5476 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5477
5478 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005479 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5480 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5481 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005482}
5483
William Lallemand8b453912019-11-21 15:48:10 +01005484/*
5485 * This function applies the SSL configuration on a SSL_CTX
5486 * It returns an error code and fills the <err> buffer
5487 */
5488int 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 +01005489{
5490 struct proxy *curproxy = bind_conf->frontend;
5491 int cfgerr = 0;
5492 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005493 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005494 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005495#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005496 const char *conf_ciphersuites;
5497#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005498 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005499
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005500 if (ssl_conf) {
5501 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5502 int i, min, max;
5503 int flags = MC_SSL_O_ALL;
5504
5505 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005506 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5507 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005508 if (min)
5509 flags |= (methodVersions[min].flag - 1);
5510 if (max)
5511 flags |= ~((methodVersions[max].flag << 1) - 1);
5512 min = max = CONF_TLSV_NONE;
5513 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5514 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5515 if (min)
5516 max = i;
5517 else
5518 min = max = i;
5519 }
5520 /* save real min/max */
5521 conf_ssl_methods->min = min;
5522 conf_ssl_methods->max = max;
5523 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005524 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5525 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005526 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005527 }
5528 }
5529
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005530 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005531 case SSL_SOCK_VERIFY_NONE:
5532 verify = SSL_VERIFY_NONE;
5533 break;
5534 case SSL_SOCK_VERIFY_OPTIONAL:
5535 verify = SSL_VERIFY_PEER;
5536 break;
5537 case SSL_SOCK_VERIFY_REQUIRED:
5538 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5539 break;
5540 }
5541 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5542 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005543 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 +01005544 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 +01005545 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 +01005546 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005547 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005548 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005549 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005550 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005551 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005552 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005553 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5554 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5555 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5556 cfgerr |= ERR_ALERT | ERR_FATAL;
5557 }
5558 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005559 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005560 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 +02005561 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005562 }
Emeric Brun850efd52014-01-29 12:24:34 +01005563 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005564 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5565 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005566 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005567 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005568#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005569 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005570 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5571
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005572 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005573 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5574 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005575 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005576 }
Emeric Brun561e5742012-10-02 15:20:55 +02005577 else {
5578 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5579 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005580 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005581#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005582 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005583 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005584#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005585 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005586 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005587 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5588 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005589 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005590 }
5591 }
5592#endif
5593
William Lallemand4f45bb92017-10-30 20:08:51 +01005594 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005595 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5596 if (conf_ciphers &&
5597 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005598 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5599 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005600 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005601 }
5602
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005603#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005604 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5605 if (conf_ciphersuites &&
5606 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005607 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5608 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005609 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005610 }
5611#endif
5612
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005613#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005614 /* If tune.ssl.default-dh-param has not been set,
5615 neither has ssl-default-dh-file and no static DH
5616 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005617 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005618 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005619 (ssl_dh_ptr_index == -1 ||
5620 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005621 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5622 const SSL_CIPHER * cipher = NULL;
5623 char cipher_description[128];
5624 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5625 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5626 which is not ephemeral DH. */
5627 const char dhe_description[] = " Kx=DH ";
5628 const char dhe_export_description[] = " Kx=DH(";
5629 int idx = 0;
5630 int dhe_found = 0;
5631 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005632
Remi Gacogne23d5d372014-10-10 17:04:26 +02005633 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005634
Remi Gacogne23d5d372014-10-10 17:04:26 +02005635 if (ssl) {
5636 ciphers = SSL_get_ciphers(ssl);
5637
5638 if (ciphers) {
5639 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5640 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5641 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5642 if (strstr(cipher_description, dhe_description) != NULL ||
5643 strstr(cipher_description, dhe_export_description) != NULL) {
5644 dhe_found = 1;
5645 break;
5646 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005647 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005648 }
5649 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005650 SSL_free(ssl);
5651 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005652 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005653
Lukas Tribus90132722014-08-18 00:56:33 +02005654 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005655 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",
5656 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005657 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005658 }
5659
Willy Tarreauef934602016-12-22 23:12:01 +01005660 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005661 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005662
Willy Tarreauef934602016-12-22 23:12:01 +01005663 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005664 if (local_dh_1024 == NULL) {
5665 local_dh_1024 = ssl_get_dh_1024();
5666 }
Willy Tarreauef934602016-12-22 23:12:01 +01005667 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005668 if (local_dh_2048 == NULL) {
5669 local_dh_2048 = ssl_get_dh_2048();
5670 }
Willy Tarreauef934602016-12-22 23:12:01 +01005671 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005672 if (local_dh_4096 == NULL) {
5673 local_dh_4096 = ssl_get_dh_4096();
5674 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005675 }
5676 }
5677 }
5678#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005679
Emeric Brunfc0421f2012-09-07 17:30:07 +02005680 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005681#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005682 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005683#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005684
Bernard Spil13c53f82018-02-15 13:34:58 +01005685#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005686 ssl_conf_cur = NULL;
5687 if (ssl_conf && ssl_conf->npn_str)
5688 ssl_conf_cur = ssl_conf;
5689 else if (bind_conf->ssl_conf.npn_str)
5690 ssl_conf_cur = &bind_conf->ssl_conf;
5691 if (ssl_conf_cur)
5692 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005693#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005694#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005695 ssl_conf_cur = NULL;
5696 if (ssl_conf && ssl_conf->alpn_str)
5697 ssl_conf_cur = ssl_conf;
5698 else if (bind_conf->ssl_conf.alpn_str)
5699 ssl_conf_cur = &bind_conf->ssl_conf;
5700 if (ssl_conf_cur)
5701 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005702#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005703#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005704 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5705 if (conf_curves) {
5706 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005707 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5708 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005709 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005710 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005711 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005712 }
5713#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005714#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005715 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005716 int i;
5717 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005718#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005719 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005720 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5721 NULL);
5722
5723 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005724 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005725 return cfgerr;
5726 }
5727#else
5728 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5729 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5730 ECDHE_DEFAULT_CURVE);
5731#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005732
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005733 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005734 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005735 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5736 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005737 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005738 }
5739 else {
5740 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5741 EC_KEY_free(ecdh);
5742 }
5743 }
5744#endif
5745
Emeric Brunfc0421f2012-09-07 17:30:07 +02005746 return cfgerr;
5747}
5748
Evan Broderbe554312013-06-27 00:05:25 -07005749static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5750{
5751 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5752 size_t prefixlen, suffixlen;
5753
5754 /* Trivial case */
5755 if (strcmp(pattern, hostname) == 0)
5756 return 1;
5757
Evan Broderbe554312013-06-27 00:05:25 -07005758 /* The rest of this logic is based on RFC 6125, section 6.4.3
5759 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5760
Emeric Bruna848dae2013-10-08 11:27:28 +02005761 pattern_wildcard = NULL;
5762 pattern_left_label_end = pattern;
5763 while (*pattern_left_label_end != '.') {
5764 switch (*pattern_left_label_end) {
5765 case 0:
5766 /* End of label not found */
5767 return 0;
5768 case '*':
5769 /* If there is more than one wildcards */
5770 if (pattern_wildcard)
5771 return 0;
5772 pattern_wildcard = pattern_left_label_end;
5773 break;
5774 }
5775 pattern_left_label_end++;
5776 }
5777
5778 /* If it's not trivial and there is no wildcard, it can't
5779 * match */
5780 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005781 return 0;
5782
5783 /* Make sure all labels match except the leftmost */
5784 hostname_left_label_end = strchr(hostname, '.');
5785 if (!hostname_left_label_end
5786 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5787 return 0;
5788
5789 /* Make sure the leftmost label of the hostname is long enough
5790 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005791 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005792 return 0;
5793
5794 /* Finally compare the string on either side of the
5795 * wildcard */
5796 prefixlen = pattern_wildcard - pattern;
5797 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005798 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5799 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005800 return 0;
5801
5802 return 1;
5803}
5804
5805static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5806{
5807 SSL *ssl;
5808 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005809 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005810 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005811 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005812
5813 int depth;
5814 X509 *cert;
5815 STACK_OF(GENERAL_NAME) *alt_names;
5816 int i;
5817 X509_NAME *cert_subject;
5818 char *str;
5819
5820 if (ok == 0)
5821 return ok;
5822
5823 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005824 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005825 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005826
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005827 /* We're checking if the provided hostnames match the desired one. The
5828 * desired hostname comes from the SNI we presented if any, or if not
5829 * provided then it may have been explicitly stated using a "verifyhost"
5830 * directive. If neither is set, we don't care about the name so the
5831 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005832 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005833 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005834 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005835 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005836 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005837 if (!servername)
5838 return ok;
5839 }
Evan Broderbe554312013-06-27 00:05:25 -07005840
5841 /* We only need to verify the CN on the actual server cert,
5842 * not the indirect CAs */
5843 depth = X509_STORE_CTX_get_error_depth(ctx);
5844 if (depth != 0)
5845 return ok;
5846
5847 /* At this point, the cert is *not* OK unless we can find a
5848 * hostname match */
5849 ok = 0;
5850
5851 cert = X509_STORE_CTX_get_current_cert(ctx);
5852 /* It seems like this might happen if verify peer isn't set */
5853 if (!cert)
5854 return ok;
5855
5856 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5857 if (alt_names) {
5858 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5859 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5860 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005861#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005862 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5863#else
Evan Broderbe554312013-06-27 00:05:25 -07005864 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005865#endif
Evan Broderbe554312013-06-27 00:05:25 -07005866 ok = ssl_sock_srv_hostcheck(str, servername);
5867 OPENSSL_free(str);
5868 }
5869 }
5870 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005871 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005872 }
5873
5874 cert_subject = X509_get_subject_name(cert);
5875 i = -1;
5876 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5877 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005878 ASN1_STRING *value;
5879 value = X509_NAME_ENTRY_get_data(entry);
5880 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005881 ok = ssl_sock_srv_hostcheck(str, servername);
5882 OPENSSL_free(str);
5883 }
5884 }
5885
Willy Tarreau71d058c2017-07-26 20:09:56 +02005886 /* report the mismatch and indicate if SNI was used or not */
5887 if (!ok && !conn->err_code)
5888 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005889 return ok;
5890}
5891
Emeric Brun94324a42012-10-11 14:00:19 +02005892/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005893int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005894{
Willy Tarreau03209342016-12-22 17:08:28 +01005895 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005896 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005897 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005898 SSL_OP_ALL | /* all known workarounds for bugs */
5899 SSL_OP_NO_SSLv2 |
5900 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005901 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005902 SSL_MODE_ENABLE_PARTIAL_WRITE |
5903 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005904 SSL_MODE_RELEASE_BUFFERS |
5905 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005906 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005907 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005908 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005909 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005910 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005911
Thierry Fournier383085f2013-01-24 14:15:43 +01005912 /* Make sure openssl opens /dev/urandom before the chroot */
5913 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005914 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005915 cfgerr++;
5916 }
5917
Willy Tarreaufce03112015-01-15 21:32:40 +01005918 /* Automatic memory computations need to know we use SSL there */
5919 global.ssl_used_backend = 1;
5920
5921 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005922 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005923 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005924 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5925 curproxy->id, srv->id,
5926 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005927 cfgerr++;
5928 return cfgerr;
5929 }
5930 }
Emeric Brun94324a42012-10-11 14:00:19 +02005931 if (srv->use_ssl)
5932 srv->xprt = &ssl_sock;
5933 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005934 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005935
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005936 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005937 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005938 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5939 proxy_type_str(curproxy), curproxy->id,
5940 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005941 cfgerr++;
5942 return cfgerr;
5943 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005944
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005945 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005946 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5947 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5948 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005949 else
5950 flags = conf_ssl_methods->flags;
5951
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005952 /* Real min and max should be determinate with configuration and openssl's capabilities */
5953 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005954 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005955 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005956 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005957
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005958 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005959 min = max = CONF_TLSV_NONE;
5960 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005961 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005962 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005963 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005964 if (min) {
5965 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005966 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5967 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5968 proxy_type_str(curproxy), curproxy->id, srv->id,
5969 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005970 hole = 0;
5971 }
5972 max = i;
5973 }
5974 else {
5975 min = max = i;
5976 }
5977 }
5978 else {
5979 if (min)
5980 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005981 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005982 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005983 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5984 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005985 cfgerr += 1;
5986 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005987
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005988#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005989 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005990 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005991 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005992 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005993 else
5994 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5995 if (flags & methodVersions[i].flag)
5996 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005997#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005998 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005999 methodVersions[min].ctx_set_version(ctx, SET_MIN);
6000 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006001#endif
6002
6003 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
6004 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01006005 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006006
Willy Tarreau5db847a2019-05-09 14:13:35 +02006007#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006008 if (global_ssl.async)
6009 mode |= SSL_MODE_ASYNC;
6010#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01006011 SSL_CTX_set_mode(ctx, mode);
6012 srv->ssl_ctx.ctx = ctx;
6013
Emeric Bruna7aa3092012-10-26 12:58:00 +02006014 if (srv->ssl_ctx.client_crt) {
6015 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 +01006016 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
6017 proxy_type_str(curproxy), curproxy->id,
6018 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006019 cfgerr++;
6020 }
6021 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 +01006022 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
6023 proxy_type_str(curproxy), curproxy->id,
6024 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006025 cfgerr++;
6026 }
6027 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006028 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
6029 proxy_type_str(curproxy), curproxy->id,
6030 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006031 cfgerr++;
6032 }
6033 }
Emeric Brun94324a42012-10-11 14:00:19 +02006034
Emeric Brun850efd52014-01-29 12:24:34 +01006035 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
6036 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01006037 switch (srv->ssl_ctx.verify) {
6038 case SSL_SOCK_VERIFY_NONE:
6039 verify = SSL_VERIFY_NONE;
6040 break;
6041 case SSL_SOCK_VERIFY_REQUIRED:
6042 verify = SSL_VERIFY_PEER;
6043 break;
6044 }
Evan Broderbe554312013-06-27 00:05:25 -07006045 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01006046 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02006047 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01006048 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02006049 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02006050 /* set CAfile to verify */
6051 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6052 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006053 curproxy->id, srv->id,
6054 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006055 cfgerr++;
6056 }
6057 }
Emeric Brun850efd52014-01-29 12:24:34 +01006058 else {
6059 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006060 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",
6061 curproxy->id, srv->id,
6062 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006063 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006064 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6065 curproxy->id, srv->id,
6066 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006067 cfgerr++;
6068 }
Emeric Brunef42d922012-10-11 16:11:36 +02006069#ifdef X509_V_FLAG_CRL_CHECK
6070 if (srv->ssl_ctx.crl_file) {
6071 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6072
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006073 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006074 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6075 curproxy->id, srv->id,
6076 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006077 cfgerr++;
6078 }
6079 else {
6080 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6081 }
6082 }
6083#endif
6084 }
6085
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006086 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6087 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6088 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006089 if (srv->ssl_ctx.ciphers &&
6090 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006091 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6092 curproxy->id, srv->id,
6093 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006094 cfgerr++;
6095 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006096
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006097#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006098 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006099 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006100 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6101 curproxy->id, srv->id,
6102 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6103 cfgerr++;
6104 }
6105#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006106#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6107 if (srv->ssl_ctx.npn_str)
6108 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6109#endif
6110#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6111 if (srv->ssl_ctx.alpn_str)
6112 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6113#endif
6114
Emeric Brun94324a42012-10-11 14:00:19 +02006115
6116 return cfgerr;
6117}
6118
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006119/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006120 * be NULL, in which case nothing is done. Returns the number of errors
6121 * encountered.
6122 */
Willy Tarreau03209342016-12-22 17:08:28 +01006123int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006124{
6125 struct ebmb_node *node;
6126 struct sni_ctx *sni;
6127 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006128 int errcode = 0;
6129 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006130
Willy Tarreaufce03112015-01-15 21:32:40 +01006131 /* Automatic memory computations need to know we use SSL there */
6132 global.ssl_used_frontend = 1;
6133
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006134 /* Make sure openssl opens /dev/urandom before the chroot */
6135 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006136 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006137 err++;
6138 }
6139 /* Create initial_ctx used to start the ssl connection before do switchctx */
6140 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006141 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006142 /* It should not be necessary to call this function, but it's
6143 necessary first to check and move all initialisation related
6144 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006145 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006146 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006147 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006148 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006149
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006150 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006151 while (node) {
6152 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006153 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6154 /* only initialize the CTX on its first occurrence and
6155 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006156 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006157 node = ebmb_next(node);
6158 }
6159
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006160 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006161 while (node) {
6162 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006163 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006164 /* only initialize the CTX on its first occurrence and
6165 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006166 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6167 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006168 node = ebmb_next(node);
6169 }
William Lallemand8b453912019-11-21 15:48:10 +01006170
6171 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006172 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006173 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006174 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006175 err++;
6176 }
6177
6178 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006179 return err;
6180}
6181
Willy Tarreau55d37912016-12-21 23:38:39 +01006182/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6183 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6184 * alerts are directly emitted since the rest of the stack does it below.
6185 */
6186int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6187{
6188 struct proxy *px = bind_conf->frontend;
6189 int alloc_ctx;
6190 int err;
6191
6192 if (!bind_conf->is_ssl) {
6193 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006194 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6195 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006196 }
6197 return 0;
6198 }
6199 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006200 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006201 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6202 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006203 }
6204 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006205 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6206 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006207 return -1;
6208 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006209 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006210 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006211 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006212 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006213 sizeof(*sh_ssl_sess_tree),
6214 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006215 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006216 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6217 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");
6218 else
6219 ha_alert("Unable to allocate SSL session cache.\n");
6220 return -1;
6221 }
6222 /* free block callback */
6223 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6224 /* init the root tree within the extra space */
6225 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6226 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006227 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006228 err = 0;
6229 /* initialize all certificate contexts */
6230 err += ssl_sock_prepare_all_ctx(bind_conf);
6231
6232 /* initialize CA variables if the certificates generation is enabled */
6233 err += ssl_sock_load_ca(bind_conf);
6234
6235 return -err;
6236}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006237
6238/* release ssl context allocated for servers. */
6239void ssl_sock_free_srv_ctx(struct server *srv)
6240{
Olivier Houchardc7566002018-11-20 23:33:50 +01006241#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6242 if (srv->ssl_ctx.alpn_str)
6243 free(srv->ssl_ctx.alpn_str);
6244#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006245#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006246 if (srv->ssl_ctx.npn_str)
6247 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006248#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006249 if (srv->ssl_ctx.ctx)
6250 SSL_CTX_free(srv->ssl_ctx.ctx);
6251}
6252
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006253/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006254 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6255 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006256void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006257{
6258 struct ebmb_node *node, *back;
6259 struct sni_ctx *sni;
6260
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006261 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006262 while (node) {
6263 sni = ebmb_entry(node, struct sni_ctx, name);
6264 back = ebmb_next(node);
6265 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006266 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02006267 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006268 ssl_sock_free_ssl_conf(sni->conf);
6269 free(sni->conf);
6270 sni->conf = NULL;
6271 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006272 free(sni);
6273 node = back;
6274 }
6275
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006276 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006277 while (node) {
6278 sni = ebmb_entry(node, struct sni_ctx, name);
6279 back = ebmb_next(node);
6280 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006281 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02006282 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006283 ssl_sock_free_ssl_conf(sni->conf);
6284 free(sni->conf);
6285 sni->conf = NULL;
6286 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006287 free(sni);
6288 node = back;
6289 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006290 SSL_CTX_free(bind_conf->initial_ctx);
6291 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006292 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006293 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006294}
6295
Willy Tarreau795cdab2016-12-22 17:30:54 +01006296/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6297void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6298{
6299 ssl_sock_free_ca(bind_conf);
6300 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006301 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006302 free(bind_conf->ca_sign_file);
6303 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006304 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006305 free(bind_conf->keys_ref->filename);
6306 free(bind_conf->keys_ref->tlskeys);
6307 LIST_DEL(&bind_conf->keys_ref->list);
6308 free(bind_conf->keys_ref);
6309 }
6310 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006311 bind_conf->ca_sign_pass = NULL;
6312 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006313}
6314
Christopher Faulet31af49d2015-06-09 17:29:50 +02006315/* Load CA cert file and private key used to generate certificates */
6316int
Willy Tarreau03209342016-12-22 17:08:28 +01006317ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006318{
Willy Tarreau03209342016-12-22 17:08:28 +01006319 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006320 FILE *fp;
6321 X509 *cacert = NULL;
6322 EVP_PKEY *capkey = NULL;
6323 int err = 0;
6324
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006325 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006326 return err;
6327
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006328#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006329 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006330 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006331 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006332 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006333 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006334#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006335
Christopher Faulet31af49d2015-06-09 17:29:50 +02006336 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006337 ha_alert("Proxy '%s': cannot enable certificate generation, "
6338 "no CA certificate File configured at [%s:%d].\n",
6339 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006340 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006341 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006342
6343 /* read in the CA certificate */
6344 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006345 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6346 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006347 goto load_error;
6348 }
6349 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006350 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6351 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006352 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006353 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006354 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006355 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006356 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6357 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006358 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006359 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006360
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006361 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006362 bind_conf->ca_sign_cert = cacert;
6363 bind_conf->ca_sign_pkey = capkey;
6364 return err;
6365
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006366 read_error:
6367 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006368 if (capkey) EVP_PKEY_free(capkey);
6369 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006370 load_error:
6371 bind_conf->generate_certs = 0;
6372 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006373 return err;
6374}
6375
6376/* Release CA cert and private key used to generate certificated */
6377void
6378ssl_sock_free_ca(struct bind_conf *bind_conf)
6379{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006380 if (bind_conf->ca_sign_pkey)
6381 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6382 if (bind_conf->ca_sign_cert)
6383 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006384 bind_conf->ca_sign_pkey = NULL;
6385 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006386}
6387
Emeric Brun46591952012-05-18 15:47:34 +02006388/*
6389 * This function is called if SSL * context is not yet allocated. The function
6390 * is designed to be called before any other data-layer operation and sets the
6391 * handshake flag on the connection. It is safe to call it multiple times.
6392 * It returns 0 on success and -1 in error case.
6393 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006394static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006395{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006396 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006397 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006398 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006399 return 0;
6400
Willy Tarreau3c728722014-01-23 13:50:42 +01006401 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006402 return 0;
6403
Olivier Houchard66ab4982019-02-26 18:37:15 +01006404 ctx = pool_alloc(ssl_sock_ctx_pool);
6405 if (!ctx) {
6406 conn->err_code = CO_ER_SSL_NO_MEM;
6407 return -1;
6408 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006409 ctx->wait_event.tasklet = tasklet_new();
6410 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006411 conn->err_code = CO_ER_SSL_NO_MEM;
6412 pool_free(ssl_sock_ctx_pool, ctx);
6413 return -1;
6414 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006415 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6416 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006417 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006418 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006419 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006420 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006421 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006422 ctx->xprt_st = 0;
6423 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006424
6425 /* Only work with sockets for now, this should be adapted when we'll
6426 * add QUIC support.
6427 */
6428 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006429 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006430 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6431 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006432 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006433
Willy Tarreau20879a02012-12-03 16:32:10 +01006434 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6435 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006436 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006437 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006438
Emeric Brun46591952012-05-18 15:47:34 +02006439 /* If it is in client mode initiate SSL session
6440 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006441 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006442 int may_retry = 1;
6443
6444 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006445 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006446 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6447 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006448 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006449 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006450 goto retry_connect;
6451 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006452 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006453 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006454 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006455 ctx->bio = BIO_new(ha_meth);
6456 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006457 SSL_free(ctx->ssl);
6458 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006459 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006460 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006461 goto retry_connect;
6462 }
Emeric Brun55476152014-11-12 17:35:37 +01006463 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006464 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006465 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006466 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006467 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006468
Evan Broderbe554312013-06-27 00:05:25 -07006469 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006470 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6471 SSL_free(ctx->ssl);
6472 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006473 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006474 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006475 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006476 goto retry_connect;
6477 }
Emeric Brun55476152014-11-12 17:35:37 +01006478 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006479 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006480 }
6481
Olivier Houchard66ab4982019-02-26 18:37:15 +01006482 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006483 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6484 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6485 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 +01006486 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006487 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006488 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6489 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006490 } else if (sess) {
6491 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006492 }
6493 }
Evan Broderbe554312013-06-27 00:05:25 -07006494
Emeric Brun46591952012-05-18 15:47:34 +02006495 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006496 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006497
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006498 _HA_ATOMIC_ADD(&sslconns, 1);
6499 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006500 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006501 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006502 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006503 return 0;
6504 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006505 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006506 int may_retry = 1;
6507
6508 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006509 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006510 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6511 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006512 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006513 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006514 goto retry_accept;
6515 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006516 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006517 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006518 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006519 ctx->bio = BIO_new(ha_meth);
6520 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006521 SSL_free(ctx->ssl);
6522 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006523 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006524 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006525 goto retry_accept;
6526 }
Emeric Brun55476152014-11-12 17:35:37 +01006527 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006528 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006529 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006530 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006531 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006532
Emeric Brune1f38db2012-09-03 20:36:47 +02006533 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006534 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6535 SSL_free(ctx->ssl);
6536 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006537 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006538 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006539 goto retry_accept;
6540 }
Emeric Brun55476152014-11-12 17:35:37 +01006541 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006542 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006543 }
6544
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006545#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6546 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6547 b_alloc(&ctx->early_buf);
6548 SSL_set_max_early_data(ctx->ssl,
6549 /* Only allow early data if we managed to allocate
6550 * a buffer.
6551 */
6552 (!b_is_null(&ctx->early_buf)) ?
6553 global.tune.bufsize - global.tune.maxrewrite : 0);
6554 }
6555#endif
6556
Olivier Houchard66ab4982019-02-26 18:37:15 +01006557 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006558
Emeric Brun46591952012-05-18 15:47:34 +02006559 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006560 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006561#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006562 conn->flags |= CO_FL_EARLY_SSL_HS;
6563#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006564
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006565 _HA_ATOMIC_ADD(&sslconns, 1);
6566 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006567 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006568 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006569 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006570 return 0;
6571 }
6572 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006573 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006574err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006575 if (ctx && ctx->wait_event.tasklet)
6576 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006577 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006578 return -1;
6579}
6580
6581
6582/* This is the callback which is used when an SSL handshake is pending. It
6583 * updates the FD status if it wants some polling before being called again.
6584 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6585 * otherwise it returns non-zero and removes itself from the connection's
6586 * flags (the bit is provided in <flag> by the caller).
6587 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006588static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006589{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006590 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006591 int ret;
6592
Willy Tarreau3c728722014-01-23 13:50:42 +01006593 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006594 return 0;
6595
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006596 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006597 goto out_error;
6598
Willy Tarreau5db847a2019-05-09 14:13:35 +02006599#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006600 /*
6601 * Check if we have early data. If we do, we have to read them
6602 * before SSL_do_handshake() is called, And there's no way to
6603 * detect early data, except to try to read them
6604 */
6605 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006606 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006607
Olivier Houchard54907bb2019-12-19 15:02:39 +01006608 while (1) {
6609 ret = SSL_read_early_data(ctx->ssl,
6610 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6611 &read_data);
6612 if (ret == SSL_READ_EARLY_DATA_ERROR)
6613 goto check_error;
6614 if (read_data > 0) {
6615 conn->flags |= CO_FL_EARLY_DATA;
6616 b_add(&ctx->early_buf, read_data);
6617 }
6618 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6619 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6620 if (!b_data(&ctx->early_buf))
6621 b_free(&ctx->early_buf);
6622 break;
6623 }
6624 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006625 }
6626#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006627 /* If we use SSL_do_handshake to process a reneg initiated by
6628 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6629 * Usually SSL_write and SSL_read are used and process implicitly
6630 * the reneg handshake.
6631 * Here we use SSL_peek as a workaround for reneg.
6632 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006633 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006634 char c;
6635
Olivier Houchard66ab4982019-02-26 18:37:15 +01006636 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006637 if (ret <= 0) {
6638 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006639 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006640
Emeric Brun674b7432012-11-08 19:21:55 +01006641 if (ret == SSL_ERROR_WANT_WRITE) {
6642 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006643 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006644 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006645 return 0;
6646 }
6647 else if (ret == SSL_ERROR_WANT_READ) {
6648 /* handshake may have been completed but we have
6649 * no more data to read.
6650 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006651 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006652 ret = 1;
6653 goto reneg_ok;
6654 }
6655 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006656 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006657 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006658 return 0;
6659 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006660#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006661 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006662 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006663 return 0;
6664 }
6665#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006666 else if (ret == SSL_ERROR_SYSCALL) {
6667 /* if errno is null, then connection was successfully established */
6668 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6669 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006670 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006671#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6672 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006673 conn->err_code = CO_ER_SSL_HANDSHAKE;
6674#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006675 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006676#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006677 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006678 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006679 empty_handshake = state == TLS_ST_BEFORE;
6680#else
Lukas Tribus49799162019-07-08 14:29:15 +02006681 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6682 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006683#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006684 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006685 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006686 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006687 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6688 else
6689 conn->err_code = CO_ER_SSL_EMPTY;
6690 }
6691 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006692 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006693 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6694 else
6695 conn->err_code = CO_ER_SSL_ABORT;
6696 }
6697 }
6698 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006699 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006700 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006701 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006702 conn->err_code = CO_ER_SSL_HANDSHAKE;
6703 }
Lukas Tribus49799162019-07-08 14:29:15 +02006704#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006705 }
Emeric Brun674b7432012-11-08 19:21:55 +01006706 goto out_error;
6707 }
6708 else {
6709 /* Fail on all other handshake errors */
6710 /* Note: OpenSSL may leave unread bytes in the socket's
6711 * buffer, causing an RST to be emitted upon close() on
6712 * TCP sockets. We first try to drain possibly pending
6713 * data to avoid this as much as possible.
6714 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006715 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006716 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006717 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006718 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006719 goto out_error;
6720 }
6721 }
6722 /* read some data: consider handshake completed */
6723 goto reneg_ok;
6724 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006725 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006726check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006727 if (ret != 1) {
6728 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006729 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006730
6731 if (ret == SSL_ERROR_WANT_WRITE) {
6732 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006733 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006734 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006735 return 0;
6736 }
6737 else if (ret == SSL_ERROR_WANT_READ) {
6738 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006739 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006740 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6741 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006742 return 0;
6743 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006744#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006745 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006746 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006747 return 0;
6748 }
6749#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006750 else if (ret == SSL_ERROR_SYSCALL) {
6751 /* if errno is null, then connection was successfully established */
6752 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6753 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006754 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006755#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6756 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006757 conn->err_code = CO_ER_SSL_HANDSHAKE;
6758#else
6759 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006760#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006761 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006762 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006763 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006764#else
Lukas Tribus49799162019-07-08 14:29:15 +02006765 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6766 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006767#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006768 if (empty_handshake) {
6769 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006770 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006771 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6772 else
6773 conn->err_code = CO_ER_SSL_EMPTY;
6774 }
6775 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006776 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006777 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6778 else
6779 conn->err_code = CO_ER_SSL_ABORT;
6780 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006781 }
6782 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006783 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006784 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6785 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006786 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006787 }
Lukas Tribus49799162019-07-08 14:29:15 +02006788#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006789 }
Willy Tarreau89230192012-09-28 20:22:13 +02006790 goto out_error;
6791 }
Emeric Brun46591952012-05-18 15:47:34 +02006792 else {
6793 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006794 /* Note: OpenSSL may leave unread bytes in the socket's
6795 * buffer, causing an RST to be emitted upon close() on
6796 * TCP sockets. We first try to drain possibly pending
6797 * data to avoid this as much as possible.
6798 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006799 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006800 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006801 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006802 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006803 goto out_error;
6804 }
6805 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006806#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006807 else {
6808 /*
6809 * If the server refused the early data, we have to send a
6810 * 425 to the client, as we no longer have the data to sent
6811 * them again.
6812 */
6813 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006814 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006815 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6816 goto out_error;
6817 }
6818 }
6819 }
6820#endif
6821
Emeric Brun46591952012-05-18 15:47:34 +02006822
Emeric Brun674b7432012-11-08 19:21:55 +01006823reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006824
Willy Tarreau5db847a2019-05-09 14:13:35 +02006825#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006826 /* ASYNC engine API doesn't support moving read/write
6827 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006828 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006829 */
6830 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006831 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006832#endif
Emeric Brun46591952012-05-18 15:47:34 +02006833 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006834 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006835 if (objt_server(conn->target)) {
6836 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6837 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6838 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006839 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006840 else {
6841 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6842 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6843 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6844 }
Emeric Brun46591952012-05-18 15:47:34 +02006845 }
6846
6847 /* The connection is now established at both layers, it's time to leave */
6848 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6849 return 1;
6850
6851 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006852 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006853 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006854 ERR_clear_error();
6855
Emeric Brun9fa89732012-10-04 17:09:56 +02006856 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006857 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6858 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6859 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006860 }
6861
Emeric Brun46591952012-05-18 15:47:34 +02006862 /* Fail on all other handshake errors */
6863 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006864 if (!conn->err_code)
6865 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006866 return 0;
6867}
6868
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006869/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6870 * event subscriber <es> is not allowed to change from a previous call as long
6871 * as at least one event is still subscribed. The <event_type> must only be a
6872 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6873 * unless the transport layer was already released.
6874 */
6875static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006876{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006877 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006878
Olivier Houchard0ff28652019-06-24 18:57:39 +02006879 if (!ctx)
6880 return -1;
6881
Willy Tarreau113d52b2020-01-10 09:20:26 +01006882 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006883 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006884
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006885 ctx->subs = es;
6886 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006887
6888 /* we may have to subscribe to lower layers for new events */
6889 event_type &= ~ctx->wait_event.events;
6890 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6891 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006892 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006893}
6894
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006895/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6896 * The <es> pointer is not allowed to differ from the one passed to the
6897 * subscribe() call. It always returns zero.
6898 */
6899static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006900{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006901 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006902
Willy Tarreau113d52b2020-01-10 09:20:26 +01006903 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006904 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006905
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006906 es->events &= ~event_type;
6907 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006908 ctx->subs = NULL;
6909
6910 /* If we subscribed, and we're not doing the handshake,
6911 * then we subscribed because the upper layer asked for it,
6912 * as the upper layer is no longer interested, we can
6913 * unsubscribe too.
6914 */
6915 event_type &= ctx->wait_event.events;
6916 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6917 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006918
6919 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006920}
6921
Olivier Houchard2e055482019-05-27 19:50:12 +02006922/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6923 * Returns 0 on success, and non-zero on failure.
6924 */
6925static 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)
6926{
6927 struct ssl_sock_ctx *ctx = xprt_ctx;
6928
6929 if (oldxprt_ops != NULL)
6930 *oldxprt_ops = ctx->xprt;
6931 if (oldxprt_ctx != NULL)
6932 *oldxprt_ctx = ctx->xprt_ctx;
6933 ctx->xprt = toadd_ops;
6934 ctx->xprt_ctx = toadd_ctx;
6935 return 0;
6936}
6937
Olivier Houchard5149b592019-05-23 17:47:36 +02006938/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6939 * return 0, otherwise just call the remove_xprt method from the underlying
6940 * XPRT.
6941 */
6942static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6943{
6944 struct ssl_sock_ctx *ctx = xprt_ctx;
6945
6946 if (ctx->xprt_ctx == toremove_ctx) {
6947 ctx->xprt_ctx = newctx;
6948 ctx->xprt = newops;
6949 return 0;
6950 }
6951 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6952}
6953
Olivier Houchardea8dd942019-05-20 14:02:16 +02006954static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6955{
6956 struct ssl_sock_ctx *ctx = context;
6957
6958 /* First if we're doing an handshake, try that */
6959 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6960 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6961 /* If we had an error, or the handshake is done and I/O is available,
6962 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006963 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006964 * we can't be sure conn_fd_handler() will be called again.
6965 */
6966 if ((ctx->conn->flags & CO_FL_ERROR) ||
6967 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6968 int ret = 0;
6969 int woke = 0;
6970
6971 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006972 if (ctx->subs) {
6973 tasklet_wakeup(ctx->subs->tasklet);
6974 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006975 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006976 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006977 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006978
Olivier Houchardea8dd942019-05-20 14:02:16 +02006979 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006980 * upper layers know. If we have no mux, create it,
6981 * and once we have a mux, call its wake method if we didn't
6982 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006983 */
6984 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006985 if (!ctx->conn->mux)
6986 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006987 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6988 ctx->conn->mux->wake(ctx->conn);
6989 return NULL;
6990 }
6991 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006992#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6993 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006994 else if (b_data(&ctx->early_buf) && ctx->subs &&
6995 ctx->subs->events & SUB_RETRY_RECV) {
6996 tasklet_wakeup(ctx->subs->tasklet);
6997 ctx->subs->events &= ~SUB_RETRY_RECV;
6998 if (!ctx->subs->events)
6999 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01007000 }
7001#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02007002 return NULL;
7003}
7004
Emeric Brun46591952012-05-18 15:47:34 +02007005/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01007006 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02007007 * buffer wraps, in which case a second call may be performed. The connection's
7008 * flags are updated with whatever special event is detected (error, read0,
7009 * empty). The caller is responsible for taking care of those events and
7010 * avoiding the call if inappropriate. The function does not call the
7011 * connection's polling update function, so the caller is responsible for this.
7012 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007013static 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 +02007014{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007015 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02007016 ssize_t ret;
7017 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02007018
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007019 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007020 goto out_error;
7021
Olivier Houchard54907bb2019-12-19 15:02:39 +01007022#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
7023 if (b_data(&ctx->early_buf)) {
7024 try = b_contig_space(buf);
7025 if (try > b_data(&ctx->early_buf))
7026 try = b_data(&ctx->early_buf);
7027 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
7028 b_add(buf, try);
7029 b_del(&ctx->early_buf, try);
7030 if (b_data(&ctx->early_buf) == 0)
7031 b_free(&ctx->early_buf);
7032 return try;
7033 }
7034#endif
7035
Willy Tarreau911db9b2020-01-23 16:27:54 +01007036 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007037 /* a handshake was requested */
7038 return 0;
7039
Emeric Brun46591952012-05-18 15:47:34 +02007040 /* read the largest possible block. For this, we perform only one call
7041 * to recv() unless the buffer wraps and we exactly fill the first hunk,
7042 * in which case we accept to do it once again. A new attempt is made on
7043 * EINTR too.
7044 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01007045 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007046
Willy Tarreau591d4452018-06-15 17:21:00 +02007047 try = b_contig_space(buf);
7048 if (!try)
7049 break;
7050
Willy Tarreauabf08d92014-01-14 11:31:27 +01007051 if (try > count)
7052 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007053
Olivier Houchard66ab4982019-02-26 18:37:15 +01007054 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007055
Emeric Brune1f38db2012-09-03 20:36:47 +02007056 if (conn->flags & CO_FL_ERROR) {
7057 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007058 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007059 }
Emeric Brun46591952012-05-18 15:47:34 +02007060 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007061 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007062 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007063 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007064 }
Emeric Brun46591952012-05-18 15:47:34 +02007065 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007066 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007067 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007068 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007069 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007070 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007071#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007072 /* Async mode can be re-enabled, because we're leaving data state.*/
7073 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007074 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007075#endif
Emeric Brun46591952012-05-18 15:47:34 +02007076 break;
7077 }
7078 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007079 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007080 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7081 SUB_RETRY_RECV,
7082 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007083 /* handshake is running, and it may need to re-enable read */
7084 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007085#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007086 /* Async mode can be re-enabled, because we're leaving data state.*/
7087 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007088 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007089#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007090 break;
7091 }
Emeric Brun46591952012-05-18 15:47:34 +02007092 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007093 } else if (ret == SSL_ERROR_ZERO_RETURN)
7094 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007095 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7096 * stack before shutting down the connection for
7097 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007098 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7099 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007100 /* otherwise it's a real error */
7101 goto out_error;
7102 }
7103 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007104 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007105 return done;
7106
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007107 clear_ssl_error:
7108 /* Clear openssl global errors stack */
7109 ssl_sock_dump_errors(conn);
7110 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007111 read0:
7112 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007113 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007114
Emeric Brun46591952012-05-18 15:47:34 +02007115 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007116 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007117 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007118 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007119 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007120 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007121}
7122
7123
Willy Tarreau787db9a2018-06-14 18:31:46 +02007124/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7125 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7126 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007127 * Only one call to send() is performed, unless the buffer wraps, in which case
7128 * a second call may be performed. The connection's flags are updated with
7129 * whatever special event is detected (error, empty). The caller is responsible
7130 * for taking care of those events and avoiding the call if inappropriate. The
7131 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007132 * is responsible for this. The buffer's output is not adjusted, it's up to the
7133 * caller to take care of this. It's up to the caller to update the buffer's
7134 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007135 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007136static 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 +02007137{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007138 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007139 ssize_t ret;
7140 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007141
7142 done = 0;
7143
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007144 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007145 goto out_error;
7146
Willy Tarreau911db9b2020-01-23 16:27:54 +01007147 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007148 /* a handshake was requested */
7149 return 0;
7150
7151 /* send the largest possible block. For this we perform only one call
7152 * to send() unless the buffer wraps and we exactly fill the first hunk,
7153 * in which case we accept to do it once again.
7154 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007155 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007156#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007157 size_t written_data;
7158#endif
7159
Willy Tarreau787db9a2018-06-14 18:31:46 +02007160 try = b_contig_data(buf, done);
7161 if (try > count)
7162 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007163
Willy Tarreau7bed9452014-02-02 02:00:24 +01007164 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007165 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007166 global_ssl.max_record && try > global_ssl.max_record) {
7167 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007168 }
7169 else {
7170 /* we need to keep the information about the fact that
7171 * we're not limiting the upcoming send(), because if it
7172 * fails, we'll have to retry with at least as many data.
7173 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007174 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007175 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007176
Willy Tarreau5db847a2019-05-09 14:13:35 +02007177#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007178 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007179 unsigned int max_early;
7180
Olivier Houchard522eea72017-11-03 16:27:47 +01007181 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007182 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007183 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007184 if (SSL_get0_session(ctx->ssl))
7185 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007186 else
7187 max_early = 0;
7188 }
7189
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007190 if (try + ctx->sent_early_data > max_early) {
7191 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007192 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007193 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007194 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007195 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007196 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007197 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007198 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007199 if (ret == 1) {
7200 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007201 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007202 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007203 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007204 /* Initiate the handshake, now */
7205 tasklet_wakeup(ctx->wait_event.tasklet);
7206 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007207
Olivier Houchardc2aae742017-09-22 18:26:28 +02007208 }
7209
7210 } else
7211#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007212 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007213
Emeric Brune1f38db2012-09-03 20:36:47 +02007214 if (conn->flags & CO_FL_ERROR) {
7215 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007216 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007217 }
Emeric Brun46591952012-05-18 15:47:34 +02007218 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007219 /* A send succeeded, so we can consider ourself connected */
7220 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007221 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007222 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007223 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007224 }
7225 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007226 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007227
Emeric Brun46591952012-05-18 15:47:34 +02007228 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007229 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007230 /* handshake is running, and it may need to re-enable write */
7231 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007232 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007233#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007234 /* Async mode can be re-enabled, because we're leaving data state.*/
7235 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007236 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007237#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007238 break;
7239 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007240
Emeric Brun46591952012-05-18 15:47:34 +02007241 break;
7242 }
7243 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007244 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007245 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007246 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7247 SUB_RETRY_RECV,
7248 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007249#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007250 /* Async mode can be re-enabled, because we're leaving data state.*/
7251 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007252 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007253#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007254 break;
7255 }
Emeric Brun46591952012-05-18 15:47:34 +02007256 goto out_error;
7257 }
7258 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007259 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007260 return done;
7261
7262 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007263 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007264 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007265 ERR_clear_error();
7266
Emeric Brun46591952012-05-18 15:47:34 +02007267 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007268 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007269}
7270
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007271static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007272
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007273 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007274
Olivier Houchardea8dd942019-05-20 14:02:16 +02007275
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007276 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007277 if (ctx->wait_event.events != 0)
7278 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7279 ctx->wait_event.events,
7280 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007281 if (ctx->subs) {
7282 ctx->subs->events = 0;
7283 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007284 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007285
Olivier Houchard692c1d02019-05-23 18:41:47 +02007286 if (ctx->xprt->close)
7287 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007288#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007289 if (global_ssl.async) {
7290 OSSL_ASYNC_FD all_fd[32], afd;
7291 size_t num_all_fds = 0;
7292 int i;
7293
Olivier Houchard66ab4982019-02-26 18:37:15 +01007294 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007295 if (num_all_fds > 32) {
7296 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7297 return;
7298 }
7299
Olivier Houchard66ab4982019-02-26 18:37:15 +01007300 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007301
7302 /* If an async job is pending, we must try to
7303 to catch the end using polling before calling
7304 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007305 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007306 for (i=0 ; i < num_all_fds ; i++) {
7307 /* switch on an handler designed to
7308 * handle the SSL_free
7309 */
7310 afd = all_fd[i];
7311 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007312 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007313 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007314 /* To ensure that the fd cache won't be used
7315 * and we'll catch a real RD event.
7316 */
7317 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007318 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007319 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007320 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007321 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007322 return;
7323 }
Emeric Brun3854e012017-05-17 20:42:48 +02007324 /* Else we can remove the fds from the fdtab
7325 * and call SSL_free.
7326 * note: we do a fd_remove and not a delete
7327 * because the fd is owned by the engine.
7328 * the engine is responsible to close
7329 */
7330 for (i=0 ; i < num_all_fds ; i++)
7331 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007332 }
7333#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007334 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007335 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007336 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007337 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007338 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007339 }
Emeric Brun46591952012-05-18 15:47:34 +02007340}
7341
7342/* This function tries to perform a clean shutdown on an SSL connection, and in
7343 * any case, flags the connection as reusable if no handshake was in progress.
7344 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007345static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007346{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007347 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007348
Willy Tarreau911db9b2020-01-23 16:27:54 +01007349 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007350 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007351 if (!clean)
7352 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007353 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007354 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007355 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007356 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007357 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007358 ERR_clear_error();
7359 }
Emeric Brun46591952012-05-18 15:47:34 +02007360}
7361
William Lallemandd4f946c2019-12-05 10:26:40 +01007362/* fill a buffer with the algorithm and size of a public key */
7363static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007364{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007365 int bits = 0;
7366 int sig = TLSEXT_signature_anonymous;
7367 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007368 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007369
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007370 pkey = X509_get_pubkey(crt);
7371 if (pkey) {
7372 bits = EVP_PKEY_bits(pkey);
7373 switch(EVP_PKEY_base_id(pkey)) {
7374 case EVP_PKEY_RSA:
7375 sig = TLSEXT_signature_rsa;
7376 break;
7377 case EVP_PKEY_EC:
7378 sig = TLSEXT_signature_ecdsa;
7379 break;
7380 case EVP_PKEY_DSA:
7381 sig = TLSEXT_signature_dsa;
7382 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007383 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007384 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007385 }
7386
7387 switch(sig) {
7388 case TLSEXT_signature_rsa:
7389 len = chunk_printf(out, "RSA%d", bits);
7390 break;
7391 case TLSEXT_signature_ecdsa:
7392 len = chunk_printf(out, "EC%d", bits);
7393 break;
7394 case TLSEXT_signature_dsa:
7395 len = chunk_printf(out, "DSA%d", bits);
7396 break;
7397 default:
7398 return 0;
7399 }
7400 if (len < 0)
7401 return 0;
7402 return 1;
7403}
7404
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007405/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007406int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7407{
7408 struct ssl_sock_ctx *ctx;
7409 X509 *crt;
7410
7411 if (!ssl_sock_is_ssl(conn))
7412 return 0;
7413
7414 ctx = conn->xprt_ctx;
7415
7416 crt = SSL_get_certificate(ctx->ssl);
7417 if (!crt)
7418 return 0;
7419
7420 return cert_get_pkey_algo(crt, out);
7421}
7422
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007423/* used for ppv2 cert signature (can be used for logging) */
7424const char *ssl_sock_get_cert_sig(struct connection *conn)
7425{
Christopher Faulet82004142019-09-10 10:12:03 +02007426 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007427
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007428 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7429 X509 *crt;
7430
7431 if (!ssl_sock_is_ssl(conn))
7432 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007433 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007434 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007435 if (!crt)
7436 return NULL;
7437 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7438 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7439}
7440
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007441/* used for ppv2 authority */
7442const char *ssl_sock_get_sni(struct connection *conn)
7443{
7444#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007445 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007446
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007447 if (!ssl_sock_is_ssl(conn))
7448 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007449 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007450 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007451#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007452 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007453#endif
7454}
7455
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007456/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007457const char *ssl_sock_get_cipher_name(struct connection *conn)
7458{
Christopher Faulet82004142019-09-10 10:12:03 +02007459 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007460
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007461 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007462 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007463 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007464 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007465}
7466
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007467/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007468const char *ssl_sock_get_proto_version(struct connection *conn)
7469{
Christopher Faulet82004142019-09-10 10:12:03 +02007470 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007471
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007472 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007473 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007474 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007475 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007476}
7477
Willy Tarreau8d598402012-10-22 17:58:39 +02007478/* Extract a serial from a cert, and copy it to a chunk.
7479 * Returns 1 if serial is found and copied, 0 if no serial found and
7480 * -1 if output is not large enough.
7481 */
7482static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007483ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007484{
7485 ASN1_INTEGER *serial;
7486
7487 serial = X509_get_serialNumber(crt);
7488 if (!serial)
7489 return 0;
7490
7491 if (out->size < serial->length)
7492 return -1;
7493
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007494 memcpy(out->area, serial->data, serial->length);
7495 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007496 return 1;
7497}
7498
Emeric Brun43e79582014-10-29 19:03:26 +01007499/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007500 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7501 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007502 */
7503static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007504ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007505{
7506 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007507 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007508
7509 len =i2d_X509(crt, NULL);
7510 if (len <= 0)
7511 return 1;
7512
7513 if (out->size < len)
7514 return -1;
7515
7516 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007517 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007518 return 1;
7519}
7520
Emeric Brunce5ad802012-10-22 14:11:22 +02007521
Willy Tarreau83061a82018-07-13 11:56:34 +02007522/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007523 * Returns 1 if serial is found and copied, 0 if no valid time found
7524 * and -1 if output is not large enough.
7525 */
7526static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007527ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007528{
7529 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7530 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7531
7532 if (gentm->length < 12)
7533 return 0;
7534 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7535 return 0;
7536 if (out->size < gentm->length-2)
7537 return -1;
7538
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007539 memcpy(out->area, gentm->data+2, gentm->length-2);
7540 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007541 return 1;
7542 }
7543 else if (tm->type == V_ASN1_UTCTIME) {
7544 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7545
7546 if (utctm->length < 10)
7547 return 0;
7548 if (utctm->data[0] >= 0x35)
7549 return 0;
7550 if (out->size < utctm->length)
7551 return -1;
7552
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007553 memcpy(out->area, utctm->data, utctm->length);
7554 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007555 return 1;
7556 }
7557
7558 return 0;
7559}
7560
Emeric Brun87855892012-10-17 17:39:35 +02007561/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7562 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7563 */
7564static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007565ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7566 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007567{
7568 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007569 ASN1_OBJECT *obj;
7570 ASN1_STRING *data;
7571 const unsigned char *data_ptr;
7572 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007573 int i, j, n;
7574 int cur = 0;
7575 const char *s;
7576 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007577 int name_count;
7578
7579 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007580
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007581 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007582 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007583 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007584 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007585 else
7586 j = i;
7587
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007588 ne = X509_NAME_get_entry(a, j);
7589 obj = X509_NAME_ENTRY_get_object(ne);
7590 data = X509_NAME_ENTRY_get_data(ne);
7591 data_ptr = ASN1_STRING_get0_data(data);
7592 data_len = ASN1_STRING_length(data);
7593 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007594 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007595 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007596 s = tmp;
7597 }
7598
7599 if (chunk_strcasecmp(entry, s) != 0)
7600 continue;
7601
7602 if (pos < 0)
7603 cur--;
7604 else
7605 cur++;
7606
7607 if (cur != pos)
7608 continue;
7609
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007610 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007611 return -1;
7612
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007613 memcpy(out->area, data_ptr, data_len);
7614 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007615 return 1;
7616 }
7617
7618 return 0;
7619
William Lallemandd4f946c2019-12-05 10:26:40 +01007620}
7621
7622/*
7623 * Extract and format the DNS SAN extensions and copy result into a chuink
7624 * Return 0;
7625 */
7626#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7627static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7628{
7629 int i;
7630 char *str;
7631 STACK_OF(GENERAL_NAME) *names = NULL;
7632
7633 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7634 if (names) {
7635 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7636 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7637 if (i > 0)
7638 chunk_appendf(out, ", ");
7639 if (name->type == GEN_DNS) {
7640 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7641 chunk_appendf(out, "DNS:%s", str);
7642 OPENSSL_free(str);
7643 }
7644 }
7645 }
7646 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7647 }
7648 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007649}
William Lallemandd4f946c2019-12-05 10:26:40 +01007650#endif
Emeric Brun87855892012-10-17 17:39:35 +02007651
Elliot Otchet71f82972020-01-15 08:12:14 -05007652/*
7653 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7654 * Currently supports rfc2253 for returning LDAP V3 DNs.
7655 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7656 */
7657static int
7658ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7659{
7660 BIO *bio = NULL;
7661 int ret = 0;
7662 int data_len = 0;
7663
7664 if (chunk_strcmp(format, "rfc2253") == 0) {
7665 bio = BIO_new(BIO_s_mem());
7666 if (bio == NULL)
7667 goto out;
7668
7669 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7670 goto out;
7671
7672 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7673 goto out;
7674
7675 out->data = data_len;
7676
7677 ret = 1;
7678 }
7679out:
7680 if (bio)
7681 BIO_free(bio);
7682 return ret;
7683}
7684
Emeric Brun87855892012-10-17 17:39:35 +02007685/* Extract and format full DN from a X509_NAME and copy result into a chunk
7686 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7687 */
7688static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007689ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007690{
7691 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007692 ASN1_OBJECT *obj;
7693 ASN1_STRING *data;
7694 const unsigned char *data_ptr;
7695 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007696 int i, n, ln;
7697 int l = 0;
7698 const char *s;
7699 char *p;
7700 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007701 int name_count;
7702
7703
7704 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007705
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007706 out->data = 0;
7707 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007708 for (i = 0; i < name_count; i++) {
7709 ne = X509_NAME_get_entry(a, i);
7710 obj = X509_NAME_ENTRY_get_object(ne);
7711 data = X509_NAME_ENTRY_get_data(ne);
7712 data_ptr = ASN1_STRING_get0_data(data);
7713 data_len = ASN1_STRING_length(data);
7714 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007715 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007716 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007717 s = tmp;
7718 }
7719 ln = strlen(s);
7720
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007721 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007722 if (l > out->size)
7723 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007724 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007725
7726 *(p++)='/';
7727 memcpy(p, s, ln);
7728 p += ln;
7729 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007730 memcpy(p, data_ptr, data_len);
7731 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007732 }
7733
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007734 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007735 return 0;
7736
7737 return 1;
7738}
7739
Olivier Houchardab28a322018-12-21 19:45:40 +01007740void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7741{
7742#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007743 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007744
Olivier Houcharde488ea82019-06-28 14:10:33 +02007745 if (!ssl_sock_is_ssl(conn))
7746 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007747 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007748 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007749#endif
7750}
7751
Willy Tarreau119a4082016-12-22 21:58:38 +01007752/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7753 * to disable SNI.
7754 */
Willy Tarreau63076412015-07-10 11:33:32 +02007755void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7756{
7757#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007758 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007759
Willy Tarreau119a4082016-12-22 21:58:38 +01007760 char *prev_name;
7761
Willy Tarreau63076412015-07-10 11:33:32 +02007762 if (!ssl_sock_is_ssl(conn))
7763 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007764 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007765
Willy Tarreau119a4082016-12-22 21:58:38 +01007766 /* if the SNI changes, we must destroy the reusable context so that a
7767 * new connection will present a new SNI. As an optimization we could
7768 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7769 * server.
7770 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007771 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007772 if ((!prev_name && hostname) ||
7773 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007774 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007775
Olivier Houchard66ab4982019-02-26 18:37:15 +01007776 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007777#endif
7778}
7779
Emeric Brun0abf8362014-06-24 18:26:41 +02007780/* Extract peer certificate's common name into the chunk dest
7781 * Returns
7782 * the len of the extracted common name
7783 * or 0 if no CN found in DN
7784 * or -1 on error case (i.e. no peer certificate)
7785 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007786int ssl_sock_get_remote_common_name(struct connection *conn,
7787 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007788{
Christopher Faulet82004142019-09-10 10:12:03 +02007789 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007790 X509 *crt = NULL;
7791 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007792 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007793 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007794 .area = (char *)&find_cn,
7795 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007796 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007797 int result = -1;
David Safb76832014-05-08 23:42:08 -04007798
7799 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007800 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007801 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007802
7803 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007804 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007805 if (!crt)
7806 goto out;
7807
7808 name = X509_get_subject_name(crt);
7809 if (!name)
7810 goto out;
David Safb76832014-05-08 23:42:08 -04007811
Emeric Brun0abf8362014-06-24 18:26:41 +02007812 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7813out:
David Safb76832014-05-08 23:42:08 -04007814 if (crt)
7815 X509_free(crt);
7816
7817 return result;
7818}
7819
Dave McCowan328fb582014-07-30 10:39:13 -04007820/* returns 1 if client passed a certificate for this session, 0 if not */
7821int ssl_sock_get_cert_used_sess(struct connection *conn)
7822{
Christopher Faulet82004142019-09-10 10:12:03 +02007823 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007824 X509 *crt = NULL;
7825
7826 if (!ssl_sock_is_ssl(conn))
7827 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007828 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007829
7830 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007831 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007832 if (!crt)
7833 return 0;
7834
7835 X509_free(crt);
7836 return 1;
7837}
7838
7839/* returns 1 if client passed a certificate for this connection, 0 if not */
7840int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007841{
Christopher Faulet82004142019-09-10 10:12:03 +02007842 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007843
David Safb76832014-05-08 23:42:08 -04007844 if (!ssl_sock_is_ssl(conn))
7845 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007846 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007847 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007848}
7849
7850/* returns result from SSL verify */
7851unsigned int ssl_sock_get_verify_result(struct connection *conn)
7852{
Christopher Faulet82004142019-09-10 10:12:03 +02007853 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007854
David Safb76832014-05-08 23:42:08 -04007855 if (!ssl_sock_is_ssl(conn))
7856 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007857 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007858 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007859}
7860
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007861/* Returns the application layer protocol name in <str> and <len> when known.
7862 * Zero is returned if the protocol name was not found, otherwise non-zero is
7863 * returned. The string is allocated in the SSL context and doesn't have to be
7864 * freed by the caller. NPN is also checked if available since older versions
7865 * of openssl (1.0.1) which are more common in field only support this one.
7866 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007867static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007868{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007869#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7870 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007871 struct ssl_sock_ctx *ctx = xprt_ctx;
7872 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007873 return 0;
7874
7875 *str = NULL;
7876
7877#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007878 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007879 if (*str)
7880 return 1;
7881#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007882#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007883 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007884 if (*str)
7885 return 1;
7886#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007887#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007888 return 0;
7889}
7890
Willy Tarreau7875d092012-09-10 08:20:03 +02007891/***** Below are some sample fetching functions for ACL/patterns *****/
7892
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007893static int
7894smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7895{
7896 struct connection *conn;
7897
7898 conn = objt_conn(smp->sess->origin);
7899 if (!conn || conn->xprt != &ssl_sock)
7900 return 0;
7901
7902 smp->flags = 0;
7903 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007904#ifdef OPENSSL_IS_BORINGSSL
7905 {
7906 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7907 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7908 SSL_early_data_accepted(ctx->ssl));
7909 }
7910#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007911 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007912 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007913#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007914 return 1;
7915}
7916
Emeric Brune64aef12012-09-21 13:15:06 +02007917/* boolean, returns true if client cert was present */
7918static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007919smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007920{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007921 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007922 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007923
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007924 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007925 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007926 return 0;
7927
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007928 ctx = conn->xprt_ctx;
7929
Willy Tarreau911db9b2020-01-23 16:27:54 +01007930 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007931 smp->flags |= SMP_F_MAY_CHANGE;
7932 return 0;
7933 }
7934
7935 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007936 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007937 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007938
7939 return 1;
7940}
7941
Emeric Brun43e79582014-10-29 19:03:26 +01007942/* binary, returns a certificate in a binary chunk (der/raw).
7943 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7944 * should be use.
7945 */
7946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007947smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007948{
7949 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7950 X509 *crt = NULL;
7951 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007952 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007953 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007954 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007955
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007956 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007957 if (!conn || conn->xprt != &ssl_sock)
7958 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007959 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007960
Willy Tarreau911db9b2020-01-23 16:27:54 +01007961 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007962 smp->flags |= SMP_F_MAY_CHANGE;
7963 return 0;
7964 }
7965
7966 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007967 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007968 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007969 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007970
7971 if (!crt)
7972 goto out;
7973
7974 smp_trash = get_trash_chunk();
7975 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7976 goto out;
7977
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007978 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007979 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007980 ret = 1;
7981out:
7982 /* SSL_get_peer_certificate, it increase X509 * ref count */
7983 if (cert_peer && crt)
7984 X509_free(crt);
7985 return ret;
7986}
7987
Emeric Brunba841a12014-04-30 17:05:08 +02007988/* binary, returns serial of certificate in a binary chunk.
7989 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7990 * should be use.
7991 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007992static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007993smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007994{
Emeric Brunba841a12014-04-30 17:05:08 +02007995 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007996 X509 *crt = NULL;
7997 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007998 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007999 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008000 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008001
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008002 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008003 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02008004 return 0;
8005
Olivier Houchard66ab4982019-02-26 18:37:15 +01008006 ctx = conn->xprt_ctx;
8007
Willy Tarreau911db9b2020-01-23 16:27:54 +01008008 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02008009 smp->flags |= SMP_F_MAY_CHANGE;
8010 return 0;
8011 }
8012
Emeric Brunba841a12014-04-30 17:05:08 +02008013 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008014 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008015 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008016 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008017
Willy Tarreau8d598402012-10-22 17:58:39 +02008018 if (!crt)
8019 goto out;
8020
Willy Tarreau47ca5452012-12-23 20:22:19 +01008021 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02008022 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
8023 goto out;
8024
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008025 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008026 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02008027 ret = 1;
8028out:
Emeric Brunba841a12014-04-30 17:05:08 +02008029 /* SSL_get_peer_certificate, it increase X509 * ref count */
8030 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02008031 X509_free(crt);
8032 return ret;
8033}
Emeric Brune64aef12012-09-21 13:15:06 +02008034
Emeric Brunba841a12014-04-30 17:05:08 +02008035/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
8036 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8037 * should be use.
8038 */
James Votha051b4a2013-05-14 20:37:59 +02008039static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008040smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02008041{
Emeric Brunba841a12014-04-30 17:05:08 +02008042 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02008043 X509 *crt = NULL;
8044 const EVP_MD *digest;
8045 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01008046 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008047 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008048 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008049 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02008050
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008051 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008052 if (!conn || conn->xprt != &ssl_sock)
8053 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008054 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008055
Willy Tarreau911db9b2020-01-23 16:27:54 +01008056 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008057 smp->flags |= SMP_F_MAY_CHANGE;
8058 return 0;
8059 }
8060
Emeric Brunba841a12014-04-30 17:05:08 +02008061 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008062 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008063 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008064 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008065 if (!crt)
8066 goto out;
8067
8068 smp_trash = get_trash_chunk();
8069 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008070 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8071 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008072 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008073 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008074 ret = 1;
8075out:
Emeric Brunba841a12014-04-30 17:05:08 +02008076 /* SSL_get_peer_certificate, it increase X509 * ref count */
8077 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008078 X509_free(crt);
8079 return ret;
8080}
8081
Emeric Brunba841a12014-04-30 17:05:08 +02008082/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8083 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8084 * should be use.
8085 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008086static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008087smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008088{
Emeric Brunba841a12014-04-30 17:05:08 +02008089 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008090 X509 *crt = NULL;
8091 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008092 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008093 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008094 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008095
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008096 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008097 if (!conn || conn->xprt != &ssl_sock)
8098 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008099 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008100
Willy Tarreau911db9b2020-01-23 16:27:54 +01008101 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008102 smp->flags |= SMP_F_MAY_CHANGE;
8103 return 0;
8104 }
8105
Emeric Brunba841a12014-04-30 17:05:08 +02008106 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008107 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008108 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008109 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008110 if (!crt)
8111 goto out;
8112
Willy Tarreau47ca5452012-12-23 20:22:19 +01008113 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008114 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008115 goto out;
8116
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008117 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008118 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008119 ret = 1;
8120out:
Emeric Brunba841a12014-04-30 17:05:08 +02008121 /* SSL_get_peer_certificate, it increase X509 * ref count */
8122 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008123 X509_free(crt);
8124 return ret;
8125}
8126
Emeric Brunba841a12014-04-30 17:05:08 +02008127/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8128 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8129 * should be use.
8130 */
Emeric Brun87855892012-10-17 17:39:35 +02008131static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008132smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008133{
Emeric Brunba841a12014-04-30 17:05:08 +02008134 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008135 X509 *crt = NULL;
8136 X509_NAME *name;
8137 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008138 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008139 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008140 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008141
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008142 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008143 if (!conn || conn->xprt != &ssl_sock)
8144 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008145 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008146
Willy Tarreau911db9b2020-01-23 16:27:54 +01008147 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008148 smp->flags |= SMP_F_MAY_CHANGE;
8149 return 0;
8150 }
8151
Emeric Brunba841a12014-04-30 17:05:08 +02008152 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008153 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008154 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008155 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008156 if (!crt)
8157 goto out;
8158
8159 name = X509_get_issuer_name(crt);
8160 if (!name)
8161 goto out;
8162
Willy Tarreau47ca5452012-12-23 20:22:19 +01008163 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008164 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008165 int pos = 1;
8166
8167 if (args[1].type == ARGT_SINT)
8168 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008169
8170 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8171 goto out;
8172 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008173 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8174 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8175 goto out;
8176 }
Emeric Brun87855892012-10-17 17:39:35 +02008177 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8178 goto out;
8179
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008180 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008181 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008182 ret = 1;
8183out:
Emeric Brunba841a12014-04-30 17:05:08 +02008184 /* SSL_get_peer_certificate, it increase X509 * ref count */
8185 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008186 X509_free(crt);
8187 return ret;
8188}
8189
Emeric Brunba841a12014-04-30 17:05:08 +02008190/* string, returns notbefore date in ASN1_UTCTIME format.
8191 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8192 * should be use.
8193 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008194static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008195smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008196{
Emeric Brunba841a12014-04-30 17:05:08 +02008197 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008198 X509 *crt = NULL;
8199 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008200 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008201 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008202 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008203
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008204 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008205 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008206 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008207 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008208
Willy Tarreau911db9b2020-01-23 16:27:54 +01008209 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008210 smp->flags |= SMP_F_MAY_CHANGE;
8211 return 0;
8212 }
8213
Emeric Brunba841a12014-04-30 17:05:08 +02008214 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008215 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008216 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008217 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008218 if (!crt)
8219 goto out;
8220
Willy Tarreau47ca5452012-12-23 20:22:19 +01008221 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008222 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008223 goto out;
8224
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008225 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008226 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008227 ret = 1;
8228out:
Emeric Brunba841a12014-04-30 17:05:08 +02008229 /* SSL_get_peer_certificate, it increase X509 * ref count */
8230 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008231 X509_free(crt);
8232 return ret;
8233}
8234
Emeric Brunba841a12014-04-30 17:05:08 +02008235/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8236 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8237 * should be use.
8238 */
Emeric Brun87855892012-10-17 17:39:35 +02008239static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008240smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008241{
Emeric Brunba841a12014-04-30 17:05:08 +02008242 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008243 X509 *crt = NULL;
8244 X509_NAME *name;
8245 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008246 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008247 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008248 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008249
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008250 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008251 if (!conn || conn->xprt != &ssl_sock)
8252 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008253 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008254
Willy Tarreau911db9b2020-01-23 16:27:54 +01008255 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008256 smp->flags |= SMP_F_MAY_CHANGE;
8257 return 0;
8258 }
8259
Emeric Brunba841a12014-04-30 17:05:08 +02008260 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008261 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008262 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008263 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008264 if (!crt)
8265 goto out;
8266
8267 name = X509_get_subject_name(crt);
8268 if (!name)
8269 goto out;
8270
Willy Tarreau47ca5452012-12-23 20:22:19 +01008271 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008272 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008273 int pos = 1;
8274
8275 if (args[1].type == ARGT_SINT)
8276 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008277
8278 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8279 goto out;
8280 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008281 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8282 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8283 goto out;
8284 }
Emeric Brun87855892012-10-17 17:39:35 +02008285 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8286 goto out;
8287
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008288 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008289 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008290 ret = 1;
8291out:
Emeric Brunba841a12014-04-30 17:05:08 +02008292 /* SSL_get_peer_certificate, it increase X509 * ref count */
8293 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008294 X509_free(crt);
8295 return ret;
8296}
Emeric Brun9143d372012-12-20 15:44:16 +01008297
8298/* integer, returns true if current session use a client certificate */
8299static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008300smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008301{
8302 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008303 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008304 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008305
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008306 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008307 if (!conn || conn->xprt != &ssl_sock)
8308 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008309 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008310
Willy Tarreau911db9b2020-01-23 16:27:54 +01008311 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008312 smp->flags |= SMP_F_MAY_CHANGE;
8313 return 0;
8314 }
8315
8316 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008317 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008318 if (crt) {
8319 X509_free(crt);
8320 }
8321
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008322 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008323 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008324 return 1;
8325}
8326
Emeric Brunba841a12014-04-30 17:05:08 +02008327/* integer, returns the certificate version
8328 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8329 * should be use.
8330 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008331static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008332smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008333{
Emeric Brunba841a12014-04-30 17:05:08 +02008334 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008335 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008336 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008337 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008338
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008339 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008340 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008341 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008342 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008343
Willy Tarreau911db9b2020-01-23 16:27:54 +01008344 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008345 smp->flags |= SMP_F_MAY_CHANGE;
8346 return 0;
8347 }
8348
Emeric Brunba841a12014-04-30 17:05:08 +02008349 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008350 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008351 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008352 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008353 if (!crt)
8354 return 0;
8355
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008356 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008357 /* SSL_get_peer_certificate increase X509 * ref count */
8358 if (cert_peer)
8359 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008360 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008361
8362 return 1;
8363}
8364
Emeric Brunba841a12014-04-30 17:05:08 +02008365/* string, returns the certificate's signature algorithm.
8366 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8367 * should be use.
8368 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008369static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008370smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008371{
Emeric Brunba841a12014-04-30 17:05:08 +02008372 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008373 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008374 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008375 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008376 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008377 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008378
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008379 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008380 if (!conn || conn->xprt != &ssl_sock)
8381 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008382 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008383
Willy Tarreau911db9b2020-01-23 16:27:54 +01008384 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008385 smp->flags |= SMP_F_MAY_CHANGE;
8386 return 0;
8387 }
8388
Emeric Brunba841a12014-04-30 17:05:08 +02008389 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008390 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008391 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008392 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008393 if (!crt)
8394 return 0;
8395
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008396 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8397 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008398
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008399 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8400 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008401 /* SSL_get_peer_certificate increase X509 * ref count */
8402 if (cert_peer)
8403 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008404 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008405 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008406
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008407 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008408 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008409 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008410 /* SSL_get_peer_certificate increase X509 * ref count */
8411 if (cert_peer)
8412 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008413
8414 return 1;
8415}
8416
Emeric Brunba841a12014-04-30 17:05:08 +02008417/* string, returns the certificate's key algorithm.
8418 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8419 * should be use.
8420 */
Emeric Brun521a0112012-10-22 12:22:55 +02008421static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008422smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008423{
Emeric Brunba841a12014-04-30 17:05:08 +02008424 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008425 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008426 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008427 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008428 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008429 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008430
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008431 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008432 if (!conn || conn->xprt != &ssl_sock)
8433 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008434 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008435
Willy Tarreau911db9b2020-01-23 16:27:54 +01008436 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008437 smp->flags |= SMP_F_MAY_CHANGE;
8438 return 0;
8439 }
8440
Emeric Brunba841a12014-04-30 17:05:08 +02008441 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008442 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008443 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008444 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008445 if (!crt)
8446 return 0;
8447
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008448 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8449 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008450
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008451 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8452 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008453 /* SSL_get_peer_certificate increase X509 * ref count */
8454 if (cert_peer)
8455 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008456 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008457 }
Emeric Brun521a0112012-10-22 12:22:55 +02008458
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008459 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008460 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008461 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008462 if (cert_peer)
8463 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008464
8465 return 1;
8466}
8467
Emeric Brun645ae792014-04-30 14:21:06 +02008468/* boolean, returns true if front conn. transport layer is SSL.
8469 * This function is also usable on backend conn if the fetch keyword 5th
8470 * char is 'b'.
8471 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008473smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008474{
Emeric Bruneb8def92018-02-19 15:59:48 +01008475 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8476 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008477
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008478 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008479 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008480 return 1;
8481}
8482
Emeric Brun2525b6b2012-10-18 15:59:43 +02008483/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008484static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008485smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008486{
8487#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008488 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008489 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008490
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008491 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008492 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008493 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008494 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008495 return 1;
8496#else
8497 return 0;
8498#endif
8499}
8500
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008501/* boolean, returns true if client session has been resumed.
8502 * This function is also usable on backend conn if the fetch keyword 5th
8503 * char is 'b'.
8504 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008505static int
8506smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8507{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008508 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8509 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008510 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008511
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008512
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008513 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008514 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008515 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008516 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008517 return 1;
8518}
8519
Emeric Brun645ae792014-04-30 14:21:06 +02008520/* string, returns the used cipher if front conn. transport layer is SSL.
8521 * This function is also usable on backend conn if the fetch keyword 5th
8522 * char is 'b'.
8523 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008524static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008525smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008526{
Emeric Bruneb8def92018-02-19 15:59:48 +01008527 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8528 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008529 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008530
Willy Tarreaube508f12016-03-10 11:47:01 +01008531 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008532 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008533 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008534 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008535
Olivier Houchard66ab4982019-02-26 18:37:15 +01008536 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008537 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008538 return 0;
8539
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008540 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008541 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008542 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008543
8544 return 1;
8545}
8546
Emeric Brun645ae792014-04-30 14:21:06 +02008547/* integer, returns the algoritm's keysize if front conn. transport layer
8548 * is SSL.
8549 * This function is also usable on backend conn if the fetch keyword 5th
8550 * char is 'b'.
8551 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008552static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008553smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008554{
Emeric Bruneb8def92018-02-19 15:59:48 +01008555 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8556 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008557 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008558 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008559
Emeric Brun589fcad2012-10-16 14:13:26 +02008560 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008561 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008562 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008563 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008564
Olivier Houchard66ab4982019-02-26 18:37:15 +01008565 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008566 return 0;
8567
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008568 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008569 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008570
8571 return 1;
8572}
8573
Emeric Brun645ae792014-04-30 14:21:06 +02008574/* integer, returns the used keysize if front conn. transport layer is SSL.
8575 * This function is also usable on backend conn if the fetch keyword 5th
8576 * char is 'b'.
8577 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008578static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008579smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008580{
Emeric Bruneb8def92018-02-19 15:59:48 +01008581 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8582 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008583 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008584
Emeric Brun589fcad2012-10-16 14:13:26 +02008585 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008586 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8587 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008588 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008589
Olivier Houchard66ab4982019-02-26 18:37:15 +01008590 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008591 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008592 return 0;
8593
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008594 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008595
8596 return 1;
8597}
8598
Bernard Spil13c53f82018-02-15 13:34:58 +01008599#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008600static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008601smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008602{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008603 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008604 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008605 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008606
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008607 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008608 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008609
Olivier Houchard6b77f492018-11-22 18:18:29 +01008610 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8611 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008612 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8613 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008614 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008615
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008616 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008617 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008618 (const unsigned char **)&smp->data.u.str.area,
8619 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008620
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008621 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008622 return 0;
8623
Willy Tarreau105599c2020-02-25 08:59:23 +01008624 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008625 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008626}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008627#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008628
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008629#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008630static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008631smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008632{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008633 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008634 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008635 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008636
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008637 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008638 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008639
Olivier Houchard6b77f492018-11-22 18:18:29 +01008640 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8641 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8642
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008643 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008644 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008645 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008647 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008648 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008649 (const unsigned char **)&smp->data.u.str.area,
8650 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008651
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008652 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008653 return 0;
8654
Willy Tarreau105599c2020-02-25 08:59:23 +01008655 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008656 return 1;
8657}
8658#endif
8659
Emeric Brun645ae792014-04-30 14:21:06 +02008660/* string, returns the used protocol if front conn. transport layer is SSL.
8661 * This function is also usable on backend conn if the fetch keyword 5th
8662 * char is 'b'.
8663 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008664static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008665smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008666{
Emeric Bruneb8def92018-02-19 15:59:48 +01008667 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8668 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008669 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008670
Emeric Brun589fcad2012-10-16 14:13:26 +02008671 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008672 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8673 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008674 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008675
Olivier Houchard66ab4982019-02-26 18:37:15 +01008676 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008677 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008678 return 0;
8679
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008680 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008681 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008682 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008683
8684 return 1;
8685}
8686
Willy Tarreau87b09662015-04-03 00:22:06 +02008687/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008688 * This function is also usable on backend conn if the fetch keyword 5th
8689 * char is 'b'.
8690 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008691#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008692static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008693smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008694{
Emeric Bruneb8def92018-02-19 15:59:48 +01008695 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8696 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008697 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008698 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008699 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008700
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008701 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008702 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008703
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008704 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8705 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008706 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008707
Olivier Houchard66ab4982019-02-26 18:37:15 +01008708 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008709 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008710 return 0;
8711
Willy Tarreau105599c2020-02-25 08:59:23 +01008712 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008713 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008714 return 0;
8715
Willy Tarreau105599c2020-02-25 08:59:23 +01008716 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008717 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008718}
Patrick Hemmer41966772018-04-28 19:15:48 -04008719#endif
8720
Emeric Brunfe68f682012-10-16 14:59:28 +02008721
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008722#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008723static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008724smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8725{
8726 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8727 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8728 struct buffer *data;
8729 struct ssl_sock_ctx *ctx;
8730
8731 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8732 return 0;
8733 ctx = conn->xprt_ctx;
8734
8735 data = get_trash_chunk();
8736 if (kw[7] == 'c')
8737 data->data = SSL_get_client_random(ctx->ssl,
8738 (unsigned char *) data->area,
8739 data->size);
8740 else
8741 data->data = SSL_get_server_random(ctx->ssl,
8742 (unsigned char *) data->area,
8743 data->size);
8744 if (!data->data)
8745 return 0;
8746
8747 smp->flags = 0;
8748 smp->data.type = SMP_T_BIN;
8749 smp->data.u.str = *data;
8750
8751 return 1;
8752}
8753
8754static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008755smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8756{
8757 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8758 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8759 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008760 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008761 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008762
8763 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8764 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008765 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008766
Olivier Houchard66ab4982019-02-26 18:37:15 +01008767 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008768 if (!ssl_sess)
8769 return 0;
8770
8771 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008772 data->data = SSL_SESSION_get_master_key(ssl_sess,
8773 (unsigned char *) data->area,
8774 data->size);
8775 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008776 return 0;
8777
8778 smp->flags = 0;
8779 smp->data.type = SMP_T_BIN;
8780 smp->data.u.str = *data;
8781
8782 return 1;
8783}
8784#endif
8785
Patrick Hemmer41966772018-04-28 19:15:48 -04008786#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008787static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008788smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008789{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008790 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008791 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008792
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008793 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008794 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008795
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008796 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008797 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8798 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008799 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008800
Olivier Houchard66ab4982019-02-26 18:37:15 +01008801 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008802 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008803 return 0;
8804
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008805 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008806 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008807}
Patrick Hemmer41966772018-04-28 19:15:48 -04008808#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008809
David Sc1ad52e2014-04-08 18:48:47 -04008810static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008811smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8812{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008813 struct connection *conn;
8814 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008815 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008816
8817 conn = objt_conn(smp->sess->origin);
8818 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8819 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008820 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008821
Olivier Houchard66ab4982019-02-26 18:37:15 +01008822 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008823 if (!capture)
8824 return 0;
8825
8826 smp->flags = SMP_F_CONST;
8827 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008828 smp->data.u.str.area = capture->ciphersuite;
8829 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008830 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008831}
8832
8833static int
8834smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8835{
Willy Tarreau83061a82018-07-13 11:56:34 +02008836 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008837
8838 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8839 return 0;
8840
8841 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008842 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008843 smp->data.type = SMP_T_BIN;
8844 smp->data.u.str = *data;
8845 return 1;
8846}
8847
8848static int
8849smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8850{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008851 struct connection *conn;
8852 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008853 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008854
8855 conn = objt_conn(smp->sess->origin);
8856 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8857 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008858 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008859
Olivier Houchard66ab4982019-02-26 18:37:15 +01008860 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008861 if (!capture)
8862 return 0;
8863
8864 smp->data.type = SMP_T_SINT;
8865 smp->data.u.sint = capture->xxh64;
8866 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008867}
8868
8869static int
8870smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8871{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008872#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008873 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008874 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008875
8876 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8877 return 0;
8878
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008879 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008880 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008881 const char *str;
8882 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008883 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008884 uint16_t id = (bin[0] << 8) | bin[1];
8885#if defined(OPENSSL_IS_BORINGSSL)
8886 cipher = SSL_get_cipher_by_value(id);
8887#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008888 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008889 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8890 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008891#endif
8892 str = SSL_CIPHER_get_name(cipher);
8893 if (!str || strcmp(str, "(NONE)") == 0)
8894 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008895 else
8896 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8897 }
8898 smp->data.type = SMP_T_STR;
8899 smp->data.u.str = *data;
8900 return 1;
8901#else
8902 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8903#endif
8904}
8905
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008906#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008907static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008908smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008909{
Emeric Bruneb8def92018-02-19 15:59:48 +01008910 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8911 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008912 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008913 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008914 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008915
8916 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008917 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8918 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008919 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008920
Willy Tarreau911db9b2020-01-23 16:27:54 +01008921 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008922 smp->flags |= SMP_F_MAY_CHANGE;
8923 return 0;
8924 }
8925
8926 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008927 if (!SSL_session_reused(ctx->ssl))
8928 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008929 finished_trash->area,
8930 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008931 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008932 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008933 finished_trash->area,
8934 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008935
8936 if (!finished_len)
8937 return 0;
8938
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008939 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008940 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008941 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008942
8943 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008944}
Patrick Hemmer41966772018-04-28 19:15:48 -04008945#endif
David Sc1ad52e2014-04-08 18:48:47 -04008946
Emeric Brun2525b6b2012-10-18 15:59:43 +02008947/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008948static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008949smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008950{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008951 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008952 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008953
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008954 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008955 if (!conn || conn->xprt != &ssl_sock)
8956 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008957 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008958
Willy Tarreau911db9b2020-01-23 16:27:54 +01008959 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008960 smp->flags = SMP_F_MAY_CHANGE;
8961 return 0;
8962 }
8963
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008964 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008965 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008966 smp->flags = 0;
8967
8968 return 1;
8969}
8970
Emeric Brun2525b6b2012-10-18 15:59:43 +02008971/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008972static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008973smp_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 +02008974{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008975 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008976 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008977
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008978 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008979 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008980 return 0;
8981
Willy Tarreau911db9b2020-01-23 16:27:54 +01008982 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008983 smp->flags = SMP_F_MAY_CHANGE;
8984 return 0;
8985 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008986 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008987
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008988 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008989 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008990 smp->flags = 0;
8991
8992 return 1;
8993}
8994
Emeric Brun2525b6b2012-10-18 15:59:43 +02008995/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008996static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008997smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008998{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008999 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009000 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009001
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009002 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009003 if (!conn || conn->xprt != &ssl_sock)
9004 return 0;
9005
Willy Tarreau911db9b2020-01-23 16:27:54 +01009006 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02009007 smp->flags = SMP_F_MAY_CHANGE;
9008 return 0;
9009 }
9010
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009011 ctx = conn->xprt_ctx;
9012
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009013 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009014 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009015 smp->flags = 0;
9016
9017 return 1;
9018}
9019
Emeric Brun2525b6b2012-10-18 15:59:43 +02009020/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009021static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009022smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009023{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009024 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009025 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009026
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009027 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009028 if (!conn || conn->xprt != &ssl_sock)
9029 return 0;
9030
Willy Tarreau911db9b2020-01-23 16:27:54 +01009031 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009032 smp->flags = SMP_F_MAY_CHANGE;
9033 return 0;
9034 }
9035
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009036 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009037 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009038 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009039
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009040 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009041 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009042 smp->flags = 0;
9043
9044 return 1;
9045}
9046
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009047/* for ca-file and ca-verify-file */
9048static 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 +02009049{
9050 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009051 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009052 return ERR_ALERT | ERR_FATAL;
9053 }
9054
Willy Tarreauef934602016-12-22 23:12:01 +01009055 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009056 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009057 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009058 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009059
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009060 if (!ssl_store_load_locations_file(*ca_file_p)) {
9061 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009062 return ERR_ALERT | ERR_FATAL;
9063 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009064 return 0;
9065}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009066
9067/* parse the "ca-file" bind keyword */
9068static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9069{
9070 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9071}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009072static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9073{
9074 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9075}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009076
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009077/* parse the "ca-verify-file" bind keyword */
9078static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9079{
9080 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9081}
9082static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9083{
9084 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9085}
9086
Christopher Faulet31af49d2015-06-09 17:29:50 +02009087/* parse the "ca-sign-file" bind keyword */
9088static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9089{
9090 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009091 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009092 return ERR_ALERT | ERR_FATAL;
9093 }
9094
Willy Tarreauef934602016-12-22 23:12:01 +01009095 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9096 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009097 else
9098 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9099
9100 return 0;
9101}
9102
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009103/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009104static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9105{
9106 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009107 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009108 return ERR_ALERT | ERR_FATAL;
9109 }
9110 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9111 return 0;
9112}
9113
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009114/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009115static 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 +02009116{
9117 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009118 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009119 return ERR_ALERT | ERR_FATAL;
9120 }
9121
Emeric Brun76d88952012-10-05 15:47:31 +02009122 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009123 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009124 return 0;
9125}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009126static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9127{
9128 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9129}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009130
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009131#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009132/* parse the "ciphersuites" bind keyword */
9133static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9134{
9135 if (!*args[cur_arg + 1]) {
9136 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9137 return ERR_ALERT | ERR_FATAL;
9138 }
9139
9140 free(conf->ciphersuites);
9141 conf->ciphersuites = strdup(args[cur_arg + 1]);
9142 return 0;
9143}
9144static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9145{
9146 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9147}
9148#endif
9149
Willy Tarreaubbc91962019-10-16 16:42:19 +02009150/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009151static 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 +02009152{
Willy Tarreau38011032013-08-13 16:59:39 +02009153 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009154
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009155 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009156 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009157 return ERR_ALERT | ERR_FATAL;
9158 }
9159
Willy Tarreauef934602016-12-22 23:12:01 +01009160 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9161 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009162 memprintf(err, "'%s' : path too long", args[cur_arg]);
9163 return ERR_ALERT | ERR_FATAL;
9164 }
Willy Tarreauef934602016-12-22 23:12:01 +01009165 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009166 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009167 }
9168
Willy Tarreaubbc91962019-10-16 16:42:19 +02009169 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009170}
9171
Willy Tarreaubbc91962019-10-16 16:42:19 +02009172/* 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 +01009173static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9174{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009175 int err_code;
9176
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009177 if (!*args[cur_arg + 1]) {
9178 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9179 return ERR_ALERT | ERR_FATAL;
9180 }
9181
William Lallemand6be66ec2020-03-06 22:26:32 +01009182 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009183 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009184 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009185
Willy Tarreaubbc91962019-10-16 16:42:19 +02009186 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009187}
9188
Emeric Brunfb510ea2012-10-05 12:00:26 +02009189/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009190static 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 +02009191{
Emeric Brun051cdab2012-10-02 19:25:50 +02009192#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009193 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009194 return ERR_ALERT | ERR_FATAL;
9195#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009196 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009197 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009198 return ERR_ALERT | ERR_FATAL;
9199 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009200
Willy Tarreauef934602016-12-22 23:12:01 +01009201 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9202 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009203 else
9204 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009205
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009206 if (!ssl_store_load_locations_file(conf->crl_file)) {
9207 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9208 return ERR_ALERT | ERR_FATAL;
9209 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009210 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009211#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009212}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009213static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9214{
9215 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9216}
Emeric Brun2b58d042012-09-20 17:10:03 +02009217
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009218/* parse the "curves" bind keyword keyword */
9219static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9220{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009221#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009222 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009223 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009224 return ERR_ALERT | ERR_FATAL;
9225 }
9226 conf->curves = strdup(args[cur_arg + 1]);
9227 return 0;
9228#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009229 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009230 return ERR_ALERT | ERR_FATAL;
9231#endif
9232}
9233static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9234{
9235 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9236}
9237
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009238/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009239static 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 +02009240{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009241#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009242 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009243 return ERR_ALERT | ERR_FATAL;
9244#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009245 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 +02009246 return ERR_ALERT | ERR_FATAL;
9247#else
9248 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009249 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009250 return ERR_ALERT | ERR_FATAL;
9251 }
9252
9253 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009254
9255 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009256#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009257}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009258static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9259{
9260 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9261}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009262
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009263/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009264static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9265{
9266 int code;
9267 char *p = args[cur_arg + 1];
9268 unsigned long long *ignerr = &conf->crt_ignerr;
9269
9270 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009271 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009272 return ERR_ALERT | ERR_FATAL;
9273 }
9274
9275 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9276 ignerr = &conf->ca_ignerr;
9277
9278 if (strcmp(p, "all") == 0) {
9279 *ignerr = ~0ULL;
9280 return 0;
9281 }
9282
9283 while (p) {
9284 code = atoi(p);
9285 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009286 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9287 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009288 return ERR_ALERT | ERR_FATAL;
9289 }
9290 *ignerr |= 1ULL << code;
9291 p = strchr(p, ',');
9292 if (p)
9293 p++;
9294 }
9295
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009296 return 0;
9297}
9298
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009299/* parse tls_method_options "no-xxx" and "force-xxx" */
9300static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009301{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009302 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009303 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009304 p = strchr(arg, '-');
9305 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009306 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009307 p++;
9308 if (!strcmp(p, "sslv3"))
9309 v = CONF_SSLV3;
9310 else if (!strcmp(p, "tlsv10"))
9311 v = CONF_TLSV10;
9312 else if (!strcmp(p, "tlsv11"))
9313 v = CONF_TLSV11;
9314 else if (!strcmp(p, "tlsv12"))
9315 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009316 else if (!strcmp(p, "tlsv13"))
9317 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009318 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009319 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009320 if (!strncmp(arg, "no-", 3))
9321 methods->flags |= methodVersions[v].flag;
9322 else if (!strncmp(arg, "force-", 6))
9323 methods->min = methods->max = v;
9324 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009325 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009326 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009327 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009328 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009329 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009330}
9331
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009332static 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 +02009333{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009334 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009335}
9336
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009337static 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 +02009338{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009339 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9340}
9341
9342/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9343static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9344{
9345 uint16_t i, v = 0;
9346 char *argv = args[cur_arg + 1];
9347 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009348 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009349 return ERR_ALERT | ERR_FATAL;
9350 }
9351 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9352 if (!strcmp(argv, methodVersions[i].name))
9353 v = i;
9354 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009355 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009356 return ERR_ALERT | ERR_FATAL;
9357 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009358 if (!strcmp("ssl-min-ver", args[cur_arg]))
9359 methods->min = v;
9360 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9361 methods->max = v;
9362 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009363 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009364 return ERR_ALERT | ERR_FATAL;
9365 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009366 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009367}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009368
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009369static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9370{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009371#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009372 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 +02009373#endif
9374 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9375}
9376
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009377static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9378{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009379 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009380}
9381
9382static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9383{
9384 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9385}
9386
Emeric Brun2d0c4822012-10-02 13:45:20 +02009387/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009388static 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 +02009389{
Emeric Brun89675492012-10-05 13:48:26 +02009390 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009391 return 0;
9392}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009393
Olivier Houchardc2aae742017-09-22 18:26:28 +02009394/* parse the "allow-0rtt" bind keyword */
9395static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9396{
9397 conf->early_data = 1;
9398 return 0;
9399}
9400
9401static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9402{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009403 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009404 return 0;
9405}
9406
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009407/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009408static 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 +02009409{
Bernard Spil13c53f82018-02-15 13:34:58 +01009410#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009411 char *p1, *p2;
9412
9413 if (!*args[cur_arg + 1]) {
9414 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9415 return ERR_ALERT | ERR_FATAL;
9416 }
9417
9418 free(conf->npn_str);
9419
Willy Tarreau3724da12016-02-12 17:11:12 +01009420 /* the NPN string is built as a suite of (<len> <name>)*,
9421 * so we reuse each comma to store the next <len> and need
9422 * one more for the end of the string.
9423 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009424 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009425 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009426 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9427
9428 /* replace commas with the name length */
9429 p1 = conf->npn_str;
9430 p2 = p1 + 1;
9431 while (1) {
9432 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9433 if (!p2)
9434 p2 = p1 + 1 + strlen(p1 + 1);
9435
9436 if (p2 - (p1 + 1) > 255) {
9437 *p2 = '\0';
9438 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9439 return ERR_ALERT | ERR_FATAL;
9440 }
9441
9442 *p1 = p2 - (p1 + 1);
9443 p1 = p2;
9444
9445 if (!*p2)
9446 break;
9447
9448 *(p2++) = '\0';
9449 }
9450 return 0;
9451#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009452 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009453 return ERR_ALERT | ERR_FATAL;
9454#endif
9455}
9456
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009457static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9458{
9459 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9460}
9461
Willy Tarreauab861d32013-04-02 02:30:41 +02009462/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009463static 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 +02009464{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009465#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009466 char *p1, *p2;
9467
9468 if (!*args[cur_arg + 1]) {
9469 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9470 return ERR_ALERT | ERR_FATAL;
9471 }
9472
9473 free(conf->alpn_str);
9474
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009475 /* the ALPN string is built as a suite of (<len> <name>)*,
9476 * so we reuse each comma to store the next <len> and need
9477 * one more for the end of the string.
9478 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009479 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009480 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009481 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9482
9483 /* replace commas with the name length */
9484 p1 = conf->alpn_str;
9485 p2 = p1 + 1;
9486 while (1) {
9487 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9488 if (!p2)
9489 p2 = p1 + 1 + strlen(p1 + 1);
9490
9491 if (p2 - (p1 + 1) > 255) {
9492 *p2 = '\0';
9493 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9494 return ERR_ALERT | ERR_FATAL;
9495 }
9496
9497 *p1 = p2 - (p1 + 1);
9498 p1 = p2;
9499
9500 if (!*p2)
9501 break;
9502
9503 *(p2++) = '\0';
9504 }
9505 return 0;
9506#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009507 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009508 return ERR_ALERT | ERR_FATAL;
9509#endif
9510}
9511
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009512static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9513{
9514 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9515}
9516
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009517/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009518static 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 +02009519{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009520 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009521 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009522
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009523 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9524 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009525#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009526 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9527 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9528#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009529 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009530 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9531 if (!conf->ssl_conf.ssl_methods.min)
9532 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9533 if (!conf->ssl_conf.ssl_methods.max)
9534 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009535
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009536 return 0;
9537}
9538
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009539/* parse the "prefer-client-ciphers" bind keyword */
9540static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9541{
9542 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9543 return 0;
9544}
9545
Christopher Faulet31af49d2015-06-09 17:29:50 +02009546/* parse the "generate-certificates" bind keyword */
9547static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9548{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009549#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009550 conf->generate_certs = 1;
9551#else
9552 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9553 err && *err ? *err : "");
9554#endif
9555 return 0;
9556}
9557
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009558/* parse the "strict-sni" bind keyword */
9559static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9560{
9561 conf->strict_sni = 1;
9562 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009563}
9564
9565/* parse the "tls-ticket-keys" bind keyword */
9566static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9567{
9568#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009569 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009570 int i = 0;
9571 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009572 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009573
9574 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009575 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009576 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009577 }
9578
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009579 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009580 if (keys_ref) {
9581 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009582 conf->keys_ref = keys_ref;
9583 return 0;
9584 }
9585
Christopher Faulete566f3d2019-10-21 09:55:49 +02009586 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009587 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009588 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009589 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009590 }
9591
Emeric Brun9e754772019-01-10 17:51:55 +01009592 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009593 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009594 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009595 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009596 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009597
9598 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009599 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009600 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009601 }
9602
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009603 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009604 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009605 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009606 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009607 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009608
Emeric Brun9e754772019-01-10 17:51:55 +01009609 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009610 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9611 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009612 int dec_size;
9613
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009614 /* Strip newline characters from the end */
9615 if(thisline[len - 1] == '\n')
9616 thisline[--len] = 0;
9617
9618 if(thisline[len - 1] == '\r')
9619 thisline[--len] = 0;
9620
Emeric Brun9e754772019-01-10 17:51:55 +01009621 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9622 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009623 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009624 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009625 }
Emeric Brun9e754772019-01-10 17:51:55 +01009626 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9627 keys_ref->key_size_bits = 128;
9628 }
9629 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9630 keys_ref->key_size_bits = 256;
9631 }
9632 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9633 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9634 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009635 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009636 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009637 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009638 i++;
9639 }
9640
9641 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009642 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 +02009643 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009644 }
9645
9646 fclose(f);
9647
9648 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009649 i -= 2;
9650 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009651 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009652 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009653 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009654 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009655
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009656 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9657
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009658 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009659
9660 fail:
9661 if (f)
9662 fclose(f);
9663 if (keys_ref) {
9664 free(keys_ref->filename);
9665 free(keys_ref->tlskeys);
9666 free(keys_ref);
9667 }
9668 return ERR_ALERT | ERR_FATAL;
9669
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009670#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009671 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009672 return ERR_ALERT | ERR_FATAL;
9673#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009674}
9675
Emeric Brund94b3fe2012-09-20 18:23:56 +02009676/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009677static 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 +02009678{
9679 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009680 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009681 return ERR_ALERT | ERR_FATAL;
9682 }
9683
9684 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009685 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009686 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009687 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009688 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009689 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009690 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009691 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9692 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009693 return ERR_ALERT | ERR_FATAL;
9694 }
9695
9696 return 0;
9697}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009698static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9699{
9700 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9701}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009702
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009703/* parse the "no-ca-names" bind keyword */
9704static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9705{
9706 conf->no_ca_names = 1;
9707 return 0;
9708}
9709static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9710{
9711 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9712}
9713
Willy Tarreau92faadf2012-10-10 23:04:25 +02009714/************** "server" keywords ****************/
9715
Olivier Houchardc7566002018-11-20 23:33:50 +01009716/* parse the "npn" bind keyword */
9717static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9718{
9719#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9720 char *p1, *p2;
9721
9722 if (!*args[*cur_arg + 1]) {
9723 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9724 return ERR_ALERT | ERR_FATAL;
9725 }
9726
9727 free(newsrv->ssl_ctx.npn_str);
9728
9729 /* the NPN string is built as a suite of (<len> <name>)*,
9730 * so we reuse each comma to store the next <len> and need
9731 * one more for the end of the string.
9732 */
9733 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9734 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9735 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9736 newsrv->ssl_ctx.npn_len);
9737
9738 /* replace commas with the name length */
9739 p1 = newsrv->ssl_ctx.npn_str;
9740 p2 = p1 + 1;
9741 while (1) {
9742 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9743 newsrv->ssl_ctx.npn_len - (p1 + 1));
9744 if (!p2)
9745 p2 = p1 + 1 + strlen(p1 + 1);
9746
9747 if (p2 - (p1 + 1) > 255) {
9748 *p2 = '\0';
9749 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9750 return ERR_ALERT | ERR_FATAL;
9751 }
9752
9753 *p1 = p2 - (p1 + 1);
9754 p1 = p2;
9755
9756 if (!*p2)
9757 break;
9758
9759 *(p2++) = '\0';
9760 }
9761 return 0;
9762#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009763 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009764 return ERR_ALERT | ERR_FATAL;
9765#endif
9766}
9767
Olivier Houchard92150142018-12-21 19:47:01 +01009768/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009769static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9770{
9771#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9772 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009773 char **alpn_str;
9774 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009775
Olivier Houchard92150142018-12-21 19:47:01 +01009776 if (*args[*cur_arg] == 'c') {
9777 alpn_str = &newsrv->check.alpn_str;
9778 alpn_len = &newsrv->check.alpn_len;
9779 } else {
9780 alpn_str = &newsrv->ssl_ctx.alpn_str;
9781 alpn_len = &newsrv->ssl_ctx.alpn_len;
9782
9783 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009784 if (!*args[*cur_arg + 1]) {
9785 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9786 return ERR_ALERT | ERR_FATAL;
9787 }
9788
Olivier Houchard92150142018-12-21 19:47:01 +01009789 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009790
9791 /* the ALPN string is built as a suite of (<len> <name>)*,
9792 * so we reuse each comma to store the next <len> and need
9793 * one more for the end of the string.
9794 */
Olivier Houchard92150142018-12-21 19:47:01 +01009795 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9796 *alpn_str = calloc(1, *alpn_len + 1);
9797 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009798
9799 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009800 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009801 p2 = p1 + 1;
9802 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009803 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009804 if (!p2)
9805 p2 = p1 + 1 + strlen(p1 + 1);
9806
9807 if (p2 - (p1 + 1) > 255) {
9808 *p2 = '\0';
9809 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9810 return ERR_ALERT | ERR_FATAL;
9811 }
9812
9813 *p1 = p2 - (p1 + 1);
9814 p1 = p2;
9815
9816 if (!*p2)
9817 break;
9818
9819 *(p2++) = '\0';
9820 }
9821 return 0;
9822#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009823 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009824 return ERR_ALERT | ERR_FATAL;
9825#endif
9826}
9827
Emeric Brunef42d922012-10-11 16:11:36 +02009828/* parse the "ca-file" server keyword */
9829static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9830{
9831 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009832 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009833 return ERR_ALERT | ERR_FATAL;
9834 }
9835
Willy Tarreauef934602016-12-22 23:12:01 +01009836 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9837 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009838 else
9839 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9840
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009841 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9842 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9843 return ERR_ALERT | ERR_FATAL;
9844 }
Emeric Brunef42d922012-10-11 16:11:36 +02009845 return 0;
9846}
9847
Olivier Houchard9130a962017-10-17 17:33:43 +02009848/* parse the "check-sni" server keyword */
9849static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9850{
9851 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009852 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009853 return ERR_ALERT | ERR_FATAL;
9854 }
9855
9856 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9857 if (!newsrv->check.sni) {
9858 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9859 return ERR_ALERT | ERR_FATAL;
9860 }
9861 return 0;
9862
9863}
9864
Willy Tarreau92faadf2012-10-10 23:04:25 +02009865/* parse the "check-ssl" server keyword */
9866static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9867{
9868 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009869 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9870 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009871#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009872 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9873 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9874#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009875 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009876 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9877 if (!newsrv->ssl_ctx.methods.min)
9878 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9879 if (!newsrv->ssl_ctx.methods.max)
9880 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9881
Willy Tarreau92faadf2012-10-10 23:04:25 +02009882 return 0;
9883}
9884
9885/* parse the "ciphers" server keyword */
9886static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9887{
9888 if (!*args[*cur_arg + 1]) {
9889 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9890 return ERR_ALERT | ERR_FATAL;
9891 }
9892
9893 free(newsrv->ssl_ctx.ciphers);
9894 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9895 return 0;
9896}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009897
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009898#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009899/* parse the "ciphersuites" server keyword */
9900static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9901{
9902 if (!*args[*cur_arg + 1]) {
9903 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9904 return ERR_ALERT | ERR_FATAL;
9905 }
9906
9907 free(newsrv->ssl_ctx.ciphersuites);
9908 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9909 return 0;
9910}
9911#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009912
Emeric Brunef42d922012-10-11 16:11:36 +02009913/* parse the "crl-file" server keyword */
9914static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9915{
9916#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009917 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009918 return ERR_ALERT | ERR_FATAL;
9919#else
9920 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009921 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009922 return ERR_ALERT | ERR_FATAL;
9923 }
9924
Willy Tarreauef934602016-12-22 23:12:01 +01009925 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9926 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009927 else
9928 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9929
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009930 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9931 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9932 return ERR_ALERT | ERR_FATAL;
9933 }
Emeric Brunef42d922012-10-11 16:11:36 +02009934 return 0;
9935#endif
9936}
9937
Emeric Bruna7aa3092012-10-26 12:58:00 +02009938/* parse the "crt" server keyword */
9939static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9940{
9941 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009942 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009943 return ERR_ALERT | ERR_FATAL;
9944 }
9945
Willy Tarreauef934602016-12-22 23:12:01 +01009946 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009947 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009948 else
9949 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9950
9951 return 0;
9952}
Emeric Brunef42d922012-10-11 16:11:36 +02009953
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009954/* parse the "no-check-ssl" server keyword */
9955static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9956{
9957 newsrv->check.use_ssl = 0;
9958 free(newsrv->ssl_ctx.ciphers);
9959 newsrv->ssl_ctx.ciphers = NULL;
9960 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9961 return 0;
9962}
9963
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009964/* parse the "no-send-proxy-v2-ssl" server keyword */
9965static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9966{
9967 newsrv->pp_opts &= ~SRV_PP_V2;
9968 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9969 return 0;
9970}
9971
9972/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9973static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9974{
9975 newsrv->pp_opts &= ~SRV_PP_V2;
9976 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9977 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9978 return 0;
9979}
9980
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009981/* parse the "no-ssl" server keyword */
9982static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9983{
9984 newsrv->use_ssl = 0;
9985 free(newsrv->ssl_ctx.ciphers);
9986 newsrv->ssl_ctx.ciphers = NULL;
9987 return 0;
9988}
9989
Olivier Houchard522eea72017-11-03 16:27:47 +01009990/* parse the "allow-0rtt" server keyword */
9991static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9992{
9993 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9994 return 0;
9995}
9996
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009997/* parse the "no-ssl-reuse" server keyword */
9998static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9999{
10000 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
10001 return 0;
10002}
10003
Emeric Brunf9c5c472012-10-11 15:28:34 +020010004/* parse the "no-tls-tickets" server keyword */
10005static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10006{
10007 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
10008 return 0;
10009}
David Safb76832014-05-08 23:42:08 -040010010/* parse the "send-proxy-v2-ssl" server keyword */
10011static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10012{
10013 newsrv->pp_opts |= SRV_PP_V2;
10014 newsrv->pp_opts |= SRV_PP_V2_SSL;
10015 return 0;
10016}
10017
10018/* parse the "send-proxy-v2-ssl-cn" server keyword */
10019static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10020{
10021 newsrv->pp_opts |= SRV_PP_V2;
10022 newsrv->pp_opts |= SRV_PP_V2_SSL;
10023 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
10024 return 0;
10025}
Emeric Brunf9c5c472012-10-11 15:28:34 +020010026
Willy Tarreau732eac42015-07-09 11:40:25 +020010027/* parse the "sni" server keyword */
10028static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10029{
10030#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
10031 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
10032 return ERR_ALERT | ERR_FATAL;
10033#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010034 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +020010035
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010036 arg = args[*cur_arg + 1];
10037 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +020010038 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
10039 return ERR_ALERT | ERR_FATAL;
10040 }
10041
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010042 free(newsrv->sni_expr);
10043 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010044
Willy Tarreau732eac42015-07-09 11:40:25 +020010045 return 0;
10046#endif
10047}
10048
Willy Tarreau92faadf2012-10-10 23:04:25 +020010049/* parse the "ssl" server keyword */
10050static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10051{
10052 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010053 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10054 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010055#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010056 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10057 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10058#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +020010059 return 0;
10060}
10061
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010062/* parse the "ssl-reuse" server keyword */
10063static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10064{
10065 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10066 return 0;
10067}
10068
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010069/* parse the "tls-tickets" server keyword */
10070static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10071{
10072 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10073 return 0;
10074}
10075
Emeric Brunef42d922012-10-11 16:11:36 +020010076/* parse the "verify" server keyword */
10077static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10078{
10079 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010080 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010081 return ERR_ALERT | ERR_FATAL;
10082 }
10083
10084 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010085 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010086 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010087 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010088 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010089 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10090 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010091 return ERR_ALERT | ERR_FATAL;
10092 }
10093
Evan Broderbe554312013-06-27 00:05:25 -070010094 return 0;
10095}
10096
10097/* parse the "verifyhost" server keyword */
10098static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10099{
10100 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010101 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010102 return ERR_ALERT | ERR_FATAL;
10103 }
10104
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010105 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010106 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10107
Emeric Brunef42d922012-10-11 16:11:36 +020010108 return 0;
10109}
10110
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010111/* parse the "ssl-default-bind-options" keyword in global section */
10112static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10113 struct proxy *defpx, const char *file, int line,
10114 char **err) {
10115 int i = 1;
10116
10117 if (*(args[i]) == 0) {
10118 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10119 return -1;
10120 }
10121 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010122 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010123 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010124 else if (!strcmp(args[i], "prefer-client-ciphers"))
10125 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010126 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10127 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10128 i++;
10129 else {
10130 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10131 return -1;
10132 }
10133 }
10134 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010135 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10136 return -1;
10137 }
10138 i++;
10139 }
10140 return 0;
10141}
10142
10143/* parse the "ssl-default-server-options" keyword in global section */
10144static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10145 struct proxy *defpx, const char *file, int line,
10146 char **err) {
10147 int i = 1;
10148
10149 if (*(args[i]) == 0) {
10150 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10151 return -1;
10152 }
10153 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010154 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010155 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010156 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10157 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10158 i++;
10159 else {
10160 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10161 return -1;
10162 }
10163 }
10164 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010165 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10166 return -1;
10167 }
10168 i++;
10169 }
10170 return 0;
10171}
10172
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010173/* parse the "ca-base" / "crt-base" keywords in global section.
10174 * Returns <0 on alert, >0 on warning, 0 on success.
10175 */
10176static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10177 struct proxy *defpx, const char *file, int line,
10178 char **err)
10179{
10180 char **target;
10181
Willy Tarreauef934602016-12-22 23:12:01 +010010182 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010183
10184 if (too_many_args(1, args, err, NULL))
10185 return -1;
10186
10187 if (*target) {
10188 memprintf(err, "'%s' already specified.", args[0]);
10189 return -1;
10190 }
10191
10192 if (*(args[1]) == 0) {
10193 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10194 return -1;
10195 }
10196 *target = strdup(args[1]);
10197 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010198}
10199
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010200/* "issuers-chain-path" load chain certificate in global */
10201static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10202{
10203 X509 *ca;
10204 X509_NAME *name = NULL;
10205 ASN1_OCTET_STRING *skid = NULL;
10206 STACK_OF(X509) *chain = NULL;
10207 struct issuer_chain *issuer;
10208 struct eb64_node *node;
10209 char *path;
10210 u64 key;
10211 int ret = 0;
10212
10213 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10214 if (chain == NULL) {
10215 chain = sk_X509_new_null();
10216 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10217 name = X509_get_subject_name(ca);
10218 }
10219 if (!sk_X509_push(chain, ca)) {
10220 X509_free(ca);
10221 goto end;
10222 }
10223 }
10224 if (!chain) {
10225 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10226 goto end;
10227 }
10228 if (!skid) {
10229 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10230 goto end;
10231 }
10232 if (!name) {
10233 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10234 goto end;
10235 }
10236 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010237 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010238 issuer = container_of(node, typeof(*issuer), node);
10239 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10240 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10241 goto end;
10242 }
10243 }
10244 issuer = calloc(1, sizeof *issuer);
10245 path = strdup(fp);
10246 if (!issuer || !path) {
10247 free(issuer);
10248 free(path);
10249 goto end;
10250 }
10251 issuer->node.key = key;
10252 issuer->path = path;
10253 issuer->chain = chain;
10254 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010255 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010256 ret = 1;
10257 end:
10258 if (skid)
10259 ASN1_OCTET_STRING_free(skid);
10260 if (chain)
10261 sk_X509_pop_free(chain, X509_free);
10262 return ret;
10263}
10264
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010265static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010266{
10267 AUTHORITY_KEYID *akid;
10268 struct issuer_chain *issuer = NULL;
10269
10270 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10271 if (akid) {
10272 struct eb64_node *node;
10273 u64 hk;
10274 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10275 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10276 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10277 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10278 issuer = ti;
10279 break;
10280 }
10281 }
10282 AUTHORITY_KEYID_free(akid);
10283 }
10284 return issuer;
10285}
10286
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010287static void ssl_free_global_issuers(void)
10288{
10289 struct eb64_node *node, *back;
10290 struct issuer_chain *issuer;
10291
William Lallemande0f3fd52020-02-25 14:53:06 +010010292 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010293 while (node) {
10294 issuer = container_of(node, typeof(*issuer), node);
10295 back = eb64_next(node);
10296 eb64_delete(node);
10297 free(issuer->path);
10298 sk_X509_pop_free(issuer->chain, X509_free);
10299 free(issuer);
10300 node = back;
10301 }
10302}
10303
10304static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10305 struct proxy *defpx, const char *file, int line,
10306 char **err)
10307{
10308 char *path;
10309 struct dirent **de_list;
10310 int i, n;
10311 struct stat buf;
10312 char *end;
10313 char fp[MAXPATHLEN+1];
10314
10315 if (too_many_args(1, args, err, NULL))
10316 return -1;
10317
10318 path = args[1];
10319 if (*path == 0 || stat(path, &buf)) {
10320 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10321 err && *err ? *err : "", args[0]);
10322 return -1;
10323 }
10324 if (S_ISDIR(buf.st_mode) == 0) {
10325 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10326 err && *err ? *err : "", args[0], path);
10327 return -1;
10328 }
10329
10330 /* strip trailing slashes, including first one */
10331 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10332 *end = 0;
10333 /* path already parsed? */
10334 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10335 return 0;
10336 /* overwrite old issuers_chain_path */
10337 free(global_ssl.issuers_chain_path);
10338 global_ssl.issuers_chain_path = strdup(path);
10339 ssl_free_global_issuers();
10340
10341 n = scandir(path, &de_list, 0, alphasort);
10342 if (n < 0) {
10343 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10344 err && *err ? *err : "", args[0], path, strerror(errno));
10345 return -1;
10346 }
10347 for (i = 0; i < n; i++) {
10348 struct dirent *de = de_list[i];
10349 BIO *in = NULL;
10350 char *warn = NULL;
10351
10352 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10353 free(de);
10354 if (stat(fp, &buf) != 0) {
10355 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10356 goto next;
10357 }
10358 if (!S_ISREG(buf.st_mode))
10359 goto next;
10360
10361 in = BIO_new(BIO_s_file());
10362 if (in == NULL)
10363 goto next;
10364 if (BIO_read_filename(in, fp) <= 0)
10365 goto next;
10366 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10367 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010368 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010369 free(warn);
10370 warn = NULL;
10371 }
10372 next:
10373 if (in)
10374 BIO_free(in);
10375 }
10376 free(de_list);
10377
10378 return 0;
10379}
10380
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010381/* parse the "ssl-mode-async" keyword in global section.
10382 * Returns <0 on alert, >0 on warning, 0 on success.
10383 */
10384static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10385 struct proxy *defpx, const char *file, int line,
10386 char **err)
10387{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010388#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010389 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010390 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010391 return 0;
10392#else
10393 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10394 return -1;
10395#endif
10396}
10397
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010398#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010399static int ssl_check_async_engine_count(void) {
10400 int err_code = 0;
10401
Emeric Brun3854e012017-05-17 20:42:48 +020010402 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010403 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010404 err_code = ERR_ABORT;
10405 }
10406 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010407}
10408
Grant Zhang872f9c22017-01-21 01:10:18 +000010409/* parse the "ssl-engine" keyword in global section.
10410 * Returns <0 on alert, >0 on warning, 0 on success.
10411 */
10412static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10413 struct proxy *defpx, const char *file, int line,
10414 char **err)
10415{
10416 char *algo;
10417 int ret = -1;
10418
10419 if (*(args[1]) == 0) {
10420 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10421 return ret;
10422 }
10423
10424 if (*(args[2]) == 0) {
10425 /* if no list of algorithms is given, it defaults to ALL */
10426 algo = strdup("ALL");
10427 goto add_engine;
10428 }
10429
10430 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10431 if (strcmp(args[2], "algo") != 0) {
10432 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10433 return ret;
10434 }
10435
10436 if (*(args[3]) == 0) {
10437 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10438 return ret;
10439 }
10440 algo = strdup(args[3]);
10441
10442add_engine:
10443 if (ssl_init_single_engine(args[1], algo)==0) {
10444 openssl_engines_initialized++;
10445 ret = 0;
10446 }
10447 free(algo);
10448 return ret;
10449}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010450#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010451
Willy Tarreauf22e9682016-12-21 23:23:19 +010010452/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10453 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10454 */
10455static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10456 struct proxy *defpx, const char *file, int line,
10457 char **err)
10458{
10459 char **target;
10460
Willy Tarreauef934602016-12-22 23:12:01 +010010461 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010462
10463 if (too_many_args(1, args, err, NULL))
10464 return -1;
10465
10466 if (*(args[1]) == 0) {
10467 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10468 return -1;
10469 }
10470
10471 free(*target);
10472 *target = strdup(args[1]);
10473 return 0;
10474}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010475
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010476#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010477/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10478 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10479 */
10480static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10481 struct proxy *defpx, const char *file, int line,
10482 char **err)
10483{
10484 char **target;
10485
10486 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10487
10488 if (too_many_args(1, args, err, NULL))
10489 return -1;
10490
10491 if (*(args[1]) == 0) {
10492 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10493 return -1;
10494 }
10495
10496 free(*target);
10497 *target = strdup(args[1]);
10498 return 0;
10499}
10500#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010501
Willy Tarreau9ceda382016-12-21 23:13:03 +010010502/* parse various global tune.ssl settings consisting in positive integers.
10503 * Returns <0 on alert, >0 on warning, 0 on success.
10504 */
10505static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10506 struct proxy *defpx, const char *file, int line,
10507 char **err)
10508{
10509 int *target;
10510
10511 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10512 target = &global.tune.sslcachesize;
10513 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010514 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010515 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010516 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010517 else if (strcmp(args[0], "maxsslconn") == 0)
10518 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010519 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10520 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010521 else {
10522 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10523 return -1;
10524 }
10525
10526 if (too_many_args(1, args, err, NULL))
10527 return -1;
10528
10529 if (*(args[1]) == 0) {
10530 memprintf(err, "'%s' expects an integer argument.", args[0]);
10531 return -1;
10532 }
10533
10534 *target = atoi(args[1]);
10535 if (*target < 0) {
10536 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10537 return -1;
10538 }
10539 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010540}
10541
10542static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10543 struct proxy *defpx, const char *file, int line,
10544 char **err)
10545{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010546 int ret;
10547
10548 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10549 if (ret != 0)
10550 return ret;
10551
Willy Tarreaubafbe012017-11-24 17:34:44 +010010552 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010553 memprintf(err, "'%s' is already configured.", args[0]);
10554 return -1;
10555 }
10556
Willy Tarreaubafbe012017-11-24 17:34:44 +010010557 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10558 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010559 memprintf(err, "Out of memory error.");
10560 return -1;
10561 }
10562 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010563}
10564
10565/* parse "ssl.force-private-cache".
10566 * Returns <0 on alert, >0 on warning, 0 on success.
10567 */
10568static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10569 struct proxy *defpx, const char *file, int line,
10570 char **err)
10571{
10572 if (too_many_args(0, args, err, NULL))
10573 return -1;
10574
Willy Tarreauef934602016-12-22 23:12:01 +010010575 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010576 return 0;
10577}
10578
10579/* parse "ssl.lifetime".
10580 * Returns <0 on alert, >0 on warning, 0 on success.
10581 */
10582static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10583 struct proxy *defpx, const char *file, int line,
10584 char **err)
10585{
10586 const char *res;
10587
10588 if (too_many_args(1, args, err, NULL))
10589 return -1;
10590
10591 if (*(args[1]) == 0) {
10592 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10593 return -1;
10594 }
10595
Willy Tarreauef934602016-12-22 23:12:01 +010010596 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010597 if (res == PARSE_TIME_OVER) {
10598 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10599 args[1], args[0]);
10600 return -1;
10601 }
10602 else if (res == PARSE_TIME_UNDER) {
10603 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10604 args[1], args[0]);
10605 return -1;
10606 }
10607 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010608 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10609 return -1;
10610 }
10611 return 0;
10612}
10613
10614#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010615/* parse "ssl-dh-param-file".
10616 * Returns <0 on alert, >0 on warning, 0 on success.
10617 */
10618static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10619 struct proxy *defpx, const char *file, int line,
10620 char **err)
10621{
10622 if (too_many_args(1, args, err, NULL))
10623 return -1;
10624
10625 if (*(args[1]) == 0) {
10626 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10627 return -1;
10628 }
10629
10630 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10631 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10632 return -1;
10633 }
10634 return 0;
10635}
10636
Willy Tarreau9ceda382016-12-21 23:13:03 +010010637/* parse "ssl.default-dh-param".
10638 * Returns <0 on alert, >0 on warning, 0 on success.
10639 */
10640static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10641 struct proxy *defpx, const char *file, int line,
10642 char **err)
10643{
10644 if (too_many_args(1, args, err, NULL))
10645 return -1;
10646
10647 if (*(args[1]) == 0) {
10648 memprintf(err, "'%s' expects an integer argument.", args[0]);
10649 return -1;
10650 }
10651
Willy Tarreauef934602016-12-22 23:12:01 +010010652 global_ssl.default_dh_param = atoi(args[1]);
10653 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010654 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10655 return -1;
10656 }
10657 return 0;
10658}
10659#endif
10660
William Lallemand3af48e72020-02-03 17:15:52 +010010661
10662/*
10663 * parse "ssl-load-extra-files".
10664 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10665 */
10666static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10667 struct proxy *defpx, const char *file, int line,
10668 char **err)
10669{
10670 int i;
10671 int gf = SSL_GF_NONE;
10672
10673 if (*(args[1]) == 0)
10674 goto err_arg;
10675
10676 for (i = 1; *args[i]; i++) {
10677
10678 if (!strcmp("bundle", args[i])) {
10679 gf |= SSL_GF_BUNDLE;
10680
10681 } else if (!strcmp("sctl", args[i])) {
10682 gf |= SSL_GF_SCTL;
10683
10684 } else if (!strcmp("ocsp", args[i])){
10685 gf |= SSL_GF_OCSP;
10686
10687 } else if (!strcmp("issuer", args[i])){
10688 gf |= SSL_GF_OCSP_ISSUER;
10689
William Lallemand4c5adbf2020-02-24 14:23:22 +010010690 } else if (!strcmp("key", args[i])) {
10691 gf |= SSL_GF_KEY;
10692
William Lallemand3af48e72020-02-03 17:15:52 +010010693 } else if (!strcmp("none", args[i])) {
10694 if (gf != SSL_GF_NONE)
10695 goto err_alone;
10696 gf = SSL_GF_NONE;
10697 i++;
10698 break;
10699
10700 } else if (!strcmp("all", args[i])) {
10701 if (gf != SSL_GF_NONE)
10702 goto err_alone;
10703 gf = SSL_GF_ALL;
10704 i++;
10705 break;
10706 } else {
10707 goto err_arg;
10708 }
10709 }
10710 /* break from loop but there are still arguments */
10711 if (*args[i])
10712 goto err_alone;
10713
10714 global_ssl.extra_files = gf;
10715
10716 return 0;
10717
10718err_alone:
10719 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10720 return -1;
10721
10722err_arg:
10723 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10724 return -1;
10725}
10726
Willy Tarreau9ceda382016-12-21 23:13:03 +010010727
William Lallemand32af2032016-10-29 18:09:35 +020010728/* This function is used with TLS ticket keys management. It permits to browse
10729 * each reference. The variable <getnext> must contain the current node,
10730 * <end> point to the root node.
10731 */
10732#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10733static inline
10734struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10735{
10736 struct tls_keys_ref *ref = getnext;
10737
10738 while (1) {
10739
10740 /* Get next list entry. */
10741 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10742
10743 /* If the entry is the last of the list, return NULL. */
10744 if (&ref->list == end)
10745 return NULL;
10746
10747 return ref;
10748 }
10749}
10750
10751static inline
10752struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10753{
10754 int id;
10755 char *error;
10756
10757 /* If the reference starts by a '#', this is numeric id. */
10758 if (reference[0] == '#') {
10759 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10760 id = strtol(reference + 1, &error, 10);
10761 if (*error != '\0')
10762 return NULL;
10763
10764 /* Perform the unique id lookup. */
10765 return tlskeys_ref_lookupid(id);
10766 }
10767
10768 /* Perform the string lookup. */
10769 return tlskeys_ref_lookup(reference);
10770}
10771#endif
10772
10773
10774#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10775
10776static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10777
10778static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10779 return cli_io_handler_tlskeys_files(appctx);
10780}
10781
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010782/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10783 * (next index to be dumped), and cli.p0 (next key reference).
10784 */
William Lallemand32af2032016-10-29 18:09:35 +020010785static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10786
10787 struct stream_interface *si = appctx->owner;
10788
10789 switch (appctx->st2) {
10790 case STAT_ST_INIT:
10791 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010792 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010793 * later and restart at the state "STAT_ST_INIT".
10794 */
10795 chunk_reset(&trash);
10796
10797 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10798 chunk_appendf(&trash, "# id secret\n");
10799 else
10800 chunk_appendf(&trash, "# id (file)\n");
10801
Willy Tarreau06d80a92017-10-19 14:32:15 +020010802 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010803 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010804 return 0;
10805 }
10806
William Lallemand32af2032016-10-29 18:09:35 +020010807 /* Now, we start the browsing of the references lists.
10808 * Note that the following call to LIST_ELEM return bad pointer. The only
10809 * available field of this pointer is <list>. It is used with the function
10810 * tlskeys_list_get_next() for retruning the first available entry
10811 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010812 if (appctx->ctx.cli.p0 == NULL) {
10813 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10814 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010815 }
10816
10817 appctx->st2 = STAT_ST_LIST;
10818 /* fall through */
10819
10820 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010821 while (appctx->ctx.cli.p0) {
10822 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010823
10824 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010825 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010826 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010827
10828 if (appctx->ctx.cli.i1 == 0)
10829 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10830
William Lallemand32af2032016-10-29 18:09:35 +020010831 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010832 int head;
10833
10834 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10835 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010836 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010837 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010838
10839 chunk_reset(t2);
10840 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010841 if (ref->key_size_bits == 128) {
10842 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10843 sizeof(struct tls_sess_key_128),
10844 t2->area, t2->size);
10845 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10846 t2->area);
10847 }
10848 else if (ref->key_size_bits == 256) {
10849 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10850 sizeof(struct tls_sess_key_256),
10851 t2->area, t2->size);
10852 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10853 t2->area);
10854 }
10855 else {
10856 /* This case should never happen */
10857 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10858 }
William Lallemand32af2032016-10-29 18:09:35 +020010859
Willy Tarreau06d80a92017-10-19 14:32:15 +020010860 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010861 /* let's try again later from this stream. We add ourselves into
10862 * this stream's users so that it can remove us upon termination.
10863 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010864 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010865 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010866 return 0;
10867 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010868 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010869 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010870 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010871 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010872 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010873 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010874 /* let's try again later from this stream. We add ourselves into
10875 * this stream's users so that it can remove us upon termination.
10876 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010877 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010878 return 0;
10879 }
10880
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010881 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010882 break;
10883
10884 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010885 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010886 }
10887
10888 appctx->st2 = STAT_ST_FIN;
10889 /* fall through */
10890
10891 default:
10892 appctx->st2 = STAT_ST_FIN;
10893 return 1;
10894 }
10895 return 0;
10896}
10897
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010898/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010899static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010900{
William Lallemand32af2032016-10-29 18:09:35 +020010901 /* no parameter, shows only file list */
10902 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010903 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010904 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010905 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010906 }
10907
10908 if (args[2][0] == '*') {
10909 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010910 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010911 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010912 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010913 if (!appctx->ctx.cli.p0)
10914 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010915 }
William Lallemand32af2032016-10-29 18:09:35 +020010916 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010917 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010918}
10919
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010920static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010921{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010922 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010923 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010924
William Lallemand32af2032016-10-29 18:09:35 +020010925 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010926 if (!*args[3] || !*args[4])
10927 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 +020010928
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010929 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010930 if (!ref)
10931 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010932
Willy Tarreau1c913e42018-08-22 05:26:57 +020010933 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010934 if (ret < 0)
10935 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010936
Willy Tarreau1c913e42018-08-22 05:26:57 +020010937 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010938 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10939 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010940
Willy Tarreau9d008692019-08-09 11:21:01 +020010941 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010942}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010943#endif
William Lallemand32af2032016-10-29 18:09:35 +020010944
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010945/*
10946 * Take an ssl_bind_conf structure and append the configuration line used to
10947 * create it in the buffer
10948 */
10949static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10950{
10951 int space = 0;
10952
10953 if (conf == NULL)
10954 return;
10955
10956 chunk_appendf(buf, " [");
10957#ifdef OPENSSL_NPN_NEGOTIATED
10958 if (conf->npn_str) {
10959 int len = conf->npn_len;
10960 char *ptr = conf->npn_str;
10961 int comma = 0;
10962
10963 if (space) chunk_appendf(buf, " ");
10964 chunk_appendf(buf, "npn ");
10965 while (len) {
10966 unsigned short size;
10967
10968 size = *ptr;
10969 ptr++;
10970 if (comma)
10971 chunk_memcat(buf, ",", 1);
10972 chunk_memcat(buf, ptr, size);
10973 ptr += size;
10974 len -= size + 1;
10975 comma = 1;
10976 }
10977 chunk_memcat(buf, "", 1); /* finish with a \0 */
10978 space++;
10979 }
10980#endif
10981#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10982 if (conf->alpn_str) {
10983 int len = conf->alpn_len;
10984 char *ptr = conf->alpn_str;
10985 int comma = 0;
10986
10987 if (space) chunk_appendf(buf, " ");
10988 chunk_appendf(buf, "alpn ");
10989 while (len) {
10990 unsigned short size;
10991
10992 size = *ptr;
10993 ptr++;
10994 if (comma)
10995 chunk_memcat(buf, ",", 1);
10996 chunk_memcat(buf, ptr, size);
10997 ptr += size;
10998 len -= size + 1;
10999 comma = 1;
11000 }
11001 chunk_memcat(buf, "", 1); /* finish with a \0 */
11002 space++;
11003 }
11004#endif
11005 /* verify */
11006 {
11007 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
11008 if (space) chunk_appendf(buf, " ");
11009 chunk_appendf(buf, "verify none");
11010 space++;
11011 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
11012 if (space) chunk_appendf(buf, " ");
11013 chunk_appendf(buf, "verify optional");
11014 space++;
11015 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
11016 if (space) chunk_appendf(buf, " ");
11017 chunk_appendf(buf, "verify required");
11018 space++;
11019 }
11020 }
11021
11022 if (conf->no_ca_names) {
11023 if (space) chunk_appendf(buf, " ");
11024 chunk_appendf(buf, "no-ca-names");
11025 space++;
11026 }
11027
11028 if (conf->early_data) {
11029 if (space) chunk_appendf(buf, " ");
11030 chunk_appendf(buf, "allow-0rtt");
11031 space++;
11032 }
11033 if (conf->ca_file) {
11034 if (space) chunk_appendf(buf, " ");
11035 chunk_appendf(buf, "ca-file %s", conf->ca_file);
11036 space++;
11037 }
11038 if (conf->crl_file) {
11039 if (space) chunk_appendf(buf, " ");
11040 chunk_appendf(buf, "crl-file %s", conf->crl_file);
11041 space++;
11042 }
11043 if (conf->ciphers) {
11044 if (space) chunk_appendf(buf, " ");
11045 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11046 space++;
11047 }
11048#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11049 if (conf->ciphersuites) {
11050 if (space) chunk_appendf(buf, " ");
11051 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11052 space++;
11053 }
11054#endif
11055 if (conf->curves) {
11056 if (space) chunk_appendf(buf, " ");
11057 chunk_appendf(buf, "curves %s", conf->curves);
11058 space++;
11059 }
11060 if (conf->ecdhe) {
11061 if (space) chunk_appendf(buf, " ");
11062 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11063 space++;
11064 }
11065
11066 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11067 /* XXX: this part need to be revamp so we don't dump the default settings */
11068 if (conf->ssl_methods.min) {
11069 if (space) chunk_appendf(buf, " ");
11070 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11071 space++;
11072 }
11073
11074 if (conf->ssl_methods.max) {
11075 if (space) chunk_appendf(buf, " ");
11076 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11077 space++;
11078 }
11079
William Lallemand58a52222020-04-02 18:11:47 +020011080 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011081
11082 return;
11083}
11084
11085/* dump a list of filters */
11086static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11087{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011088 int i;
11089
11090 if (!entry->fcount)
11091 return;
11092
11093 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011094 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011095 }
11096 return;
11097}
11098
11099/* CLI IO handler for '(show|dump) ssl crt-list' */
11100static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11101{
11102 struct buffer *trash = alloc_trash_chunk();
11103 struct stream_interface *si = appctx->owner;
11104 struct ebmb_node *lnode;
11105
11106 if (trash == NULL)
11107 return 1;
11108
11109 /* dump the list of crt-lists */
11110 lnode = appctx->ctx.cli.p1;
11111 if (lnode == NULL)
11112 lnode = ebmb_first(&crtlists_tree);
11113 while (lnode) {
11114 chunk_appendf(trash, "%s\n", lnode->key);
11115 if (ci_putchk(si_ic(si), trash) == -1) {
11116 si_rx_room_blk(si);
11117 goto yield;
11118 }
11119 lnode = ebmb_next(lnode);
11120 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011121 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011122 return 1;
11123yield:
11124 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011125 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011126 return 0;
11127}
11128
11129/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11130static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11131{
11132 struct buffer *trash = alloc_trash_chunk();
11133 struct crtlist *crtlist;
11134 struct stream_interface *si = appctx->owner;
11135 struct crtlist_entry *entry;
11136
11137 if (trash == NULL)
11138 return 1;
11139
11140 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11141
11142 entry = appctx->ctx.cli.p1;
11143 if (entry == NULL) {
11144 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11145 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11146 if (ci_putchk(si_ic(si), trash) == -1) {
11147 si_rx_room_blk(si);
11148 goto yield;
11149 }
11150 }
11151
11152 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11153 struct ckch_store *store;
11154 const char *filename;
11155
11156 store = entry->node.key;
11157 filename = store->path;
11158 if (appctx->ctx.cli.i0 == 's') /* show */
11159 chunk_appendf(trash, "%p ", entry);
11160 chunk_appendf(trash, "%s", filename);
11161 dump_crtlist_sslconf(trash, entry->ssl_conf);
11162 dump_crtlist_filters(trash, entry);
11163 chunk_appendf(trash, "\n");
11164
11165 if (ci_putchk(si_ic(si), trash) == -1) {
11166 si_rx_room_blk(si);
11167 goto yield;
11168 }
11169 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011170 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011171 return 1;
11172yield:
11173 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011174 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011175 return 0;
11176}
11177
11178/* CLI argument parser for '(show|dump) ssl crt-list' */
11179static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11180{
11181 struct ebmb_node *lnode;
11182 int mode;
11183
11184 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11185 return 1;
11186
11187 appctx->ctx.cli.p0 = NULL;
11188 appctx->ctx.cli.p1 = NULL;
11189 mode = (int)args[0][0]; /* 'd' or 's' */
11190
11191 if (mode == 'd' && !*args[3])
11192 return cli_err(appctx, "'dump ssl crt-list' expects a filename or a directory\n");
11193
11194 if (*args[3]) {
11195 lnode = ebst_lookup(&crtlists_tree, args[3]);
11196 if (lnode == NULL)
11197 return cli_err(appctx, "didn't find the specified filename\n");
11198
11199 appctx->ctx.cli.p0 = lnode;
11200 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11201 }
11202 appctx->ctx.cli.i0 = mode;
11203
11204 return 0;
11205}
William Lallemand44b35322019-10-17 16:28:40 +020011206
William Lallemande67c80b2020-03-25 14:42:37 +010011207/* release function of the "add ssl crt-list' command, free things and unlock
11208 the spinlock */
11209static void cli_release_add_crtlist(struct appctx *appctx)
11210{
11211 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11212
11213 if (appctx->st2 != SETCERT_ST_FIN) {
11214 struct ckch_inst *inst, *inst_s;
11215 /* upon error free the ckch_inst and everything inside */
11216 ebpt_delete(&entry->node);
11217 LIST_DEL(&entry->by_crtlist);
11218 LIST_DEL(&entry->by_ckch_store);
11219
11220 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
11221 struct sni_ctx *sni, *sni_s;
11222
11223 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11224 if (sni->order == 0) /* we only free if it's the first inserted */
11225 SSL_CTX_free(sni->ctx);
11226 LIST_DEL(&sni->by_ckch_inst);
11227 free(sni);
11228 }
11229 LIST_DEL(&inst->by_ckchs);
11230 free(inst);
11231 }
11232 crtlist_free_filters(entry->filters);
11233 ssl_sock_free_ssl_conf(entry->ssl_conf);
11234 free(entry->ssl_conf);
11235 free(entry);
11236 }
11237
11238 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11239}
11240
11241
11242/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11243 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11244 *
11245 * The logic is the same as the "commit ssl cert" command but without the
11246 * freeing of the old structures, because there are none.
11247 */
11248static int cli_io_handler_add_crtlist(struct appctx *appctx)
11249{
11250 struct bind_conf_list *bind_conf_node;
11251 struct stream_interface *si = appctx->owner;
11252 struct crtlist *crtlist = appctx->ctx.cli.p0;
11253 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11254 struct ckch_store *store = entry->node.key;
11255 struct buffer *trash = alloc_trash_chunk();
11256 struct ckch_inst *new_inst;
11257 char *err = NULL;
11258 int i = 0;
11259 int errcode = 0;
11260
11261 if (trash == NULL)
11262 goto error;
11263
11264 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11265 * created.
11266 */
11267 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11268 goto error;
11269
11270 while (1) {
11271 switch (appctx->st2) {
11272 case SETCERT_ST_INIT:
11273 /* This state just print the update message */
11274 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11275 if (ci_putchk(si_ic(si), trash) == -1) {
11276 si_rx_room_blk(si);
11277 goto yield;
11278 }
11279 appctx->st2 = SETCERT_ST_GEN;
11280 /* fallthrough */
11281 case SETCERT_ST_GEN:
11282 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11283 if (bind_conf_node == NULL)
11284 bind_conf_node = crtlist->bind_conf;
11285 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11286 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11287 struct sni_ctx *sni;
11288
11289 /* yield every 10 generations */
11290 if (i > 10) {
11291 appctx->ctx.cli.p2 = bind_conf_node;
11292 goto yield;
11293 }
11294
11295 /* we don't support multi-cert bundles, only simple ones */
11296 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11297 if (errcode & ERR_CODE)
11298 goto error;
11299
11300 /* we need to initialize the SSL_CTX generated */
11301 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11302 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11303 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11304 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11305 if (errcode & ERR_CODE)
11306 goto error;
11307 }
11308 }
11309 /* display one dot for each new instance */
11310 chunk_appendf(trash, ".");
11311 i++;
11312 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11313 }
11314 appctx->st2 = SETCERT_ST_INSERT;
11315 /* fallthrough */
11316 case SETCERT_ST_INSERT:
11317 /* insert SNIs in bind_conf */
11318 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11319 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11320 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11321 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11322 }
11323 appctx->st2 = SETCERT_ST_FIN;
11324 goto end;
11325 }
11326 }
11327
11328end:
11329 chunk_appendf(trash, "\n");
11330 if (errcode & ERR_WARN)
11331 chunk_appendf(trash, "%s", err);
11332 chunk_appendf(trash, "Success!\n");
11333 if (ci_putchk(si_ic(si), trash) == -1)
11334 si_rx_room_blk(si);
11335 free_trash_chunk(trash);
11336 /* success: call the release function and don't come back */
11337 return 1;
11338yield:
11339 /* store the state */
11340 if (ci_putchk(si_ic(si), trash) == -1)
11341 si_rx_room_blk(si);
11342 free_trash_chunk(trash);
11343 si_rx_endp_more(si); /* let's come back later */
11344 return 0; /* should come back */
11345
11346error:
11347 /* spin unlock and free are done in the release function */
11348 if (trash) {
11349 chunk_appendf(trash, "\n%sFailed!\n", err);
11350 if (ci_putchk(si_ic(si), trash) == -1)
11351 si_rx_room_blk(si);
11352 free_trash_chunk(trash);
11353 }
11354 /* error: call the release function and don't come back */
11355 return 1;
11356}
11357
11358
11359/*
11360 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011361 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011362 */
11363static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11364{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011365 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011366 struct ckch_store *store;
11367 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011368 char path[MAXPATHLEN+1];
11369 char *crtlist_path;
11370 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011371 struct ebmb_node *eb;
11372 struct ebpt_node *inserted;
11373 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011374 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011375
11376 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11377 return 1;
11378
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011379 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011380 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11381
11382 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011383
11384 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11385 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11386
11387 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11388 if (!eb) {
11389 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11390 goto error;
11391 }
11392 crtlist = ebmb_entry(eb, struct crtlist, node);
11393
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011394 entry = malloc(sizeof(*entry));
11395 if (entry == NULL) {
11396 memprintf(&err, "Not enough memory!");
11397 goto error;
11398 }
William Lallemanda690fed2020-04-02 12:02:54 +020011399 entry->fcount = 0;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011400 entry->filters = NULL;
11401 entry->ssl_conf = NULL;
11402
11403 if (payload) {
11404 char *lf;
11405
11406 lf = strrchr(payload, '\n');
11407 if (lf) {
11408 memprintf(&err, "only one line of payload is supported!");
11409 goto error;
11410 }
11411 /* cert_path is filled here */
11412 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11413 if (cfgerr & ERR_CODE)
11414 goto error;
11415 } else {
11416 cert_path = args[4];
11417 }
11418
11419 if (!cert_path) {
11420 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11421 cfgerr |= ERR_ALERT | ERR_FATAL;
11422 goto error;
11423 }
11424
11425 if (*cert_path != '/' && global_ssl.crt_base) {
11426 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11427 memprintf(&err, "'%s' : path too long", cert_path);
11428 cfgerr |= ERR_ALERT | ERR_FATAL;
11429 goto error;
11430 }
11431 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11432 cert_path = path;
11433 }
11434
William Lallemande67c80b2020-03-25 14:42:37 +010011435 store = ckchs_lookup(cert_path);
11436 if (store == NULL) {
11437 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11438 goto error;
11439 }
11440 if (store->ckch == NULL || store->ckch->cert == NULL) {
11441 memprintf(&err, "certificate '%s' is empty!", cert_path);
11442 goto error;
11443 }
11444
William Lallemande67c80b2020-03-25 14:42:37 +010011445 /* check if it's possible to insert this new crtlist_entry */
11446 entry->node.key = store;
11447 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11448 if (inserted != &entry->node) {
11449 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011450 goto error;
11451 }
11452
11453 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
11454 LIST_INIT(&entry->ckch_inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011455 entry->crtlist = crtlist;
11456 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11457
11458 appctx->st2 = SETCERT_ST_INIT;
11459 appctx->ctx.cli.p0 = crtlist;
11460 appctx->ctx.cli.p1 = entry;
11461
11462 /* unlock is done in the release handler */
11463 return 0;
11464
11465error:
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011466 if (entry) {
11467 crtlist_free_filters(entry->filters);
11468 ssl_sock_free_ssl_conf(entry->ssl_conf);
11469 free(entry->ssl_conf);
11470 free(entry);
11471 }
William Lallemande67c80b2020-03-25 14:42:37 +010011472 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11473 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11474 return cli_dynerr(appctx, err);
11475}
11476
William Lallemand0a9b9412020-04-06 17:43:05 +020011477/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11478static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11479{
11480 struct ckch_store *store;
11481 char *err = NULL;
11482 char *crtlist_path, *cert_path;
11483 struct ebmb_node *ebmb;
11484 struct ebpt_node *ebpt;
11485 struct crtlist *crtlist;
11486 struct crtlist_entry *entry = NULL;
11487 struct ckch_inst *inst, *inst_s;
11488 int linenum = 0;
11489 char *colons;
11490
11491 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11492 return 1;
11493
11494 if (!*args[3] || !*args[4])
11495 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11496
11497 crtlist_path = args[3];
11498 cert_path = args[4];
11499
11500 colons = strchr(cert_path, ':');
11501 if (colons) {
11502 char *endptr;
11503
11504 linenum = strtol(colons + 1, &endptr, 10);
11505 if (colons + 1 == endptr || *endptr != '\0') {
11506 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11507 goto error;
11508 }
11509 *colons = '\0';
11510 }
11511 /* look for crtlist */
11512 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11513 if (!ebmb) {
11514 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11515 goto error;
11516 }
11517 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11518
11519 /* look for store */
11520 store = ckchs_lookup(cert_path);
11521 if (store == NULL) {
11522 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11523 goto error;
11524 }
11525 if (store->ckch == NULL || store->ckch->cert == NULL) {
11526 memprintf(&err, "certificate '%s' is empty!", cert_path);
11527 goto error;
11528 }
11529
11530 ebpt = ebpt_lookup(&crtlist->entries, store);
11531 if (!ebpt) {
11532 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11533 goto error;
11534 }
11535
11536 /* list the line number of entries for errors in err, and select the right ebpt */
11537 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11538 struct crtlist_entry *tmp;
11539
11540 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11541 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11542
11543 /* select the entry we wanted */
11544 if (linenum == 0 || tmp->linenum == linenum) {
11545 if (!entry)
11546 entry = tmp;
11547 }
11548 }
11549
11550 /* we didn't found the specified entry */
11551 if (!entry) {
11552 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);
11553 goto error;
11554 }
11555
11556 /* we didn't specified a line number but there were several entries */
11557 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11558 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11559 goto error;
11560 }
11561
11562 /* upon error free the ckch_inst and everything inside */
11563
11564 ebpt_delete(&entry->node);
11565 LIST_DEL(&entry->by_crtlist);
11566 LIST_DEL(&entry->by_ckch_store);
11567
11568 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11569 struct sni_ctx *sni, *sni_s;
11570
11571 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11572 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11573 ebmb_delete(&sni->name);
11574 LIST_DEL(&sni->by_ckch_inst);
11575 if (sni->order == 0) /* we only free if it's the first inserted */
11576 SSL_CTX_free(sni->ctx);
11577 free(sni);
11578 }
11579 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11580 LIST_DEL(&inst->by_ckchs);
11581 free(inst);
11582 }
11583
11584 crtlist_free_filters(entry->filters);
11585 ssl_sock_free_ssl_conf(entry->ssl_conf);
11586 free(entry->ssl_conf);
11587 free(entry);
11588
11589 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11590 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11591 return cli_dynmsg(appctx, LOG_NOTICE, err);
11592
11593error:
11594 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11595 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11596 return cli_dynerr(appctx, err);
11597}
William Lallemande67c80b2020-03-25 14:42:37 +010011598
William Lallemand44b35322019-10-17 16:28:40 +020011599/* Type of SSL payloads that can be updated over the CLI */
11600
11601enum {
11602 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011603 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011604#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011605 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011606#endif
William Lallemand44b35322019-10-17 16:28:40 +020011607 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011608#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011609 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011610#endif
William Lallemand44b35322019-10-17 16:28:40 +020011611 CERT_TYPE_MAX,
11612};
11613
11614struct {
11615 const char *ext;
11616 int type;
11617 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11618 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011619} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011620 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011621 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011622#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011623 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011624#endif
11625#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011626 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011627#endif
William Lallemand44b35322019-10-17 16:28:40 +020011628 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011629 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011630};
11631
William Lallemand8f840d72019-10-23 10:53:05 +020011632
William Lallemandd4f946c2019-12-05 10:26:40 +010011633/* release function of the `show ssl cert' command */
11634static void cli_release_show_cert(struct appctx *appctx)
11635{
11636 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11637}
11638
11639/* IO handler of "show ssl cert <filename>" */
11640static int cli_io_handler_show_cert(struct appctx *appctx)
11641{
11642 struct buffer *trash = alloc_trash_chunk();
11643 struct ebmb_node *node;
11644 struct stream_interface *si = appctx->owner;
11645 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011646
11647 if (trash == NULL)
11648 return 1;
11649
11650 if (!appctx->ctx.ssl.old_ckchs) {
11651 if (ckchs_transaction.old_ckchs) {
11652 ckchs = ckchs_transaction.old_ckchs;
11653 chunk_appendf(trash, "# transaction\n");
11654 if (!ckchs->multi) {
11655 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011656#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011657 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011658 int n;
11659
William Lallemandd4f946c2019-12-05 10:26:40 +010011660 chunk_appendf(trash, "*%s:", ckchs->path);
11661 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11662 if (ckchs->ckch[n].cert)
11663 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11664 }
11665 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011666#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011667 }
11668 }
11669 }
11670
11671 if (!appctx->ctx.cli.p0) {
11672 chunk_appendf(trash, "# filename\n");
11673 node = ebmb_first(&ckchs_tree);
11674 } else {
11675 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11676 }
11677 while (node) {
11678 ckchs = ebmb_entry(node, struct ckch_store, node);
11679 if (!ckchs->multi) {
11680 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011681#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011682 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011683 int n;
11684
William Lallemandd4f946c2019-12-05 10:26:40 +010011685 chunk_appendf(trash, "%s:", ckchs->path);
11686 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11687 if (ckchs->ckch[n].cert)
11688 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11689 }
11690 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011691#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011692 }
11693
11694 node = ebmb_next(node);
11695 if (ci_putchk(si_ic(si), trash) == -1) {
11696 si_rx_room_blk(si);
11697 goto yield;
11698 }
11699 }
11700
11701 appctx->ctx.cli.p0 = NULL;
11702 free_trash_chunk(trash);
11703 return 1;
11704yield:
11705
11706 free_trash_chunk(trash);
11707 appctx->ctx.cli.p0 = ckchs;
11708 return 0; /* should come back */
11709}
11710
11711/* IO handler of the details "show ssl cert <filename>" */
11712static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11713{
11714 struct stream_interface *si = appctx->owner;
11715 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11716 struct buffer *out = alloc_trash_chunk();
11717 struct buffer *tmp = alloc_trash_chunk();
11718 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011719 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011720 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011721 int write = -1;
11722 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011723 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011724
11725 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011726 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011727
11728 if (!ckchs->multi) {
11729 chunk_appendf(out, "Filename: ");
11730 if (ckchs == ckchs_transaction.new_ckchs)
11731 chunk_appendf(out, "*");
11732 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011733
William Lallemand59c16fc2020-03-19 20:26:02 +010011734 chunk_appendf(out, "Status: ");
11735 if (ckchs->ckch->cert == NULL)
11736 chunk_appendf(out, "Empty\n");
11737 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11738 chunk_appendf(out, "Unused\n");
11739 else
11740 chunk_appendf(out, "Used\n");
11741
William Lallemandea987ed2020-03-19 16:48:33 +010011742 if (ckchs->ckch->cert == NULL)
11743 goto end;
11744
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011745 chain = ckchs->ckch->chain;
11746 if (chain == NULL) {
11747 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011748 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011749 if (issuer) {
11750 chain = issuer->chain;
11751 chunk_appendf(out, "Chain Filename: ");
11752 chunk_appendf(out, "%s\n", issuer->path);
11753 }
11754 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011755 chunk_appendf(out, "Serial: ");
11756 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11757 goto end;
11758 dump_binary(out, tmp->area, tmp->data);
11759 chunk_appendf(out, "\n");
11760
11761 chunk_appendf(out, "notBefore: ");
11762 chunk_reset(tmp);
11763 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11764 goto end;
11765 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11766 goto end;
11767 write = BIO_read(bio, tmp->area, tmp->size-1);
11768 tmp->area[write] = '\0';
11769 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011770 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011771 chunk_appendf(out, "%s\n", tmp->area);
11772
11773 chunk_appendf(out, "notAfter: ");
11774 chunk_reset(tmp);
11775 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11776 goto end;
11777 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11778 goto end;
11779 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11780 goto end;
11781 tmp->area[write] = '\0';
11782 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011783 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011784 chunk_appendf(out, "%s\n", tmp->area);
11785
William Lallemandd4f946c2019-12-05 10:26:40 +010011786#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11787 chunk_appendf(out, "Subject Alternative Name: ");
11788 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11789 goto end;
11790 *(out->area + out->data) = '\0';
11791 chunk_appendf(out, "\n");
11792#endif
11793 chunk_reset(tmp);
11794 chunk_appendf(out, "Algorithm: ");
11795 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11796 goto end;
11797 chunk_appendf(out, "%s\n", tmp->area);
11798
11799 chunk_reset(tmp);
11800 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011801 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011802 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011803 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011804 dump_binary(out, tmp->area, tmp->data);
11805 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011806
William Lallemanda90e5932020-02-25 14:07:58 +010011807 chunk_appendf(out, "Subject: ");
11808 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11809 goto end;
11810 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11811 goto end;
11812 *(tmp->area + tmp->data) = '\0';
11813 chunk_appendf(out, "%s\n", tmp->area);
11814
11815 chunk_appendf(out, "Issuer: ");
11816 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11817 goto end;
11818 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11819 goto end;
11820 *(tmp->area + tmp->data) = '\0';
11821 chunk_appendf(out, "%s\n", tmp->area);
11822
William Lallemand35f4a9d2020-02-25 11:56:32 +010011823 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011824 for (i = 0; i < sk_X509_num(chain); i++) {
11825 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011826
William Lallemandbb7288a2020-02-25 14:04:33 +010011827 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011828 if ((name = X509_get_subject_name(ca)) == NULL)
11829 goto end;
11830 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11831 goto end;
11832 *(tmp->area + tmp->data) = '\0';
11833 chunk_appendf(out, "%s\n", tmp->area);
11834
William Lallemandbb7288a2020-02-25 14:04:33 +010011835 chunk_appendf(out, "Chain Issuer: ");
11836 if ((name = X509_get_issuer_name(ca)) == NULL)
11837 goto end;
11838 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11839 goto end;
11840 *(tmp->area + tmp->data) = '\0';
11841 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011842 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011843 }
11844
William Lallemandea987ed2020-03-19 16:48:33 +010011845end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011846 if (ci_putchk(si_ic(si), out) == -1) {
11847 si_rx_room_blk(si);
11848 goto yield;
11849 }
11850
William Lallemand18eeb8e2020-03-20 14:42:36 +010011851end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011852 if (bio)
11853 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011854 free_trash_chunk(tmp);
11855 free_trash_chunk(out);
11856 return 1;
11857yield:
11858 free_trash_chunk(tmp);
11859 free_trash_chunk(out);
11860 return 0; /* should come back */
11861}
11862
11863/* parsing function for 'show ssl cert [certfile]' */
11864static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11865{
11866 struct ckch_store *ckchs;
11867
11868 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11869 return cli_err(appctx, "Can't allocate memory!\n");
11870
11871 /* The operations on the CKCH architecture are locked so we can
11872 * manipulate ckch_store and ckch_inst */
11873 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11874 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11875
11876 /* check if there is a certificate to lookup */
11877 if (*args[3]) {
11878 if (*args[3] == '*') {
11879 if (!ckchs_transaction.new_ckchs)
11880 goto error;
11881
11882 ckchs = ckchs_transaction.new_ckchs;
11883
11884 if (strcmp(args[3] + 1, ckchs->path))
11885 goto error;
11886
11887 } else {
11888 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11889 goto error;
11890
11891 }
11892
11893 if (ckchs->multi)
11894 goto error;
11895
11896 appctx->ctx.cli.p0 = ckchs;
11897 /* use the IO handler that shows details */
11898 appctx->io_handler = cli_io_handler_show_cert_detail;
11899 }
11900
11901 return 0;
11902
11903error:
11904 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11905 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11906}
11907
William Lallemand430413e2019-10-28 14:30:47 +010011908/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011909static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011910{
11911 struct ckch_store *new_ckchs;
11912 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011913
William Lallemand430413e2019-10-28 14:30:47 +010011914 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011915
William Lallemand430413e2019-10-28 14:30:47 +010011916 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011917 /* 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 +010011918 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011919
William Lallemandbeea2a42019-10-30 17:45:33 +010011920 if (!new_ckchs)
11921 return;
William Lallemand8f840d72019-10-23 10:53:05 +020011922
William Lallemandbeea2a42019-10-30 17:45:33 +010011923 /* if the allocation failed, we need to free everything from the temporary list */
11924 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
11925 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020011926
William Lallemandbeea2a42019-10-30 17:45:33 +010011927 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
11928 if (sc0->order == 0) /* we only free if it's the first inserted */
11929 SSL_CTX_free(sc0->ctx);
11930 LIST_DEL(&sc0->by_ckch_inst);
11931 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020011932 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011933 LIST_DEL(&ckchi->by_ckchs);
11934 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020011935 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011936 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011937 }
11938}
11939
11940
11941/*
11942 * This function tries to create the new ckch_inst and their SNIs
11943 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011944static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011945{
11946 struct stream_interface *si = appctx->owner;
11947 int y = 0;
11948 char *err = NULL;
11949 int errcode = 0;
11950 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11951 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011952 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011953 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011954 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011955
William Lallemand33cc76f2019-10-31 11:43:45 +010011956 if (trash == NULL)
11957 goto error;
11958
William Lallemand8f840d72019-10-23 10:53:05 +020011959 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11960 goto error;
11961
William Lallemand430413e2019-10-28 14:30:47 +010011962 while (1) {
11963 switch (appctx->st2) {
11964 case SETCERT_ST_INIT:
11965 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011966 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011967 if (ci_putchk(si_ic(si), trash) == -1) {
11968 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011969 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011970 }
11971 appctx->st2 = SETCERT_ST_GEN;
11972 /* fallthrough */
11973 case SETCERT_ST_GEN:
11974 /*
11975 * This state generates the ckch instances with their
11976 * sni_ctxs and SSL_CTX.
11977 *
William Lallemand430413e2019-10-28 14:30:47 +010011978 * Since the SSL_CTX generation can be CPU consumer, we
11979 * yield every 10 instances.
11980 */
William Lallemand8f840d72019-10-23 10:53:05 +020011981
William Lallemandbeea2a42019-10-30 17:45:33 +010011982 old_ckchs = appctx->ctx.ssl.old_ckchs;
11983 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011984
William Lallemandbeea2a42019-10-30 17:45:33 +010011985 if (!new_ckchs)
11986 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020011987
William Lallemandbeea2a42019-10-30 17:45:33 +010011988 /* get the next ckchi to regenerate */
11989 ckchi = appctx->ctx.ssl.next_ckchi;
11990 /* we didn't start yet, set it to the first elem */
11991 if (ckchi == NULL)
11992 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011993
William Lallemandbeea2a42019-10-30 17:45:33 +010011994 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
11995 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
11996 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010011997 char **sni_filter = NULL;
11998 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020011999
William Lallemandbeea2a42019-10-30 17:45:33 +010012000 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
12001 if (y >= 10) {
12002 /* save the next ckchi to compute */
12003 appctx->ctx.ssl.next_ckchi = ckchi;
12004 goto yield;
12005 }
William Lallemand67630162020-03-09 16:56:39 +010012006 if (ckchi->filters) {
12007 errcode |= ckch_inst_sni_ctx_to_sni_filters(ckchi, &sni_filter, &fcount, &err);
12008 if (errcode & ERR_CODE)
12009 goto error;
12010 }
William Lallemand38df1c82019-12-04 15:39:35 +010012011
William Lallemandbeea2a42019-10-30 17:45:33 +010012012 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010012013 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 +010012014 else
William Lallemand38df1c82019-12-04 15:39:35 +010012015 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
12016
12017 free_sni_filters(sni_filter, fcount);
12018 sni_filter = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012019
William Lallemandbeea2a42019-10-30 17:45:33 +010012020 if (errcode & ERR_CODE)
12021 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020012022
William Lallemand21724f02019-11-04 17:56:13 +010012023 /* if the previous ckchi was used as the default */
12024 if (ckchi->is_default)
12025 new_inst->is_default = 1;
12026
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012027 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010012028 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
12029 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012030 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 +010012031 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
12032 if (errcode & ERR_CODE)
12033 goto error;
12034 }
12035 }
12036
12037
William Lallemandbeea2a42019-10-30 17:45:33 +010012038 /* display one dot per new instance */
12039 chunk_appendf(trash, ".");
12040 /* link the new ckch_inst to the duplicate */
12041 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
12042 y++;
12043 }
William Lallemand430413e2019-10-28 14:30:47 +010012044 appctx->st2 = SETCERT_ST_INSERT;
12045 /* fallthrough */
12046 case SETCERT_ST_INSERT:
12047 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012048
William Lallemandbeea2a42019-10-30 17:45:33 +010012049 old_ckchs = appctx->ctx.ssl.old_ckchs;
12050 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012051
William Lallemandbeea2a42019-10-30 17:45:33 +010012052 if (!new_ckchs)
12053 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012054
William Lallemand90afe902020-03-30 19:29:45 +020012055 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12056 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12057 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12058 ebpt_delete(&entry->node);
12059 /* change the ptr and reinsert the node */
12060 entry->node.key = new_ckchs;
12061 ebpt_insert(&entry->crtlist->entries, &entry->node);
12062 }
12063
William Lallemand21724f02019-11-04 17:56:13 +010012064 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012065 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12066 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12067 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12068 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12069 }
William Lallemand8f840d72019-10-23 10:53:05 +020012070
William Lallemandbeea2a42019-10-30 17:45:33 +010012071 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12072 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010012073
William Lallemandbeea2a42019-10-30 17:45:33 +010012074 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12075 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
12076 ebmb_delete(&sc0->name);
12077 LIST_DEL(&sc0->by_ckch_inst);
12078 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010012079 }
William Lallemandbeea2a42019-10-30 17:45:33 +010012080 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12081 LIST_DEL(&ckchi->by_ckchs);
12082 free(ckchi);
12083 }
William Lallemand8f840d72019-10-23 10:53:05 +020012084
William Lallemandbeea2a42019-10-30 17:45:33 +010012085 /* Replace the old ckchs by the new one */
12086 ebmb_delete(&old_ckchs->node);
12087 ckchs_free(old_ckchs);
12088 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012089 appctx->st2 = SETCERT_ST_FIN;
12090 /* fallthrough */
12091 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012092 /* we achieved the transaction, we can set everything to NULL */
12093 free(ckchs_transaction.path);
12094 ckchs_transaction.path = NULL;
12095 ckchs_transaction.new_ckchs = NULL;
12096 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012097 goto end;
12098 }
William Lallemand8f840d72019-10-23 10:53:05 +020012099 }
William Lallemand430413e2019-10-28 14:30:47 +010012100end:
William Lallemand8f840d72019-10-23 10:53:05 +020012101
William Lallemanded442432019-11-21 16:41:07 +010012102 chunk_appendf(trash, "\n");
12103 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012104 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012105 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012106 if (ci_putchk(si_ic(si), trash) == -1)
12107 si_rx_room_blk(si);
12108 free_trash_chunk(trash);
12109 /* success: call the release function and don't come back */
12110 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012111yield:
12112 /* store the state */
12113 if (ci_putchk(si_ic(si), trash) == -1)
12114 si_rx_room_blk(si);
12115 free_trash_chunk(trash);
12116 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012117 return 0; /* should come back */
12118
12119error:
12120 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012121 if (trash) {
12122 chunk_appendf(trash, "\n%sFailed!\n", err);
12123 if (ci_putchk(si_ic(si), trash) == -1)
12124 si_rx_room_blk(si);
12125 free_trash_chunk(trash);
12126 }
William Lallemand430413e2019-10-28 14:30:47 +010012127 /* error: call the release function and don't come back */
12128 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012129}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012130
12131/*
12132 * Parsing function of 'commit ssl cert'
12133 */
12134static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12135{
12136 char *err = NULL;
12137
William Lallemand230662a2019-12-03 13:32:54 +010012138 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12139 return 1;
12140
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012141 if (!*args[3])
12142 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12143
12144 /* The operations on the CKCH architecture are locked so we can
12145 * manipulate ckch_store and ckch_inst */
12146 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12147 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12148
12149 if (!ckchs_transaction.path) {
12150 memprintf(&err, "No ongoing transaction! !\n");
12151 goto error;
12152 }
12153
12154 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12155 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12156 goto error;
12157 }
12158
William Lallemand4c5adbf2020-02-24 14:23:22 +010012159#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12160 if (ckchs_transaction.new_ckchs->multi) {
12161 int n;
12162
12163 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12164 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)) {
12165 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12166 goto error;
12167 }
12168 }
12169 } else
12170#endif
12171 {
12172 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12173 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12174 goto error;
12175 }
12176 }
12177
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012178 /* init the appctx structure */
12179 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012180 appctx->ctx.ssl.next_ckchi = NULL;
12181 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12182 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12183
12184 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12185 return 0;
12186
12187error:
12188
12189 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12190 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12191
12192 return cli_dynerr(appctx, err);
12193}
12194
William Lallemand8f840d72019-10-23 10:53:05 +020012195/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012196 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012197 */
William Lallemand150bfa82019-09-19 17:12:49 +020012198static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12199{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012200 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012201 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012202 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012203 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012204 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012205 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012206 char *end;
12207 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012208 struct cert_key_and_chain *ckch;
12209 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012210
William Lallemand230662a2019-12-03 13:32:54 +010012211 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12212 return 1;
12213
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012214 if ((buf = alloc_trash_chunk()) == NULL)
12215 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012216
12217 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012218 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012219
12220 /* The operations on the CKCH architecture are locked so we can
12221 * manipulate ckch_store and ckch_inst */
12222 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12223 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12224
William Lallemand8f840d72019-10-23 10:53:05 +020012225 if (!chunk_strcpy(buf, args[3])) {
12226 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12227 errcode |= ERR_ALERT | ERR_FATAL;
12228 goto end;
12229 }
12230
William Lallemand44b35322019-10-17 16:28:40 +020012231 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012232 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012233 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012234 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12235 *end = '\0';
12236 type = cert_exts[i].type;
12237 break;
12238 }
12239 }
12240
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012241 appctx->ctx.ssl.old_ckchs = NULL;
12242 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012243
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012244 /* if there is an ongoing transaction */
12245 if (ckchs_transaction.path) {
12246 /* 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 +020012247#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012248 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012249 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012250 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012251
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012252 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012253 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012254 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012255 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012256 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12257 bundle = j; /* keep the type of certificate so we insert it at the right place */
12258 *end = '\0'; /* it's a bundle let's end the string*/
12259 break;
12260 }
William Lallemand150bfa82019-09-19 17:12:49 +020012261 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012262 if (bundle < 0) {
12263 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);
12264 errcode |= ERR_ALERT | ERR_FATAL;
12265 goto end;
12266 }
12267 }
12268#endif
12269
12270 /* if there is an ongoing transaction, check if this is the same file */
12271 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12272 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12273 errcode |= ERR_ALERT | ERR_FATAL;
12274 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012275 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012276
12277 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12278
12279 } else {
12280 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12281
12282 /* lookup for the certificate in the tree:
12283 * check if this is used as a bundle AND as a unique certificate */
12284 for (i = 0; i < 2; i++) {
12285
12286 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12287 /* only the bundle name is in the tree and you should
12288 * never update a bundle name, only a filename */
12289 if (bundle < 0 && find_ckchs[i]->multi) {
12290 /* we tried to look for a non-bundle and we found a bundle */
12291 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12292 err ? err : "", args[3], args[3]);
12293 errcode |= ERR_ALERT | ERR_FATAL;
12294 goto end;
12295 }
William Lallemand3246d942019-11-04 14:02:11 +010012296 /* If we want a bundle but this is not a bundle
12297 * example: When you try to update <file>.rsa, but
12298 * <file> is a regular file */
12299 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12300 find_ckchs[i] = NULL;
12301 break;
12302 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012303 }
12304#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12305 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012306 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012307 int j;
12308
12309 /* check if it was used in a bundle by removing the
12310 * .dsa/.rsa/.ecdsa at the end of the filename */
12311 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012312 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012313 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12314 bundle = j; /* keep the type of certificate so we insert it at the right place */
12315 *end = '\0'; /* it's a bundle let's end the string*/
12316 break;
12317 }
12318 }
William Lallemand37031b82019-11-04 13:38:53 +010012319 if (bundle < 0) /* we didn't find a bundle extension */
12320 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012321 }
William Lallemand963b2e72019-10-14 11:38:36 +020012322#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012323 /* bundles are not supported here, so we don't need to lookup again */
12324 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012325#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012326 }
12327
12328 if (find_ckchs[0] && find_ckchs[1]) {
12329 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",
12330 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12331 errcode |= ERR_ALERT | ERR_FATAL;
12332 goto end;
12333 }
12334
12335 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012336 }
12337
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012338 if (!appctx->ctx.ssl.old_ckchs) {
12339 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012340 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012341 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012342 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012343 }
12344
William Lallemand8a7fdf02019-11-04 10:59:32 +010012345 if (!appctx->ctx.ssl.path) {
12346 /* this is a new transaction, set the path of the transaction */
12347 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12348 if (!appctx->ctx.ssl.path) {
12349 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12350 errcode |= ERR_ALERT | ERR_FATAL;
12351 goto end;
12352 }
12353 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012354
12355 old_ckchs = appctx->ctx.ssl.old_ckchs;
12356
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012357 /* duplicate the ckch store */
12358 new_ckchs = ckchs_dup(old_ckchs);
12359 if (!new_ckchs) {
12360 memprintf(&err, "%sCannot allocate memory!\n",
12361 err ? err : "");
12362 errcode |= ERR_ALERT | ERR_FATAL;
12363 goto end;
12364 }
12365
12366 if (!new_ckchs->multi)
12367 ckch = new_ckchs->ckch;
12368 else
12369 ckch = &new_ckchs->ckch[bundle];
12370
12371 /* appply the change on the duplicate */
12372 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12373 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12374 errcode |= ERR_ALERT | ERR_FATAL;
12375 goto end;
12376 }
12377
12378 appctx->ctx.ssl.new_ckchs = new_ckchs;
12379
12380 /* we succeed, we can save the ckchs in the transaction */
12381
12382 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012383 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012384 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12385 ckchs_transaction.path = appctx->ctx.ssl.path;
12386 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12387 } else {
12388 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12389
12390 }
12391
12392 /* free the previous ckchs if there was a transaction */
12393 ckchs_free(ckchs_transaction.new_ckchs);
12394
12395 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12396
12397
William Lallemand8f840d72019-10-23 10:53:05 +020012398 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012399
William Lallemand8f840d72019-10-23 10:53:05 +020012400end:
12401 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012402
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012403 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012404
12405 ckchs_free(appctx->ctx.ssl.new_ckchs);
12406 appctx->ctx.ssl.new_ckchs = NULL;
12407
12408 appctx->ctx.ssl.old_ckchs = NULL;
12409
12410 free(appctx->ctx.ssl.path);
12411 appctx->ctx.ssl.path = NULL;
12412
12413 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012414 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012415 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012416
12417 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12418 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012419 }
William Lallemand8f840d72019-10-23 10:53:05 +020012420 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012421}
12422
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012423/* parsing function of 'abort ssl cert' */
12424static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12425{
12426 char *err = NULL;
12427
William Lallemand230662a2019-12-03 13:32:54 +010012428 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12429 return 1;
12430
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012431 if (!*args[3])
12432 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12433
12434 /* The operations on the CKCH architecture are locked so we can
12435 * manipulate ckch_store and ckch_inst */
12436 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12437 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12438
12439 if (!ckchs_transaction.path) {
12440 memprintf(&err, "No ongoing transaction!\n");
12441 goto error;
12442 }
12443
12444 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12445 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12446 goto error;
12447 }
12448
12449 /* Only free the ckchs there, because the SNI and instances were not generated yet */
12450 ckchs_free(ckchs_transaction.new_ckchs);
12451 ckchs_transaction.new_ckchs = NULL;
12452 ckchs_free(ckchs_transaction.old_ckchs);
12453 ckchs_transaction.old_ckchs = NULL;
12454 free(ckchs_transaction.path);
12455 ckchs_transaction.path = NULL;
12456
12457 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12458
12459 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12460 return cli_dynmsg(appctx, LOG_NOTICE, err);
12461
12462error:
12463 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12464
12465 return cli_dynerr(appctx, err);
12466}
12467
William Lallemandea987ed2020-03-19 16:48:33 +010012468/* parsing function of 'new ssl cert' */
12469static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12470{
12471 struct ckch_store *store;
12472 char *err = NULL;
12473 char *path;
12474
12475 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12476 return 1;
12477
12478 if (!*args[3])
12479 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12480
12481 path = args[3];
12482
12483 /* The operations on the CKCH architecture are locked so we can
12484 * manipulate ckch_store and ckch_inst */
12485 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12486 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12487
12488 store = ckchs_lookup(path);
12489 if (store != NULL) {
12490 memprintf(&err, "Certificate '%s' already exists!\n", path);
12491 store = NULL; /* we don't want to free it */
12492 goto error;
12493 }
12494 store = calloc(1, sizeof(*store) + strlen(path) + 1);
12495 if (!store) {
12496 memprintf(&err, "unable to allocate memory.\n");
12497 goto error;
12498 }
12499 store->ckch = calloc(1, sizeof(*store->ckch));
12500 if (!store->ckch) {
12501 memprintf(&err, "unable to allocate memory.\n");
12502 goto error;
12503 }
12504 /* we won't create any instance */
12505 LIST_INIT(&store->ckch_inst);
William Lallemand23d61c02020-03-30 18:27:58 +020012506 LIST_INIT(&store->crtlist_entry);
William Lallemandea987ed2020-03-19 16:48:33 +010012507
12508 /* we won't support multi-certificate bundle here */
12509 store->multi = 0;
12510
12511 /* insert into the ckchs tree */
12512 memcpy(store->path, path, strlen(path) + 1);
12513 ebst_insert(&ckchs_tree, &store->node);
12514 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12515
12516 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12517 return cli_dynmsg(appctx, LOG_NOTICE, err);
12518error:
12519 free(store);
12520 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12521 return cli_dynerr(appctx, err);
12522}
12523
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012524static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012525{
12526#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12527 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012528 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012529
12530 if (!payload)
12531 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012532
12533 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012534 if (!*payload)
12535 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012536
12537 /* remove \r and \n from the payload */
12538 for (i = 0, j = 0; payload[i]; i++) {
12539 if (payload[i] == '\r' || payload[i] == '\n')
12540 continue;
12541 payload[j++] = payload[i];
12542 }
12543 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012544
Willy Tarreau1c913e42018-08-22 05:26:57 +020012545 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012546 if (ret < 0)
12547 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012548
Willy Tarreau1c913e42018-08-22 05:26:57 +020012549 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012550 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012551 if (err)
12552 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12553 else
12554 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012555 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012556
12557 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012558#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012559 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 +020012560#endif
12561
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012562}
12563
Willy Tarreau86a394e2019-05-09 14:15:32 +020012564#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012565static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12566{
12567 switch (arg->type) {
12568 case ARGT_STR:
12569 smp->data.type = SMP_T_STR;
12570 smp->data.u.str = arg->data.str;
12571 return 1;
12572 case ARGT_VAR:
12573 if (!vars_get_by_desc(&arg->data.var, smp))
12574 return 0;
12575 if (!sample_casts[smp->data.type][SMP_T_STR])
12576 return 0;
12577 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12578 return 0;
12579 return 1;
12580 default:
12581 return 0;
12582 }
12583}
12584
12585static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12586 const char *file, int line, char **err)
12587{
12588 switch(args[0].data.sint) {
12589 case 128:
12590 case 192:
12591 case 256:
12592 break;
12593 default:
12594 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12595 return 0;
12596 }
12597 /* Try to decode a variable. */
12598 vars_check_arg(&args[1], NULL);
12599 vars_check_arg(&args[2], NULL);
12600 vars_check_arg(&args[3], NULL);
12601 return 1;
12602}
12603
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012604/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012605static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12606{
12607 struct sample nonce, key, aead_tag;
12608 struct buffer *smp_trash, *smp_trash_alloc;
12609 EVP_CIPHER_CTX *ctx;
12610 int dec_size, ret;
12611
12612 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12613 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12614 return 0;
12615
12616 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12617 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12618 return 0;
12619
12620 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12621 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12622 return 0;
12623
12624 smp_trash = get_trash_chunk();
12625 smp_trash_alloc = alloc_trash_chunk();
12626 if (!smp_trash_alloc)
12627 return 0;
12628
12629 ctx = EVP_CIPHER_CTX_new();
12630
12631 if (!ctx)
12632 goto err;
12633
12634 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12635 if (dec_size < 0)
12636 goto err;
12637 smp_trash->data = dec_size;
12638
12639 /* Set cipher type and mode */
12640 switch(arg_p[0].data.sint) {
12641 case 128:
12642 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12643 break;
12644 case 192:
12645 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12646 break;
12647 case 256:
12648 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12649 break;
12650 }
12651
12652 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12653
12654 /* Initialise IV */
12655 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12656 goto err;
12657
12658 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12659 if (dec_size < 0)
12660 goto err;
12661 smp_trash->data = dec_size;
12662
12663 /* Initialise key */
12664 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12665 goto err;
12666
12667 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12668 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12669 goto err;
12670
12671 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12672 if (dec_size < 0)
12673 goto err;
12674 smp_trash_alloc->data = dec_size;
12675 dec_size = smp_trash->data;
12676
12677 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12678 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12679
12680 if (ret <= 0)
12681 goto err;
12682
12683 smp->data.u.str.data = dec_size + smp_trash->data;
12684 smp->data.u.str.area = smp_trash->area;
12685 smp->data.type = SMP_T_BIN;
12686 smp->flags &= ~SMP_F_CONST;
12687 free_trash_chunk(smp_trash_alloc);
12688 return 1;
12689
12690err:
12691 free_trash_chunk(smp_trash_alloc);
12692 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012693}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012694# endif
William Lallemand32af2032016-10-29 18:09:35 +020012695
Elliot Otchet71f82972020-01-15 08:12:14 -050012696/* Argument validation functions */
12697
12698/* This function is used to validate the arguments passed to any "x_dn" ssl
12699 * keywords. These keywords support specifying a third parameter that must be
12700 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12701 */
12702int val_dnfmt(struct arg *arg, char **err_msg)
12703{
12704 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12705 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12706 return 0;
12707 }
12708 return 1;
12709}
12710
William Lallemand32af2032016-10-29 18:09:35 +020012711/* register cli keywords */
12712static struct cli_kw_list cli_kws = {{ },{
12713#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12714 { { "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 +020012715 { { "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 +020012716#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012717 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012718 { { "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 +010012719 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12720 { { "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 +010012721 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
William Lallemandd4f946c2019-12-05 10:26:40 +010012722 { { "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 +010012723 { { "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 +020012724 { { "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 Lallemanda6ffd5b2020-03-09 13:35:19 +010012725 { { "dump", "ssl", "crt-list", NULL }, "dump ssl crt-list <filename> : dump the content of a crt-list <filename>", cli_parse_dump_crtlist, cli_io_handler_dump_crtlist, NULL },
12726 { { "show", "ssl", "crt-list", NULL }, "show ssl crt-list [<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 +020012727 { { NULL }, NULL, NULL, NULL }
12728}};
12729
Willy Tarreau0108d902018-11-25 19:14:37 +010012730INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012731
Willy Tarreau7875d092012-09-10 08:20:03 +020012732/* Note: must not be declared <const> as its list will be overwritten.
12733 * Please take care of keeping this list alphabetically sorted.
12734 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012735static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012736 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012737 { "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 +010012738#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012739 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012740#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012741 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012742#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12743 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12744#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012745 { "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 +020012746 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012747 { "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 +020012748 { "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 +020012749#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012750 { "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 -040012751#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012752#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012753 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12754 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012755 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12756#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012757 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12758 { "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 +010012759 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012760 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012761 { "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 +020012762 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12763 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12764 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12765 { "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 -050012766 { "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 +020012767 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12768 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012769 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012770 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12771 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012772 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012773 { "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 +020012774 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12775 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12776 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12777 { "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 -050012778 { "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 +020012779 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012780 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012781 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012782 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012783 { "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 +010012784 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012785 { "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 +020012786 { "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 +010012787 { "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 +020012788 { "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 +010012789#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012790 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012791#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012792#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012793 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012794#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012795 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012796#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012797 { "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 -040012798#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012799 { "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 +020012800#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012801 { "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 -040012802#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012803#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012804 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12805 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012806 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12807#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012808#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012809 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012810#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012811 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12812 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12813 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12814 { "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 +020012815 { NULL, NULL, 0, 0, 0 },
12816}};
12817
Willy Tarreau0108d902018-11-25 19:14:37 +010012818INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12819
Willy Tarreau7875d092012-09-10 08:20:03 +020012820/* Note: must not be declared <const> as its list will be overwritten.
12821 * Please take care of keeping this list alphabetically sorted.
12822 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012823static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012824 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12825 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012826 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012827}};
12828
Willy Tarreau0108d902018-11-25 19:14:37 +010012829INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12830
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012831/* Note: must not be declared <const> as its list will be overwritten.
12832 * Please take care of keeping this list alphabetically sorted, doing so helps
12833 * all code contributors.
12834 * Optional keywords are also declared with a NULL ->parse() function so that
12835 * the config parser can report an appropriate error when a known keyword was
12836 * not enabled.
12837 */
William Lallemand557823f2020-04-01 17:42:47 +020012838
12839/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12840 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012841static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012842 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012843 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012844 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12845 { "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 +010012846 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012847#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012848 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12849#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012850 { "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 +010012851 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012852 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012853 { "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 +010012854 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012855 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12856 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012857 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12858 { NULL, NULL, 0 },
12859};
12860
Willy Tarreau0108d902018-11-25 19:14:37 +010012861/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12862
Willy Tarreau51fb7652012-09-18 18:24:39 +020012863static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012864 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012865 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012866 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12867 { "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 +020012868 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12869 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12870 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12871 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012872#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012873 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12874#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012875 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012876 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012877 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012878 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12879 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12880 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12881 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12882 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12883 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12884 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012885 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012886 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012887 { "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 +020012888 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12889 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12890 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12891 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012892 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012893 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12894 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012895 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12896 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012897 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12898 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12899 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12900 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12901 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012902 { NULL, NULL, 0 },
12903}};
Emeric Brun46591952012-05-18 15:47:34 +020012904
Willy Tarreau0108d902018-11-25 19:14:37 +010012905INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12906
Willy Tarreau92faadf2012-10-10 23:04:25 +020012907/* Note: must not be declared <const> as its list will be overwritten.
12908 * Please take care of keeping this list alphabetically sorted, doing so helps
12909 * all code contributors.
12910 * Optional keywords are also declared with a NULL ->parse() function so that
12911 * the config parser can report an appropriate error when a known keyword was
12912 * not enabled.
12913 */
12914static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012915 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012916 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012917 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012918 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012919 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012920 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12921 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012922#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012923 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12924#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012925 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12926 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12927 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12928 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12929 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12930 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12931 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12932 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12933 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12934 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12935 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12936 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12937 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12938 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12939 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12940 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12941 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12942 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012943 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012944 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12945 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12946 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12947 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12948 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12949 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12950 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12951 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12952 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12953 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012954 { NULL, NULL, 0, 0 },
12955}};
12956
Willy Tarreau0108d902018-11-25 19:14:37 +010012957INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12958
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012959static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012960 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12961 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012962 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012963 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012964 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12965 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012966#ifndef OPENSSL_NO_DH
12967 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12968#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012969 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012970#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012971 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012972#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010012973 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12974#ifndef OPENSSL_NO_DH
12975 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12976#endif
12977 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12978 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12979 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12980 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012981 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012982 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
12983 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012984#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012985 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
12986 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
12987#endif
William Lallemand3af48e72020-02-03 17:15:52 +010012988 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012989 { 0, NULL, NULL },
12990}};
12991
Willy Tarreau0108d902018-11-25 19:14:37 +010012992INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
12993
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012994/* Note: must not be declared <const> as its list will be overwritten */
12995static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020012996#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012997 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
12998#endif
12999 { NULL, NULL, 0, 0, 0 },
13000}};
13001
13002INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
13003
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020013004/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010013005static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020013006 .snd_buf = ssl_sock_from_buf,
13007 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010013008 .subscribe = ssl_subscribe,
13009 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020013010 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020013011 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020013012 .rcv_pipe = NULL,
13013 .snd_pipe = NULL,
13014 .shutr = NULL,
13015 .shutw = ssl_sock_shutw,
13016 .close = ssl_sock_close,
13017 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010013018 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010013019 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010013020 .prepare_srv = ssl_sock_prepare_srv_ctx,
13021 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013022 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013023 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013024};
13025
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013026enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13027 struct session *sess, struct stream *s, int flags)
13028{
13029 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013030 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013031
13032 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013033 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013034
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013035 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013036 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013037 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013038 s->req.flags |= CF_READ_NULL;
13039 return ACT_RET_YIELD;
13040 }
13041 }
13042 return (ACT_RET_CONT);
13043}
13044
13045static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13046{
13047 rule->action_ptr = ssl_action_wait_for_hs;
13048
13049 return ACT_RET_PRS_OK;
13050}
13051
13052static struct action_kw_list http_req_actions = {ILH, {
13053 { "wait-for-handshake", ssl_parse_wait_for_hs },
13054 { /* END */ }
13055}};
13056
Willy Tarreau0108d902018-11-25 19:14:37 +010013057INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13058
Willy Tarreau5db847a2019-05-09 14:13:35 +020013059#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013060
13061static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13062{
13063 if (ptr) {
13064 chunk_destroy(ptr);
13065 free(ptr);
13066 }
13067}
13068
13069#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013070static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13071{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013072 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013073}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013074
Emeric Brun46591952012-05-18 15:47:34 +020013075__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013076static void __ssl_sock_init(void)
13077{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013078#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013079 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013080 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013081#endif
Emeric Brun46591952012-05-18 15:47:34 +020013082
Willy Tarreauef934602016-12-22 23:12:01 +010013083 if (global_ssl.listen_default_ciphers)
13084 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13085 if (global_ssl.connect_default_ciphers)
13086 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013087#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013088 if (global_ssl.listen_default_ciphersuites)
13089 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13090 if (global_ssl.connect_default_ciphersuites)
13091 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13092#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013093
Willy Tarreau13e14102016-12-22 20:25:26 +010013094 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013095#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013096 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013097#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013098#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013099 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013100 n = sk_SSL_COMP_num(cm);
13101 while (n--) {
13102 (void) sk_SSL_COMP_pop(cm);
13103 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013104#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013105
Willy Tarreau5db847a2019-05-09 14:13:35 +020013106#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013107 ssl_locking_init();
13108#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013109#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013110 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13111#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013112 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013113 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 +020013114#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013115 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013116 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013117#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013118#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13119 hap_register_post_check(tlskeys_finalize_config);
13120#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013121
13122 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13123 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13124
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013125 hap_register_post_deinit(ssl_free_global_issuers);
13126
Willy Tarreau80713382018-11-26 10:19:54 +010013127#ifndef OPENSSL_NO_DH
13128 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13129 hap_register_post_deinit(ssl_free_dh);
13130#endif
13131#ifndef OPENSSL_NO_ENGINE
13132 hap_register_post_deinit(ssl_free_engines);
13133#endif
13134 /* Load SSL string for the verbose & debug mode. */
13135 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013136 ha_meth = BIO_meth_new(0x666, "ha methods");
13137 BIO_meth_set_write(ha_meth, ha_ssl_write);
13138 BIO_meth_set_read(ha_meth, ha_ssl_read);
13139 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13140 BIO_meth_set_create(ha_meth, ha_ssl_new);
13141 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13142 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13143 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013144
13145 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013146}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013147
Willy Tarreau80713382018-11-26 10:19:54 +010013148/* Compute and register the version string */
13149static void ssl_register_build_options()
13150{
13151 char *ptr = NULL;
13152 int i;
13153
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013154 memprintf(&ptr, "Built with OpenSSL version : "
13155#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013156 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013157#else /* OPENSSL_IS_BORINGSSL */
13158 OPENSSL_VERSION_TEXT
13159 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013160 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013161 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013162#endif
13163 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013164#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013165 "no (library version too old)"
13166#elif defined(OPENSSL_NO_TLSEXT)
13167 "no (disabled via OPENSSL_NO_TLSEXT)"
13168#else
13169 "yes"
13170#endif
13171 "", ptr);
13172
13173 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13174#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13175 "yes"
13176#else
13177#ifdef OPENSSL_NO_TLSEXT
13178 "no (because of OPENSSL_NO_TLSEXT)"
13179#else
13180 "no (version might be too old, 0.9.8f min needed)"
13181#endif
13182#endif
13183 "", ptr);
13184
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013185 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13186 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13187 if (methodVersions[i].option)
13188 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013189
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013190 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013191}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013192
Willy Tarreau80713382018-11-26 10:19:54 +010013193INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013194
Emeric Brun46591952012-05-18 15:47:34 +020013195
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013196#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013197void ssl_free_engines(void) {
13198 struct ssl_engine_list *wl, *wlb;
13199 /* free up engine list */
13200 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13201 ENGINE_finish(wl->e);
13202 ENGINE_free(wl->e);
13203 LIST_DEL(&wl->list);
13204 free(wl);
13205 }
13206}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013207#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013208
Remi Gacogned3a23c32015-05-28 16:39:47 +020013209#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013210void ssl_free_dh(void) {
13211 if (local_dh_1024) {
13212 DH_free(local_dh_1024);
13213 local_dh_1024 = NULL;
13214 }
13215 if (local_dh_2048) {
13216 DH_free(local_dh_2048);
13217 local_dh_2048 = NULL;
13218 }
13219 if (local_dh_4096) {
13220 DH_free(local_dh_4096);
13221 local_dh_4096 = NULL;
13222 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013223 if (global_dh) {
13224 DH_free(global_dh);
13225 global_dh = NULL;
13226 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013227}
13228#endif
13229
13230__attribute__((destructor))
13231static void __ssl_sock_deinit(void)
13232{
13233#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013234 if (ssl_ctx_lru_tree) {
13235 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013236 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013237 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013238#endif
13239
Willy Tarreau5db847a2019-05-09 14:13:35 +020013240#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013241 ERR_remove_state(0);
13242 ERR_free_strings();
13243
13244 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013245#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013246
Willy Tarreau5db847a2019-05-09 14:13:35 +020013247#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013248 CRYPTO_cleanup_all_ex_data();
13249#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013250 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013251}
13252
13253
Emeric Brun46591952012-05-18 15:47:34 +020013254/*
13255 * Local variables:
13256 * c-indent-level: 8
13257 * c-basic-offset: 8
13258 * End:
13259 */