blob: 9b44e9d0b91b8b3d55ad36f5ae28ed2e3c692f38 [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 Hocdet75a7aa12020-02-18 15:19:24 +0100164static struct issuer_chain* ssl_get_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;
3632 issuer = ssl_get_issuer_chain(ckch->cert);
3633 if (issuer)
3634 find_chain = issuer->chain;
3635 }
William Lallemand85888572020-02-27 14:48:35 +01003636
3637 /* If we didn't find a chain we *MUST* use an empty X509 structure */
3638 if (find_chain == NULL)
3639 find_chain = sk_X509_new_null();
3640
yanbzhu488a4d22015-12-01 15:16:07 -05003641 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003642#ifdef SSL_CTX_set1_chain
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003643 if (!SSL_CTX_set1_chain(ctx, find_chain)) {
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003644 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3645 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003646 errcode |= ERR_ALERT | ERR_FATAL;
3647 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003648 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003649#else
3650 { /* legacy compat (< openssl 1.0.2) */
3651 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003652 STACK_OF(X509) *chain;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003653 chain = X509_chain_up_ref(find_chain);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003654 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003655 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3656 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3657 err && *err ? *err : "", path);
3658 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003659 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003660 errcode |= ERR_ALERT | ERR_FATAL;
3661 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003662 }
3663 }
3664#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003665
William Lallemandfa892222019-07-23 16:06:08 +02003666#ifndef OPENSSL_NO_DH
3667 /* store a NULL pointer to indicate we have not yet loaded
3668 a custom DH param file */
3669 if (ssl_dh_ptr_index >= 0) {
3670 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3671 }
3672
Emeric Brun7a883362019-10-17 13:27:40 +02003673 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3674 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003675 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3676 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003677 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003678 }
3679#endif
3680
William Lallemanda17f4112019-10-10 15:16:44 +02003681#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3682 if (sctl_ex_index >= 0 && ckch->sctl) {
3683 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3684 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003685 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003686 errcode |= ERR_ALERT | ERR_FATAL;
3687 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003688 }
3689 }
3690#endif
3691
William Lallemand4a660132019-10-14 14:51:41 +02003692#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003693 /* Load OCSP Info into context */
3694 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003695 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003696 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",
3697 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003698 errcode |= ERR_ALERT | ERR_FATAL;
3699 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003700 }
3701 }
William Lallemand246c0242019-10-11 08:59:13 +02003702#endif
3703
Emeric Bruna96b5822019-10-17 13:25:14 +02003704 end:
3705 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003706}
3707
William Lallemandc4ecddf2019-07-31 16:50:08 +02003708#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003709
William Lallemand28a8fce2019-10-04 17:36:55 +02003710static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003711{
3712 struct sni_keytype *s_kt = NULL;
3713 struct ebmb_node *node;
3714 int i;
3715
3716 for (i = 0; i < trash.size; i++) {
3717 if (!str[i])
3718 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003719 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003720 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003721 trash.area[i] = 0;
3722 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003723 if (!node) {
3724 /* CN not found in tree */
3725 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3726 /* Using memcpy here instead of strncpy.
3727 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3728 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3729 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003730 if (!s_kt)
3731 return -1;
3732
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003733 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003734 s_kt->keytypes = 0;
3735 ebst_insert(sni_keytypes, &s_kt->name);
3736 } else {
3737 /* CN found in tree */
3738 s_kt = container_of(node, struct sni_keytype, name);
3739 }
3740
3741 /* Mark that this CN has the keytype of key_index via keytypes mask */
3742 s_kt->keytypes |= 1<<key_index;
3743
William Lallemand28a8fce2019-10-04 17:36:55 +02003744 return 0;
3745
yanbzhu08ce6ab2015-12-02 13:01:29 -05003746}
3747
William Lallemandc4ecddf2019-07-31 16:50:08 +02003748#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003749/*
3750 * Free a ckch_store and its ckch(s)
3751 * The linked ckch_inst are not free'd
3752 */
3753void ckchs_free(struct ckch_store *ckchs)
3754{
3755 if (!ckchs)
3756 return;
3757
3758#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3759 if (ckchs->multi) {
3760 int n;
3761
3762 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3763 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3764 } else
3765#endif
3766 {
3767 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3768 ckchs->ckch = NULL;
3769 }
3770
3771 free(ckchs);
3772}
3773
3774/* allocate and duplicate a ckch_store
3775 * Return a new ckch_store or NULL */
3776static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3777{
3778 struct ckch_store *dst;
3779 int pathlen;
3780
3781 pathlen = strlen(src->path);
3782 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3783 if (!dst)
3784 return NULL;
3785 /* copy previous key */
3786 memcpy(dst->path, src->path, pathlen + 1);
3787 dst->multi = src->multi;
3788 LIST_INIT(&dst->ckch_inst);
3789
3790 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3791 if (!dst->ckch)
3792 goto error;
3793
3794#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3795 if (src->multi) {
3796 int n;
3797
3798 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3799 if (&src->ckch[n]) {
3800 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3801 goto error;
3802 }
3803 }
3804 } else
3805#endif
3806 {
3807 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3808 goto error;
3809 }
3810
3811 return dst;
3812
3813error:
3814 ckchs_free(dst);
3815
3816 return NULL;
3817}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003818
William Lallemand36b84632019-07-18 19:28:17 +02003819/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003820 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003821 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003822static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003823{
3824 struct ebmb_node *eb;
3825
William Lallemande3af8fb2019-10-08 11:36:53 +02003826 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003827 if (!eb)
3828 return NULL;
3829
William Lallemande3af8fb2019-10-08 11:36:53 +02003830 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003831}
3832
3833/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003834 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003835 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003836static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003837{
William Lallemande3af8fb2019-10-08 11:36:53 +02003838 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003839
William Lallemande3af8fb2019-10-08 11:36:53 +02003840 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3841 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003842 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3843 goto end;
3844 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003845 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003846
William Lallemande3af8fb2019-10-08 11:36:53 +02003847 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003848 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3849 goto end;
3850 }
3851
William Lallemand9117de92019-10-04 00:29:42 +02003852 LIST_INIT(&ckchs->ckch_inst);
3853
William Lallemand36b84632019-07-18 19:28:17 +02003854 if (!multi) {
3855
William Lallemand96a9c972019-10-17 11:56:17 +02003856 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003857 goto end;
3858
William Lallemande3af8fb2019-10-08 11:36:53 +02003859 /* insert into the ckchs tree */
3860 memcpy(ckchs->path, path, strlen(path) + 1);
3861 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003862 } else {
3863 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003864#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3865 char fp[MAXPATHLEN+1] = {0};
3866 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003867
3868 /* Load all possible certs and keys */
3869 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3870 struct stat buf;
3871 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3872 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003873 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003874 goto end;
3875 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003876 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003877 }
3878 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003879#endif
William Lallemand36b84632019-07-18 19:28:17 +02003880
3881 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003882 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003883 goto end;
3884 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003885 /* insert into the ckchs tree */
3886 memcpy(ckchs->path, path, strlen(path) + 1);
3887 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003888 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003889 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003890
William Lallemand36b84632019-07-18 19:28:17 +02003891end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003892 if (ckchs) {
3893 free(ckchs->ckch);
3894 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003895 }
3896
William Lallemande3af8fb2019-10-08 11:36:53 +02003897 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003898
3899 return NULL;
3900}
3901
William Lallemand38df1c82019-12-04 15:39:35 +01003902
3903/*
3904 * Free a sni filters array generated by ckch_inst_sni_ctx_to_sni_filters()
3905 */
3906static inline void free_sni_filters(char **sni_filter, int fcount)
3907{
3908 int i;
3909
3910 if (sni_filter) {
3911 for (i = 0; i < fcount; i++) {
3912 if (sni_filter[i]) {
3913 free(sni_filter[i]);
3914 sni_filter[i] = NULL;
3915 }
3916 }
3917 free(sni_filter);
3918 }
3919}
3920
3921/*
3922 * Fill <*sni_filter> with an allocated array of ptr to the existing filters,
3923 * The caller should free <*sni_filter>.
3924 * Fill <*fcount> with the number of filters
3925 * Return an ERR_* code.
3926 */
3927static int ckch_inst_sni_ctx_to_sni_filters(const struct ckch_inst *ckchi, char ***sni_filter, int *fcount, char **err)
3928{
3929 struct sni_ctx *sc0;
3930 int errcode = 0;
3931 int i = 0;
3932 char **tmp_filter;
3933 int tmp_fcount = 0;
3934
3935 list_for_each_entry(sc0, &ckchi->sni_ctx, by_ckch_inst) {
3936 tmp_fcount++;
3937 }
3938
3939 if (!tmp_fcount)
3940 goto end;
3941
Tim Duesterhus8c120252020-03-19 16:12:09 +01003942 tmp_filter = calloc(tmp_fcount, sizeof(*tmp_filter));
William Lallemand38df1c82019-12-04 15:39:35 +01003943 if (!tmp_filter) {
3944 errcode |= ERR_FATAL|ERR_ALERT;
3945 goto error;
3946 }
3947
3948 list_for_each_entry(sc0, &ckchi->sni_ctx, by_ckch_inst) {
3949 size_t len = strlen((char *)sc0->name.key);
3950
3951 /* we need to alloc and copy to insert a '!' or/and a '*' */
3952 tmp_filter[i] = calloc(1, len + sc0->neg + sc0->wild + 1);
3953 if (!tmp_filter[i]) {
3954 errcode |= ERR_FATAL|ERR_ALERT;
3955 goto error;
3956 }
3957
3958 if (sc0->neg)
3959 *tmp_filter[i] = '!';
3960 if (sc0->wild)
3961 *(tmp_filter[i] + sc0->neg) = '*';
3962
3963 memcpy(tmp_filter[i] + sc0->neg + sc0->wild, (char *)sc0->name.key, len + 1);
3964 i++;
3965 }
William Lallemand38df1c82019-12-04 15:39:35 +01003966 *sni_filter = tmp_filter;
Willy Tarreaud04a2a62020-03-05 16:26:12 +01003967end:
William Lallemand38df1c82019-12-04 15:39:35 +01003968 *fcount = tmp_fcount;
3969
3970 return errcode;
3971error:
3972 memprintf(err, "%sUnable to generate filters!",
3973 err && *err ? *err : "");
3974 free_sni_filters(tmp_filter, tmp_fcount);
3975
3976 return errcode;
3977}
3978
3979
William Lallemandc4ecddf2019-07-31 16:50:08 +02003980#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3981
William Lallemand36b84632019-07-18 19:28:17 +02003982/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003983 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003984 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003985 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3986 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003987 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003988 *
Emeric Brun054563d2019-10-17 13:16:58 +02003989 * Returns a bitfield containing the flags:
3990 * ERR_FATAL in any fatal error case
3991 * ERR_ALERT if the reason of the error is available in err
3992 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003993 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003994 */
Emeric Brun054563d2019-10-17 13:16:58 +02003995static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3996 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3997 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003998{
William Lallemand36b84632019-07-18 19:28:17 +02003999 int i = 0, n = 0;
4000 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02004001 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004002 struct ebmb_node *node;
4003 struct ebmb_node *next;
4004 /* Array of SSL_CTX pointers corresponding to each possible combo
4005 * of keytypes
4006 */
4007 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02004008 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004009 X509_NAME *xname = NULL;
4010 char *str = NULL;
4011#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4012 STACK_OF(GENERAL_NAME) *names = NULL;
4013#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02004014 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004015
Emeric Brun054563d2019-10-17 13:16:58 +02004016 *ckchi = NULL;
4017
William Lallemande3af8fb2019-10-08 11:36:53 +02004018 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02004019 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4020 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004021 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004022 }
4023
4024 ckch_inst = ckch_inst_new();
4025 if (!ckch_inst) {
4026 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4027 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004028 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004029 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004030 }
4031
William Lallemande3af8fb2019-10-08 11:36:53 +02004032 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004033
yanbzhu08ce6ab2015-12-02 13:01:29 -05004034 /* Process each ckch and update keytypes for each CN/SAN
4035 * for example, if CN/SAN www.a.com is associated with
4036 * certs with keytype 0 and 2, then at the end of the loop,
4037 * www.a.com will have:
4038 * keyindex = 0 | 1 | 4 = 5
4039 */
4040 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004041 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004042
4043 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4044 continue;
4045
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004046 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004047 for (i = 0; i < fcount; i++) {
4048 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4049 if (ret < 0) {
4050 memprintf(err, "%sunable to allocate SSL context.\n",
4051 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004052 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004053 goto end;
4054 }
4055 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004056 } else {
4057 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4058 * so the line that contains logic is marked via comments
4059 */
4060 xname = X509_get_subject_name(certs_and_keys[n].cert);
4061 i = -1;
4062 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4063 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004064 ASN1_STRING *value;
4065 value = X509_NAME_ENTRY_get_data(entry);
4066 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004067 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004068 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004069
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004070 OPENSSL_free(str);
4071 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004072 if (ret < 0) {
4073 memprintf(err, "%sunable to allocate SSL context.\n",
4074 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004075 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004076 goto end;
4077 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004078 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004079 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004080
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004081 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004082#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004083 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4084 if (names) {
4085 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4086 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004087
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004088 if (name->type == GEN_DNS) {
4089 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4090 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004091 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004092
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004093 OPENSSL_free(str);
4094 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004095 if (ret < 0) {
4096 memprintf(err, "%sunable to allocate SSL context.\n",
4097 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004098 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004099 goto end;
4100 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004101 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004102 }
4103 }
4104 }
4105 }
4106#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4107 }
4108
4109 /* If no files found, return error */
4110 if (eb_is_empty(&sni_keytypes_map)) {
4111 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4112 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004113 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004114 goto end;
4115 }
4116
4117 /* We now have a map of CN/SAN to keytypes that are loaded in
4118 * Iterate through the map to create the SSL_CTX's (if needed)
4119 * and add each CTX to the SNI tree
4120 *
4121 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004122 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004123 * combination is denoted by the key in the map. Each key
4124 * has a value between 1 and 2^n - 1. Conveniently, the array
4125 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4126 * entry in the array to correspond to the unique combo (key)
4127 * associated with i. This unique key combo (i) will be associated
4128 * with combos[i-1]
4129 */
4130
4131 node = ebmb_first(&sni_keytypes_map);
4132 while (node) {
4133 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004134 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004135 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004136
4137 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4138 i = container_of(node, struct sni_keytype, name)->keytypes;
4139 cur_ctx = key_combos[i-1].ctx;
4140
4141 if (cur_ctx == NULL) {
4142 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004143 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004144 if (cur_ctx == NULL) {
4145 memprintf(err, "%sunable to allocate SSL context.\n",
4146 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004147 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004148 goto end;
4149 }
4150
yanbzhube2774d2015-12-10 15:07:30 -05004151 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004152 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4153 if (i & (1<<n)) {
4154 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004155 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004156 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4157 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004158 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004159 }
4160 }
4161
yanbzhu08ce6ab2015-12-02 13:01:29 -05004162 /* Update key_combos */
4163 key_combos[i-1].ctx = cur_ctx;
4164 }
4165
4166 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004167
William Lallemand1d29c742019-10-04 00:53:29 +02004168 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 +02004169 kinfo, str, key_combos[i-1].order);
4170 if (key_combos[i-1].order < 0) {
4171 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004172 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004173 goto end;
4174 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004175 node = ebmb_next(node);
4176 }
4177
4178
4179 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4180 if (!bind_conf->default_ctx) {
4181 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4182 if (key_combos[i].ctx) {
4183 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004184 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004185 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004186 break;
4187 }
4188 }
4189 }
4190
William Lallemand614ca0d2019-10-07 13:52:11 +02004191 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004192 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004193 ckch_inst->ckch_store = ckchs;
William Lallemand67630162020-03-09 16:56:39 +01004194 ckch_inst->filters = !!fcount;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004195end:
4196
4197 if (names)
4198 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4199
yanbzhu08ce6ab2015-12-02 13:01:29 -05004200 node = ebmb_first(&sni_keytypes_map);
4201 while (node) {
4202 next = ebmb_next(node);
4203 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004204 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004205 node = next;
4206 }
4207
Emeric Brun054563d2019-10-17 13:16:58 +02004208 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004209 struct sni_ctx *sc0, *sc0b;
4210
4211 /* free the SSL_CTX in case of error */
4212 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4213 if (key_combos[i].ctx)
4214 SSL_CTX_free(key_combos[i].ctx);
4215 }
4216
4217 /* free the sni_ctx in case of error */
4218 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4219
4220 ebmb_delete(&sc0->name);
4221 LIST_DEL(&sc0->by_ckch_inst);
4222 free(sc0);
4223 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004224 free(ckch_inst);
4225 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004226 }
4227
Emeric Brun054563d2019-10-17 13:16:58 +02004228 *ckchi = ckch_inst;
4229 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004230}
4231#else
4232/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004233static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4234 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4235 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004236{
4237 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4238 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004239 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004240}
4241
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004242#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004243
William Lallemand614ca0d2019-10-07 13:52:11 +02004244/*
4245 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004246 *
4247 * Returns a bitfield containing the flags:
4248 * ERR_FATAL in any fatal error case
4249 * ERR_ALERT if the reason of the error is available in err
4250 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004251 */
Emeric Brun054563d2019-10-17 13:16:58 +02004252static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4253 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004254{
William Lallemandc9402072019-05-15 15:33:54 +02004255 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004256 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004257 int order = 0;
4258 X509_NAME *xname;
4259 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004260 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004261 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004262#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4263 STACK_OF(GENERAL_NAME) *names;
4264#endif
William Lallemand36b84632019-07-18 19:28:17 +02004265 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004266 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004267 int errcode = 0;
4268
4269 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004270
William Lallemande3af8fb2019-10-08 11:36:53 +02004271 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004272 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004273
William Lallemande3af8fb2019-10-08 11:36:53 +02004274 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004275
William Lallemandc9402072019-05-15 15:33:54 +02004276 ctx = SSL_CTX_new(SSLv23_server_method());
4277 if (!ctx) {
4278 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4279 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004280 errcode |= ERR_ALERT | ERR_FATAL;
4281 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004282 }
4283
Emeric Bruna96b5822019-10-17 13:25:14 +02004284 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4285 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004286 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004287
4288 ckch_inst = ckch_inst_new();
4289 if (!ckch_inst) {
4290 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4291 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004292 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004293 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004294 }
4295
William Lallemand36b84632019-07-18 19:28:17 +02004296 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004297 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004298 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004299 switch(EVP_PKEY_base_id(pkey)) {
4300 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004301 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004302 break;
4303 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004304 kinfo.sig = TLSEXT_signature_ecdsa;
4305 break;
4306 case EVP_PKEY_DSA:
4307 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004308 break;
4309 }
4310 EVP_PKEY_free(pkey);
4311 }
4312
Emeric Brun50bcecc2013-04-22 13:05:23 +02004313 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004314 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004315 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 +02004316 if (order < 0) {
4317 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004318 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004319 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004320 }
4321 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004322 }
4323 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004324#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004325 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004326 if (names) {
4327 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4328 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4329 if (name->type == GEN_DNS) {
4330 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004331 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004332 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004333 if (order < 0) {
4334 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004335 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004336 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004337 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004338 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004339 }
4340 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004341 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004342 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004343#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004344 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004345 i = -1;
4346 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4347 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004348 ASN1_STRING *value;
4349
4350 value = X509_NAME_ENTRY_get_data(entry);
4351 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004352 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004353 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004354 if (order < 0) {
4355 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004356 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004357 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004358 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004359 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004360 }
4361 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004362 /* we must not free the SSL_CTX anymore below, since it's already in
4363 * the tree, so it will be discovered and cleaned in time.
4364 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004365
Emeric Brunfc0421f2012-09-07 17:30:07 +02004366#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004367 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004368 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4369 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004370 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004371 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004372 }
4373#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004374 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004375 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004376 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004377 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004378 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004379
William Lallemand9117de92019-10-04 00:29:42 +02004380 /* everything succeed, the ckch instance can be used */
4381 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004382 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004383 ckch_inst->ckch_store = ckchs;
William Lallemand67630162020-03-09 16:56:39 +01004384 ckch_inst->filters = !!fcount;
William Lallemand9117de92019-10-04 00:29:42 +02004385
Emeric Brun054563d2019-10-17 13:16:58 +02004386 *ckchi = ckch_inst;
4387 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004388
4389error:
4390 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004391 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004392 struct sni_ctx *sc0, *sc0b;
4393
4394 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4395
4396 ebmb_delete(&sc0->name);
4397 LIST_DEL(&sc0->by_ckch_inst);
4398 free(sc0);
4399 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004400 free(ckch_inst);
4401 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004402 }
4403 /* We only created 1 SSL_CTX so we can free it there */
4404 SSL_CTX_free(ctx);
4405
Emeric Brun054563d2019-10-17 13:16:58 +02004406 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004407}
4408
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004409/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004410static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4411 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004412 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004413{
Emeric Brun054563d2019-10-17 13:16:58 +02004414 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004415
4416 /* we found the ckchs in the tree, we can use it directly */
4417 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004418 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 +02004419 else
William Lallemand24bde432020-03-09 16:48:43 +01004420 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 +02004421
Emeric Brun054563d2019-10-17 13:16:58 +02004422 if (errcode & ERR_CODE)
4423 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004424
William Lallemand24bde432020-03-09 16:48:43 +01004425 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004426
4427 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004428 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004429 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004430}
4431
William Lallemand6be66ec2020-03-06 22:26:32 +01004432
4433/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4434 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4435 *
4436 * This function tries to open and store certificate files.
4437 */
4438static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004439{
William Lallemand6be66ec2020-03-06 22:26:32 +01004440 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004441 struct dirent **de_list;
4442 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004443 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004444 char *end;
4445 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004446 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004447 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004448#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004449 int is_bundle;
4450 int j;
4451#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004452
William Lallemand2d232c22020-03-06 22:12:35 +01004453 /* strip trailing slashes, including first one */
4454 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4455 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004456
William Lallemand6be66ec2020-03-06 22:26:32 +01004457 dir = malloc(sizeof(*dir) + strlen(path) + 1);
4458 if (dir == NULL) {
4459 memprintf(err, "not enough memory");
4460 return ERR_ALERT | ERR_FATAL;
4461 }
4462 memcpy(dir->node.key, path, strlen(path) + 1);
4463 dir->entries = EB_ROOT_UNIQUE; /* it's a directory, files are unique */
William Lallemand83918e22020-03-16 17:21:51 +01004464 LIST_INIT(&dir->ord_entries);
William Lallemand6be66ec2020-03-06 22:26:32 +01004465
William Lallemand2d232c22020-03-06 22:12:35 +01004466 n = scandir(path, &de_list, 0, alphasort);
4467 if (n < 0) {
4468 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4469 err && *err ? *err : "", path, strerror(errno));
4470 cfgerr |= ERR_ALERT | ERR_FATAL;
4471 }
4472 else {
4473 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004474 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004475 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004476
William Lallemand2d232c22020-03-06 22:12:35 +01004477 end = strrchr(de->d_name, '.');
4478 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4479 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004480
William Lallemanda64593c2020-03-17 20:11:41 +01004481 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4482 if (stat(fp, &buf) != 0) {
4483 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4484 err && *err ? *err : "", fp, strerror(errno));
4485 cfgerr |= ERR_ALERT | ERR_FATAL;
4486 goto ignore_entry;
4487 }
4488 if (!S_ISREG(buf.st_mode))
4489 goto ignore_entry;
4490
William Lallemand6be66ec2020-03-06 22:26:32 +01004491 entry = malloc(sizeof(*entry));
4492 if (entry == NULL) {
4493 memprintf(err, "not enough memory '%s'", fp);
4494 cfgerr |= ERR_ALERT | ERR_FATAL;
4495 goto ignore_entry;
4496 }
4497
William Lallemand83918e22020-03-16 17:21:51 +01004498 /* directories don't use ssl_conf and filters */
4499 entry->fcount = 0;
4500 entry->filters = NULL;
4501 entry->ssl_conf = NULL;
4502
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004503#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004504 is_bundle = 0;
4505 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004506
William Lallemand2d232c22020-03-06 22:12:35 +01004507 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4508 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4509 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4510 is_bundle = 1;
4511 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004512 }
William Lallemand2d232c22020-03-06 22:12:35 +01004513 }
yanbzhu63ea8462015-12-09 13:35:14 -05004514
William Lallemand2d232c22020-03-06 22:12:35 +01004515 if (is_bundle) {
4516 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004517
William Lallemand2d232c22020-03-06 22:12:35 +01004518 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004519
William Lallemand2d232c22020-03-06 22:12:35 +01004520 /* increment i and free de until we get to a non-bundle cert
4521 * Note here that we look at de_list[i + 1] before freeing de
4522 * this is important since ignore_entry will free de. This also
4523 * guarantees that de->d_name continues to hold the same prefix.
4524 */
4525 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4526 free(de);
4527 i++;
4528 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004529 }
William Lallemand2d232c22020-03-06 22:12:35 +01004530
4531 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004532 ckchs = ckchs_lookup(fp);
4533 if (ckchs == NULL)
4534 ckchs = ckchs_load_cert_file(fp, 1, err);
4535 if (ckchs == NULL) {
4536 free(de);
4537 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004538 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004539 goto end;
4540 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004541 entry->node.key = ckchs;
William Lallemand83918e22020-03-16 17:21:51 +01004542 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004543 ebpt_insert(&dir->entries, &entry->node);
4544
William Lallemand2d232c22020-03-06 22:12:35 +01004545 /* Successfully processed the bundle */
4546 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004547 }
William Lallemand2d232c22020-03-06 22:12:35 +01004548 }
yanbzhu63ea8462015-12-09 13:35:14 -05004549
4550#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004551 ckchs = ckchs_lookup(fp);
4552 if (ckchs == NULL)
4553 ckchs = ckchs_load_cert_file(fp, 0, err);
4554 if (ckchs == NULL) {
4555 free(de);
4556 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004557 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004558 goto end;
4559 }
4560 entry->node.key = ckchs;
William Lallemand83918e22020-03-16 17:21:51 +01004561 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004562 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004563
yanbzhu08ce6ab2015-12-02 13:01:29 -05004564ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004565 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004566 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004567end:
William Lallemand2d232c22020-03-06 22:12:35 +01004568 free(de_list);
4569 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004570
4571 if (cfgerr & ERR_CODE) {
4572 /* free the dir and entries on error */
4573 struct ebpt_node *node;
4574
4575 node = ebpt_first(&dir->entries);
4576 while (node) {
4577 struct crtlist_entry *entry;
4578
4579 entry = ebpt_entry(node, typeof(*entry), node);
4580 node = ebpt_next(node);
4581 ebpt_delete(&entry->node);
4582 free(entry);
4583 }
4584 free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004585 } else {
4586 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004587 }
William Lallemand2d232c22020-03-06 22:12:35 +01004588 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004589
William Lallemand2d232c22020-03-06 22:12:35 +01004590}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004591
Emeric Brunfc0421f2012-09-07 17:30:07 +02004592
Thierry Fournier383085f2013-01-24 14:15:43 +01004593/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4594 * done once. Zero is returned if the operation fails. No error is returned
4595 * if the random is said as not implemented, because we expect that openssl
4596 * will use another method once needed.
4597 */
4598static int ssl_initialize_random()
4599{
4600 unsigned char random;
4601 static int random_initialized = 0;
4602
4603 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4604 random_initialized = 1;
4605
4606 return random_initialized;
4607}
4608
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004609/* release ssl bind conf */
4610void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004611{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004612 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004613#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004614 free(conf->npn_str);
4615 conf->npn_str = NULL;
4616#endif
4617#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4618 free(conf->alpn_str);
4619 conf->alpn_str = NULL;
4620#endif
4621 free(conf->ca_file);
4622 conf->ca_file = NULL;
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004623 free(conf->ca_verify_file);
4624 conf->ca_verify_file = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004625 free(conf->crl_file);
4626 conf->crl_file = NULL;
4627 free(conf->ciphers);
4628 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004629#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004630 free(conf->ciphersuites);
4631 conf->ciphersuites = NULL;
4632#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004633 free(conf->curves);
4634 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004635 free(conf->ecdhe);
4636 conf->ecdhe = NULL;
4637 }
4638}
William Lallemand2954c472020-03-06 21:54:13 +01004639/* free sni filters */
4640static void crtlist_free_filters(char **args)
4641{
4642 int i;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004643
William Lallemand2954c472020-03-06 21:54:13 +01004644 if (!args)
4645 return;
4646
4647 for (i = 0; args[i]; i++)
4648 free(args[i]);
4649
4650 free(args);
4651}
4652
4653/* Alloc and duplicate a char ** array */
4654static char **crtlist_dup_filters(char **args, int fcount)
4655{
4656 char **dst;
4657 int i;
4658
William Lallemand3c516fc2020-03-20 10:04:34 +01004659 if (fcount == 0)
4660 return NULL;
4661
Tim Duesterhus2445f8d2020-03-19 16:12:10 +01004662 dst = calloc(fcount + 1, sizeof(*dst));
William Lallemand2954c472020-03-06 21:54:13 +01004663 if (!dst)
4664 return NULL;
4665
4666 for (i = 0; i < fcount; i++) {
4667 dst[i] = strdup(args[i]);
4668 if (!dst[i])
4669 goto error;
4670 }
4671 return dst;
4672
4673error:
4674 crtlist_free_filters(dst);
4675 return NULL;
4676}
4677
4678
4679/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
4680static void crtlist_free(struct crtlist *crtlist)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004681{
William Lallemand2954c472020-03-06 21:54:13 +01004682 struct crtlist_entry *entry, *s_entry;
4683
4684 if (crtlist == NULL)
4685 return;
4686
4687 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
4688 ebpt_delete(&entry->node);
4689 LIST_DEL(&entry->by_crtlist);
4690 crtlist_free_filters(entry->filters);
4691 ssl_sock_free_ssl_conf(entry->ssl_conf);
4692 free(entry->ssl_conf);
4693 free(entry);
4694 }
4695 ebmb_delete(&crtlist->node);
4696 free(crtlist);
4697}
4698
4699/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4700 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4701 *
4702 * This function tries to open and store certificate files.
4703 */
4704static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4705{
4706 struct crtlist *newlist;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004707 char thisline[CRT_LINESIZE];
4708 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004709 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004710 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004711 int linenum = 0;
4712 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004713
Willy Tarreauad1731d2013-04-02 17:35:58 +02004714 if ((f = fopen(file, "r")) == NULL) {
4715 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004716 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004717 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004718
William Lallemand2954c472020-03-06 21:54:13 +01004719 newlist = malloc(sizeof(*newlist) + strlen(file) + 1);
4720 if (newlist == NULL) {
4721 memprintf(err, "Not enough memory!");
4722 cfgerr |= ERR_ALERT | ERR_FATAL;
4723 goto error;
4724 }
4725 memcpy(newlist->node.key, file, strlen(file) + 1);
4726 newlist->entries = EB_ROOT;
4727 LIST_INIT(&newlist->ord_entries);
4728
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004729 while (fgets(thisline, sizeof(thisline), f) != NULL) {
William Lallemand2954c472020-03-06 21:54:13 +01004730 struct crtlist_entry *entry;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004731 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004732 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004733 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004734 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004735 char *crt_path;
4736 struct ssl_bind_conf *ssl_conf = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004737 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004738
4739 linenum++;
4740 end = line + strlen(line);
4741 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4742 /* Check if we reached the limit and the last char is not \n.
4743 * Watch out for the last line without the terminating '\n'!
4744 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004745 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4746 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004747 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004748 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004749 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004750 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004751 newarg = 1;
4752 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004753 if (*line == '#' || *line == '\n' || *line == '\r') {
4754 /* end of string, end of loop */
4755 *line = 0;
4756 break;
Willy Tarreauded15b72020-02-25 07:51:59 +01004757 } else if (isspace((unsigned char)*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004758 newarg = 1;
4759 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004760 } else if (*line == '[') {
4761 if (ssl_b) {
4762 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004763 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004764 break;
4765 }
4766 if (!arg) {
4767 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004768 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004769 break;
4770 }
4771 ssl_b = arg;
4772 newarg = 1;
4773 *line = 0;
4774 } else if (*line == ']') {
4775 if (ssl_e) {
4776 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004777 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004778 break;
4779 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004780 if (!ssl_b) {
4781 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004782 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004783 break;
4784 }
4785 ssl_e = arg;
4786 newarg = 1;
4787 *line = 0;
4788 } else if (newarg) {
4789 if (arg == MAX_CRT_ARGS) {
4790 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004791 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004792 break;
4793 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004794 newarg = 0;
4795 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004796 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004797 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004798 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004799 if (cfgerr)
4800 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004801 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004802
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004803 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004804 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004805 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004806
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004807 crt_path = args[0];
4808 if (*crt_path != '/' && global_ssl.crt_base) {
4809 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4810 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4811 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004812 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004813 break;
4814 }
4815 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4816 crt_path = path;
4817 }
4818
4819 ssl_conf = calloc(1, sizeof *ssl_conf);
4820 cur_arg = ssl_b ? ssl_b : 1;
4821 while (cur_arg < ssl_e) {
4822 newarg = 0;
4823 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4824 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4825 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004826 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004827 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4828 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4829 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004830 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004831 }
4832 cur_arg += 1 + ssl_bind_kws[i].skip;
4833 break;
4834 }
4835 }
4836 if (!cfgerr && !newarg) {
4837 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4838 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004839 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004840 break;
4841 }
4842 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004843
William Lallemand2954c472020-03-06 21:54:13 +01004844 /* Look for a ckch_store or create one */
4845 ckchs = ckchs_lookup(crt_path);
4846 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004847 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004848 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004849 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004850 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004851 }
William Lallemand909086e2020-03-17 16:53:27 +01004852 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004853 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004854
William Lallemand2954c472020-03-06 21:54:13 +01004855 entry = malloc(sizeof(*entry));
4856 if (entry == NULL) {
4857 memprintf(err, "Not enough memory!");
Willy Tarreaubbc91962019-10-16 16:42:19 +02004858 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand2954c472020-03-06 21:54:13 +01004859 }
William Lallemandeed4bf22019-10-10 11:38:13 +02004860
William Lallemand2954c472020-03-06 21:54:13 +01004861 if (cfgerr & ERR_CODE) {
4862 free(entry);
4863 entry = NULL;
4864 ssl_sock_free_ssl_conf(ssl_conf);
4865 free(ssl_conf);
4866 ssl_conf = NULL;
4867 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004868 }
William Lallemand2954c472020-03-06 21:54:13 +01004869 entry->node.key = ckchs;
4870 entry->ssl_conf = ssl_conf;
4871 /* filters */
4872 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4873 entry->fcount = arg - cur_arg - 1;
4874 ebpt_insert(&newlist->entries, &entry->node);
4875 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004876 }
William Lallemand2954c472020-03-06 21:54:13 +01004877 if (cfgerr & ERR_CODE)
4878 goto error;
4879
4880 fclose(f);
4881 *crtlist = newlist;
4882
4883 return cfgerr;
4884error:
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004885 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004886 crtlist_free(newlist);
4887 return cfgerr;
4888}
4889
4890/* Load a crt-list file, this is done in 2 parts:
4891 * - store the content of the file in a crtlist structure with crtlist_entry structures
4892 * - generate the instances by iterating on entries in the crtlist struct
4893 *
4894 * Nothing is locked there, this function is used in the configuration parser.
4895 *
4896 * Returns a set of ERR_* flags possibly with an error in <err>.
4897 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004898int 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 +01004899{
4900 struct crtlist *crtlist = NULL;
4901 struct ebmb_node *eb;
4902 struct crtlist_entry *entry;
4903 struct list instances; /* temporary list head */
4904 int cfgerr = 0;
4905
4906 LIST_INIT(&instances);
4907 /* look for an existing crtlist or create one */
4908 eb = ebst_lookup(&crtlists_tree, file);
4909 if (eb) {
4910 crtlist = ebmb_entry(eb, struct crtlist, node);
4911 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004912 /* load a crt-list OR a directory */
4913 if (dir)
4914 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4915 else
4916 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4917
William Lallemand2954c472020-03-06 21:54:13 +01004918 if (!(cfgerr & ERR_CODE))
4919 ebst_insert(&crtlists_tree, &crtlist->node);
4920 }
4921
4922 if (cfgerr & ERR_CODE) {
4923 cfgerr |= ERR_FATAL | ERR_ALERT;
4924 goto error;
4925 }
4926
4927 /* generates ckch instance from the crtlist_entry */
4928 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4929 struct ckch_store *store;
4930 struct ckch_inst *ckch_inst = NULL;
4931
4932 store = entry->node.key;
4933 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
4934 if (cfgerr & ERR_CODE) {
4935 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
4936 goto error;
4937 }
4938 LIST_ADDQ(&instances, &ckch_inst->by_crtlist_entry);
4939 }
4940 /* add the instances to the actual instance list in the crtlist_entry */
4941 LIST_SPLICE(&entry->ckch_inst, &instances);
4942
4943 return cfgerr;
4944error:
4945 {
4946 struct ckch_inst *inst, *s_inst;
4947
4948 list_for_each_entry_safe(inst, s_inst, &instances, by_crtlist_entry) {
4949 struct sni_ctx *sni, *s_sni;
4950
4951 /* free the sni_ctx */
4952 list_for_each_entry_safe(sni, s_sni, &inst->sni_ctx, by_ckch_inst) {
4953 ebmb_delete(&sni->name);
4954 LIST_DEL(&sni->by_ckch_inst);
4955 free(sni);
4956 }
4957 LIST_DEL(&inst->by_crtlist_entry);
4958 free(inst);
4959 }
4960 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004961 return cfgerr;
4962}
4963
William Lallemand06b22a82020-03-16 14:45:55 +01004964/* Returns a set of ERR_* flags possibly with an error in <err>. */
4965int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
4966{
4967 struct stat buf;
4968 char fp[MAXPATHLEN+1];
4969 int cfgerr = 0;
4970 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01004971 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01004972
4973 if ((ckchs = ckchs_lookup(path))) {
4974 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01004975 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01004976 }
4977 if (stat(path, &buf) == 0) {
4978 if (S_ISDIR(buf.st_mode) == 0) {
4979 ckchs = ckchs_load_cert_file(path, 0, err);
4980 if (!ckchs)
4981 return ERR_ALERT | ERR_FATAL;
4982
William Lallemand24bde432020-03-09 16:48:43 +01004983 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01004984 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004985 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01004986 }
4987 } else {
4988 /* stat failed, could be a bundle */
4989 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
4990 /* try to load a bundle if it is permitted */
4991 ckchs = ckchs_load_cert_file(path, 1, err);
4992 if (!ckchs)
4993 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01004994 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01004995 } else {
4996 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4997 err && *err ? *err : "", fp, strerror(errno));
4998 cfgerr |= ERR_ALERT | ERR_FATAL;
4999 }
5000 }
5001
5002 return cfgerr;
5003}
5004
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005005/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005006static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005007ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005008{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005009 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005010 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005011 SSL_OP_ALL | /* all known workarounds for bugs */
5012 SSL_OP_NO_SSLv2 |
5013 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005014 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005015 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005016 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005017 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005018 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005019 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005020 SSL_MODE_ENABLE_PARTIAL_WRITE |
5021 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005022 SSL_MODE_RELEASE_BUFFERS |
5023 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005024 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005025 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005026 int flags = MC_SSL_O_ALL;
5027 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005028
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005029 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005030 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005031
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005032 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005033 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5034 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5035 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005036 else
5037 flags = conf_ssl_methods->flags;
5038
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005039 min = conf_ssl_methods->min;
5040 max = conf_ssl_methods->max;
5041 /* start with TLSv10 to remove SSLv3 per default */
5042 if (!min && (!max || max >= CONF_TLSV10))
5043 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005044 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005045 if (min)
5046 flags |= (methodVersions[min].flag - 1);
5047 if (max)
5048 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005049 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005050 min = max = CONF_TLSV_NONE;
5051 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005052 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005053 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005054 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005055 if (min) {
5056 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005057 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5058 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5059 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5060 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005061 hole = 0;
5062 }
5063 max = i;
5064 }
5065 else {
5066 min = max = i;
5067 }
5068 }
5069 else {
5070 if (min)
5071 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005072 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005073 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005074 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5075 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005076 cfgerr += 1;
5077 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005078 /* save real min/max in bind_conf */
5079 conf_ssl_methods->min = min;
5080 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005081
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005082#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005083 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005084 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005085 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005086 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005087 else
5088 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5089 if (flags & methodVersions[i].flag)
5090 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005091#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005092 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005093 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5094 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005095#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005096
5097 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5098 options |= SSL_OP_NO_TICKET;
5099 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5100 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005101
5102#ifdef SSL_OP_NO_RENEGOTIATION
5103 options |= SSL_OP_NO_RENEGOTIATION;
5104#endif
5105
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005106 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005107
Willy Tarreau5db847a2019-05-09 14:13:35 +02005108#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005109 if (global_ssl.async)
5110 mode |= SSL_MODE_ASYNC;
5111#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005112 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005113 if (global_ssl.life_time)
5114 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005115
5116#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5117#ifdef OPENSSL_IS_BORINGSSL
5118 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5119 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005120#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005121 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005122 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005123 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5124 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005125#else
5126 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005127#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005128 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005129#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005130 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005131}
5132
William Lallemand4f45bb92017-10-30 20:08:51 +01005133
5134static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5135{
5136 if (first == block) {
5137 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5138 if (first->len > 0)
5139 sh_ssl_sess_tree_delete(sh_ssl_sess);
5140 }
5141}
5142
5143/* return first block from sh_ssl_sess */
5144static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5145{
5146 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5147
5148}
5149
5150/* store a session into the cache
5151 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5152 * data: asn1 encoded session
5153 * data_len: asn1 encoded session length
5154 * Returns 1 id session was stored (else 0)
5155 */
5156static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5157{
5158 struct shared_block *first;
5159 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5160
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005161 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005162 if (!first) {
5163 /* Could not retrieve enough free blocks to store that session */
5164 return 0;
5165 }
5166
5167 /* STORE the key in the first elem */
5168 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5169 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5170 first->len = sizeof(struct sh_ssl_sess_hdr);
5171
5172 /* it returns the already existing node
5173 or current node if none, never returns null */
5174 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5175 if (oldsh_ssl_sess != sh_ssl_sess) {
5176 /* NOTE: Row couldn't be in use because we lock read & write function */
5177 /* release the reserved row */
5178 shctx_row_dec_hot(ssl_shctx, first);
5179 /* replace the previous session already in the tree */
5180 sh_ssl_sess = oldsh_ssl_sess;
5181 /* ignore the previous session data, only use the header */
5182 first = sh_ssl_sess_first_block(sh_ssl_sess);
5183 shctx_row_inc_hot(ssl_shctx, first);
5184 first->len = sizeof(struct sh_ssl_sess_hdr);
5185 }
5186
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005187 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005188 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005189 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005190 }
5191
5192 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005193
5194 return 1;
5195}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005196
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005197/* SSL callback used when a new session is created while connecting to a server */
5198static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5199{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005200 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005201 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005202
Willy Tarreau07d94e42018-09-20 10:57:52 +02005203 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005204
Olivier Houcharde6060c52017-11-16 17:42:52 +01005205 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5206 int len;
5207 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005208
Olivier Houcharde6060c52017-11-16 17:42:52 +01005209 len = i2d_SSL_SESSION(sess, NULL);
5210 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5211 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5212 } else {
5213 free(s->ssl_ctx.reused_sess[tid].ptr);
5214 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5215 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5216 }
5217 if (s->ssl_ctx.reused_sess[tid].ptr) {
5218 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5219 &ptr);
5220 }
5221 } else {
5222 free(s->ssl_ctx.reused_sess[tid].ptr);
5223 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5224 }
5225
5226 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005227}
5228
Olivier Houcharde6060c52017-11-16 17:42:52 +01005229
William Lallemanded0b5ad2017-10-30 19:36:36 +01005230/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005231int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005232{
5233 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5234 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5235 unsigned char *p;
5236 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005237 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005238 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005239
5240 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005241 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005242 * note: SSL_SESSION_set1_id is using
5243 * a memcpy so we need to use a different pointer
5244 * than sid_data or sid_ctx_data to avoid valgrind
5245 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005246 */
5247
5248 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005249
5250 /* copy value in an other buffer */
5251 memcpy(encid, sid_data, sid_length);
5252
5253 /* pad with 0 */
5254 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5255 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5256
5257 /* force length to zero to avoid ASN1 encoding */
5258 SSL_SESSION_set1_id(sess, encid, 0);
5259
5260 /* force length to zero to avoid ASN1 encoding */
5261 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005262
5263 /* check if buffer is large enough for the ASN1 encoded session */
5264 data_len = i2d_SSL_SESSION(sess, NULL);
5265 if (data_len > SHSESS_MAX_DATA_LEN)
5266 goto err;
5267
5268 p = encsess;
5269
5270 /* process ASN1 session encoding before the lock */
5271 i2d_SSL_SESSION(sess, &p);
5272
William Lallemanded0b5ad2017-10-30 19:36:36 +01005273
William Lallemanda3c77cf2017-10-30 23:44:40 +01005274 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005275 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005276 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005277 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005278err:
5279 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005280 SSL_SESSION_set1_id(sess, encid, sid_length);
5281 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005282
5283 return 0; /* do not increment session reference count */
5284}
5285
5286/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005287SSL_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 +01005288{
William Lallemand4f45bb92017-10-30 20:08:51 +01005289 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005290 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5291 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005292 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005293 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005294
5295 global.shctx_lookups++;
5296
5297 /* allow the session to be freed automatically by openssl */
5298 *do_copy = 0;
5299
5300 /* tree key is zeros padded sessionid */
5301 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5302 memcpy(tmpkey, key, key_len);
5303 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5304 key = tmpkey;
5305 }
5306
5307 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005308 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005309
5310 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005311 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5312 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005313 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005314 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005315 global.shctx_misses++;
5316 return NULL;
5317 }
5318
William Lallemand4f45bb92017-10-30 20:08:51 +01005319 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5320 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005321
William Lallemand4f45bb92017-10-30 20:08:51 +01005322 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 +01005323
William Lallemanda3c77cf2017-10-30 23:44:40 +01005324 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005325
5326 /* decode ASN1 session */
5327 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005328 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005329 /* Reset session id and session id contenxt */
5330 if (sess) {
5331 SSL_SESSION_set1_id(sess, key, key_len);
5332 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5333 }
5334
5335 return sess;
5336}
5337
William Lallemand4f45bb92017-10-30 20:08:51 +01005338
William Lallemanded0b5ad2017-10-30 19:36:36 +01005339/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005340void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005341{
William Lallemand4f45bb92017-10-30 20:08:51 +01005342 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005343 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5344 unsigned int sid_length;
5345 const unsigned char *sid_data;
5346 (void)ctx;
5347
5348 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5349 /* tree key is zeros padded sessionid */
5350 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5351 memcpy(tmpkey, sid_data, sid_length);
5352 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5353 sid_data = tmpkey;
5354 }
5355
William Lallemanda3c77cf2017-10-30 23:44:40 +01005356 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005357
5358 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005359 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5360 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005361 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005362 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005363 }
5364
5365 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005366 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005367}
5368
5369/* Set session cache mode to server and disable openssl internal cache.
5370 * Set shared cache callbacks on an ssl context.
5371 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005372void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005373{
5374 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5375
5376 if (!ssl_shctx) {
5377 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5378 return;
5379 }
5380
5381 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5382 SSL_SESS_CACHE_NO_INTERNAL |
5383 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5384
5385 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005386 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5387 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5388 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005389}
5390
William Lallemand8b453912019-11-21 15:48:10 +01005391/*
5392 * This function applies the SSL configuration on a SSL_CTX
5393 * It returns an error code and fills the <err> buffer
5394 */
5395int 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 +01005396{
5397 struct proxy *curproxy = bind_conf->frontend;
5398 int cfgerr = 0;
5399 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005400 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005401 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005402#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005403 const char *conf_ciphersuites;
5404#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005405 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005406
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005407 if (ssl_conf) {
5408 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5409 int i, min, max;
5410 int flags = MC_SSL_O_ALL;
5411
5412 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005413 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5414 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005415 if (min)
5416 flags |= (methodVersions[min].flag - 1);
5417 if (max)
5418 flags |= ~((methodVersions[max].flag << 1) - 1);
5419 min = max = CONF_TLSV_NONE;
5420 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5421 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5422 if (min)
5423 max = i;
5424 else
5425 min = max = i;
5426 }
5427 /* save real min/max */
5428 conf_ssl_methods->min = min;
5429 conf_ssl_methods->max = max;
5430 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005431 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5432 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005433 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005434 }
5435 }
5436
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005437 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005438 case SSL_SOCK_VERIFY_NONE:
5439 verify = SSL_VERIFY_NONE;
5440 break;
5441 case SSL_SOCK_VERIFY_OPTIONAL:
5442 verify = SSL_VERIFY_PEER;
5443 break;
5444 case SSL_SOCK_VERIFY_REQUIRED:
5445 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5446 break;
5447 }
5448 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5449 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005450 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 +01005451 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 +01005452 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 +01005453 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005454 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005455 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005456 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005457 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005458 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005459 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005460 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5461 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5462 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5463 cfgerr |= ERR_ALERT | ERR_FATAL;
5464 }
5465 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005466 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005467 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 +02005468 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005469 }
Emeric Brun850efd52014-01-29 12:24:34 +01005470 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005471 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5472 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005473 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005474 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005475#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005476 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005477 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5478
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005479 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005480 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5481 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005482 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005483 }
Emeric Brun561e5742012-10-02 15:20:55 +02005484 else {
5485 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5486 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005487 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005488#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005489 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005490 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005491#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005492 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005493 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005494 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5495 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005496 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005497 }
5498 }
5499#endif
5500
William Lallemand4f45bb92017-10-30 20:08:51 +01005501 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005502 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5503 if (conf_ciphers &&
5504 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005505 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5506 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005507 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005508 }
5509
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005510#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005511 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5512 if (conf_ciphersuites &&
5513 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005514 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5515 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005516 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005517 }
5518#endif
5519
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005520#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005521 /* If tune.ssl.default-dh-param has not been set,
5522 neither has ssl-default-dh-file and no static DH
5523 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005524 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005525 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005526 (ssl_dh_ptr_index == -1 ||
5527 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005528 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5529 const SSL_CIPHER * cipher = NULL;
5530 char cipher_description[128];
5531 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5532 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5533 which is not ephemeral DH. */
5534 const char dhe_description[] = " Kx=DH ";
5535 const char dhe_export_description[] = " Kx=DH(";
5536 int idx = 0;
5537 int dhe_found = 0;
5538 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005539
Remi Gacogne23d5d372014-10-10 17:04:26 +02005540 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005541
Remi Gacogne23d5d372014-10-10 17:04:26 +02005542 if (ssl) {
5543 ciphers = SSL_get_ciphers(ssl);
5544
5545 if (ciphers) {
5546 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5547 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5548 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5549 if (strstr(cipher_description, dhe_description) != NULL ||
5550 strstr(cipher_description, dhe_export_description) != NULL) {
5551 dhe_found = 1;
5552 break;
5553 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005554 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005555 }
5556 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005557 SSL_free(ssl);
5558 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005559 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005560
Lukas Tribus90132722014-08-18 00:56:33 +02005561 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005562 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",
5563 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005564 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005565 }
5566
Willy Tarreauef934602016-12-22 23:12:01 +01005567 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005568 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005569
Willy Tarreauef934602016-12-22 23:12:01 +01005570 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005571 if (local_dh_1024 == NULL) {
5572 local_dh_1024 = ssl_get_dh_1024();
5573 }
Willy Tarreauef934602016-12-22 23:12:01 +01005574 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005575 if (local_dh_2048 == NULL) {
5576 local_dh_2048 = ssl_get_dh_2048();
5577 }
Willy Tarreauef934602016-12-22 23:12:01 +01005578 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005579 if (local_dh_4096 == NULL) {
5580 local_dh_4096 = ssl_get_dh_4096();
5581 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005582 }
5583 }
5584 }
5585#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005586
Emeric Brunfc0421f2012-09-07 17:30:07 +02005587 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005588#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005589 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005590#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005591
Bernard Spil13c53f82018-02-15 13:34:58 +01005592#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005593 ssl_conf_cur = NULL;
5594 if (ssl_conf && ssl_conf->npn_str)
5595 ssl_conf_cur = ssl_conf;
5596 else if (bind_conf->ssl_conf.npn_str)
5597 ssl_conf_cur = &bind_conf->ssl_conf;
5598 if (ssl_conf_cur)
5599 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005600#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005601#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005602 ssl_conf_cur = NULL;
5603 if (ssl_conf && ssl_conf->alpn_str)
5604 ssl_conf_cur = ssl_conf;
5605 else if (bind_conf->ssl_conf.alpn_str)
5606 ssl_conf_cur = &bind_conf->ssl_conf;
5607 if (ssl_conf_cur)
5608 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005609#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005610#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005611 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5612 if (conf_curves) {
5613 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005614 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5615 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005616 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005617 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005618 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005619 }
5620#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005621#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005622 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005623 int i;
5624 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005625#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005626 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005627 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5628 NULL);
5629
5630 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005631 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005632 return cfgerr;
5633 }
5634#else
5635 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5636 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5637 ECDHE_DEFAULT_CURVE);
5638#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005639
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005640 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005641 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005642 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5643 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005644 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005645 }
5646 else {
5647 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5648 EC_KEY_free(ecdh);
5649 }
5650 }
5651#endif
5652
Emeric Brunfc0421f2012-09-07 17:30:07 +02005653 return cfgerr;
5654}
5655
Evan Broderbe554312013-06-27 00:05:25 -07005656static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5657{
5658 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5659 size_t prefixlen, suffixlen;
5660
5661 /* Trivial case */
5662 if (strcmp(pattern, hostname) == 0)
5663 return 1;
5664
Evan Broderbe554312013-06-27 00:05:25 -07005665 /* The rest of this logic is based on RFC 6125, section 6.4.3
5666 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5667
Emeric Bruna848dae2013-10-08 11:27:28 +02005668 pattern_wildcard = NULL;
5669 pattern_left_label_end = pattern;
5670 while (*pattern_left_label_end != '.') {
5671 switch (*pattern_left_label_end) {
5672 case 0:
5673 /* End of label not found */
5674 return 0;
5675 case '*':
5676 /* If there is more than one wildcards */
5677 if (pattern_wildcard)
5678 return 0;
5679 pattern_wildcard = pattern_left_label_end;
5680 break;
5681 }
5682 pattern_left_label_end++;
5683 }
5684
5685 /* If it's not trivial and there is no wildcard, it can't
5686 * match */
5687 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005688 return 0;
5689
5690 /* Make sure all labels match except the leftmost */
5691 hostname_left_label_end = strchr(hostname, '.');
5692 if (!hostname_left_label_end
5693 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5694 return 0;
5695
5696 /* Make sure the leftmost label of the hostname is long enough
5697 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005698 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005699 return 0;
5700
5701 /* Finally compare the string on either side of the
5702 * wildcard */
5703 prefixlen = pattern_wildcard - pattern;
5704 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005705 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5706 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005707 return 0;
5708
5709 return 1;
5710}
5711
5712static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5713{
5714 SSL *ssl;
5715 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005716 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005717 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005718 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005719
5720 int depth;
5721 X509 *cert;
5722 STACK_OF(GENERAL_NAME) *alt_names;
5723 int i;
5724 X509_NAME *cert_subject;
5725 char *str;
5726
5727 if (ok == 0)
5728 return ok;
5729
5730 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005731 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005732 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005733
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005734 /* We're checking if the provided hostnames match the desired one. The
5735 * desired hostname comes from the SNI we presented if any, or if not
5736 * provided then it may have been explicitly stated using a "verifyhost"
5737 * directive. If neither is set, we don't care about the name so the
5738 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005739 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005740 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005741 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005742 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005743 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005744 if (!servername)
5745 return ok;
5746 }
Evan Broderbe554312013-06-27 00:05:25 -07005747
5748 /* We only need to verify the CN on the actual server cert,
5749 * not the indirect CAs */
5750 depth = X509_STORE_CTX_get_error_depth(ctx);
5751 if (depth != 0)
5752 return ok;
5753
5754 /* At this point, the cert is *not* OK unless we can find a
5755 * hostname match */
5756 ok = 0;
5757
5758 cert = X509_STORE_CTX_get_current_cert(ctx);
5759 /* It seems like this might happen if verify peer isn't set */
5760 if (!cert)
5761 return ok;
5762
5763 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5764 if (alt_names) {
5765 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5766 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5767 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005768#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005769 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5770#else
Evan Broderbe554312013-06-27 00:05:25 -07005771 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005772#endif
Evan Broderbe554312013-06-27 00:05:25 -07005773 ok = ssl_sock_srv_hostcheck(str, servername);
5774 OPENSSL_free(str);
5775 }
5776 }
5777 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005778 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005779 }
5780
5781 cert_subject = X509_get_subject_name(cert);
5782 i = -1;
5783 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5784 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005785 ASN1_STRING *value;
5786 value = X509_NAME_ENTRY_get_data(entry);
5787 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005788 ok = ssl_sock_srv_hostcheck(str, servername);
5789 OPENSSL_free(str);
5790 }
5791 }
5792
Willy Tarreau71d058c2017-07-26 20:09:56 +02005793 /* report the mismatch and indicate if SNI was used or not */
5794 if (!ok && !conn->err_code)
5795 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005796 return ok;
5797}
5798
Emeric Brun94324a42012-10-11 14:00:19 +02005799/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005800int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005801{
Willy Tarreau03209342016-12-22 17:08:28 +01005802 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005803 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005804 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005805 SSL_OP_ALL | /* all known workarounds for bugs */
5806 SSL_OP_NO_SSLv2 |
5807 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005808 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005809 SSL_MODE_ENABLE_PARTIAL_WRITE |
5810 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005811 SSL_MODE_RELEASE_BUFFERS |
5812 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005813 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005814 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005815 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005816 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005817 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005818
Thierry Fournier383085f2013-01-24 14:15:43 +01005819 /* Make sure openssl opens /dev/urandom before the chroot */
5820 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005821 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005822 cfgerr++;
5823 }
5824
Willy Tarreaufce03112015-01-15 21:32:40 +01005825 /* Automatic memory computations need to know we use SSL there */
5826 global.ssl_used_backend = 1;
5827
5828 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005829 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005830 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005831 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5832 curproxy->id, srv->id,
5833 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005834 cfgerr++;
5835 return cfgerr;
5836 }
5837 }
Emeric Brun94324a42012-10-11 14:00:19 +02005838 if (srv->use_ssl)
5839 srv->xprt = &ssl_sock;
5840 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005841 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005842
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005843 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005844 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005845 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5846 proxy_type_str(curproxy), curproxy->id,
5847 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005848 cfgerr++;
5849 return cfgerr;
5850 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005851
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005852 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005853 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5854 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5855 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005856 else
5857 flags = conf_ssl_methods->flags;
5858
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005859 /* Real min and max should be determinate with configuration and openssl's capabilities */
5860 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005861 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005862 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005863 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005864
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005865 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005866 min = max = CONF_TLSV_NONE;
5867 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005868 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005869 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005870 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005871 if (min) {
5872 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005873 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5874 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5875 proxy_type_str(curproxy), curproxy->id, srv->id,
5876 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005877 hole = 0;
5878 }
5879 max = i;
5880 }
5881 else {
5882 min = max = i;
5883 }
5884 }
5885 else {
5886 if (min)
5887 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005888 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005889 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005890 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5891 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005892 cfgerr += 1;
5893 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005894
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005895#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005896 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005897 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005898 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005899 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005900 else
5901 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5902 if (flags & methodVersions[i].flag)
5903 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005904#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005905 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005906 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5907 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005908#endif
5909
5910 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5911 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005912 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005913
Willy Tarreau5db847a2019-05-09 14:13:35 +02005914#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005915 if (global_ssl.async)
5916 mode |= SSL_MODE_ASYNC;
5917#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005918 SSL_CTX_set_mode(ctx, mode);
5919 srv->ssl_ctx.ctx = ctx;
5920
Emeric Bruna7aa3092012-10-26 12:58:00 +02005921 if (srv->ssl_ctx.client_crt) {
5922 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 +01005923 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5924 proxy_type_str(curproxy), curproxy->id,
5925 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005926 cfgerr++;
5927 }
5928 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 +01005929 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5930 proxy_type_str(curproxy), curproxy->id,
5931 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005932 cfgerr++;
5933 }
5934 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005935 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5936 proxy_type_str(curproxy), curproxy->id,
5937 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005938 cfgerr++;
5939 }
5940 }
Emeric Brun94324a42012-10-11 14:00:19 +02005941
Emeric Brun850efd52014-01-29 12:24:34 +01005942 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5943 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005944 switch (srv->ssl_ctx.verify) {
5945 case SSL_SOCK_VERIFY_NONE:
5946 verify = SSL_VERIFY_NONE;
5947 break;
5948 case SSL_SOCK_VERIFY_REQUIRED:
5949 verify = SSL_VERIFY_PEER;
5950 break;
5951 }
Evan Broderbe554312013-06-27 00:05:25 -07005952 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005953 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005954 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005955 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005956 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005957 /* set CAfile to verify */
5958 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5959 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005960 curproxy->id, srv->id,
5961 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005962 cfgerr++;
5963 }
5964 }
Emeric Brun850efd52014-01-29 12:24:34 +01005965 else {
5966 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005967 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",
5968 curproxy->id, srv->id,
5969 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005970 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005971 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5972 curproxy->id, srv->id,
5973 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005974 cfgerr++;
5975 }
Emeric Brunef42d922012-10-11 16:11:36 +02005976#ifdef X509_V_FLAG_CRL_CHECK
5977 if (srv->ssl_ctx.crl_file) {
5978 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5979
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005980 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005981 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5982 curproxy->id, srv->id,
5983 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005984 cfgerr++;
5985 }
5986 else {
5987 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5988 }
5989 }
5990#endif
5991 }
5992
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005993 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5994 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5995 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005996 if (srv->ssl_ctx.ciphers &&
5997 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005998 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5999 curproxy->id, srv->id,
6000 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006001 cfgerr++;
6002 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006003
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006004#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006005 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006006 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006007 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6008 curproxy->id, srv->id,
6009 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6010 cfgerr++;
6011 }
6012#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006013#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6014 if (srv->ssl_ctx.npn_str)
6015 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6016#endif
6017#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6018 if (srv->ssl_ctx.alpn_str)
6019 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6020#endif
6021
Emeric Brun94324a42012-10-11 14:00:19 +02006022
6023 return cfgerr;
6024}
6025
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006026/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006027 * be NULL, in which case nothing is done. Returns the number of errors
6028 * encountered.
6029 */
Willy Tarreau03209342016-12-22 17:08:28 +01006030int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006031{
6032 struct ebmb_node *node;
6033 struct sni_ctx *sni;
6034 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006035 int errcode = 0;
6036 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006037
Willy Tarreaufce03112015-01-15 21:32:40 +01006038 /* Automatic memory computations need to know we use SSL there */
6039 global.ssl_used_frontend = 1;
6040
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006041 /* Make sure openssl opens /dev/urandom before the chroot */
6042 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006043 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006044 err++;
6045 }
6046 /* Create initial_ctx used to start the ssl connection before do switchctx */
6047 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006048 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006049 /* It should not be necessary to call this function, but it's
6050 necessary first to check and move all initialisation related
6051 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006052 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006053 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006054 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006055 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006056
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006057 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006058 while (node) {
6059 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006060 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6061 /* only initialize the CTX on its first occurrence and
6062 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006063 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006064 node = ebmb_next(node);
6065 }
6066
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006067 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006068 while (node) {
6069 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006070 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006071 /* only initialize the CTX on its first occurrence and
6072 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006073 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6074 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006075 node = ebmb_next(node);
6076 }
William Lallemand8b453912019-11-21 15:48:10 +01006077
6078 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006079 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006080 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006081 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006082 err++;
6083 }
6084
6085 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006086 return err;
6087}
6088
Willy Tarreau55d37912016-12-21 23:38:39 +01006089/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6090 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6091 * alerts are directly emitted since the rest of the stack does it below.
6092 */
6093int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6094{
6095 struct proxy *px = bind_conf->frontend;
6096 int alloc_ctx;
6097 int err;
6098
6099 if (!bind_conf->is_ssl) {
6100 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006101 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6102 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006103 }
6104 return 0;
6105 }
6106 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006107 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006108 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6109 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006110 }
6111 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006112 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6113 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006114 return -1;
6115 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006116 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006117 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006118 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006119 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006120 sizeof(*sh_ssl_sess_tree),
6121 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006122 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006123 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6124 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");
6125 else
6126 ha_alert("Unable to allocate SSL session cache.\n");
6127 return -1;
6128 }
6129 /* free block callback */
6130 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6131 /* init the root tree within the extra space */
6132 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6133 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006134 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006135 err = 0;
6136 /* initialize all certificate contexts */
6137 err += ssl_sock_prepare_all_ctx(bind_conf);
6138
6139 /* initialize CA variables if the certificates generation is enabled */
6140 err += ssl_sock_load_ca(bind_conf);
6141
6142 return -err;
6143}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006144
6145/* release ssl context allocated for servers. */
6146void ssl_sock_free_srv_ctx(struct server *srv)
6147{
Olivier Houchardc7566002018-11-20 23:33:50 +01006148#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6149 if (srv->ssl_ctx.alpn_str)
6150 free(srv->ssl_ctx.alpn_str);
6151#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006152#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006153 if (srv->ssl_ctx.npn_str)
6154 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006155#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006156 if (srv->ssl_ctx.ctx)
6157 SSL_CTX_free(srv->ssl_ctx.ctx);
6158}
6159
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006160/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006161 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6162 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006163void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006164{
6165 struct ebmb_node *node, *back;
6166 struct sni_ctx *sni;
6167
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006168 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006169 while (node) {
6170 sni = ebmb_entry(node, struct sni_ctx, name);
6171 back = ebmb_next(node);
6172 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006173 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02006174 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006175 ssl_sock_free_ssl_conf(sni->conf);
6176 free(sni->conf);
6177 sni->conf = NULL;
6178 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006179 free(sni);
6180 node = back;
6181 }
6182
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006183 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006184 while (node) {
6185 sni = ebmb_entry(node, struct sni_ctx, name);
6186 back = ebmb_next(node);
6187 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006188 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02006189 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006190 ssl_sock_free_ssl_conf(sni->conf);
6191 free(sni->conf);
6192 sni->conf = NULL;
6193 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006194 free(sni);
6195 node = back;
6196 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006197 SSL_CTX_free(bind_conf->initial_ctx);
6198 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006199 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006200 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006201}
6202
Willy Tarreau795cdab2016-12-22 17:30:54 +01006203/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6204void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6205{
6206 ssl_sock_free_ca(bind_conf);
6207 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006208 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006209 free(bind_conf->ca_sign_file);
6210 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006211 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006212 free(bind_conf->keys_ref->filename);
6213 free(bind_conf->keys_ref->tlskeys);
6214 LIST_DEL(&bind_conf->keys_ref->list);
6215 free(bind_conf->keys_ref);
6216 }
6217 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006218 bind_conf->ca_sign_pass = NULL;
6219 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006220}
6221
Christopher Faulet31af49d2015-06-09 17:29:50 +02006222/* Load CA cert file and private key used to generate certificates */
6223int
Willy Tarreau03209342016-12-22 17:08:28 +01006224ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006225{
Willy Tarreau03209342016-12-22 17:08:28 +01006226 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006227 FILE *fp;
6228 X509 *cacert = NULL;
6229 EVP_PKEY *capkey = NULL;
6230 int err = 0;
6231
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006232 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006233 return err;
6234
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006235#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006236 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006237 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006238 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006239 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006240 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006241#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006242
Christopher Faulet31af49d2015-06-09 17:29:50 +02006243 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006244 ha_alert("Proxy '%s': cannot enable certificate generation, "
6245 "no CA certificate File configured at [%s:%d].\n",
6246 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006247 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006248 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006249
6250 /* read in the CA certificate */
6251 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006252 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6253 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006254 goto load_error;
6255 }
6256 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006257 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6258 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006259 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006260 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006261 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006262 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006263 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6264 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006265 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006266 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006267
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006268 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006269 bind_conf->ca_sign_cert = cacert;
6270 bind_conf->ca_sign_pkey = capkey;
6271 return err;
6272
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006273 read_error:
6274 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006275 if (capkey) EVP_PKEY_free(capkey);
6276 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006277 load_error:
6278 bind_conf->generate_certs = 0;
6279 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006280 return err;
6281}
6282
6283/* Release CA cert and private key used to generate certificated */
6284void
6285ssl_sock_free_ca(struct bind_conf *bind_conf)
6286{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006287 if (bind_conf->ca_sign_pkey)
6288 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6289 if (bind_conf->ca_sign_cert)
6290 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006291 bind_conf->ca_sign_pkey = NULL;
6292 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006293}
6294
Emeric Brun46591952012-05-18 15:47:34 +02006295/*
6296 * This function is called if SSL * context is not yet allocated. The function
6297 * is designed to be called before any other data-layer operation and sets the
6298 * handshake flag on the connection. It is safe to call it multiple times.
6299 * It returns 0 on success and -1 in error case.
6300 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006301static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006302{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006303 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006304 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006305 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006306 return 0;
6307
Willy Tarreau3c728722014-01-23 13:50:42 +01006308 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006309 return 0;
6310
Olivier Houchard66ab4982019-02-26 18:37:15 +01006311 ctx = pool_alloc(ssl_sock_ctx_pool);
6312 if (!ctx) {
6313 conn->err_code = CO_ER_SSL_NO_MEM;
6314 return -1;
6315 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006316 ctx->wait_event.tasklet = tasklet_new();
6317 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006318 conn->err_code = CO_ER_SSL_NO_MEM;
6319 pool_free(ssl_sock_ctx_pool, ctx);
6320 return -1;
6321 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006322 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6323 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006324 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006325 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006326 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006327 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006328 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006329 ctx->xprt_st = 0;
6330 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006331
6332 /* Only work with sockets for now, this should be adapted when we'll
6333 * add QUIC support.
6334 */
6335 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006336 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006337 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6338 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006339 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006340
Willy Tarreau20879a02012-12-03 16:32:10 +01006341 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6342 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006343 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006344 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006345
Emeric Brun46591952012-05-18 15:47:34 +02006346 /* If it is in client mode initiate SSL session
6347 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006348 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006349 int may_retry = 1;
6350
6351 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006352 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006353 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6354 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006355 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006356 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006357 goto retry_connect;
6358 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006359 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006360 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006361 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006362 ctx->bio = BIO_new(ha_meth);
6363 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006364 SSL_free(ctx->ssl);
6365 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006366 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006367 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006368 goto retry_connect;
6369 }
Emeric Brun55476152014-11-12 17:35:37 +01006370 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006371 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006372 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006373 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006374 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006375
Evan Broderbe554312013-06-27 00:05:25 -07006376 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006377 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6378 SSL_free(ctx->ssl);
6379 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006380 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006381 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006382 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006383 goto retry_connect;
6384 }
Emeric Brun55476152014-11-12 17:35:37 +01006385 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006386 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006387 }
6388
Olivier Houchard66ab4982019-02-26 18:37:15 +01006389 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006390 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6391 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6392 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 +01006393 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006394 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006395 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6396 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006397 } else if (sess) {
6398 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006399 }
6400 }
Evan Broderbe554312013-06-27 00:05:25 -07006401
Emeric Brun46591952012-05-18 15:47:34 +02006402 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006403 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006404
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006405 _HA_ATOMIC_ADD(&sslconns, 1);
6406 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006407 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006408 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006409 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006410 return 0;
6411 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006412 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006413 int may_retry = 1;
6414
6415 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006416 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006417 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6418 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006419 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006420 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006421 goto retry_accept;
6422 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006423 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006424 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006425 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006426 ctx->bio = BIO_new(ha_meth);
6427 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006428 SSL_free(ctx->ssl);
6429 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006430 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006431 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006432 goto retry_accept;
6433 }
Emeric Brun55476152014-11-12 17:35:37 +01006434 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006435 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006436 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006437 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006438 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006439
Emeric Brune1f38db2012-09-03 20:36:47 +02006440 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006441 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6442 SSL_free(ctx->ssl);
6443 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006444 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006445 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006446 goto retry_accept;
6447 }
Emeric Brun55476152014-11-12 17:35:37 +01006448 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006449 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006450 }
6451
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006452#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6453 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6454 b_alloc(&ctx->early_buf);
6455 SSL_set_max_early_data(ctx->ssl,
6456 /* Only allow early data if we managed to allocate
6457 * a buffer.
6458 */
6459 (!b_is_null(&ctx->early_buf)) ?
6460 global.tune.bufsize - global.tune.maxrewrite : 0);
6461 }
6462#endif
6463
Olivier Houchard66ab4982019-02-26 18:37:15 +01006464 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006465
Emeric Brun46591952012-05-18 15:47:34 +02006466 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006467 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006468#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006469 conn->flags |= CO_FL_EARLY_SSL_HS;
6470#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006471
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006472 _HA_ATOMIC_ADD(&sslconns, 1);
6473 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006474 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006475 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006476 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006477 return 0;
6478 }
6479 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006480 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006481err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006482 if (ctx && ctx->wait_event.tasklet)
6483 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006484 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006485 return -1;
6486}
6487
6488
6489/* This is the callback which is used when an SSL handshake is pending. It
6490 * updates the FD status if it wants some polling before being called again.
6491 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6492 * otherwise it returns non-zero and removes itself from the connection's
6493 * flags (the bit is provided in <flag> by the caller).
6494 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006495static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006496{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006497 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006498 int ret;
6499
Willy Tarreau3c728722014-01-23 13:50:42 +01006500 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006501 return 0;
6502
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006503 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006504 goto out_error;
6505
Willy Tarreau5db847a2019-05-09 14:13:35 +02006506#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006507 /*
6508 * Check if we have early data. If we do, we have to read them
6509 * before SSL_do_handshake() is called, And there's no way to
6510 * detect early data, except to try to read them
6511 */
6512 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006513 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006514
Olivier Houchard54907bb2019-12-19 15:02:39 +01006515 while (1) {
6516 ret = SSL_read_early_data(ctx->ssl,
6517 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6518 &read_data);
6519 if (ret == SSL_READ_EARLY_DATA_ERROR)
6520 goto check_error;
6521 if (read_data > 0) {
6522 conn->flags |= CO_FL_EARLY_DATA;
6523 b_add(&ctx->early_buf, read_data);
6524 }
6525 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6526 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6527 if (!b_data(&ctx->early_buf))
6528 b_free(&ctx->early_buf);
6529 break;
6530 }
6531 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006532 }
6533#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006534 /* If we use SSL_do_handshake to process a reneg initiated by
6535 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6536 * Usually SSL_write and SSL_read are used and process implicitly
6537 * the reneg handshake.
6538 * Here we use SSL_peek as a workaround for reneg.
6539 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006540 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006541 char c;
6542
Olivier Houchard66ab4982019-02-26 18:37:15 +01006543 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006544 if (ret <= 0) {
6545 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006546 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006547
Emeric Brun674b7432012-11-08 19:21:55 +01006548 if (ret == SSL_ERROR_WANT_WRITE) {
6549 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006550 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006551 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006552 return 0;
6553 }
6554 else if (ret == SSL_ERROR_WANT_READ) {
6555 /* handshake may have been completed but we have
6556 * no more data to read.
6557 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006558 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006559 ret = 1;
6560 goto reneg_ok;
6561 }
6562 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006563 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006564 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006565 return 0;
6566 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006567#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006568 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006569 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006570 return 0;
6571 }
6572#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006573 else if (ret == SSL_ERROR_SYSCALL) {
6574 /* if errno is null, then connection was successfully established */
6575 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6576 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006577 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006578#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6579 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006580 conn->err_code = CO_ER_SSL_HANDSHAKE;
6581#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006582 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006583#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006584 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006585 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006586 empty_handshake = state == TLS_ST_BEFORE;
6587#else
Lukas Tribus49799162019-07-08 14:29:15 +02006588 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6589 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006590#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006591 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006592 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006593 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006594 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6595 else
6596 conn->err_code = CO_ER_SSL_EMPTY;
6597 }
6598 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006599 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006600 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6601 else
6602 conn->err_code = CO_ER_SSL_ABORT;
6603 }
6604 }
6605 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006606 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006607 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006608 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006609 conn->err_code = CO_ER_SSL_HANDSHAKE;
6610 }
Lukas Tribus49799162019-07-08 14:29:15 +02006611#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006612 }
Emeric Brun674b7432012-11-08 19:21:55 +01006613 goto out_error;
6614 }
6615 else {
6616 /* Fail on all other handshake errors */
6617 /* Note: OpenSSL may leave unread bytes in the socket's
6618 * buffer, causing an RST to be emitted upon close() on
6619 * TCP sockets. We first try to drain possibly pending
6620 * data to avoid this as much as possible.
6621 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006622 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006623 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006624 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006625 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006626 goto out_error;
6627 }
6628 }
6629 /* read some data: consider handshake completed */
6630 goto reneg_ok;
6631 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006632 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006633check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006634 if (ret != 1) {
6635 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006636 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006637
6638 if (ret == SSL_ERROR_WANT_WRITE) {
6639 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006640 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006641 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006642 return 0;
6643 }
6644 else if (ret == SSL_ERROR_WANT_READ) {
6645 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006646 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006647 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6648 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006649 return 0;
6650 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006651#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006652 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006653 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006654 return 0;
6655 }
6656#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006657 else if (ret == SSL_ERROR_SYSCALL) {
6658 /* if errno is null, then connection was successfully established */
6659 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6660 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006661 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006662#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6663 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006664 conn->err_code = CO_ER_SSL_HANDSHAKE;
6665#else
6666 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006667#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006668 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006669 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006670 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006671#else
Lukas Tribus49799162019-07-08 14:29:15 +02006672 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6673 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006674#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006675 if (empty_handshake) {
6676 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006677 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006678 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6679 else
6680 conn->err_code = CO_ER_SSL_EMPTY;
6681 }
6682 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006683 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006684 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6685 else
6686 conn->err_code = CO_ER_SSL_ABORT;
6687 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006688 }
6689 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006690 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006691 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6692 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006693 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006694 }
Lukas Tribus49799162019-07-08 14:29:15 +02006695#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006696 }
Willy Tarreau89230192012-09-28 20:22:13 +02006697 goto out_error;
6698 }
Emeric Brun46591952012-05-18 15:47:34 +02006699 else {
6700 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006701 /* Note: OpenSSL may leave unread bytes in the socket's
6702 * buffer, causing an RST to be emitted upon close() on
6703 * TCP sockets. We first try to drain possibly pending
6704 * data to avoid this as much as possible.
6705 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006706 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006707 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006708 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006709 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006710 goto out_error;
6711 }
6712 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006713#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006714 else {
6715 /*
6716 * If the server refused the early data, we have to send a
6717 * 425 to the client, as we no longer have the data to sent
6718 * them again.
6719 */
6720 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006721 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006722 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6723 goto out_error;
6724 }
6725 }
6726 }
6727#endif
6728
Emeric Brun46591952012-05-18 15:47:34 +02006729
Emeric Brun674b7432012-11-08 19:21:55 +01006730reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006731
Willy Tarreau5db847a2019-05-09 14:13:35 +02006732#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006733 /* ASYNC engine API doesn't support moving read/write
6734 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006735 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006736 */
6737 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006738 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006739#endif
Emeric Brun46591952012-05-18 15:47:34 +02006740 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006741 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006742 if (objt_server(conn->target)) {
6743 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6744 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6745 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006746 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006747 else {
6748 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6749 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6750 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6751 }
Emeric Brun46591952012-05-18 15:47:34 +02006752 }
6753
6754 /* The connection is now established at both layers, it's time to leave */
6755 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6756 return 1;
6757
6758 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006759 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006760 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006761 ERR_clear_error();
6762
Emeric Brun9fa89732012-10-04 17:09:56 +02006763 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006764 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6765 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6766 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006767 }
6768
Emeric Brun46591952012-05-18 15:47:34 +02006769 /* Fail on all other handshake errors */
6770 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006771 if (!conn->err_code)
6772 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006773 return 0;
6774}
6775
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006776/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6777 * event subscriber <es> is not allowed to change from a previous call as long
6778 * as at least one event is still subscribed. The <event_type> must only be a
6779 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6780 * unless the transport layer was already released.
6781 */
6782static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006783{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006784 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006785
Olivier Houchard0ff28652019-06-24 18:57:39 +02006786 if (!ctx)
6787 return -1;
6788
Willy Tarreau113d52b2020-01-10 09:20:26 +01006789 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006790 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006791
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006792 ctx->subs = es;
6793 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006794
6795 /* we may have to subscribe to lower layers for new events */
6796 event_type &= ~ctx->wait_event.events;
6797 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6798 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006799 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006800}
6801
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006802/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6803 * The <es> pointer is not allowed to differ from the one passed to the
6804 * subscribe() call. It always returns zero.
6805 */
6806static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006807{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006808 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006809
Willy Tarreau113d52b2020-01-10 09:20:26 +01006810 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006811 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006812
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006813 es->events &= ~event_type;
6814 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006815 ctx->subs = NULL;
6816
6817 /* If we subscribed, and we're not doing the handshake,
6818 * then we subscribed because the upper layer asked for it,
6819 * as the upper layer is no longer interested, we can
6820 * unsubscribe too.
6821 */
6822 event_type &= ctx->wait_event.events;
6823 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6824 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006825
6826 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006827}
6828
Olivier Houchard2e055482019-05-27 19:50:12 +02006829/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6830 * Returns 0 on success, and non-zero on failure.
6831 */
6832static 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)
6833{
6834 struct ssl_sock_ctx *ctx = xprt_ctx;
6835
6836 if (oldxprt_ops != NULL)
6837 *oldxprt_ops = ctx->xprt;
6838 if (oldxprt_ctx != NULL)
6839 *oldxprt_ctx = ctx->xprt_ctx;
6840 ctx->xprt = toadd_ops;
6841 ctx->xprt_ctx = toadd_ctx;
6842 return 0;
6843}
6844
Olivier Houchard5149b592019-05-23 17:47:36 +02006845/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6846 * return 0, otherwise just call the remove_xprt method from the underlying
6847 * XPRT.
6848 */
6849static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6850{
6851 struct ssl_sock_ctx *ctx = xprt_ctx;
6852
6853 if (ctx->xprt_ctx == toremove_ctx) {
6854 ctx->xprt_ctx = newctx;
6855 ctx->xprt = newops;
6856 return 0;
6857 }
6858 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6859}
6860
Olivier Houchardea8dd942019-05-20 14:02:16 +02006861static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6862{
6863 struct ssl_sock_ctx *ctx = context;
6864
6865 /* First if we're doing an handshake, try that */
6866 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6867 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6868 /* If we had an error, or the handshake is done and I/O is available,
6869 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006870 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006871 * we can't be sure conn_fd_handler() will be called again.
6872 */
6873 if ((ctx->conn->flags & CO_FL_ERROR) ||
6874 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6875 int ret = 0;
6876 int woke = 0;
6877
6878 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006879 if (ctx->subs) {
6880 tasklet_wakeup(ctx->subs->tasklet);
6881 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006882 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006883 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006884 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006885
Olivier Houchardea8dd942019-05-20 14:02:16 +02006886 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006887 * upper layers know. If we have no mux, create it,
6888 * and once we have a mux, call its wake method if we didn't
6889 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006890 */
6891 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006892 if (!ctx->conn->mux)
6893 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006894 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6895 ctx->conn->mux->wake(ctx->conn);
6896 return NULL;
6897 }
6898 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006899#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6900 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006901 else if (b_data(&ctx->early_buf) && ctx->subs &&
6902 ctx->subs->events & SUB_RETRY_RECV) {
6903 tasklet_wakeup(ctx->subs->tasklet);
6904 ctx->subs->events &= ~SUB_RETRY_RECV;
6905 if (!ctx->subs->events)
6906 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006907 }
6908#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006909 return NULL;
6910}
6911
Emeric Brun46591952012-05-18 15:47:34 +02006912/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006913 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006914 * buffer wraps, in which case a second call may be performed. The connection's
6915 * flags are updated with whatever special event is detected (error, read0,
6916 * empty). The caller is responsible for taking care of those events and
6917 * avoiding the call if inappropriate. The function does not call the
6918 * connection's polling update function, so the caller is responsible for this.
6919 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006920static 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 +02006921{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006922 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006923 ssize_t ret;
6924 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006925
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006926 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006927 goto out_error;
6928
Olivier Houchard54907bb2019-12-19 15:02:39 +01006929#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6930 if (b_data(&ctx->early_buf)) {
6931 try = b_contig_space(buf);
6932 if (try > b_data(&ctx->early_buf))
6933 try = b_data(&ctx->early_buf);
6934 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6935 b_add(buf, try);
6936 b_del(&ctx->early_buf, try);
6937 if (b_data(&ctx->early_buf) == 0)
6938 b_free(&ctx->early_buf);
6939 return try;
6940 }
6941#endif
6942
Willy Tarreau911db9b2020-01-23 16:27:54 +01006943 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006944 /* a handshake was requested */
6945 return 0;
6946
Emeric Brun46591952012-05-18 15:47:34 +02006947 /* read the largest possible block. For this, we perform only one call
6948 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6949 * in which case we accept to do it once again. A new attempt is made on
6950 * EINTR too.
6951 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006952 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006953
Willy Tarreau591d4452018-06-15 17:21:00 +02006954 try = b_contig_space(buf);
6955 if (!try)
6956 break;
6957
Willy Tarreauabf08d92014-01-14 11:31:27 +01006958 if (try > count)
6959 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006960
Olivier Houchard66ab4982019-02-26 18:37:15 +01006961 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006962
Emeric Brune1f38db2012-09-03 20:36:47 +02006963 if (conn->flags & CO_FL_ERROR) {
6964 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006965 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006966 }
Emeric Brun46591952012-05-18 15:47:34 +02006967 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006968 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006969 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006970 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006971 }
Emeric Brun46591952012-05-18 15:47:34 +02006972 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006973 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006974 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006975 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006976 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006977 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006978#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006979 /* Async mode can be re-enabled, because we're leaving data state.*/
6980 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006981 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006982#endif
Emeric Brun46591952012-05-18 15:47:34 +02006983 break;
6984 }
6985 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006986 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006987 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6988 SUB_RETRY_RECV,
6989 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006990 /* handshake is running, and it may need to re-enable read */
6991 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006992#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006993 /* Async mode can be re-enabled, because we're leaving data state.*/
6994 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006995 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006996#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006997 break;
6998 }
Emeric Brun46591952012-05-18 15:47:34 +02006999 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007000 } else if (ret == SSL_ERROR_ZERO_RETURN)
7001 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007002 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7003 * stack before shutting down the connection for
7004 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007005 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7006 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007007 /* otherwise it's a real error */
7008 goto out_error;
7009 }
7010 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007011 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007012 return done;
7013
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007014 clear_ssl_error:
7015 /* Clear openssl global errors stack */
7016 ssl_sock_dump_errors(conn);
7017 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007018 read0:
7019 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007020 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007021
Emeric Brun46591952012-05-18 15:47:34 +02007022 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007023 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007024 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007025 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007026 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007027 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007028}
7029
7030
Willy Tarreau787db9a2018-06-14 18:31:46 +02007031/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7032 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7033 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007034 * Only one call to send() is performed, unless the buffer wraps, in which case
7035 * a second call may be performed. The connection's flags are updated with
7036 * whatever special event is detected (error, empty). The caller is responsible
7037 * for taking care of those events and avoiding the call if inappropriate. The
7038 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007039 * is responsible for this. The buffer's output is not adjusted, it's up to the
7040 * caller to take care of this. It's up to the caller to update the buffer's
7041 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007042 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007043static 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 +02007044{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007045 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007046 ssize_t ret;
7047 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007048
7049 done = 0;
7050
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007051 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007052 goto out_error;
7053
Willy Tarreau911db9b2020-01-23 16:27:54 +01007054 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007055 /* a handshake was requested */
7056 return 0;
7057
7058 /* send the largest possible block. For this we perform only one call
7059 * to send() unless the buffer wraps and we exactly fill the first hunk,
7060 * in which case we accept to do it once again.
7061 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007062 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007063#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007064 size_t written_data;
7065#endif
7066
Willy Tarreau787db9a2018-06-14 18:31:46 +02007067 try = b_contig_data(buf, done);
7068 if (try > count)
7069 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007070
Willy Tarreau7bed9452014-02-02 02:00:24 +01007071 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007072 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007073 global_ssl.max_record && try > global_ssl.max_record) {
7074 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007075 }
7076 else {
7077 /* we need to keep the information about the fact that
7078 * we're not limiting the upcoming send(), because if it
7079 * fails, we'll have to retry with at least as many data.
7080 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007081 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007082 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007083
Willy Tarreau5db847a2019-05-09 14:13:35 +02007084#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007085 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007086 unsigned int max_early;
7087
Olivier Houchard522eea72017-11-03 16:27:47 +01007088 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007089 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007090 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007091 if (SSL_get0_session(ctx->ssl))
7092 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007093 else
7094 max_early = 0;
7095 }
7096
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007097 if (try + ctx->sent_early_data > max_early) {
7098 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007099 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007100 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007101 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007102 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007103 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007104 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007105 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007106 if (ret == 1) {
7107 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007108 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007109 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007110 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007111 /* Initiate the handshake, now */
7112 tasklet_wakeup(ctx->wait_event.tasklet);
7113 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007114
Olivier Houchardc2aae742017-09-22 18:26:28 +02007115 }
7116
7117 } else
7118#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007119 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007120
Emeric Brune1f38db2012-09-03 20:36:47 +02007121 if (conn->flags & CO_FL_ERROR) {
7122 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007123 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007124 }
Emeric Brun46591952012-05-18 15:47:34 +02007125 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007126 /* A send succeeded, so we can consider ourself connected */
7127 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007128 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007129 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007130 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007131 }
7132 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007133 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007134
Emeric Brun46591952012-05-18 15:47:34 +02007135 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007136 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007137 /* handshake is running, and it may need to re-enable write */
7138 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007139 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007140#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007141 /* Async mode can be re-enabled, because we're leaving data state.*/
7142 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007143 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007144#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007145 break;
7146 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007147
Emeric Brun46591952012-05-18 15:47:34 +02007148 break;
7149 }
7150 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007151 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007152 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007153 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7154 SUB_RETRY_RECV,
7155 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007156#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007157 /* Async mode can be re-enabled, because we're leaving data state.*/
7158 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007159 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007160#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007161 break;
7162 }
Emeric Brun46591952012-05-18 15:47:34 +02007163 goto out_error;
7164 }
7165 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007166 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007167 return done;
7168
7169 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007170 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007171 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007172 ERR_clear_error();
7173
Emeric Brun46591952012-05-18 15:47:34 +02007174 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007175 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007176}
7177
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007178static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007179
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007180 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007181
Olivier Houchardea8dd942019-05-20 14:02:16 +02007182
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007183 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007184 if (ctx->wait_event.events != 0)
7185 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7186 ctx->wait_event.events,
7187 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007188 if (ctx->subs) {
7189 ctx->subs->events = 0;
7190 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007191 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007192
Olivier Houchard692c1d02019-05-23 18:41:47 +02007193 if (ctx->xprt->close)
7194 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007195#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007196 if (global_ssl.async) {
7197 OSSL_ASYNC_FD all_fd[32], afd;
7198 size_t num_all_fds = 0;
7199 int i;
7200
Olivier Houchard66ab4982019-02-26 18:37:15 +01007201 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007202 if (num_all_fds > 32) {
7203 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7204 return;
7205 }
7206
Olivier Houchard66ab4982019-02-26 18:37:15 +01007207 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007208
7209 /* If an async job is pending, we must try to
7210 to catch the end using polling before calling
7211 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007212 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007213 for (i=0 ; i < num_all_fds ; i++) {
7214 /* switch on an handler designed to
7215 * handle the SSL_free
7216 */
7217 afd = all_fd[i];
7218 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007219 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007220 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007221 /* To ensure that the fd cache won't be used
7222 * and we'll catch a real RD event.
7223 */
7224 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007225 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007226 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007227 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007228 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007229 return;
7230 }
Emeric Brun3854e012017-05-17 20:42:48 +02007231 /* Else we can remove the fds from the fdtab
7232 * and call SSL_free.
7233 * note: we do a fd_remove and not a delete
7234 * because the fd is owned by the engine.
7235 * the engine is responsible to close
7236 */
7237 for (i=0 ; i < num_all_fds ; i++)
7238 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007239 }
7240#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007241 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007242 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007243 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007244 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007245 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007246 }
Emeric Brun46591952012-05-18 15:47:34 +02007247}
7248
7249/* This function tries to perform a clean shutdown on an SSL connection, and in
7250 * any case, flags the connection as reusable if no handshake was in progress.
7251 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007252static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007253{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007254 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007255
Willy Tarreau911db9b2020-01-23 16:27:54 +01007256 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007257 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007258 if (!clean)
7259 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007260 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007261 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007262 if (SSL_shutdown(ctx->ssl) <= 0) {
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}
7268
William Lallemandd4f946c2019-12-05 10:26:40 +01007269/* fill a buffer with the algorithm and size of a public key */
7270static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007271{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007272 int bits = 0;
7273 int sig = TLSEXT_signature_anonymous;
7274 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007275 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007276
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007277 pkey = X509_get_pubkey(crt);
7278 if (pkey) {
7279 bits = EVP_PKEY_bits(pkey);
7280 switch(EVP_PKEY_base_id(pkey)) {
7281 case EVP_PKEY_RSA:
7282 sig = TLSEXT_signature_rsa;
7283 break;
7284 case EVP_PKEY_EC:
7285 sig = TLSEXT_signature_ecdsa;
7286 break;
7287 case EVP_PKEY_DSA:
7288 sig = TLSEXT_signature_dsa;
7289 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007290 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007291 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007292 }
7293
7294 switch(sig) {
7295 case TLSEXT_signature_rsa:
7296 len = chunk_printf(out, "RSA%d", bits);
7297 break;
7298 case TLSEXT_signature_ecdsa:
7299 len = chunk_printf(out, "EC%d", bits);
7300 break;
7301 case TLSEXT_signature_dsa:
7302 len = chunk_printf(out, "DSA%d", bits);
7303 break;
7304 default:
7305 return 0;
7306 }
7307 if (len < 0)
7308 return 0;
7309 return 1;
7310}
7311
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007312/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007313int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7314{
7315 struct ssl_sock_ctx *ctx;
7316 X509 *crt;
7317
7318 if (!ssl_sock_is_ssl(conn))
7319 return 0;
7320
7321 ctx = conn->xprt_ctx;
7322
7323 crt = SSL_get_certificate(ctx->ssl);
7324 if (!crt)
7325 return 0;
7326
7327 return cert_get_pkey_algo(crt, out);
7328}
7329
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007330/* used for ppv2 cert signature (can be used for logging) */
7331const char *ssl_sock_get_cert_sig(struct connection *conn)
7332{
Christopher Faulet82004142019-09-10 10:12:03 +02007333 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007334
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007335 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7336 X509 *crt;
7337
7338 if (!ssl_sock_is_ssl(conn))
7339 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007340 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007341 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007342 if (!crt)
7343 return NULL;
7344 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7345 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7346}
7347
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007348/* used for ppv2 authority */
7349const char *ssl_sock_get_sni(struct connection *conn)
7350{
7351#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007352 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007353
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007354 if (!ssl_sock_is_ssl(conn))
7355 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007356 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007357 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007358#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007359 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007360#endif
7361}
7362
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007363/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007364const char *ssl_sock_get_cipher_name(struct connection *conn)
7365{
Christopher Faulet82004142019-09-10 10:12:03 +02007366 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007367
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007368 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007369 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007370 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007371 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007372}
7373
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007374/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007375const char *ssl_sock_get_proto_version(struct connection *conn)
7376{
Christopher Faulet82004142019-09-10 10:12:03 +02007377 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007378
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007379 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007380 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007381 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007382 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007383}
7384
Willy Tarreau8d598402012-10-22 17:58:39 +02007385/* Extract a serial from a cert, and copy it to a chunk.
7386 * Returns 1 if serial is found and copied, 0 if no serial found and
7387 * -1 if output is not large enough.
7388 */
7389static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007390ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007391{
7392 ASN1_INTEGER *serial;
7393
7394 serial = X509_get_serialNumber(crt);
7395 if (!serial)
7396 return 0;
7397
7398 if (out->size < serial->length)
7399 return -1;
7400
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007401 memcpy(out->area, serial->data, serial->length);
7402 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007403 return 1;
7404}
7405
Emeric Brun43e79582014-10-29 19:03:26 +01007406/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007407 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7408 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007409 */
7410static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007411ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007412{
7413 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007414 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007415
7416 len =i2d_X509(crt, NULL);
7417 if (len <= 0)
7418 return 1;
7419
7420 if (out->size < len)
7421 return -1;
7422
7423 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007424 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007425 return 1;
7426}
7427
Emeric Brunce5ad802012-10-22 14:11:22 +02007428
Willy Tarreau83061a82018-07-13 11:56:34 +02007429/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007430 * Returns 1 if serial is found and copied, 0 if no valid time found
7431 * and -1 if output is not large enough.
7432 */
7433static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007434ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007435{
7436 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7437 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7438
7439 if (gentm->length < 12)
7440 return 0;
7441 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7442 return 0;
7443 if (out->size < gentm->length-2)
7444 return -1;
7445
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007446 memcpy(out->area, gentm->data+2, gentm->length-2);
7447 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007448 return 1;
7449 }
7450 else if (tm->type == V_ASN1_UTCTIME) {
7451 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7452
7453 if (utctm->length < 10)
7454 return 0;
7455 if (utctm->data[0] >= 0x35)
7456 return 0;
7457 if (out->size < utctm->length)
7458 return -1;
7459
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007460 memcpy(out->area, utctm->data, utctm->length);
7461 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007462 return 1;
7463 }
7464
7465 return 0;
7466}
7467
Emeric Brun87855892012-10-17 17:39:35 +02007468/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7469 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7470 */
7471static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007472ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7473 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007474{
7475 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007476 ASN1_OBJECT *obj;
7477 ASN1_STRING *data;
7478 const unsigned char *data_ptr;
7479 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007480 int i, j, n;
7481 int cur = 0;
7482 const char *s;
7483 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007484 int name_count;
7485
7486 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007487
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007488 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007489 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007490 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007491 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007492 else
7493 j = i;
7494
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007495 ne = X509_NAME_get_entry(a, j);
7496 obj = X509_NAME_ENTRY_get_object(ne);
7497 data = X509_NAME_ENTRY_get_data(ne);
7498 data_ptr = ASN1_STRING_get0_data(data);
7499 data_len = ASN1_STRING_length(data);
7500 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007501 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007502 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007503 s = tmp;
7504 }
7505
7506 if (chunk_strcasecmp(entry, s) != 0)
7507 continue;
7508
7509 if (pos < 0)
7510 cur--;
7511 else
7512 cur++;
7513
7514 if (cur != pos)
7515 continue;
7516
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007517 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007518 return -1;
7519
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007520 memcpy(out->area, data_ptr, data_len);
7521 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007522 return 1;
7523 }
7524
7525 return 0;
7526
William Lallemandd4f946c2019-12-05 10:26:40 +01007527}
7528
7529/*
7530 * Extract and format the DNS SAN extensions and copy result into a chuink
7531 * Return 0;
7532 */
7533#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7534static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7535{
7536 int i;
7537 char *str;
7538 STACK_OF(GENERAL_NAME) *names = NULL;
7539
7540 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7541 if (names) {
7542 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7543 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7544 if (i > 0)
7545 chunk_appendf(out, ", ");
7546 if (name->type == GEN_DNS) {
7547 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7548 chunk_appendf(out, "DNS:%s", str);
7549 OPENSSL_free(str);
7550 }
7551 }
7552 }
7553 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7554 }
7555 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007556}
William Lallemandd4f946c2019-12-05 10:26:40 +01007557#endif
Emeric Brun87855892012-10-17 17:39:35 +02007558
Elliot Otchet71f82972020-01-15 08:12:14 -05007559/*
7560 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7561 * Currently supports rfc2253 for returning LDAP V3 DNs.
7562 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7563 */
7564static int
7565ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7566{
7567 BIO *bio = NULL;
7568 int ret = 0;
7569 int data_len = 0;
7570
7571 if (chunk_strcmp(format, "rfc2253") == 0) {
7572 bio = BIO_new(BIO_s_mem());
7573 if (bio == NULL)
7574 goto out;
7575
7576 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7577 goto out;
7578
7579 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7580 goto out;
7581
7582 out->data = data_len;
7583
7584 ret = 1;
7585 }
7586out:
7587 if (bio)
7588 BIO_free(bio);
7589 return ret;
7590}
7591
Emeric Brun87855892012-10-17 17:39:35 +02007592/* Extract and format full DN from a X509_NAME and copy result into a chunk
7593 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7594 */
7595static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007596ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007597{
7598 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007599 ASN1_OBJECT *obj;
7600 ASN1_STRING *data;
7601 const unsigned char *data_ptr;
7602 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007603 int i, n, ln;
7604 int l = 0;
7605 const char *s;
7606 char *p;
7607 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007608 int name_count;
7609
7610
7611 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007612
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007613 out->data = 0;
7614 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007615 for (i = 0; i < name_count; i++) {
7616 ne = X509_NAME_get_entry(a, i);
7617 obj = X509_NAME_ENTRY_get_object(ne);
7618 data = X509_NAME_ENTRY_get_data(ne);
7619 data_ptr = ASN1_STRING_get0_data(data);
7620 data_len = ASN1_STRING_length(data);
7621 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007622 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007623 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007624 s = tmp;
7625 }
7626 ln = strlen(s);
7627
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007628 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007629 if (l > out->size)
7630 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007631 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007632
7633 *(p++)='/';
7634 memcpy(p, s, ln);
7635 p += ln;
7636 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007637 memcpy(p, data_ptr, data_len);
7638 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007639 }
7640
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007641 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007642 return 0;
7643
7644 return 1;
7645}
7646
Olivier Houchardab28a322018-12-21 19:45:40 +01007647void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7648{
7649#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007650 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007651
Olivier Houcharde488ea82019-06-28 14:10:33 +02007652 if (!ssl_sock_is_ssl(conn))
7653 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007654 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007655 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007656#endif
7657}
7658
Willy Tarreau119a4082016-12-22 21:58:38 +01007659/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7660 * to disable SNI.
7661 */
Willy Tarreau63076412015-07-10 11:33:32 +02007662void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7663{
7664#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007665 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007666
Willy Tarreau119a4082016-12-22 21:58:38 +01007667 char *prev_name;
7668
Willy Tarreau63076412015-07-10 11:33:32 +02007669 if (!ssl_sock_is_ssl(conn))
7670 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007671 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007672
Willy Tarreau119a4082016-12-22 21:58:38 +01007673 /* if the SNI changes, we must destroy the reusable context so that a
7674 * new connection will present a new SNI. As an optimization we could
7675 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7676 * server.
7677 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007678 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007679 if ((!prev_name && hostname) ||
7680 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007681 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007682
Olivier Houchard66ab4982019-02-26 18:37:15 +01007683 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007684#endif
7685}
7686
Emeric Brun0abf8362014-06-24 18:26:41 +02007687/* Extract peer certificate's common name into the chunk dest
7688 * Returns
7689 * the len of the extracted common name
7690 * or 0 if no CN found in DN
7691 * or -1 on error case (i.e. no peer certificate)
7692 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007693int ssl_sock_get_remote_common_name(struct connection *conn,
7694 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007695{
Christopher Faulet82004142019-09-10 10:12:03 +02007696 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007697 X509 *crt = NULL;
7698 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007699 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007700 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007701 .area = (char *)&find_cn,
7702 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007703 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007704 int result = -1;
David Safb76832014-05-08 23:42:08 -04007705
7706 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007707 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007708 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007709
7710 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007711 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007712 if (!crt)
7713 goto out;
7714
7715 name = X509_get_subject_name(crt);
7716 if (!name)
7717 goto out;
David Safb76832014-05-08 23:42:08 -04007718
Emeric Brun0abf8362014-06-24 18:26:41 +02007719 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7720out:
David Safb76832014-05-08 23:42:08 -04007721 if (crt)
7722 X509_free(crt);
7723
7724 return result;
7725}
7726
Dave McCowan328fb582014-07-30 10:39:13 -04007727/* returns 1 if client passed a certificate for this session, 0 if not */
7728int ssl_sock_get_cert_used_sess(struct connection *conn)
7729{
Christopher Faulet82004142019-09-10 10:12:03 +02007730 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007731 X509 *crt = NULL;
7732
7733 if (!ssl_sock_is_ssl(conn))
7734 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007735 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007736
7737 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007738 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007739 if (!crt)
7740 return 0;
7741
7742 X509_free(crt);
7743 return 1;
7744}
7745
7746/* returns 1 if client passed a certificate for this connection, 0 if not */
7747int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007748{
Christopher Faulet82004142019-09-10 10:12:03 +02007749 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007750
David Safb76832014-05-08 23:42:08 -04007751 if (!ssl_sock_is_ssl(conn))
7752 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007753 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007754 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007755}
7756
7757/* returns result from SSL verify */
7758unsigned int ssl_sock_get_verify_result(struct connection *conn)
7759{
Christopher Faulet82004142019-09-10 10:12:03 +02007760 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007761
David Safb76832014-05-08 23:42:08 -04007762 if (!ssl_sock_is_ssl(conn))
7763 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007764 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007765 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007766}
7767
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007768/* Returns the application layer protocol name in <str> and <len> when known.
7769 * Zero is returned if the protocol name was not found, otherwise non-zero is
7770 * returned. The string is allocated in the SSL context and doesn't have to be
7771 * freed by the caller. NPN is also checked if available since older versions
7772 * of openssl (1.0.1) which are more common in field only support this one.
7773 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007774static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007775{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007776#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7777 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007778 struct ssl_sock_ctx *ctx = xprt_ctx;
7779 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007780 return 0;
7781
7782 *str = NULL;
7783
7784#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007785 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007786 if (*str)
7787 return 1;
7788#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007789#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007790 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007791 if (*str)
7792 return 1;
7793#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007794#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007795 return 0;
7796}
7797
Willy Tarreau7875d092012-09-10 08:20:03 +02007798/***** Below are some sample fetching functions for ACL/patterns *****/
7799
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007800static int
7801smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7802{
7803 struct connection *conn;
7804
7805 conn = objt_conn(smp->sess->origin);
7806 if (!conn || conn->xprt != &ssl_sock)
7807 return 0;
7808
7809 smp->flags = 0;
7810 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007811#ifdef OPENSSL_IS_BORINGSSL
7812 {
7813 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7814 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7815 SSL_early_data_accepted(ctx->ssl));
7816 }
7817#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007818 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007819 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007820#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007821 return 1;
7822}
7823
Emeric Brune64aef12012-09-21 13:15:06 +02007824/* boolean, returns true if client cert was present */
7825static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007826smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007827{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007828 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007829 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007830
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007831 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007832 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007833 return 0;
7834
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007835 ctx = conn->xprt_ctx;
7836
Willy Tarreau911db9b2020-01-23 16:27:54 +01007837 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007838 smp->flags |= SMP_F_MAY_CHANGE;
7839 return 0;
7840 }
7841
7842 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007843 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007844 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007845
7846 return 1;
7847}
7848
Emeric Brun43e79582014-10-29 19:03:26 +01007849/* binary, returns a certificate in a binary chunk (der/raw).
7850 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7851 * should be use.
7852 */
7853static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007854smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007855{
7856 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7857 X509 *crt = NULL;
7858 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007859 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007860 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007861 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007862
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007863 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007864 if (!conn || conn->xprt != &ssl_sock)
7865 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007866 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007867
Willy Tarreau911db9b2020-01-23 16:27:54 +01007868 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007869 smp->flags |= SMP_F_MAY_CHANGE;
7870 return 0;
7871 }
7872
7873 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007874 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007875 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007876 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007877
7878 if (!crt)
7879 goto out;
7880
7881 smp_trash = get_trash_chunk();
7882 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7883 goto out;
7884
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007885 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007886 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007887 ret = 1;
7888out:
7889 /* SSL_get_peer_certificate, it increase X509 * ref count */
7890 if (cert_peer && crt)
7891 X509_free(crt);
7892 return ret;
7893}
7894
Emeric Brunba841a12014-04-30 17:05:08 +02007895/* binary, returns serial of certificate in a binary chunk.
7896 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7897 * should be use.
7898 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007899static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007900smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007901{
Emeric Brunba841a12014-04-30 17:05:08 +02007902 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007903 X509 *crt = NULL;
7904 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007905 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007906 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007907 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007908
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007909 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007910 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007911 return 0;
7912
Olivier Houchard66ab4982019-02-26 18:37:15 +01007913 ctx = conn->xprt_ctx;
7914
Willy Tarreau911db9b2020-01-23 16:27:54 +01007915 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007916 smp->flags |= SMP_F_MAY_CHANGE;
7917 return 0;
7918 }
7919
Emeric Brunba841a12014-04-30 17:05:08 +02007920 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007921 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007922 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007923 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007924
Willy Tarreau8d598402012-10-22 17:58:39 +02007925 if (!crt)
7926 goto out;
7927
Willy Tarreau47ca5452012-12-23 20:22:19 +01007928 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007929 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7930 goto out;
7931
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007932 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007933 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007934 ret = 1;
7935out:
Emeric Brunba841a12014-04-30 17:05:08 +02007936 /* SSL_get_peer_certificate, it increase X509 * ref count */
7937 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007938 X509_free(crt);
7939 return ret;
7940}
Emeric Brune64aef12012-09-21 13:15:06 +02007941
Emeric Brunba841a12014-04-30 17:05:08 +02007942/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7943 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7944 * should be use.
7945 */
James Votha051b4a2013-05-14 20:37:59 +02007946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007947smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007948{
Emeric Brunba841a12014-04-30 17:05:08 +02007949 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007950 X509 *crt = NULL;
7951 const EVP_MD *digest;
7952 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01007953 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007954 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007955 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007956 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007957
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007958 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007959 if (!conn || conn->xprt != &ssl_sock)
7960 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007961 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007962
Willy Tarreau911db9b2020-01-23 16:27:54 +01007963 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02007964 smp->flags |= SMP_F_MAY_CHANGE;
7965 return 0;
7966 }
7967
Emeric Brunba841a12014-04-30 17:05:08 +02007968 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007969 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007970 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007971 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007972 if (!crt)
7973 goto out;
7974
7975 smp_trash = get_trash_chunk();
7976 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01007977 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
7978 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007979 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007980 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007981 ret = 1;
7982out:
Emeric Brunba841a12014-04-30 17:05:08 +02007983 /* SSL_get_peer_certificate, it increase X509 * ref count */
7984 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007985 X509_free(crt);
7986 return ret;
7987}
7988
Emeric Brunba841a12014-04-30 17:05:08 +02007989/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7990 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7991 * should be use.
7992 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007993static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007994smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007995{
Emeric Brunba841a12014-04-30 17:05:08 +02007996 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007997 X509 *crt = NULL;
7998 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007999 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008000 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008001 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008002
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008003 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008004 if (!conn || conn->xprt != &ssl_sock)
8005 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008006 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008007
Willy Tarreau911db9b2020-01-23 16:27:54 +01008008 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +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 Brunce5ad802012-10-22 14:11:22 +02008017 if (!crt)
8018 goto out;
8019
Willy Tarreau47ca5452012-12-23 20:22:19 +01008020 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008021 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008022 goto out;
8023
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008024 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008025 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008026 ret = 1;
8027out:
Emeric Brunba841a12014-04-30 17:05:08 +02008028 /* SSL_get_peer_certificate, it increase X509 * ref count */
8029 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008030 X509_free(crt);
8031 return ret;
8032}
8033
Emeric Brunba841a12014-04-30 17:05:08 +02008034/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8035 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8036 * should be use.
8037 */
Emeric Brun87855892012-10-17 17:39:35 +02008038static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008039smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008040{
Emeric Brunba841a12014-04-30 17:05:08 +02008041 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008042 X509 *crt = NULL;
8043 X509_NAME *name;
8044 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008045 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008046 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008047 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008048
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008049 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008050 if (!conn || conn->xprt != &ssl_sock)
8051 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008052 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008053
Willy Tarreau911db9b2020-01-23 16:27:54 +01008054 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008055 smp->flags |= SMP_F_MAY_CHANGE;
8056 return 0;
8057 }
8058
Emeric Brunba841a12014-04-30 17:05:08 +02008059 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008060 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008061 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008062 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008063 if (!crt)
8064 goto out;
8065
8066 name = X509_get_issuer_name(crt);
8067 if (!name)
8068 goto out;
8069
Willy Tarreau47ca5452012-12-23 20:22:19 +01008070 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008071 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008072 int pos = 1;
8073
8074 if (args[1].type == ARGT_SINT)
8075 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008076
8077 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8078 goto out;
8079 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008080 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8081 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8082 goto out;
8083 }
Emeric Brun87855892012-10-17 17:39:35 +02008084 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8085 goto out;
8086
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008087 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008088 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008089 ret = 1;
8090out:
Emeric Brunba841a12014-04-30 17:05:08 +02008091 /* SSL_get_peer_certificate, it increase X509 * ref count */
8092 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008093 X509_free(crt);
8094 return ret;
8095}
8096
Emeric Brunba841a12014-04-30 17:05:08 +02008097/* string, returns notbefore date in ASN1_UTCTIME format.
8098 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8099 * should be use.
8100 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008101static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008102smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008103{
Emeric Brunba841a12014-04-30 17:05:08 +02008104 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008105 X509 *crt = NULL;
8106 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008107 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008108 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008109 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008110
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008111 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008112 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008113 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008114 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008115
Willy Tarreau911db9b2020-01-23 16:27:54 +01008116 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008117 smp->flags |= SMP_F_MAY_CHANGE;
8118 return 0;
8119 }
8120
Emeric Brunba841a12014-04-30 17:05:08 +02008121 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008122 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008123 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008124 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008125 if (!crt)
8126 goto out;
8127
Willy Tarreau47ca5452012-12-23 20:22:19 +01008128 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008129 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008130 goto out;
8131
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008132 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008133 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008134 ret = 1;
8135out:
Emeric Brunba841a12014-04-30 17:05:08 +02008136 /* SSL_get_peer_certificate, it increase X509 * ref count */
8137 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008138 X509_free(crt);
8139 return ret;
8140}
8141
Emeric Brunba841a12014-04-30 17:05:08 +02008142/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8143 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8144 * should be use.
8145 */
Emeric Brun87855892012-10-17 17:39:35 +02008146static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008147smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008148{
Emeric Brunba841a12014-04-30 17:05:08 +02008149 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008150 X509 *crt = NULL;
8151 X509_NAME *name;
8152 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008153 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008154 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008155 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008156
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008157 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008158 if (!conn || conn->xprt != &ssl_sock)
8159 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008160 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008161
Willy Tarreau911db9b2020-01-23 16:27:54 +01008162 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008163 smp->flags |= SMP_F_MAY_CHANGE;
8164 return 0;
8165 }
8166
Emeric Brunba841a12014-04-30 17:05:08 +02008167 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008168 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008169 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008170 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008171 if (!crt)
8172 goto out;
8173
8174 name = X509_get_subject_name(crt);
8175 if (!name)
8176 goto out;
8177
Willy Tarreau47ca5452012-12-23 20:22:19 +01008178 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008179 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008180 int pos = 1;
8181
8182 if (args[1].type == ARGT_SINT)
8183 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008184
8185 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8186 goto out;
8187 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008188 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8189 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8190 goto out;
8191 }
Emeric Brun87855892012-10-17 17:39:35 +02008192 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8193 goto out;
8194
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008195 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008196 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008197 ret = 1;
8198out:
Emeric Brunba841a12014-04-30 17:05:08 +02008199 /* SSL_get_peer_certificate, it increase X509 * ref count */
8200 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008201 X509_free(crt);
8202 return ret;
8203}
Emeric Brun9143d372012-12-20 15:44:16 +01008204
8205/* integer, returns true if current session use a client certificate */
8206static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008207smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008208{
8209 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008210 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008211 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008212
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008213 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008214 if (!conn || conn->xprt != &ssl_sock)
8215 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008216 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008217
Willy Tarreau911db9b2020-01-23 16:27:54 +01008218 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008219 smp->flags |= SMP_F_MAY_CHANGE;
8220 return 0;
8221 }
8222
8223 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008224 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008225 if (crt) {
8226 X509_free(crt);
8227 }
8228
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008229 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008230 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008231 return 1;
8232}
8233
Emeric Brunba841a12014-04-30 17:05:08 +02008234/* integer, returns the certificate version
8235 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8236 * should be use.
8237 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008238static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008239smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008240{
Emeric Brunba841a12014-04-30 17:05:08 +02008241 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008242 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008243 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008244 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008245
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008246 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008247 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008248 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008249 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008250
Willy Tarreau911db9b2020-01-23 16:27:54 +01008251 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008252 smp->flags |= SMP_F_MAY_CHANGE;
8253 return 0;
8254 }
8255
Emeric Brunba841a12014-04-30 17:05:08 +02008256 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008257 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008258 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008259 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008260 if (!crt)
8261 return 0;
8262
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008263 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008264 /* SSL_get_peer_certificate increase X509 * ref count */
8265 if (cert_peer)
8266 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008267 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008268
8269 return 1;
8270}
8271
Emeric Brunba841a12014-04-30 17:05:08 +02008272/* string, returns the certificate's signature algorithm.
8273 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8274 * should be use.
8275 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008276static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008277smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008278{
Emeric Brunba841a12014-04-30 17:05:08 +02008279 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008280 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008281 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008282 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008283 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008284 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008285
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008286 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008287 if (!conn || conn->xprt != &ssl_sock)
8288 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008289 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008290
Willy Tarreau911db9b2020-01-23 16:27:54 +01008291 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008292 smp->flags |= SMP_F_MAY_CHANGE;
8293 return 0;
8294 }
8295
Emeric Brunba841a12014-04-30 17:05:08 +02008296 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008297 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008298 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008299 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008300 if (!crt)
8301 return 0;
8302
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008303 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8304 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008305
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008306 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8307 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008308 /* SSL_get_peer_certificate increase X509 * ref count */
8309 if (cert_peer)
8310 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008311 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008312 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008313
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008314 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008315 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008316 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008317 /* SSL_get_peer_certificate increase X509 * ref count */
8318 if (cert_peer)
8319 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008320
8321 return 1;
8322}
8323
Emeric Brunba841a12014-04-30 17:05:08 +02008324/* string, returns the certificate's key algorithm.
8325 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8326 * should be use.
8327 */
Emeric Brun521a0112012-10-22 12:22:55 +02008328static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008329smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008330{
Emeric Brunba841a12014-04-30 17:05:08 +02008331 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008332 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008333 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008334 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008335 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008336 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008337
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008338 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008339 if (!conn || conn->xprt != &ssl_sock)
8340 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008341 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008342
Willy Tarreau911db9b2020-01-23 16:27:54 +01008343 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008344 smp->flags |= SMP_F_MAY_CHANGE;
8345 return 0;
8346 }
8347
Emeric Brunba841a12014-04-30 17:05:08 +02008348 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008349 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008350 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008351 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008352 if (!crt)
8353 return 0;
8354
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008355 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8356 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008357
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008358 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8359 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008360 /* SSL_get_peer_certificate increase X509 * ref count */
8361 if (cert_peer)
8362 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008363 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008364 }
Emeric Brun521a0112012-10-22 12:22:55 +02008365
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008366 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008367 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008368 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008369 if (cert_peer)
8370 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008371
8372 return 1;
8373}
8374
Emeric Brun645ae792014-04-30 14:21:06 +02008375/* boolean, returns true if front conn. transport layer is SSL.
8376 * This function is also usable on backend conn if the fetch keyword 5th
8377 * char is 'b'.
8378 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008379static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008380smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008381{
Emeric Bruneb8def92018-02-19 15:59:48 +01008382 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8383 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008385 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008386 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008387 return 1;
8388}
8389
Emeric Brun2525b6b2012-10-18 15:59:43 +02008390/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008391static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008392smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008393{
8394#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008395 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008396 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008397
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008398 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008399 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008400 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008401 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008402 return 1;
8403#else
8404 return 0;
8405#endif
8406}
8407
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008408/* boolean, returns true if client session has been resumed.
8409 * This function is also usable on backend conn if the fetch keyword 5th
8410 * char is 'b'.
8411 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008412static int
8413smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8414{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008415 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8416 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008417 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008418
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008419
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008420 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008421 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008422 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008423 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008424 return 1;
8425}
8426
Emeric Brun645ae792014-04-30 14:21:06 +02008427/* string, returns the used cipher if front conn. transport layer is SSL.
8428 * This function is also usable on backend conn if the fetch keyword 5th
8429 * char is 'b'.
8430 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008431static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008432smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008433{
Emeric Bruneb8def92018-02-19 15:59:48 +01008434 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8435 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008436 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008437
Willy Tarreaube508f12016-03-10 11:47:01 +01008438 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008439 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008440 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008441 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008442
Olivier Houchard66ab4982019-02-26 18:37:15 +01008443 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008444 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008445 return 0;
8446
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008447 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008448 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008449 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008450
8451 return 1;
8452}
8453
Emeric Brun645ae792014-04-30 14:21:06 +02008454/* integer, returns the algoritm's keysize if front conn. transport layer
8455 * is SSL.
8456 * This function is also usable on backend conn if the fetch keyword 5th
8457 * char is 'b'.
8458 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008459static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008460smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008461{
Emeric Bruneb8def92018-02-19 15:59:48 +01008462 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8463 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008464 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008465 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008466
Emeric Brun589fcad2012-10-16 14:13:26 +02008467 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008468 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008469 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008470 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008471
Olivier Houchard66ab4982019-02-26 18:37:15 +01008472 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008473 return 0;
8474
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008475 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008476 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008477
8478 return 1;
8479}
8480
Emeric Brun645ae792014-04-30 14:21:06 +02008481/* integer, returns the used keysize if front conn. transport layer is SSL.
8482 * This function is also usable on backend conn if the fetch keyword 5th
8483 * char is 'b'.
8484 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008485static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008486smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008487{
Emeric Bruneb8def92018-02-19 15:59:48 +01008488 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8489 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008490 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008491
Emeric Brun589fcad2012-10-16 14:13:26 +02008492 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008493 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8494 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008495 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008496
Olivier Houchard66ab4982019-02-26 18:37:15 +01008497 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008498 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008499 return 0;
8500
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008501 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008502
8503 return 1;
8504}
8505
Bernard Spil13c53f82018-02-15 13:34:58 +01008506#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008507static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008508smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008509{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008510 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008511 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008512 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008513
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008514 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008515 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008516
Olivier Houchard6b77f492018-11-22 18:18:29 +01008517 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8518 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008519 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8520 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008521 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008522
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008523 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008524 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008525 (const unsigned char **)&smp->data.u.str.area,
8526 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008527
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008528 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008529 return 0;
8530
Willy Tarreau105599c2020-02-25 08:59:23 +01008531 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008532 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008533}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008534#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008535
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008536#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008537static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008538smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008539{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008540 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008541 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008542 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008543
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008544 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008545 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008546
Olivier Houchard6b77f492018-11-22 18:18:29 +01008547 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8548 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8549
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008550 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008551 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008552 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008553
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008554 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008555 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008556 (const unsigned char **)&smp->data.u.str.area,
8557 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008558
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008559 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008560 return 0;
8561
Willy Tarreau105599c2020-02-25 08:59:23 +01008562 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008563 return 1;
8564}
8565#endif
8566
Emeric Brun645ae792014-04-30 14:21:06 +02008567/* string, returns the used protocol if front conn. transport layer is SSL.
8568 * This function is also usable on backend conn if the fetch keyword 5th
8569 * char is 'b'.
8570 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008571static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008572smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008573{
Emeric Bruneb8def92018-02-19 15:59:48 +01008574 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8575 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008576 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008577
Emeric Brun589fcad2012-10-16 14:13:26 +02008578 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008579 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8580 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008581 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008582
Olivier Houchard66ab4982019-02-26 18:37:15 +01008583 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008584 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008585 return 0;
8586
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008587 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008588 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008589 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008590
8591 return 1;
8592}
8593
Willy Tarreau87b09662015-04-03 00:22:06 +02008594/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008595 * This function is also usable on backend conn if the fetch keyword 5th
8596 * char is 'b'.
8597 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008598#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008599static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008600smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008601{
Emeric Bruneb8def92018-02-19 15:59:48 +01008602 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8603 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008604 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008605 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008606 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008607
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008608 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008609 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008610
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008611 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8612 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008613 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008614
Olivier Houchard66ab4982019-02-26 18:37:15 +01008615 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008616 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008617 return 0;
8618
Willy Tarreau105599c2020-02-25 08:59:23 +01008619 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008620 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008621 return 0;
8622
Willy Tarreau105599c2020-02-25 08:59:23 +01008623 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008624 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008625}
Patrick Hemmer41966772018-04-28 19:15:48 -04008626#endif
8627
Emeric Brunfe68f682012-10-16 14:59:28 +02008628
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008629#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008630static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008631smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8632{
8633 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8634 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8635 struct buffer *data;
8636 struct ssl_sock_ctx *ctx;
8637
8638 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8639 return 0;
8640 ctx = conn->xprt_ctx;
8641
8642 data = get_trash_chunk();
8643 if (kw[7] == 'c')
8644 data->data = SSL_get_client_random(ctx->ssl,
8645 (unsigned char *) data->area,
8646 data->size);
8647 else
8648 data->data = SSL_get_server_random(ctx->ssl,
8649 (unsigned char *) data->area,
8650 data->size);
8651 if (!data->data)
8652 return 0;
8653
8654 smp->flags = 0;
8655 smp->data.type = SMP_T_BIN;
8656 smp->data.u.str = *data;
8657
8658 return 1;
8659}
8660
8661static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008662smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8663{
8664 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8665 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8666 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008667 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008668 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008669
8670 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8671 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008672 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008673
Olivier Houchard66ab4982019-02-26 18:37:15 +01008674 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008675 if (!ssl_sess)
8676 return 0;
8677
8678 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008679 data->data = SSL_SESSION_get_master_key(ssl_sess,
8680 (unsigned char *) data->area,
8681 data->size);
8682 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008683 return 0;
8684
8685 smp->flags = 0;
8686 smp->data.type = SMP_T_BIN;
8687 smp->data.u.str = *data;
8688
8689 return 1;
8690}
8691#endif
8692
Patrick Hemmer41966772018-04-28 19:15:48 -04008693#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008694static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008695smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008696{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008697 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008698 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008699
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008700 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008701 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008702
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008703 conn = objt_conn(smp->sess->origin);
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 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008709 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008710 return 0;
8711
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008712 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008713 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008714}
Patrick Hemmer41966772018-04-28 19:15:48 -04008715#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008716
David Sc1ad52e2014-04-08 18:48:47 -04008717static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008718smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8719{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008720 struct connection *conn;
8721 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008722 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008723
8724 conn = objt_conn(smp->sess->origin);
8725 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8726 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008727 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008728
Olivier Houchard66ab4982019-02-26 18:37:15 +01008729 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008730 if (!capture)
8731 return 0;
8732
8733 smp->flags = SMP_F_CONST;
8734 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008735 smp->data.u.str.area = capture->ciphersuite;
8736 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008737 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008738}
8739
8740static int
8741smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8742{
Willy Tarreau83061a82018-07-13 11:56:34 +02008743 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008744
8745 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8746 return 0;
8747
8748 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008749 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008750 smp->data.type = SMP_T_BIN;
8751 smp->data.u.str = *data;
8752 return 1;
8753}
8754
8755static int
8756smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8757{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008758 struct connection *conn;
8759 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008760 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008761
8762 conn = objt_conn(smp->sess->origin);
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;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008766
Olivier Houchard66ab4982019-02-26 18:37:15 +01008767 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008768 if (!capture)
8769 return 0;
8770
8771 smp->data.type = SMP_T_SINT;
8772 smp->data.u.sint = capture->xxh64;
8773 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008774}
8775
8776static int
8777smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8778{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008779#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008780 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008781 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008782
8783 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8784 return 0;
8785
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008786 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008787 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008788 const char *str;
8789 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008790 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008791 uint16_t id = (bin[0] << 8) | bin[1];
8792#if defined(OPENSSL_IS_BORINGSSL)
8793 cipher = SSL_get_cipher_by_value(id);
8794#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008795 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008796 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8797 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008798#endif
8799 str = SSL_CIPHER_get_name(cipher);
8800 if (!str || strcmp(str, "(NONE)") == 0)
8801 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008802 else
8803 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8804 }
8805 smp->data.type = SMP_T_STR;
8806 smp->data.u.str = *data;
8807 return 1;
8808#else
8809 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8810#endif
8811}
8812
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008813#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008814static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008815smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008816{
Emeric Bruneb8def92018-02-19 15:59:48 +01008817 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8818 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008819 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008820 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008821 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008822
8823 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008824 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8825 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008826 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008827
Willy Tarreau911db9b2020-01-23 16:27:54 +01008828 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008829 smp->flags |= SMP_F_MAY_CHANGE;
8830 return 0;
8831 }
8832
8833 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008834 if (!SSL_session_reused(ctx->ssl))
8835 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008836 finished_trash->area,
8837 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008838 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008839 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008840 finished_trash->area,
8841 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008842
8843 if (!finished_len)
8844 return 0;
8845
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008846 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008847 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008848 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008849
8850 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008851}
Patrick Hemmer41966772018-04-28 19:15:48 -04008852#endif
David Sc1ad52e2014-04-08 18:48:47 -04008853
Emeric Brun2525b6b2012-10-18 15:59:43 +02008854/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008855static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008856smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008857{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008858 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008859 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008860
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008861 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008862 if (!conn || conn->xprt != &ssl_sock)
8863 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008864 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008865
Willy Tarreau911db9b2020-01-23 16:27:54 +01008866 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008867 smp->flags = SMP_F_MAY_CHANGE;
8868 return 0;
8869 }
8870
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008871 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008872 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008873 smp->flags = 0;
8874
8875 return 1;
8876}
8877
Emeric Brun2525b6b2012-10-18 15:59:43 +02008878/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008879static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008880smp_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 +02008881{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008882 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008883 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008884
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008885 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008886 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008887 return 0;
8888
Willy Tarreau911db9b2020-01-23 16:27:54 +01008889 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008890 smp->flags = SMP_F_MAY_CHANGE;
8891 return 0;
8892 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008893 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008894
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008895 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008896 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008897 smp->flags = 0;
8898
8899 return 1;
8900}
8901
Emeric Brun2525b6b2012-10-18 15:59:43 +02008902/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008903static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008904smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008905{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008906 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008907 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008908
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008909 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008910 if (!conn || conn->xprt != &ssl_sock)
8911 return 0;
8912
Willy Tarreau911db9b2020-01-23 16:27:54 +01008913 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008914 smp->flags = SMP_F_MAY_CHANGE;
8915 return 0;
8916 }
8917
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008918 ctx = conn->xprt_ctx;
8919
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008920 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008921 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008922 smp->flags = 0;
8923
8924 return 1;
8925}
8926
Emeric Brun2525b6b2012-10-18 15:59:43 +02008927/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008928static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008929smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008930{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008931 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008932 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008933
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008934 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008935 if (!conn || conn->xprt != &ssl_sock)
8936 return 0;
8937
Willy Tarreau911db9b2020-01-23 16:27:54 +01008938 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008939 smp->flags = SMP_F_MAY_CHANGE;
8940 return 0;
8941 }
8942
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008943 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008944 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008945 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008946
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008947 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008948 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008949 smp->flags = 0;
8950
8951 return 1;
8952}
8953
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008954/* for ca-file and ca-verify-file */
8955static 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 +02008956{
8957 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008958 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008959 return ERR_ALERT | ERR_FATAL;
8960 }
8961
Willy Tarreauef934602016-12-22 23:12:01 +01008962 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008963 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008964 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008965 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008966
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008967 if (!ssl_store_load_locations_file(*ca_file_p)) {
8968 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008969 return ERR_ALERT | ERR_FATAL;
8970 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008971 return 0;
8972}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008973
8974/* parse the "ca-file" bind keyword */
8975static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8976{
8977 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
8978}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008979static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8980{
8981 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8982}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008983
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01008984/* parse the "ca-verify-file" bind keyword */
8985static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8986{
8987 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
8988}
8989static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8990{
8991 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
8992}
8993
Christopher Faulet31af49d2015-06-09 17:29:50 +02008994/* parse the "ca-sign-file" bind keyword */
8995static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8996{
8997 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008998 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008999 return ERR_ALERT | ERR_FATAL;
9000 }
9001
Willy Tarreauef934602016-12-22 23:12:01 +01009002 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9003 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009004 else
9005 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9006
9007 return 0;
9008}
9009
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009010/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009011static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9012{
9013 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009014 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009015 return ERR_ALERT | ERR_FATAL;
9016 }
9017 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9018 return 0;
9019}
9020
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009021/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009022static 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 +02009023{
9024 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009025 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009026 return ERR_ALERT | ERR_FATAL;
9027 }
9028
Emeric Brun76d88952012-10-05 15:47:31 +02009029 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009030 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009031 return 0;
9032}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009033static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9034{
9035 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9036}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009037
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009038#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009039/* parse the "ciphersuites" bind keyword */
9040static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9041{
9042 if (!*args[cur_arg + 1]) {
9043 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9044 return ERR_ALERT | ERR_FATAL;
9045 }
9046
9047 free(conf->ciphersuites);
9048 conf->ciphersuites = strdup(args[cur_arg + 1]);
9049 return 0;
9050}
9051static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9052{
9053 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9054}
9055#endif
9056
Willy Tarreaubbc91962019-10-16 16:42:19 +02009057/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009058static 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 +02009059{
Willy Tarreau38011032013-08-13 16:59:39 +02009060 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009061
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009062 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009063 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009064 return ERR_ALERT | ERR_FATAL;
9065 }
9066
Willy Tarreauef934602016-12-22 23:12:01 +01009067 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9068 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009069 memprintf(err, "'%s' : path too long", args[cur_arg]);
9070 return ERR_ALERT | ERR_FATAL;
9071 }
Willy Tarreauef934602016-12-22 23:12:01 +01009072 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009073 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009074 }
9075
Willy Tarreaubbc91962019-10-16 16:42:19 +02009076 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009077}
9078
Willy Tarreaubbc91962019-10-16 16:42:19 +02009079/* 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 +01009080static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9081{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009082 int err_code;
9083
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009084 if (!*args[cur_arg + 1]) {
9085 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9086 return ERR_ALERT | ERR_FATAL;
9087 }
9088
William Lallemand6be66ec2020-03-06 22:26:32 +01009089 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009090 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009091 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009092
Willy Tarreaubbc91962019-10-16 16:42:19 +02009093 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009094}
9095
Emeric Brunfb510ea2012-10-05 12:00:26 +02009096/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009097static 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 +02009098{
Emeric Brun051cdab2012-10-02 19:25:50 +02009099#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009100 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009101 return ERR_ALERT | ERR_FATAL;
9102#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009103 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009104 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009105 return ERR_ALERT | ERR_FATAL;
9106 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009107
Willy Tarreauef934602016-12-22 23:12:01 +01009108 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9109 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009110 else
9111 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009112
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009113 if (!ssl_store_load_locations_file(conf->crl_file)) {
9114 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9115 return ERR_ALERT | ERR_FATAL;
9116 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009117 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009118#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009119}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009120static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9121{
9122 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9123}
Emeric Brun2b58d042012-09-20 17:10:03 +02009124
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009125/* parse the "curves" bind keyword keyword */
9126static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9127{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009128#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009129 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009130 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009131 return ERR_ALERT | ERR_FATAL;
9132 }
9133 conf->curves = strdup(args[cur_arg + 1]);
9134 return 0;
9135#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009136 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009137 return ERR_ALERT | ERR_FATAL;
9138#endif
9139}
9140static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9141{
9142 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9143}
9144
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009145/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009146static 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 +02009147{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009148#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009149 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009150 return ERR_ALERT | ERR_FATAL;
9151#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009152 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 +02009153 return ERR_ALERT | ERR_FATAL;
9154#else
9155 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009156 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009157 return ERR_ALERT | ERR_FATAL;
9158 }
9159
9160 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009161
9162 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009163#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009164}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009165static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9166{
9167 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9168}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009169
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009170/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009171static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9172{
9173 int code;
9174 char *p = args[cur_arg + 1];
9175 unsigned long long *ignerr = &conf->crt_ignerr;
9176
9177 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009178 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009179 return ERR_ALERT | ERR_FATAL;
9180 }
9181
9182 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9183 ignerr = &conf->ca_ignerr;
9184
9185 if (strcmp(p, "all") == 0) {
9186 *ignerr = ~0ULL;
9187 return 0;
9188 }
9189
9190 while (p) {
9191 code = atoi(p);
9192 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009193 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9194 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009195 return ERR_ALERT | ERR_FATAL;
9196 }
9197 *ignerr |= 1ULL << code;
9198 p = strchr(p, ',');
9199 if (p)
9200 p++;
9201 }
9202
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009203 return 0;
9204}
9205
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009206/* parse tls_method_options "no-xxx" and "force-xxx" */
9207static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009208{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009209 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009210 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009211 p = strchr(arg, '-');
9212 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009213 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009214 p++;
9215 if (!strcmp(p, "sslv3"))
9216 v = CONF_SSLV3;
9217 else if (!strcmp(p, "tlsv10"))
9218 v = CONF_TLSV10;
9219 else if (!strcmp(p, "tlsv11"))
9220 v = CONF_TLSV11;
9221 else if (!strcmp(p, "tlsv12"))
9222 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009223 else if (!strcmp(p, "tlsv13"))
9224 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009225 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009226 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009227 if (!strncmp(arg, "no-", 3))
9228 methods->flags |= methodVersions[v].flag;
9229 else if (!strncmp(arg, "force-", 6))
9230 methods->min = methods->max = v;
9231 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009232 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009233 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009234 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009235 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009236 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009237}
9238
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009239static 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 +02009240{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009241 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009242}
9243
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009244static 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 +02009245{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009246 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9247}
9248
9249/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9250static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9251{
9252 uint16_t i, v = 0;
9253 char *argv = args[cur_arg + 1];
9254 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009255 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009256 return ERR_ALERT | ERR_FATAL;
9257 }
9258 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9259 if (!strcmp(argv, methodVersions[i].name))
9260 v = i;
9261 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009262 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009263 return ERR_ALERT | ERR_FATAL;
9264 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009265 if (!strcmp("ssl-min-ver", args[cur_arg]))
9266 methods->min = v;
9267 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9268 methods->max = v;
9269 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009270 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009271 return ERR_ALERT | ERR_FATAL;
9272 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009273 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009274}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009275
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009276static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9277{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009278#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009279 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 +02009280#endif
9281 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9282}
9283
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009284static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9285{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009286 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009287}
9288
9289static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9290{
9291 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9292}
9293
Emeric Brun2d0c4822012-10-02 13:45:20 +02009294/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009295static 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 +02009296{
Emeric Brun89675492012-10-05 13:48:26 +02009297 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009298 return 0;
9299}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009300
Olivier Houchardc2aae742017-09-22 18:26:28 +02009301/* parse the "allow-0rtt" bind keyword */
9302static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9303{
9304 conf->early_data = 1;
9305 return 0;
9306}
9307
9308static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9309{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009310 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009311 return 0;
9312}
9313
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009314/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009315static 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 +02009316{
Bernard Spil13c53f82018-02-15 13:34:58 +01009317#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009318 char *p1, *p2;
9319
9320 if (!*args[cur_arg + 1]) {
9321 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9322 return ERR_ALERT | ERR_FATAL;
9323 }
9324
9325 free(conf->npn_str);
9326
Willy Tarreau3724da12016-02-12 17:11:12 +01009327 /* the NPN string is built as a suite of (<len> <name>)*,
9328 * so we reuse each comma to store the next <len> and need
9329 * one more for the end of the string.
9330 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009331 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009332 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009333 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9334
9335 /* replace commas with the name length */
9336 p1 = conf->npn_str;
9337 p2 = p1 + 1;
9338 while (1) {
9339 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9340 if (!p2)
9341 p2 = p1 + 1 + strlen(p1 + 1);
9342
9343 if (p2 - (p1 + 1) > 255) {
9344 *p2 = '\0';
9345 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9346 return ERR_ALERT | ERR_FATAL;
9347 }
9348
9349 *p1 = p2 - (p1 + 1);
9350 p1 = p2;
9351
9352 if (!*p2)
9353 break;
9354
9355 *(p2++) = '\0';
9356 }
9357 return 0;
9358#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009359 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009360 return ERR_ALERT | ERR_FATAL;
9361#endif
9362}
9363
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009364static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9365{
9366 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9367}
9368
Willy Tarreauab861d32013-04-02 02:30:41 +02009369/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009370static 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 +02009371{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009372#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009373 char *p1, *p2;
9374
9375 if (!*args[cur_arg + 1]) {
9376 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9377 return ERR_ALERT | ERR_FATAL;
9378 }
9379
9380 free(conf->alpn_str);
9381
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009382 /* the ALPN string is built as a suite of (<len> <name>)*,
9383 * so we reuse each comma to store the next <len> and need
9384 * one more for the end of the string.
9385 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009386 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009387 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009388 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9389
9390 /* replace commas with the name length */
9391 p1 = conf->alpn_str;
9392 p2 = p1 + 1;
9393 while (1) {
9394 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9395 if (!p2)
9396 p2 = p1 + 1 + strlen(p1 + 1);
9397
9398 if (p2 - (p1 + 1) > 255) {
9399 *p2 = '\0';
9400 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9401 return ERR_ALERT | ERR_FATAL;
9402 }
9403
9404 *p1 = p2 - (p1 + 1);
9405 p1 = p2;
9406
9407 if (!*p2)
9408 break;
9409
9410 *(p2++) = '\0';
9411 }
9412 return 0;
9413#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009414 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009415 return ERR_ALERT | ERR_FATAL;
9416#endif
9417}
9418
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009419static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9420{
9421 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9422}
9423
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009424/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009425static 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 +02009426{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009427 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009428 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009429
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009430 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9431 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009432#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009433 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9434 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9435#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009436 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009437 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9438 if (!conf->ssl_conf.ssl_methods.min)
9439 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9440 if (!conf->ssl_conf.ssl_methods.max)
9441 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009442
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009443 return 0;
9444}
9445
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009446/* parse the "prefer-client-ciphers" bind keyword */
9447static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9448{
9449 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9450 return 0;
9451}
9452
Christopher Faulet31af49d2015-06-09 17:29:50 +02009453/* parse the "generate-certificates" bind keyword */
9454static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9455{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009456#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009457 conf->generate_certs = 1;
9458#else
9459 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9460 err && *err ? *err : "");
9461#endif
9462 return 0;
9463}
9464
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009465/* parse the "strict-sni" bind keyword */
9466static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9467{
9468 conf->strict_sni = 1;
9469 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009470}
9471
9472/* parse the "tls-ticket-keys" bind keyword */
9473static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9474{
9475#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009476 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009477 int i = 0;
9478 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009479 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009480
9481 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009482 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009483 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009484 }
9485
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009486 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009487 if (keys_ref) {
9488 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009489 conf->keys_ref = keys_ref;
9490 return 0;
9491 }
9492
Christopher Faulete566f3d2019-10-21 09:55:49 +02009493 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009494 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009495 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009496 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009497 }
9498
Emeric Brun9e754772019-01-10 17:51:55 +01009499 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009500 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009501 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009502 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009503 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009504
9505 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009506 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009507 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009508 }
9509
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009510 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009511 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009512 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009513 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009514 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009515
Emeric Brun9e754772019-01-10 17:51:55 +01009516 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009517 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9518 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009519 int dec_size;
9520
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009521 /* Strip newline characters from the end */
9522 if(thisline[len - 1] == '\n')
9523 thisline[--len] = 0;
9524
9525 if(thisline[len - 1] == '\r')
9526 thisline[--len] = 0;
9527
Emeric Brun9e754772019-01-10 17:51:55 +01009528 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9529 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009530 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009531 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009532 }
Emeric Brun9e754772019-01-10 17:51:55 +01009533 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9534 keys_ref->key_size_bits = 128;
9535 }
9536 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9537 keys_ref->key_size_bits = 256;
9538 }
9539 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9540 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9541 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009542 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009543 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009544 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009545 i++;
9546 }
9547
9548 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009549 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 +02009550 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009551 }
9552
9553 fclose(f);
9554
9555 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009556 i -= 2;
9557 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009558 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009559 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009560 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009561 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009562
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009563 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9564
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009565 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009566
9567 fail:
9568 if (f)
9569 fclose(f);
9570 if (keys_ref) {
9571 free(keys_ref->filename);
9572 free(keys_ref->tlskeys);
9573 free(keys_ref);
9574 }
9575 return ERR_ALERT | ERR_FATAL;
9576
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009577#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009578 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009579 return ERR_ALERT | ERR_FATAL;
9580#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009581}
9582
Emeric Brund94b3fe2012-09-20 18:23:56 +02009583/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009584static 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 +02009585{
9586 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009587 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009588 return ERR_ALERT | ERR_FATAL;
9589 }
9590
9591 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009592 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009593 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009594 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009595 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009596 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009597 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009598 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9599 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009600 return ERR_ALERT | ERR_FATAL;
9601 }
9602
9603 return 0;
9604}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009605static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9606{
9607 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9608}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009609
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009610/* parse the "no-ca-names" bind keyword */
9611static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9612{
9613 conf->no_ca_names = 1;
9614 return 0;
9615}
9616static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9617{
9618 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9619}
9620
Willy Tarreau92faadf2012-10-10 23:04:25 +02009621/************** "server" keywords ****************/
9622
Olivier Houchardc7566002018-11-20 23:33:50 +01009623/* parse the "npn" bind keyword */
9624static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9625{
9626#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9627 char *p1, *p2;
9628
9629 if (!*args[*cur_arg + 1]) {
9630 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9631 return ERR_ALERT | ERR_FATAL;
9632 }
9633
9634 free(newsrv->ssl_ctx.npn_str);
9635
9636 /* the NPN string is built as a suite of (<len> <name>)*,
9637 * so we reuse each comma to store the next <len> and need
9638 * one more for the end of the string.
9639 */
9640 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9641 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9642 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9643 newsrv->ssl_ctx.npn_len);
9644
9645 /* replace commas with the name length */
9646 p1 = newsrv->ssl_ctx.npn_str;
9647 p2 = p1 + 1;
9648 while (1) {
9649 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9650 newsrv->ssl_ctx.npn_len - (p1 + 1));
9651 if (!p2)
9652 p2 = p1 + 1 + strlen(p1 + 1);
9653
9654 if (p2 - (p1 + 1) > 255) {
9655 *p2 = '\0';
9656 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9657 return ERR_ALERT | ERR_FATAL;
9658 }
9659
9660 *p1 = p2 - (p1 + 1);
9661 p1 = p2;
9662
9663 if (!*p2)
9664 break;
9665
9666 *(p2++) = '\0';
9667 }
9668 return 0;
9669#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009670 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009671 return ERR_ALERT | ERR_FATAL;
9672#endif
9673}
9674
Olivier Houchard92150142018-12-21 19:47:01 +01009675/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009676static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9677{
9678#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9679 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009680 char **alpn_str;
9681 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009682
Olivier Houchard92150142018-12-21 19:47:01 +01009683 if (*args[*cur_arg] == 'c') {
9684 alpn_str = &newsrv->check.alpn_str;
9685 alpn_len = &newsrv->check.alpn_len;
9686 } else {
9687 alpn_str = &newsrv->ssl_ctx.alpn_str;
9688 alpn_len = &newsrv->ssl_ctx.alpn_len;
9689
9690 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009691 if (!*args[*cur_arg + 1]) {
9692 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9693 return ERR_ALERT | ERR_FATAL;
9694 }
9695
Olivier Houchard92150142018-12-21 19:47:01 +01009696 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009697
9698 /* the ALPN string is built as a suite of (<len> <name>)*,
9699 * so we reuse each comma to store the next <len> and need
9700 * one more for the end of the string.
9701 */
Olivier Houchard92150142018-12-21 19:47:01 +01009702 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9703 *alpn_str = calloc(1, *alpn_len + 1);
9704 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009705
9706 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009707 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009708 p2 = p1 + 1;
9709 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009710 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009711 if (!p2)
9712 p2 = p1 + 1 + strlen(p1 + 1);
9713
9714 if (p2 - (p1 + 1) > 255) {
9715 *p2 = '\0';
9716 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9717 return ERR_ALERT | ERR_FATAL;
9718 }
9719
9720 *p1 = p2 - (p1 + 1);
9721 p1 = p2;
9722
9723 if (!*p2)
9724 break;
9725
9726 *(p2++) = '\0';
9727 }
9728 return 0;
9729#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009730 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009731 return ERR_ALERT | ERR_FATAL;
9732#endif
9733}
9734
Emeric Brunef42d922012-10-11 16:11:36 +02009735/* parse the "ca-file" server keyword */
9736static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9737{
9738 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009739 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009740 return ERR_ALERT | ERR_FATAL;
9741 }
9742
Willy Tarreauef934602016-12-22 23:12:01 +01009743 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9744 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009745 else
9746 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9747
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009748 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9749 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9750 return ERR_ALERT | ERR_FATAL;
9751 }
Emeric Brunef42d922012-10-11 16:11:36 +02009752 return 0;
9753}
9754
Olivier Houchard9130a962017-10-17 17:33:43 +02009755/* parse the "check-sni" server keyword */
9756static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9757{
9758 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009759 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009760 return ERR_ALERT | ERR_FATAL;
9761 }
9762
9763 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9764 if (!newsrv->check.sni) {
9765 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9766 return ERR_ALERT | ERR_FATAL;
9767 }
9768 return 0;
9769
9770}
9771
Willy Tarreau92faadf2012-10-10 23:04:25 +02009772/* parse the "check-ssl" server keyword */
9773static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9774{
9775 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009776 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9777 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009778#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009779 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9780 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9781#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009782 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009783 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9784 if (!newsrv->ssl_ctx.methods.min)
9785 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9786 if (!newsrv->ssl_ctx.methods.max)
9787 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9788
Willy Tarreau92faadf2012-10-10 23:04:25 +02009789 return 0;
9790}
9791
9792/* parse the "ciphers" server keyword */
9793static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9794{
9795 if (!*args[*cur_arg + 1]) {
9796 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9797 return ERR_ALERT | ERR_FATAL;
9798 }
9799
9800 free(newsrv->ssl_ctx.ciphers);
9801 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9802 return 0;
9803}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009804
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009805#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009806/* parse the "ciphersuites" server keyword */
9807static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9808{
9809 if (!*args[*cur_arg + 1]) {
9810 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9811 return ERR_ALERT | ERR_FATAL;
9812 }
9813
9814 free(newsrv->ssl_ctx.ciphersuites);
9815 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9816 return 0;
9817}
9818#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009819
Emeric Brunef42d922012-10-11 16:11:36 +02009820/* parse the "crl-file" server keyword */
9821static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9822{
9823#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009824 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009825 return ERR_ALERT | ERR_FATAL;
9826#else
9827 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009828 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009829 return ERR_ALERT | ERR_FATAL;
9830 }
9831
Willy Tarreauef934602016-12-22 23:12:01 +01009832 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9833 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009834 else
9835 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9836
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009837 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9838 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9839 return ERR_ALERT | ERR_FATAL;
9840 }
Emeric Brunef42d922012-10-11 16:11:36 +02009841 return 0;
9842#endif
9843}
9844
Emeric Bruna7aa3092012-10-26 12:58:00 +02009845/* parse the "crt" server keyword */
9846static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9847{
9848 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009849 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009850 return ERR_ALERT | ERR_FATAL;
9851 }
9852
Willy Tarreauef934602016-12-22 23:12:01 +01009853 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009854 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009855 else
9856 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9857
9858 return 0;
9859}
Emeric Brunef42d922012-10-11 16:11:36 +02009860
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009861/* parse the "no-check-ssl" server keyword */
9862static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9863{
9864 newsrv->check.use_ssl = 0;
9865 free(newsrv->ssl_ctx.ciphers);
9866 newsrv->ssl_ctx.ciphers = NULL;
9867 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9868 return 0;
9869}
9870
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009871/* parse the "no-send-proxy-v2-ssl" server keyword */
9872static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9873{
9874 newsrv->pp_opts &= ~SRV_PP_V2;
9875 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9876 return 0;
9877}
9878
9879/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9880static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9881{
9882 newsrv->pp_opts &= ~SRV_PP_V2;
9883 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9884 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9885 return 0;
9886}
9887
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009888/* parse the "no-ssl" server keyword */
9889static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9890{
9891 newsrv->use_ssl = 0;
9892 free(newsrv->ssl_ctx.ciphers);
9893 newsrv->ssl_ctx.ciphers = NULL;
9894 return 0;
9895}
9896
Olivier Houchard522eea72017-11-03 16:27:47 +01009897/* parse the "allow-0rtt" server keyword */
9898static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9899{
9900 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9901 return 0;
9902}
9903
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009904/* parse the "no-ssl-reuse" server keyword */
9905static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9906{
9907 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9908 return 0;
9909}
9910
Emeric Brunf9c5c472012-10-11 15:28:34 +02009911/* parse the "no-tls-tickets" server keyword */
9912static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9913{
9914 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9915 return 0;
9916}
David Safb76832014-05-08 23:42:08 -04009917/* parse the "send-proxy-v2-ssl" server keyword */
9918static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9919{
9920 newsrv->pp_opts |= SRV_PP_V2;
9921 newsrv->pp_opts |= SRV_PP_V2_SSL;
9922 return 0;
9923}
9924
9925/* parse the "send-proxy-v2-ssl-cn" server keyword */
9926static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9927{
9928 newsrv->pp_opts |= SRV_PP_V2;
9929 newsrv->pp_opts |= SRV_PP_V2_SSL;
9930 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9931 return 0;
9932}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009933
Willy Tarreau732eac42015-07-09 11:40:25 +02009934/* parse the "sni" server keyword */
9935static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9936{
9937#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9938 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9939 return ERR_ALERT | ERR_FATAL;
9940#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009941 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009942
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009943 arg = args[*cur_arg + 1];
9944 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009945 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9946 return ERR_ALERT | ERR_FATAL;
9947 }
9948
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009949 free(newsrv->sni_expr);
9950 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009951
Willy Tarreau732eac42015-07-09 11:40:25 +02009952 return 0;
9953#endif
9954}
9955
Willy Tarreau92faadf2012-10-10 23:04:25 +02009956/* parse the "ssl" server keyword */
9957static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9958{
9959 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009960 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9961 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009962#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009963 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9964 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9965#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009966 return 0;
9967}
9968
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009969/* parse the "ssl-reuse" server keyword */
9970static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9971{
9972 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9973 return 0;
9974}
9975
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009976/* parse the "tls-tickets" server keyword */
9977static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9978{
9979 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9980 return 0;
9981}
9982
Emeric Brunef42d922012-10-11 16:11:36 +02009983/* parse the "verify" server keyword */
9984static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9985{
9986 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009987 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009988 return ERR_ALERT | ERR_FATAL;
9989 }
9990
9991 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009992 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009993 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009994 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009995 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009996 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9997 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009998 return ERR_ALERT | ERR_FATAL;
9999 }
10000
Evan Broderbe554312013-06-27 00:05:25 -070010001 return 0;
10002}
10003
10004/* parse the "verifyhost" server keyword */
10005static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10006{
10007 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010008 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010009 return ERR_ALERT | ERR_FATAL;
10010 }
10011
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010012 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010013 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10014
Emeric Brunef42d922012-10-11 16:11:36 +020010015 return 0;
10016}
10017
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010018/* parse the "ssl-default-bind-options" keyword in global section */
10019static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10020 struct proxy *defpx, const char *file, int line,
10021 char **err) {
10022 int i = 1;
10023
10024 if (*(args[i]) == 0) {
10025 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10026 return -1;
10027 }
10028 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010029 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010030 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010031 else if (!strcmp(args[i], "prefer-client-ciphers"))
10032 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010033 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10034 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10035 i++;
10036 else {
10037 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10038 return -1;
10039 }
10040 }
10041 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010042 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10043 return -1;
10044 }
10045 i++;
10046 }
10047 return 0;
10048}
10049
10050/* parse the "ssl-default-server-options" keyword in global section */
10051static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10052 struct proxy *defpx, const char *file, int line,
10053 char **err) {
10054 int i = 1;
10055
10056 if (*(args[i]) == 0) {
10057 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10058 return -1;
10059 }
10060 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010061 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010062 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010063 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10064 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10065 i++;
10066 else {
10067 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10068 return -1;
10069 }
10070 }
10071 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010072 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10073 return -1;
10074 }
10075 i++;
10076 }
10077 return 0;
10078}
10079
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010080/* parse the "ca-base" / "crt-base" keywords in global section.
10081 * Returns <0 on alert, >0 on warning, 0 on success.
10082 */
10083static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10084 struct proxy *defpx, const char *file, int line,
10085 char **err)
10086{
10087 char **target;
10088
Willy Tarreauef934602016-12-22 23:12:01 +010010089 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010090
10091 if (too_many_args(1, args, err, NULL))
10092 return -1;
10093
10094 if (*target) {
10095 memprintf(err, "'%s' already specified.", args[0]);
10096 return -1;
10097 }
10098
10099 if (*(args[1]) == 0) {
10100 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10101 return -1;
10102 }
10103 *target = strdup(args[1]);
10104 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010105}
10106
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010107/* "issuers-chain-path" load chain certificate in global */
10108static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10109{
10110 X509 *ca;
10111 X509_NAME *name = NULL;
10112 ASN1_OCTET_STRING *skid = NULL;
10113 STACK_OF(X509) *chain = NULL;
10114 struct issuer_chain *issuer;
10115 struct eb64_node *node;
10116 char *path;
10117 u64 key;
10118 int ret = 0;
10119
10120 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10121 if (chain == NULL) {
10122 chain = sk_X509_new_null();
10123 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10124 name = X509_get_subject_name(ca);
10125 }
10126 if (!sk_X509_push(chain, ca)) {
10127 X509_free(ca);
10128 goto end;
10129 }
10130 }
10131 if (!chain) {
10132 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10133 goto end;
10134 }
10135 if (!skid) {
10136 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10137 goto end;
10138 }
10139 if (!name) {
10140 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10141 goto end;
10142 }
10143 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010144 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010145 issuer = container_of(node, typeof(*issuer), node);
10146 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10147 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10148 goto end;
10149 }
10150 }
10151 issuer = calloc(1, sizeof *issuer);
10152 path = strdup(fp);
10153 if (!issuer || !path) {
10154 free(issuer);
10155 free(path);
10156 goto end;
10157 }
10158 issuer->node.key = key;
10159 issuer->path = path;
10160 issuer->chain = chain;
10161 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010162 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010163 ret = 1;
10164 end:
10165 if (skid)
10166 ASN1_OCTET_STRING_free(skid);
10167 if (chain)
10168 sk_X509_pop_free(chain, X509_free);
10169 return ret;
10170}
10171
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010172static struct issuer_chain* ssl_get_issuer_chain(X509 *cert)
10173{
10174 AUTHORITY_KEYID *akid;
10175 struct issuer_chain *issuer = NULL;
10176
10177 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10178 if (akid) {
10179 struct eb64_node *node;
10180 u64 hk;
10181 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10182 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10183 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10184 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10185 issuer = ti;
10186 break;
10187 }
10188 }
10189 AUTHORITY_KEYID_free(akid);
10190 }
10191 return issuer;
10192}
10193
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010194static void ssl_free_global_issuers(void)
10195{
10196 struct eb64_node *node, *back;
10197 struct issuer_chain *issuer;
10198
William Lallemande0f3fd52020-02-25 14:53:06 +010010199 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010200 while (node) {
10201 issuer = container_of(node, typeof(*issuer), node);
10202 back = eb64_next(node);
10203 eb64_delete(node);
10204 free(issuer->path);
10205 sk_X509_pop_free(issuer->chain, X509_free);
10206 free(issuer);
10207 node = back;
10208 }
10209}
10210
10211static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10212 struct proxy *defpx, const char *file, int line,
10213 char **err)
10214{
10215 char *path;
10216 struct dirent **de_list;
10217 int i, n;
10218 struct stat buf;
10219 char *end;
10220 char fp[MAXPATHLEN+1];
10221
10222 if (too_many_args(1, args, err, NULL))
10223 return -1;
10224
10225 path = args[1];
10226 if (*path == 0 || stat(path, &buf)) {
10227 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10228 err && *err ? *err : "", args[0]);
10229 return -1;
10230 }
10231 if (S_ISDIR(buf.st_mode) == 0) {
10232 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10233 err && *err ? *err : "", args[0], path);
10234 return -1;
10235 }
10236
10237 /* strip trailing slashes, including first one */
10238 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10239 *end = 0;
10240 /* path already parsed? */
10241 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10242 return 0;
10243 /* overwrite old issuers_chain_path */
10244 free(global_ssl.issuers_chain_path);
10245 global_ssl.issuers_chain_path = strdup(path);
10246 ssl_free_global_issuers();
10247
10248 n = scandir(path, &de_list, 0, alphasort);
10249 if (n < 0) {
10250 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10251 err && *err ? *err : "", args[0], path, strerror(errno));
10252 return -1;
10253 }
10254 for (i = 0; i < n; i++) {
10255 struct dirent *de = de_list[i];
10256 BIO *in = NULL;
10257 char *warn = NULL;
10258
10259 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10260 free(de);
10261 if (stat(fp, &buf) != 0) {
10262 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10263 goto next;
10264 }
10265 if (!S_ISREG(buf.st_mode))
10266 goto next;
10267
10268 in = BIO_new(BIO_s_file());
10269 if (in == NULL)
10270 goto next;
10271 if (BIO_read_filename(in, fp) <= 0)
10272 goto next;
10273 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10274 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010275 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010276 free(warn);
10277 warn = NULL;
10278 }
10279 next:
10280 if (in)
10281 BIO_free(in);
10282 }
10283 free(de_list);
10284
10285 return 0;
10286}
10287
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010288/* parse the "ssl-mode-async" keyword in global section.
10289 * Returns <0 on alert, >0 on warning, 0 on success.
10290 */
10291static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10292 struct proxy *defpx, const char *file, int line,
10293 char **err)
10294{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010295#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010296 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010297 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010298 return 0;
10299#else
10300 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10301 return -1;
10302#endif
10303}
10304
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010305#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010306static int ssl_check_async_engine_count(void) {
10307 int err_code = 0;
10308
Emeric Brun3854e012017-05-17 20:42:48 +020010309 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010310 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010311 err_code = ERR_ABORT;
10312 }
10313 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010314}
10315
Grant Zhang872f9c22017-01-21 01:10:18 +000010316/* parse the "ssl-engine" keyword in global section.
10317 * Returns <0 on alert, >0 on warning, 0 on success.
10318 */
10319static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10320 struct proxy *defpx, const char *file, int line,
10321 char **err)
10322{
10323 char *algo;
10324 int ret = -1;
10325
10326 if (*(args[1]) == 0) {
10327 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10328 return ret;
10329 }
10330
10331 if (*(args[2]) == 0) {
10332 /* if no list of algorithms is given, it defaults to ALL */
10333 algo = strdup("ALL");
10334 goto add_engine;
10335 }
10336
10337 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10338 if (strcmp(args[2], "algo") != 0) {
10339 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10340 return ret;
10341 }
10342
10343 if (*(args[3]) == 0) {
10344 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10345 return ret;
10346 }
10347 algo = strdup(args[3]);
10348
10349add_engine:
10350 if (ssl_init_single_engine(args[1], algo)==0) {
10351 openssl_engines_initialized++;
10352 ret = 0;
10353 }
10354 free(algo);
10355 return ret;
10356}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010357#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010358
Willy Tarreauf22e9682016-12-21 23:23:19 +010010359/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10360 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10361 */
10362static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10363 struct proxy *defpx, const char *file, int line,
10364 char **err)
10365{
10366 char **target;
10367
Willy Tarreauef934602016-12-22 23:12:01 +010010368 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010369
10370 if (too_many_args(1, args, err, NULL))
10371 return -1;
10372
10373 if (*(args[1]) == 0) {
10374 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10375 return -1;
10376 }
10377
10378 free(*target);
10379 *target = strdup(args[1]);
10380 return 0;
10381}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010382
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010383#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010384/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10385 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10386 */
10387static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10388 struct proxy *defpx, const char *file, int line,
10389 char **err)
10390{
10391 char **target;
10392
10393 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10394
10395 if (too_many_args(1, args, err, NULL))
10396 return -1;
10397
10398 if (*(args[1]) == 0) {
10399 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10400 return -1;
10401 }
10402
10403 free(*target);
10404 *target = strdup(args[1]);
10405 return 0;
10406}
10407#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010408
Willy Tarreau9ceda382016-12-21 23:13:03 +010010409/* parse various global tune.ssl settings consisting in positive integers.
10410 * Returns <0 on alert, >0 on warning, 0 on success.
10411 */
10412static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10413 struct proxy *defpx, const char *file, int line,
10414 char **err)
10415{
10416 int *target;
10417
10418 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10419 target = &global.tune.sslcachesize;
10420 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010421 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010422 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010423 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010424 else if (strcmp(args[0], "maxsslconn") == 0)
10425 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010426 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10427 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010428 else {
10429 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10430 return -1;
10431 }
10432
10433 if (too_many_args(1, args, err, NULL))
10434 return -1;
10435
10436 if (*(args[1]) == 0) {
10437 memprintf(err, "'%s' expects an integer argument.", args[0]);
10438 return -1;
10439 }
10440
10441 *target = atoi(args[1]);
10442 if (*target < 0) {
10443 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10444 return -1;
10445 }
10446 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010447}
10448
10449static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10450 struct proxy *defpx, const char *file, int line,
10451 char **err)
10452{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010453 int ret;
10454
10455 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10456 if (ret != 0)
10457 return ret;
10458
Willy Tarreaubafbe012017-11-24 17:34:44 +010010459 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010460 memprintf(err, "'%s' is already configured.", args[0]);
10461 return -1;
10462 }
10463
Willy Tarreaubafbe012017-11-24 17:34:44 +010010464 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10465 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010466 memprintf(err, "Out of memory error.");
10467 return -1;
10468 }
10469 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010470}
10471
10472/* parse "ssl.force-private-cache".
10473 * Returns <0 on alert, >0 on warning, 0 on success.
10474 */
10475static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10476 struct proxy *defpx, const char *file, int line,
10477 char **err)
10478{
10479 if (too_many_args(0, args, err, NULL))
10480 return -1;
10481
Willy Tarreauef934602016-12-22 23:12:01 +010010482 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010483 return 0;
10484}
10485
10486/* parse "ssl.lifetime".
10487 * Returns <0 on alert, >0 on warning, 0 on success.
10488 */
10489static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10490 struct proxy *defpx, const char *file, int line,
10491 char **err)
10492{
10493 const char *res;
10494
10495 if (too_many_args(1, args, err, NULL))
10496 return -1;
10497
10498 if (*(args[1]) == 0) {
10499 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10500 return -1;
10501 }
10502
Willy Tarreauef934602016-12-22 23:12:01 +010010503 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010504 if (res == PARSE_TIME_OVER) {
10505 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10506 args[1], args[0]);
10507 return -1;
10508 }
10509 else if (res == PARSE_TIME_UNDER) {
10510 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10511 args[1], args[0]);
10512 return -1;
10513 }
10514 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010515 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10516 return -1;
10517 }
10518 return 0;
10519}
10520
10521#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010522/* parse "ssl-dh-param-file".
10523 * Returns <0 on alert, >0 on warning, 0 on success.
10524 */
10525static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10526 struct proxy *defpx, const char *file, int line,
10527 char **err)
10528{
10529 if (too_many_args(1, args, err, NULL))
10530 return -1;
10531
10532 if (*(args[1]) == 0) {
10533 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10534 return -1;
10535 }
10536
10537 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10538 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10539 return -1;
10540 }
10541 return 0;
10542}
10543
Willy Tarreau9ceda382016-12-21 23:13:03 +010010544/* parse "ssl.default-dh-param".
10545 * Returns <0 on alert, >0 on warning, 0 on success.
10546 */
10547static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10548 struct proxy *defpx, const char *file, int line,
10549 char **err)
10550{
10551 if (too_many_args(1, args, err, NULL))
10552 return -1;
10553
10554 if (*(args[1]) == 0) {
10555 memprintf(err, "'%s' expects an integer argument.", args[0]);
10556 return -1;
10557 }
10558
Willy Tarreauef934602016-12-22 23:12:01 +010010559 global_ssl.default_dh_param = atoi(args[1]);
10560 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010561 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10562 return -1;
10563 }
10564 return 0;
10565}
10566#endif
10567
William Lallemand3af48e72020-02-03 17:15:52 +010010568
10569/*
10570 * parse "ssl-load-extra-files".
10571 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10572 */
10573static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10574 struct proxy *defpx, const char *file, int line,
10575 char **err)
10576{
10577 int i;
10578 int gf = SSL_GF_NONE;
10579
10580 if (*(args[1]) == 0)
10581 goto err_arg;
10582
10583 for (i = 1; *args[i]; i++) {
10584
10585 if (!strcmp("bundle", args[i])) {
10586 gf |= SSL_GF_BUNDLE;
10587
10588 } else if (!strcmp("sctl", args[i])) {
10589 gf |= SSL_GF_SCTL;
10590
10591 } else if (!strcmp("ocsp", args[i])){
10592 gf |= SSL_GF_OCSP;
10593
10594 } else if (!strcmp("issuer", args[i])){
10595 gf |= SSL_GF_OCSP_ISSUER;
10596
William Lallemand4c5adbf2020-02-24 14:23:22 +010010597 } else if (!strcmp("key", args[i])) {
10598 gf |= SSL_GF_KEY;
10599
William Lallemand3af48e72020-02-03 17:15:52 +010010600 } else if (!strcmp("none", args[i])) {
10601 if (gf != SSL_GF_NONE)
10602 goto err_alone;
10603 gf = SSL_GF_NONE;
10604 i++;
10605 break;
10606
10607 } else if (!strcmp("all", args[i])) {
10608 if (gf != SSL_GF_NONE)
10609 goto err_alone;
10610 gf = SSL_GF_ALL;
10611 i++;
10612 break;
10613 } else {
10614 goto err_arg;
10615 }
10616 }
10617 /* break from loop but there are still arguments */
10618 if (*args[i])
10619 goto err_alone;
10620
10621 global_ssl.extra_files = gf;
10622
10623 return 0;
10624
10625err_alone:
10626 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10627 return -1;
10628
10629err_arg:
10630 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10631 return -1;
10632}
10633
Willy Tarreau9ceda382016-12-21 23:13:03 +010010634
William Lallemand32af2032016-10-29 18:09:35 +020010635/* This function is used with TLS ticket keys management. It permits to browse
10636 * each reference. The variable <getnext> must contain the current node,
10637 * <end> point to the root node.
10638 */
10639#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10640static inline
10641struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10642{
10643 struct tls_keys_ref *ref = getnext;
10644
10645 while (1) {
10646
10647 /* Get next list entry. */
10648 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10649
10650 /* If the entry is the last of the list, return NULL. */
10651 if (&ref->list == end)
10652 return NULL;
10653
10654 return ref;
10655 }
10656}
10657
10658static inline
10659struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10660{
10661 int id;
10662 char *error;
10663
10664 /* If the reference starts by a '#', this is numeric id. */
10665 if (reference[0] == '#') {
10666 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10667 id = strtol(reference + 1, &error, 10);
10668 if (*error != '\0')
10669 return NULL;
10670
10671 /* Perform the unique id lookup. */
10672 return tlskeys_ref_lookupid(id);
10673 }
10674
10675 /* Perform the string lookup. */
10676 return tlskeys_ref_lookup(reference);
10677}
10678#endif
10679
10680
10681#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10682
10683static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10684
10685static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10686 return cli_io_handler_tlskeys_files(appctx);
10687}
10688
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010689/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10690 * (next index to be dumped), and cli.p0 (next key reference).
10691 */
William Lallemand32af2032016-10-29 18:09:35 +020010692static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10693
10694 struct stream_interface *si = appctx->owner;
10695
10696 switch (appctx->st2) {
10697 case STAT_ST_INIT:
10698 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010699 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010700 * later and restart at the state "STAT_ST_INIT".
10701 */
10702 chunk_reset(&trash);
10703
10704 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10705 chunk_appendf(&trash, "# id secret\n");
10706 else
10707 chunk_appendf(&trash, "# id (file)\n");
10708
Willy Tarreau06d80a92017-10-19 14:32:15 +020010709 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010710 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010711 return 0;
10712 }
10713
William Lallemand32af2032016-10-29 18:09:35 +020010714 /* Now, we start the browsing of the references lists.
10715 * Note that the following call to LIST_ELEM return bad pointer. The only
10716 * available field of this pointer is <list>. It is used with the function
10717 * tlskeys_list_get_next() for retruning the first available entry
10718 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010719 if (appctx->ctx.cli.p0 == NULL) {
10720 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10721 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010722 }
10723
10724 appctx->st2 = STAT_ST_LIST;
10725 /* fall through */
10726
10727 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010728 while (appctx->ctx.cli.p0) {
10729 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010730
10731 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010732 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010733 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010734
10735 if (appctx->ctx.cli.i1 == 0)
10736 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10737
William Lallemand32af2032016-10-29 18:09:35 +020010738 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010739 int head;
10740
10741 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10742 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010743 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010744 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010745
10746 chunk_reset(t2);
10747 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010748 if (ref->key_size_bits == 128) {
10749 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10750 sizeof(struct tls_sess_key_128),
10751 t2->area, t2->size);
10752 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10753 t2->area);
10754 }
10755 else if (ref->key_size_bits == 256) {
10756 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10757 sizeof(struct tls_sess_key_256),
10758 t2->area, t2->size);
10759 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10760 t2->area);
10761 }
10762 else {
10763 /* This case should never happen */
10764 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10765 }
William Lallemand32af2032016-10-29 18:09:35 +020010766
Willy Tarreau06d80a92017-10-19 14:32:15 +020010767 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010768 /* let's try again later from this stream. We add ourselves into
10769 * this stream's users so that it can remove us upon termination.
10770 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010771 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010772 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010773 return 0;
10774 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010775 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010776 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010777 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010778 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010779 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010780 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010781 /* let's try again later from this stream. We add ourselves into
10782 * this stream's users so that it can remove us upon termination.
10783 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010784 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010785 return 0;
10786 }
10787
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010788 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010789 break;
10790
10791 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010792 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010793 }
10794
10795 appctx->st2 = STAT_ST_FIN;
10796 /* fall through */
10797
10798 default:
10799 appctx->st2 = STAT_ST_FIN;
10800 return 1;
10801 }
10802 return 0;
10803}
10804
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010805/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010806static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010807{
William Lallemand32af2032016-10-29 18:09:35 +020010808 /* no parameter, shows only file list */
10809 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010810 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010811 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010812 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010813 }
10814
10815 if (args[2][0] == '*') {
10816 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010817 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010818 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010819 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010820 if (!appctx->ctx.cli.p0)
10821 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010822 }
William Lallemand32af2032016-10-29 18:09:35 +020010823 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010824 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010825}
10826
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010827static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010828{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010829 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010830 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010831
William Lallemand32af2032016-10-29 18:09:35 +020010832 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010833 if (!*args[3] || !*args[4])
10834 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 +020010835
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010836 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010837 if (!ref)
10838 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010839
Willy Tarreau1c913e42018-08-22 05:26:57 +020010840 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010841 if (ret < 0)
10842 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010843
Willy Tarreau1c913e42018-08-22 05:26:57 +020010844 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010845 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10846 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010847
Willy Tarreau9d008692019-08-09 11:21:01 +020010848 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010849}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010850#endif
William Lallemand32af2032016-10-29 18:09:35 +020010851
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010852/*
10853 * Take an ssl_bind_conf structure and append the configuration line used to
10854 * create it in the buffer
10855 */
10856static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10857{
10858 int space = 0;
10859
10860 if (conf == NULL)
10861 return;
10862
10863 chunk_appendf(buf, " [");
10864#ifdef OPENSSL_NPN_NEGOTIATED
10865 if (conf->npn_str) {
10866 int len = conf->npn_len;
10867 char *ptr = conf->npn_str;
10868 int comma = 0;
10869
10870 if (space) chunk_appendf(buf, " ");
10871 chunk_appendf(buf, "npn ");
10872 while (len) {
10873 unsigned short size;
10874
10875 size = *ptr;
10876 ptr++;
10877 if (comma)
10878 chunk_memcat(buf, ",", 1);
10879 chunk_memcat(buf, ptr, size);
10880 ptr += size;
10881 len -= size + 1;
10882 comma = 1;
10883 }
10884 chunk_memcat(buf, "", 1); /* finish with a \0 */
10885 space++;
10886 }
10887#endif
10888#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10889 if (conf->alpn_str) {
10890 int len = conf->alpn_len;
10891 char *ptr = conf->alpn_str;
10892 int comma = 0;
10893
10894 if (space) chunk_appendf(buf, " ");
10895 chunk_appendf(buf, "alpn ");
10896 while (len) {
10897 unsigned short size;
10898
10899 size = *ptr;
10900 ptr++;
10901 if (comma)
10902 chunk_memcat(buf, ",", 1);
10903 chunk_memcat(buf, ptr, size);
10904 ptr += size;
10905 len -= size + 1;
10906 comma = 1;
10907 }
10908 chunk_memcat(buf, "", 1); /* finish with a \0 */
10909 space++;
10910 }
10911#endif
10912 /* verify */
10913 {
10914 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
10915 if (space) chunk_appendf(buf, " ");
10916 chunk_appendf(buf, "verify none");
10917 space++;
10918 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
10919 if (space) chunk_appendf(buf, " ");
10920 chunk_appendf(buf, "verify optional");
10921 space++;
10922 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
10923 if (space) chunk_appendf(buf, " ");
10924 chunk_appendf(buf, "verify required");
10925 space++;
10926 }
10927 }
10928
10929 if (conf->no_ca_names) {
10930 if (space) chunk_appendf(buf, " ");
10931 chunk_appendf(buf, "no-ca-names");
10932 space++;
10933 }
10934
10935 if (conf->early_data) {
10936 if (space) chunk_appendf(buf, " ");
10937 chunk_appendf(buf, "allow-0rtt");
10938 space++;
10939 }
10940 if (conf->ca_file) {
10941 if (space) chunk_appendf(buf, " ");
10942 chunk_appendf(buf, "ca-file %s", conf->ca_file);
10943 space++;
10944 }
10945 if (conf->crl_file) {
10946 if (space) chunk_appendf(buf, " ");
10947 chunk_appendf(buf, "crl-file %s", conf->crl_file);
10948 space++;
10949 }
10950 if (conf->ciphers) {
10951 if (space) chunk_appendf(buf, " ");
10952 chunk_appendf(buf, "ciphers %s", conf->ciphers);
10953 space++;
10954 }
10955#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
10956 if (conf->ciphersuites) {
10957 if (space) chunk_appendf(buf, " ");
10958 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
10959 space++;
10960 }
10961#endif
10962 if (conf->curves) {
10963 if (space) chunk_appendf(buf, " ");
10964 chunk_appendf(buf, "curves %s", conf->curves);
10965 space++;
10966 }
10967 if (conf->ecdhe) {
10968 if (space) chunk_appendf(buf, " ");
10969 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
10970 space++;
10971 }
10972
10973 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
10974 /* XXX: this part need to be revamp so we don't dump the default settings */
10975 if (conf->ssl_methods.min) {
10976 if (space) chunk_appendf(buf, " ");
10977 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
10978 space++;
10979 }
10980
10981 if (conf->ssl_methods.max) {
10982 if (space) chunk_appendf(buf, " ");
10983 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
10984 space++;
10985 }
10986
10987 chunk_appendf(buf, "] ");
10988
10989 return;
10990}
10991
10992/* dump a list of filters */
10993static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
10994{
10995 int space = 0;
10996 int i;
10997
10998 if (!entry->fcount)
10999 return;
11000
11001 for (i = 0; i < entry->fcount; i++) {
11002 if (space)
11003 chunk_appendf(buf, " ");
11004 chunk_appendf(buf, "%s", entry->filters[i]);
11005 space = 1;
11006 }
11007 return;
11008}
11009
11010/* CLI IO handler for '(show|dump) ssl crt-list' */
11011static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11012{
11013 struct buffer *trash = alloc_trash_chunk();
11014 struct stream_interface *si = appctx->owner;
11015 struct ebmb_node *lnode;
11016
11017 if (trash == NULL)
11018 return 1;
11019
11020 /* dump the list of crt-lists */
11021 lnode = appctx->ctx.cli.p1;
11022 if (lnode == NULL)
11023 lnode = ebmb_first(&crtlists_tree);
11024 while (lnode) {
11025 chunk_appendf(trash, "%s\n", lnode->key);
11026 if (ci_putchk(si_ic(si), trash) == -1) {
11027 si_rx_room_blk(si);
11028 goto yield;
11029 }
11030 lnode = ebmb_next(lnode);
11031 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011032 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011033 return 1;
11034yield:
11035 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011036 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011037 return 0;
11038}
11039
11040/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11041static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11042{
11043 struct buffer *trash = alloc_trash_chunk();
11044 struct crtlist *crtlist;
11045 struct stream_interface *si = appctx->owner;
11046 struct crtlist_entry *entry;
11047
11048 if (trash == NULL)
11049 return 1;
11050
11051 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11052
11053 entry = appctx->ctx.cli.p1;
11054 if (entry == NULL) {
11055 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11056 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11057 if (ci_putchk(si_ic(si), trash) == -1) {
11058 si_rx_room_blk(si);
11059 goto yield;
11060 }
11061 }
11062
11063 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11064 struct ckch_store *store;
11065 const char *filename;
11066
11067 store = entry->node.key;
11068 filename = store->path;
11069 if (appctx->ctx.cli.i0 == 's') /* show */
11070 chunk_appendf(trash, "%p ", entry);
11071 chunk_appendf(trash, "%s", filename);
11072 dump_crtlist_sslconf(trash, entry->ssl_conf);
11073 dump_crtlist_filters(trash, entry);
11074 chunk_appendf(trash, "\n");
11075
11076 if (ci_putchk(si_ic(si), trash) == -1) {
11077 si_rx_room_blk(si);
11078 goto yield;
11079 }
11080 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011081 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011082 return 1;
11083yield:
11084 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011085 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011086 return 0;
11087}
11088
11089/* CLI argument parser for '(show|dump) ssl crt-list' */
11090static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11091{
11092 struct ebmb_node *lnode;
11093 int mode;
11094
11095 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11096 return 1;
11097
11098 appctx->ctx.cli.p0 = NULL;
11099 appctx->ctx.cli.p1 = NULL;
11100 mode = (int)args[0][0]; /* 'd' or 's' */
11101
11102 if (mode == 'd' && !*args[3])
11103 return cli_err(appctx, "'dump ssl crt-list' expects a filename or a directory\n");
11104
11105 if (*args[3]) {
11106 lnode = ebst_lookup(&crtlists_tree, args[3]);
11107 if (lnode == NULL)
11108 return cli_err(appctx, "didn't find the specified filename\n");
11109
11110 appctx->ctx.cli.p0 = lnode;
11111 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11112 }
11113 appctx->ctx.cli.i0 = mode;
11114
11115 return 0;
11116}
William Lallemand44b35322019-10-17 16:28:40 +020011117
11118/* Type of SSL payloads that can be updated over the CLI */
11119
11120enum {
11121 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011122 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011123#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011124 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011125#endif
William Lallemand44b35322019-10-17 16:28:40 +020011126 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011127#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011128 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011129#endif
William Lallemand44b35322019-10-17 16:28:40 +020011130 CERT_TYPE_MAX,
11131};
11132
11133struct {
11134 const char *ext;
11135 int type;
11136 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11137 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011138} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011139 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011140 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011141#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011142 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011143#endif
11144#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011145 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011146#endif
William Lallemand44b35322019-10-17 16:28:40 +020011147 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011148 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011149};
11150
William Lallemand430413e2019-10-28 14:30:47 +010011151/* states of the CLI IO handler for 'set ssl cert' */
11152enum {
11153 SETCERT_ST_INIT = 0,
11154 SETCERT_ST_GEN,
11155 SETCERT_ST_INSERT,
11156 SETCERT_ST_FIN,
11157};
William Lallemand8f840d72019-10-23 10:53:05 +020011158
William Lallemandd4f946c2019-12-05 10:26:40 +010011159/* release function of the `show ssl cert' command */
11160static void cli_release_show_cert(struct appctx *appctx)
11161{
11162 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11163}
11164
11165/* IO handler of "show ssl cert <filename>" */
11166static int cli_io_handler_show_cert(struct appctx *appctx)
11167{
11168 struct buffer *trash = alloc_trash_chunk();
11169 struct ebmb_node *node;
11170 struct stream_interface *si = appctx->owner;
11171 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011172
11173 if (trash == NULL)
11174 return 1;
11175
11176 if (!appctx->ctx.ssl.old_ckchs) {
11177 if (ckchs_transaction.old_ckchs) {
11178 ckchs = ckchs_transaction.old_ckchs;
11179 chunk_appendf(trash, "# transaction\n");
11180 if (!ckchs->multi) {
11181 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011182#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011183 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011184 int n;
11185
William Lallemandd4f946c2019-12-05 10:26:40 +010011186 chunk_appendf(trash, "*%s:", ckchs->path);
11187 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11188 if (ckchs->ckch[n].cert)
11189 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11190 }
11191 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011192#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011193 }
11194 }
11195 }
11196
11197 if (!appctx->ctx.cli.p0) {
11198 chunk_appendf(trash, "# filename\n");
11199 node = ebmb_first(&ckchs_tree);
11200 } else {
11201 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11202 }
11203 while (node) {
11204 ckchs = ebmb_entry(node, struct ckch_store, node);
11205 if (!ckchs->multi) {
11206 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011207#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011208 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011209 int n;
11210
William Lallemandd4f946c2019-12-05 10:26:40 +010011211 chunk_appendf(trash, "%s:", ckchs->path);
11212 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11213 if (ckchs->ckch[n].cert)
11214 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11215 }
11216 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011217#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011218 }
11219
11220 node = ebmb_next(node);
11221 if (ci_putchk(si_ic(si), trash) == -1) {
11222 si_rx_room_blk(si);
11223 goto yield;
11224 }
11225 }
11226
11227 appctx->ctx.cli.p0 = NULL;
11228 free_trash_chunk(trash);
11229 return 1;
11230yield:
11231
11232 free_trash_chunk(trash);
11233 appctx->ctx.cli.p0 = ckchs;
11234 return 0; /* should come back */
11235}
11236
11237/* IO handler of the details "show ssl cert <filename>" */
11238static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11239{
11240 struct stream_interface *si = appctx->owner;
11241 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11242 struct buffer *out = alloc_trash_chunk();
11243 struct buffer *tmp = alloc_trash_chunk();
11244 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011245 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011246 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011247 int write = -1;
11248 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011249 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011250
11251 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011252 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011253
11254 if (!ckchs->multi) {
11255 chunk_appendf(out, "Filename: ");
11256 if (ckchs == ckchs_transaction.new_ckchs)
11257 chunk_appendf(out, "*");
11258 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011259
William Lallemand59c16fc2020-03-19 20:26:02 +010011260 chunk_appendf(out, "Status: ");
11261 if (ckchs->ckch->cert == NULL)
11262 chunk_appendf(out, "Empty\n");
11263 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11264 chunk_appendf(out, "Unused\n");
11265 else
11266 chunk_appendf(out, "Used\n");
11267
William Lallemandea987ed2020-03-19 16:48:33 +010011268 if (ckchs->ckch->cert == NULL)
11269 goto end;
11270
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011271 chain = ckchs->ckch->chain;
11272 if (chain == NULL) {
11273 struct issuer_chain *issuer;
11274 issuer = ssl_get_issuer_chain(ckchs->ckch->cert);
11275 if (issuer) {
11276 chain = issuer->chain;
11277 chunk_appendf(out, "Chain Filename: ");
11278 chunk_appendf(out, "%s\n", issuer->path);
11279 }
11280 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011281 chunk_appendf(out, "Serial: ");
11282 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11283 goto end;
11284 dump_binary(out, tmp->area, tmp->data);
11285 chunk_appendf(out, "\n");
11286
11287 chunk_appendf(out, "notBefore: ");
11288 chunk_reset(tmp);
11289 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11290 goto end;
11291 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11292 goto end;
11293 write = BIO_read(bio, tmp->area, tmp->size-1);
11294 tmp->area[write] = '\0';
11295 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011296 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011297 chunk_appendf(out, "%s\n", tmp->area);
11298
11299 chunk_appendf(out, "notAfter: ");
11300 chunk_reset(tmp);
11301 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11302 goto end;
11303 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11304 goto end;
11305 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11306 goto end;
11307 tmp->area[write] = '\0';
11308 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011309 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011310 chunk_appendf(out, "%s\n", tmp->area);
11311
William Lallemandd4f946c2019-12-05 10:26:40 +010011312#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11313 chunk_appendf(out, "Subject Alternative Name: ");
11314 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11315 goto end;
11316 *(out->area + out->data) = '\0';
11317 chunk_appendf(out, "\n");
11318#endif
11319 chunk_reset(tmp);
11320 chunk_appendf(out, "Algorithm: ");
11321 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11322 goto end;
11323 chunk_appendf(out, "%s\n", tmp->area);
11324
11325 chunk_reset(tmp);
11326 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011327 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011328 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011329 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011330 dump_binary(out, tmp->area, tmp->data);
11331 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011332
William Lallemanda90e5932020-02-25 14:07:58 +010011333 chunk_appendf(out, "Subject: ");
11334 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11335 goto end;
11336 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11337 goto end;
11338 *(tmp->area + tmp->data) = '\0';
11339 chunk_appendf(out, "%s\n", tmp->area);
11340
11341 chunk_appendf(out, "Issuer: ");
11342 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11343 goto end;
11344 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11345 goto end;
11346 *(tmp->area + tmp->data) = '\0';
11347 chunk_appendf(out, "%s\n", tmp->area);
11348
William Lallemand35f4a9d2020-02-25 11:56:32 +010011349 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011350 for (i = 0; i < sk_X509_num(chain); i++) {
11351 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011352
William Lallemandbb7288a2020-02-25 14:04:33 +010011353 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011354 if ((name = X509_get_subject_name(ca)) == NULL)
11355 goto end;
11356 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11357 goto end;
11358 *(tmp->area + tmp->data) = '\0';
11359 chunk_appendf(out, "%s\n", tmp->area);
11360
William Lallemandbb7288a2020-02-25 14:04:33 +010011361 chunk_appendf(out, "Chain Issuer: ");
11362 if ((name = X509_get_issuer_name(ca)) == NULL)
11363 goto end;
11364 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11365 goto end;
11366 *(tmp->area + tmp->data) = '\0';
11367 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011368 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011369 }
11370
William Lallemandea987ed2020-03-19 16:48:33 +010011371end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011372 if (ci_putchk(si_ic(si), out) == -1) {
11373 si_rx_room_blk(si);
11374 goto yield;
11375 }
11376
William Lallemand18eeb8e2020-03-20 14:42:36 +010011377end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011378 if (bio)
11379 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011380 free_trash_chunk(tmp);
11381 free_trash_chunk(out);
11382 return 1;
11383yield:
11384 free_trash_chunk(tmp);
11385 free_trash_chunk(out);
11386 return 0; /* should come back */
11387}
11388
11389/* parsing function for 'show ssl cert [certfile]' */
11390static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11391{
11392 struct ckch_store *ckchs;
11393
11394 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11395 return cli_err(appctx, "Can't allocate memory!\n");
11396
11397 /* The operations on the CKCH architecture are locked so we can
11398 * manipulate ckch_store and ckch_inst */
11399 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11400 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11401
11402 /* check if there is a certificate to lookup */
11403 if (*args[3]) {
11404 if (*args[3] == '*') {
11405 if (!ckchs_transaction.new_ckchs)
11406 goto error;
11407
11408 ckchs = ckchs_transaction.new_ckchs;
11409
11410 if (strcmp(args[3] + 1, ckchs->path))
11411 goto error;
11412
11413 } else {
11414 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11415 goto error;
11416
11417 }
11418
11419 if (ckchs->multi)
11420 goto error;
11421
11422 appctx->ctx.cli.p0 = ckchs;
11423 /* use the IO handler that shows details */
11424 appctx->io_handler = cli_io_handler_show_cert_detail;
11425 }
11426
11427 return 0;
11428
11429error:
11430 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11431 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11432}
11433
William Lallemand430413e2019-10-28 14:30:47 +010011434/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011435static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011436{
11437 struct ckch_store *new_ckchs;
11438 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011439
William Lallemand430413e2019-10-28 14:30:47 +010011440 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011441
William Lallemand430413e2019-10-28 14:30:47 +010011442 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011443 /* 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 +010011444 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011445
William Lallemandbeea2a42019-10-30 17:45:33 +010011446 if (!new_ckchs)
11447 return;
William Lallemand8f840d72019-10-23 10:53:05 +020011448
William Lallemandbeea2a42019-10-30 17:45:33 +010011449 /* if the allocation failed, we need to free everything from the temporary list */
11450 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
11451 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020011452
William Lallemandbeea2a42019-10-30 17:45:33 +010011453 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
11454 if (sc0->order == 0) /* we only free if it's the first inserted */
11455 SSL_CTX_free(sc0->ctx);
11456 LIST_DEL(&sc0->by_ckch_inst);
11457 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020011458 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011459 LIST_DEL(&ckchi->by_ckchs);
11460 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020011461 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011462 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011463 }
11464}
11465
11466
11467/*
11468 * This function tries to create the new ckch_inst and their SNIs
11469 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011470static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011471{
11472 struct stream_interface *si = appctx->owner;
11473 int y = 0;
11474 char *err = NULL;
11475 int errcode = 0;
11476 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11477 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011478 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011479 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020011480
William Lallemand33cc76f2019-10-31 11:43:45 +010011481 if (trash == NULL)
11482 goto error;
11483
William Lallemand8f840d72019-10-23 10:53:05 +020011484 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11485 goto error;
11486
William Lallemand430413e2019-10-28 14:30:47 +010011487 while (1) {
11488 switch (appctx->st2) {
11489 case SETCERT_ST_INIT:
11490 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011491 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011492 if (ci_putchk(si_ic(si), trash) == -1) {
11493 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011494 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011495 }
11496 appctx->st2 = SETCERT_ST_GEN;
11497 /* fallthrough */
11498 case SETCERT_ST_GEN:
11499 /*
11500 * This state generates the ckch instances with their
11501 * sni_ctxs and SSL_CTX.
11502 *
William Lallemand430413e2019-10-28 14:30:47 +010011503 * Since the SSL_CTX generation can be CPU consumer, we
11504 * yield every 10 instances.
11505 */
William Lallemand8f840d72019-10-23 10:53:05 +020011506
William Lallemandbeea2a42019-10-30 17:45:33 +010011507 old_ckchs = appctx->ctx.ssl.old_ckchs;
11508 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011509
William Lallemandbeea2a42019-10-30 17:45:33 +010011510 if (!new_ckchs)
11511 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020011512
William Lallemandbeea2a42019-10-30 17:45:33 +010011513 /* get the next ckchi to regenerate */
11514 ckchi = appctx->ctx.ssl.next_ckchi;
11515 /* we didn't start yet, set it to the first elem */
11516 if (ckchi == NULL)
11517 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011518
William Lallemandbeea2a42019-10-30 17:45:33 +010011519 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
11520 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
11521 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010011522 char **sni_filter = NULL;
11523 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020011524
William Lallemandbeea2a42019-10-30 17:45:33 +010011525 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
11526 if (y >= 10) {
11527 /* save the next ckchi to compute */
11528 appctx->ctx.ssl.next_ckchi = ckchi;
11529 goto yield;
11530 }
William Lallemand67630162020-03-09 16:56:39 +010011531 if (ckchi->filters) {
11532 errcode |= ckch_inst_sni_ctx_to_sni_filters(ckchi, &sni_filter, &fcount, &err);
11533 if (errcode & ERR_CODE)
11534 goto error;
11535 }
William Lallemand38df1c82019-12-04 15:39:35 +010011536
William Lallemandbeea2a42019-10-30 17:45:33 +010011537 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010011538 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 +010011539 else
William Lallemand38df1c82019-12-04 15:39:35 +010011540 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
11541
11542 free_sni_filters(sni_filter, fcount);
11543 sni_filter = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020011544
William Lallemandbeea2a42019-10-30 17:45:33 +010011545 if (errcode & ERR_CODE)
11546 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020011547
William Lallemand21724f02019-11-04 17:56:13 +010011548 /* if the previous ckchi was used as the default */
11549 if (ckchi->is_default)
11550 new_inst->is_default = 1;
11551
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011552 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010011553 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11554 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011555 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 +010011556 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
11557 if (errcode & ERR_CODE)
11558 goto error;
11559 }
11560 }
11561
11562
William Lallemandbeea2a42019-10-30 17:45:33 +010011563 /* display one dot per new instance */
11564 chunk_appendf(trash, ".");
11565 /* link the new ckch_inst to the duplicate */
11566 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
11567 y++;
11568 }
William Lallemand430413e2019-10-28 14:30:47 +010011569 appctx->st2 = SETCERT_ST_INSERT;
11570 /* fallthrough */
11571 case SETCERT_ST_INSERT:
11572 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020011573
William Lallemandbeea2a42019-10-30 17:45:33 +010011574 old_ckchs = appctx->ctx.ssl.old_ckchs;
11575 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011576
William Lallemandbeea2a42019-10-30 17:45:33 +010011577 if (!new_ckchs)
11578 continue;
William Lallemand430413e2019-10-28 14:30:47 +010011579
William Lallemand21724f02019-11-04 17:56:13 +010011580 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010011581 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
11582 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
11583 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
11584 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
11585 }
William Lallemand8f840d72019-10-23 10:53:05 +020011586
William Lallemandbeea2a42019-10-30 17:45:33 +010011587 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
11588 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010011589
William Lallemandbeea2a42019-10-30 17:45:33 +010011590 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
11591 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
11592 ebmb_delete(&sc0->name);
11593 LIST_DEL(&sc0->by_ckch_inst);
11594 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010011595 }
William Lallemandbeea2a42019-10-30 17:45:33 +010011596 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
11597 LIST_DEL(&ckchi->by_ckchs);
11598 free(ckchi);
11599 }
William Lallemand8f840d72019-10-23 10:53:05 +020011600
William Lallemandbeea2a42019-10-30 17:45:33 +010011601 /* Replace the old ckchs by the new one */
11602 ebmb_delete(&old_ckchs->node);
11603 ckchs_free(old_ckchs);
11604 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010011605 appctx->st2 = SETCERT_ST_FIN;
11606 /* fallthrough */
11607 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011608 /* we achieved the transaction, we can set everything to NULL */
11609 free(ckchs_transaction.path);
11610 ckchs_transaction.path = NULL;
11611 ckchs_transaction.new_ckchs = NULL;
11612 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010011613 goto end;
11614 }
William Lallemand8f840d72019-10-23 10:53:05 +020011615 }
William Lallemand430413e2019-10-28 14:30:47 +010011616end:
William Lallemand8f840d72019-10-23 10:53:05 +020011617
William Lallemanded442432019-11-21 16:41:07 +010011618 chunk_appendf(trash, "\n");
11619 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010011620 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010011621 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010011622 if (ci_putchk(si_ic(si), trash) == -1)
11623 si_rx_room_blk(si);
11624 free_trash_chunk(trash);
11625 /* success: call the release function and don't come back */
11626 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020011627yield:
11628 /* store the state */
11629 if (ci_putchk(si_ic(si), trash) == -1)
11630 si_rx_room_blk(si);
11631 free_trash_chunk(trash);
11632 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020011633 return 0; /* should come back */
11634
11635error:
11636 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010011637 if (trash) {
11638 chunk_appendf(trash, "\n%sFailed!\n", err);
11639 if (ci_putchk(si_ic(si), trash) == -1)
11640 si_rx_room_blk(si);
11641 free_trash_chunk(trash);
11642 }
William Lallemand430413e2019-10-28 14:30:47 +010011643 /* error: call the release function and don't come back */
11644 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020011645}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011646
11647/*
11648 * Parsing function of 'commit ssl cert'
11649 */
11650static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
11651{
11652 char *err = NULL;
11653
William Lallemand230662a2019-12-03 13:32:54 +010011654 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11655 return 1;
11656
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011657 if (!*args[3])
11658 return cli_err(appctx, "'commit ssl cert expects a filename\n");
11659
11660 /* The operations on the CKCH architecture are locked so we can
11661 * manipulate ckch_store and ckch_inst */
11662 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11663 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
11664
11665 if (!ckchs_transaction.path) {
11666 memprintf(&err, "No ongoing transaction! !\n");
11667 goto error;
11668 }
11669
11670 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
11671 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
11672 goto error;
11673 }
11674
William Lallemand4c5adbf2020-02-24 14:23:22 +010011675#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
11676 if (ckchs_transaction.new_ckchs->multi) {
11677 int n;
11678
11679 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11680 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)) {
11681 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
11682 goto error;
11683 }
11684 }
11685 } else
11686#endif
11687 {
11688 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
11689 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
11690 goto error;
11691 }
11692 }
11693
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011694 /* init the appctx structure */
11695 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011696 appctx->ctx.ssl.next_ckchi = NULL;
11697 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
11698 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
11699
11700 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
11701 return 0;
11702
11703error:
11704
11705 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11706 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
11707
11708 return cli_dynerr(appctx, err);
11709}
11710
William Lallemand8f840d72019-10-23 10:53:05 +020011711/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011712 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020011713 */
William Lallemand150bfa82019-09-19 17:12:49 +020011714static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
11715{
William Lallemand0c3b7d92019-10-18 11:27:07 +020011716 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020011717 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020011718 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020011719 int i;
William Lallemand849eed62019-10-17 16:23:50 +020011720 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011721 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020011722 char *end;
11723 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011724 struct cert_key_and_chain *ckch;
11725 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020011726
William Lallemand230662a2019-12-03 13:32:54 +010011727 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11728 return 1;
11729
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011730 if ((buf = alloc_trash_chunk()) == NULL)
11731 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020011732
11733 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011734 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020011735
11736 /* The operations on the CKCH architecture are locked so we can
11737 * manipulate ckch_store and ckch_inst */
11738 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11739 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
11740
William Lallemand8f840d72019-10-23 10:53:05 +020011741 if (!chunk_strcpy(buf, args[3])) {
11742 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
11743 errcode |= ERR_ALERT | ERR_FATAL;
11744 goto end;
11745 }
11746
William Lallemand44b35322019-10-17 16:28:40 +020011747 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020011748 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020011749 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020011750 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
11751 *end = '\0';
11752 type = cert_exts[i].type;
11753 break;
11754 }
11755 }
11756
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011757 appctx->ctx.ssl.old_ckchs = NULL;
11758 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020011759
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011760 /* if there is an ongoing transaction */
11761 if (ckchs_transaction.path) {
11762 /* 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 +020011763#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011764 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011765 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020011766 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020011767
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011768 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020011769 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020011770 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011771 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020011772 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
11773 bundle = j; /* keep the type of certificate so we insert it at the right place */
11774 *end = '\0'; /* it's a bundle let's end the string*/
11775 break;
11776 }
William Lallemand150bfa82019-09-19 17:12:49 +020011777 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011778 if (bundle < 0) {
11779 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);
11780 errcode |= ERR_ALERT | ERR_FATAL;
11781 goto end;
11782 }
11783 }
11784#endif
11785
11786 /* if there is an ongoing transaction, check if this is the same file */
11787 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
11788 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
11789 errcode |= ERR_ALERT | ERR_FATAL;
11790 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020011791 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011792
11793 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
11794
11795 } else {
11796 struct ckch_store *find_ckchs[2] = { NULL, NULL };
11797
11798 /* lookup for the certificate in the tree:
11799 * check if this is used as a bundle AND as a unique certificate */
11800 for (i = 0; i < 2; i++) {
11801
11802 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
11803 /* only the bundle name is in the tree and you should
11804 * never update a bundle name, only a filename */
11805 if (bundle < 0 && find_ckchs[i]->multi) {
11806 /* we tried to look for a non-bundle and we found a bundle */
11807 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
11808 err ? err : "", args[3], args[3]);
11809 errcode |= ERR_ALERT | ERR_FATAL;
11810 goto end;
11811 }
William Lallemand3246d942019-11-04 14:02:11 +010011812 /* If we want a bundle but this is not a bundle
11813 * example: When you try to update <file>.rsa, but
11814 * <file> is a regular file */
11815 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
11816 find_ckchs[i] = NULL;
11817 break;
11818 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011819 }
11820#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
11821 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011822 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011823 int j;
11824
11825 /* check if it was used in a bundle by removing the
11826 * .dsa/.rsa/.ecdsa at the end of the filename */
11827 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011828 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011829 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
11830 bundle = j; /* keep the type of certificate so we insert it at the right place */
11831 *end = '\0'; /* it's a bundle let's end the string*/
11832 break;
11833 }
11834 }
William Lallemand37031b82019-11-04 13:38:53 +010011835 if (bundle < 0) /* we didn't find a bundle extension */
11836 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011837 }
William Lallemand963b2e72019-10-14 11:38:36 +020011838#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011839 /* bundles are not supported here, so we don't need to lookup again */
11840 break;
William Lallemand963b2e72019-10-14 11:38:36 +020011841#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011842 }
11843
11844 if (find_ckchs[0] && find_ckchs[1]) {
11845 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",
11846 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
11847 errcode |= ERR_ALERT | ERR_FATAL;
11848 goto end;
11849 }
11850
11851 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020011852 }
11853
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011854 if (!appctx->ctx.ssl.old_ckchs) {
11855 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020011856 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011857 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020011858 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020011859 }
11860
William Lallemand8a7fdf02019-11-04 10:59:32 +010011861 if (!appctx->ctx.ssl.path) {
11862 /* this is a new transaction, set the path of the transaction */
11863 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
11864 if (!appctx->ctx.ssl.path) {
11865 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
11866 errcode |= ERR_ALERT | ERR_FATAL;
11867 goto end;
11868 }
11869 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011870
11871 old_ckchs = appctx->ctx.ssl.old_ckchs;
11872
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011873 /* duplicate the ckch store */
11874 new_ckchs = ckchs_dup(old_ckchs);
11875 if (!new_ckchs) {
11876 memprintf(&err, "%sCannot allocate memory!\n",
11877 err ? err : "");
11878 errcode |= ERR_ALERT | ERR_FATAL;
11879 goto end;
11880 }
11881
11882 if (!new_ckchs->multi)
11883 ckch = new_ckchs->ckch;
11884 else
11885 ckch = &new_ckchs->ckch[bundle];
11886
11887 /* appply the change on the duplicate */
11888 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
11889 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
11890 errcode |= ERR_ALERT | ERR_FATAL;
11891 goto end;
11892 }
11893
11894 appctx->ctx.ssl.new_ckchs = new_ckchs;
11895
11896 /* we succeed, we can save the ckchs in the transaction */
11897
11898 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010011899 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011900 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
11901 ckchs_transaction.path = appctx->ctx.ssl.path;
11902 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
11903 } else {
11904 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
11905
11906 }
11907
11908 /* free the previous ckchs if there was a transaction */
11909 ckchs_free(ckchs_transaction.new_ckchs);
11910
11911 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
11912
11913
William Lallemand8f840d72019-10-23 10:53:05 +020011914 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020011915
William Lallemand8f840d72019-10-23 10:53:05 +020011916end:
11917 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020011918
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011919 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011920
11921 ckchs_free(appctx->ctx.ssl.new_ckchs);
11922 appctx->ctx.ssl.new_ckchs = NULL;
11923
11924 appctx->ctx.ssl.old_ckchs = NULL;
11925
11926 free(appctx->ctx.ssl.path);
11927 appctx->ctx.ssl.path = NULL;
11928
11929 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020011930 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010011931 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011932
11933 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11934 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010011935 }
William Lallemand8f840d72019-10-23 10:53:05 +020011936 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020011937}
11938
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011939/* parsing function of 'abort ssl cert' */
11940static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
11941{
11942 char *err = NULL;
11943
William Lallemand230662a2019-12-03 13:32:54 +010011944 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11945 return 1;
11946
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011947 if (!*args[3])
11948 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
11949
11950 /* The operations on the CKCH architecture are locked so we can
11951 * manipulate ckch_store and ckch_inst */
11952 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11953 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
11954
11955 if (!ckchs_transaction.path) {
11956 memprintf(&err, "No ongoing transaction!\n");
11957 goto error;
11958 }
11959
11960 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
11961 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
11962 goto error;
11963 }
11964
11965 /* Only free the ckchs there, because the SNI and instances were not generated yet */
11966 ckchs_free(ckchs_transaction.new_ckchs);
11967 ckchs_transaction.new_ckchs = NULL;
11968 ckchs_free(ckchs_transaction.old_ckchs);
11969 ckchs_transaction.old_ckchs = NULL;
11970 free(ckchs_transaction.path);
11971 ckchs_transaction.path = NULL;
11972
11973 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11974
11975 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
11976 return cli_dynmsg(appctx, LOG_NOTICE, err);
11977
11978error:
11979 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11980
11981 return cli_dynerr(appctx, err);
11982}
11983
William Lallemandea987ed2020-03-19 16:48:33 +010011984/* parsing function of 'new ssl cert' */
11985static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
11986{
11987 struct ckch_store *store;
11988 char *err = NULL;
11989 char *path;
11990
11991 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11992 return 1;
11993
11994 if (!*args[3])
11995 return cli_err(appctx, "'new ssl cert' expects a filename\n");
11996
11997 path = args[3];
11998
11999 /* The operations on the CKCH architecture are locked so we can
12000 * manipulate ckch_store and ckch_inst */
12001 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12002 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12003
12004 store = ckchs_lookup(path);
12005 if (store != NULL) {
12006 memprintf(&err, "Certificate '%s' already exists!\n", path);
12007 store = NULL; /* we don't want to free it */
12008 goto error;
12009 }
12010 store = calloc(1, sizeof(*store) + strlen(path) + 1);
12011 if (!store) {
12012 memprintf(&err, "unable to allocate memory.\n");
12013 goto error;
12014 }
12015 store->ckch = calloc(1, sizeof(*store->ckch));
12016 if (!store->ckch) {
12017 memprintf(&err, "unable to allocate memory.\n");
12018 goto error;
12019 }
12020 /* we won't create any instance */
12021 LIST_INIT(&store->ckch_inst);
12022
12023 /* we won't support multi-certificate bundle here */
12024 store->multi = 0;
12025
12026 /* insert into the ckchs tree */
12027 memcpy(store->path, path, strlen(path) + 1);
12028 ebst_insert(&ckchs_tree, &store->node);
12029 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12030
12031 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12032 return cli_dynmsg(appctx, LOG_NOTICE, err);
12033error:
12034 free(store);
12035 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12036 return cli_dynerr(appctx, err);
12037}
12038
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012039static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012040{
12041#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12042 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012043 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012044
12045 if (!payload)
12046 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012047
12048 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012049 if (!*payload)
12050 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012051
12052 /* remove \r and \n from the payload */
12053 for (i = 0, j = 0; payload[i]; i++) {
12054 if (payload[i] == '\r' || payload[i] == '\n')
12055 continue;
12056 payload[j++] = payload[i];
12057 }
12058 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012059
Willy Tarreau1c913e42018-08-22 05:26:57 +020012060 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012061 if (ret < 0)
12062 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012063
Willy Tarreau1c913e42018-08-22 05:26:57 +020012064 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012065 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012066 if (err)
12067 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12068 else
12069 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012070 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012071
12072 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012073#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012074 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 +020012075#endif
12076
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012077}
12078
Willy Tarreau86a394e2019-05-09 14:15:32 +020012079#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012080static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12081{
12082 switch (arg->type) {
12083 case ARGT_STR:
12084 smp->data.type = SMP_T_STR;
12085 smp->data.u.str = arg->data.str;
12086 return 1;
12087 case ARGT_VAR:
12088 if (!vars_get_by_desc(&arg->data.var, smp))
12089 return 0;
12090 if (!sample_casts[smp->data.type][SMP_T_STR])
12091 return 0;
12092 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12093 return 0;
12094 return 1;
12095 default:
12096 return 0;
12097 }
12098}
12099
12100static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12101 const char *file, int line, char **err)
12102{
12103 switch(args[0].data.sint) {
12104 case 128:
12105 case 192:
12106 case 256:
12107 break;
12108 default:
12109 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12110 return 0;
12111 }
12112 /* Try to decode a variable. */
12113 vars_check_arg(&args[1], NULL);
12114 vars_check_arg(&args[2], NULL);
12115 vars_check_arg(&args[3], NULL);
12116 return 1;
12117}
12118
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012119/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012120static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12121{
12122 struct sample nonce, key, aead_tag;
12123 struct buffer *smp_trash, *smp_trash_alloc;
12124 EVP_CIPHER_CTX *ctx;
12125 int dec_size, ret;
12126
12127 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12128 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12129 return 0;
12130
12131 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12132 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12133 return 0;
12134
12135 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12136 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12137 return 0;
12138
12139 smp_trash = get_trash_chunk();
12140 smp_trash_alloc = alloc_trash_chunk();
12141 if (!smp_trash_alloc)
12142 return 0;
12143
12144 ctx = EVP_CIPHER_CTX_new();
12145
12146 if (!ctx)
12147 goto err;
12148
12149 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12150 if (dec_size < 0)
12151 goto err;
12152 smp_trash->data = dec_size;
12153
12154 /* Set cipher type and mode */
12155 switch(arg_p[0].data.sint) {
12156 case 128:
12157 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12158 break;
12159 case 192:
12160 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12161 break;
12162 case 256:
12163 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12164 break;
12165 }
12166
12167 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12168
12169 /* Initialise IV */
12170 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12171 goto err;
12172
12173 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12174 if (dec_size < 0)
12175 goto err;
12176 smp_trash->data = dec_size;
12177
12178 /* Initialise key */
12179 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12180 goto err;
12181
12182 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12183 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12184 goto err;
12185
12186 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12187 if (dec_size < 0)
12188 goto err;
12189 smp_trash_alloc->data = dec_size;
12190 dec_size = smp_trash->data;
12191
12192 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12193 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12194
12195 if (ret <= 0)
12196 goto err;
12197
12198 smp->data.u.str.data = dec_size + smp_trash->data;
12199 smp->data.u.str.area = smp_trash->area;
12200 smp->data.type = SMP_T_BIN;
12201 smp->flags &= ~SMP_F_CONST;
12202 free_trash_chunk(smp_trash_alloc);
12203 return 1;
12204
12205err:
12206 free_trash_chunk(smp_trash_alloc);
12207 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012208}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012209# endif
William Lallemand32af2032016-10-29 18:09:35 +020012210
Elliot Otchet71f82972020-01-15 08:12:14 -050012211/* Argument validation functions */
12212
12213/* This function is used to validate the arguments passed to any "x_dn" ssl
12214 * keywords. These keywords support specifying a third parameter that must be
12215 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12216 */
12217int val_dnfmt(struct arg *arg, char **err_msg)
12218{
12219 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12220 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12221 return 0;
12222 }
12223 return 1;
12224}
12225
William Lallemand32af2032016-10-29 18:09:35 +020012226/* register cli keywords */
12227static struct cli_kw_list cli_kws = {{ },{
12228#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12229 { { "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 +020012230 { { "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 +020012231#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012232 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012233 { { "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 +010012234 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12235 { { "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 +010012236 { { "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 +010012237 { { "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 Lallemanda6ffd5b2020-03-09 13:35:19 +010012238 { { "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 },
12239 { { "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 +020012240 { { NULL }, NULL, NULL, NULL }
12241}};
12242
Willy Tarreau0108d902018-11-25 19:14:37 +010012243INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012244
Willy Tarreau7875d092012-09-10 08:20:03 +020012245/* Note: must not be declared <const> as its list will be overwritten.
12246 * Please take care of keeping this list alphabetically sorted.
12247 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012248static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012249 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012250 { "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 +010012251#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012252 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012253#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012254 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012255#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12256 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12257#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012258 { "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 +020012259 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012260 { "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 +020012261 { "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 +020012262#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012263 { "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 -040012264#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012265#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012266 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12267 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012268 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12269#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012270 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12271 { "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 +010012272 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012273 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012274 { "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 +020012275 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12276 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12277 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12278 { "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 -050012279 { "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 +020012280 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12281 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012282 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012283 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12284 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012285 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012286 { "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 +020012287 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12288 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12289 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12290 { "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 -050012291 { "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 +020012292 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012293 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012294 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012295 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012296 { "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 +010012297 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012298 { "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 +020012299 { "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 +010012300 { "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 +020012301 { "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 +010012302#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012303 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012304#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012305#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012306 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012307#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012308 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012309#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012310 { "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 -040012311#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012312 { "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 +020012313#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012314 { "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 -040012315#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012316#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012317 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12318 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012319 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12320#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012321#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012322 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012323#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012324 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12325 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12326 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12327 { "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 +020012328 { NULL, NULL, 0, 0, 0 },
12329}};
12330
Willy Tarreau0108d902018-11-25 19:14:37 +010012331INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12332
Willy Tarreau7875d092012-09-10 08:20:03 +020012333/* Note: must not be declared <const> as its list will be overwritten.
12334 * Please take care of keeping this list alphabetically sorted.
12335 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012336static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012337 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12338 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012339 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012340}};
12341
Willy Tarreau0108d902018-11-25 19:14:37 +010012342INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12343
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012344/* Note: must not be declared <const> as its list will be overwritten.
12345 * Please take care of keeping this list alphabetically sorted, doing so helps
12346 * all code contributors.
12347 * Optional keywords are also declared with a NULL ->parse() function so that
12348 * the config parser can report an appropriate error when a known keyword was
12349 * not enabled.
12350 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012351static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012352 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012353 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012354 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12355 { "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 +010012356 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012357#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012358 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12359#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012360 { "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 +010012361 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012362 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012363 { "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 +010012364 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012365 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12366 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012367 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12368 { NULL, NULL, 0 },
12369};
12370
Willy Tarreau0108d902018-11-25 19:14:37 +010012371/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12372
Willy Tarreau51fb7652012-09-18 18:24:39 +020012373static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012374 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012375 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012376 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12377 { "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 +020012378 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12379 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12380 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12381 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012382#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012383 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12384#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012385 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012386 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012387 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012388 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12389 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12390 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12391 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12392 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12393 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12394 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012395 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012396 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012397 { "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 +020012398 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12399 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12400 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12401 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012402 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012403 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12404 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012405 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12406 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012407 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12408 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12409 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12410 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12411 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012412 { NULL, NULL, 0 },
12413}};
Emeric Brun46591952012-05-18 15:47:34 +020012414
Willy Tarreau0108d902018-11-25 19:14:37 +010012415INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12416
Willy Tarreau92faadf2012-10-10 23:04:25 +020012417/* Note: must not be declared <const> as its list will be overwritten.
12418 * Please take care of keeping this list alphabetically sorted, doing so helps
12419 * all code contributors.
12420 * Optional keywords are also declared with a NULL ->parse() function so that
12421 * the config parser can report an appropriate error when a known keyword was
12422 * not enabled.
12423 */
12424static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012425 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012426 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012427 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012428 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012429 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012430 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12431 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012432#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012433 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12434#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012435 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12436 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12437 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12438 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12439 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12440 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12441 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12442 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12443 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12444 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12445 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12446 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12447 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12448 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12449 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12450 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12451 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12452 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012453 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012454 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12455 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12456 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12457 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12458 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12459 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12460 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12461 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12462 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12463 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012464 { NULL, NULL, 0, 0 },
12465}};
12466
Willy Tarreau0108d902018-11-25 19:14:37 +010012467INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12468
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012469static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012470 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12471 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012472 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012473 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012474 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12475 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012476#ifndef OPENSSL_NO_DH
12477 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12478#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012479 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012480#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012481 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012482#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010012483 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12484#ifndef OPENSSL_NO_DH
12485 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12486#endif
12487 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12488 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12489 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12490 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012491 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012492 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
12493 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012494#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012495 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
12496 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
12497#endif
William Lallemand3af48e72020-02-03 17:15:52 +010012498 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012499 { 0, NULL, NULL },
12500}};
12501
Willy Tarreau0108d902018-11-25 19:14:37 +010012502INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
12503
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012504/* Note: must not be declared <const> as its list will be overwritten */
12505static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020012506#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012507 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
12508#endif
12509 { NULL, NULL, 0, 0, 0 },
12510}};
12511
12512INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
12513
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020012514/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010012515static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020012516 .snd_buf = ssl_sock_from_buf,
12517 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010012518 .subscribe = ssl_subscribe,
12519 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020012520 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020012521 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020012522 .rcv_pipe = NULL,
12523 .snd_pipe = NULL,
12524 .shutr = NULL,
12525 .shutw = ssl_sock_shutw,
12526 .close = ssl_sock_close,
12527 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010012528 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010012529 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010012530 .prepare_srv = ssl_sock_prepare_srv_ctx,
12531 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010012532 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010012533 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020012534};
12535
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012536enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
12537 struct session *sess, struct stream *s, int flags)
12538{
12539 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012540 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012541
12542 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012543 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012544
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012545 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012546 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012547 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012548 s->req.flags |= CF_READ_NULL;
12549 return ACT_RET_YIELD;
12550 }
12551 }
12552 return (ACT_RET_CONT);
12553}
12554
12555static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
12556{
12557 rule->action_ptr = ssl_action_wait_for_hs;
12558
12559 return ACT_RET_PRS_OK;
12560}
12561
12562static struct action_kw_list http_req_actions = {ILH, {
12563 { "wait-for-handshake", ssl_parse_wait_for_hs },
12564 { /* END */ }
12565}};
12566
Willy Tarreau0108d902018-11-25 19:14:37 +010012567INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
12568
Willy Tarreau5db847a2019-05-09 14:13:35 +020012569#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010012570
12571static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
12572{
12573 if (ptr) {
12574 chunk_destroy(ptr);
12575 free(ptr);
12576 }
12577}
12578
12579#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010012580static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
12581{
Willy Tarreaubafbe012017-11-24 17:34:44 +010012582 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010012583}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010012584
Emeric Brun46591952012-05-18 15:47:34 +020012585__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020012586static void __ssl_sock_init(void)
12587{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050012588#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020012589 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050012590 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050012591#endif
Emeric Brun46591952012-05-18 15:47:34 +020012592
Willy Tarreauef934602016-12-22 23:12:01 +010012593 if (global_ssl.listen_default_ciphers)
12594 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
12595 if (global_ssl.connect_default_ciphers)
12596 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012597#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012598 if (global_ssl.listen_default_ciphersuites)
12599 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
12600 if (global_ssl.connect_default_ciphersuites)
12601 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
12602#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010012603
Willy Tarreau13e14102016-12-22 20:25:26 +010012604 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012605#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020012606 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080012607#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050012608#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020012609 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050012610 n = sk_SSL_COMP_num(cm);
12611 while (n--) {
12612 (void) sk_SSL_COMP_pop(cm);
12613 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050012614#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050012615
Willy Tarreau5db847a2019-05-09 14:13:35 +020012616#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020012617 ssl_locking_init();
12618#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020012619#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010012620 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
12621#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020012622 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020012623 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 +020012624#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012625 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012626 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012627#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010012628#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12629 hap_register_post_check(tlskeys_finalize_config);
12630#endif
Willy Tarreau80713382018-11-26 10:19:54 +010012631
12632 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
12633 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
12634
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012635 hap_register_post_deinit(ssl_free_global_issuers);
12636
Willy Tarreau80713382018-11-26 10:19:54 +010012637#ifndef OPENSSL_NO_DH
12638 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
12639 hap_register_post_deinit(ssl_free_dh);
12640#endif
12641#ifndef OPENSSL_NO_ENGINE
12642 hap_register_post_deinit(ssl_free_engines);
12643#endif
12644 /* Load SSL string for the verbose & debug mode. */
12645 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020012646 ha_meth = BIO_meth_new(0x666, "ha methods");
12647 BIO_meth_set_write(ha_meth, ha_ssl_write);
12648 BIO_meth_set_read(ha_meth, ha_ssl_read);
12649 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
12650 BIO_meth_set_create(ha_meth, ha_ssl_new);
12651 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
12652 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
12653 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020012654
12655 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010012656}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010012657
Willy Tarreau80713382018-11-26 10:19:54 +010012658/* Compute and register the version string */
12659static void ssl_register_build_options()
12660{
12661 char *ptr = NULL;
12662 int i;
12663
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012664 memprintf(&ptr, "Built with OpenSSL version : "
12665#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010012666 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012667#else /* OPENSSL_IS_BORINGSSL */
12668 OPENSSL_VERSION_TEXT
12669 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080012670 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020012671 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012672#endif
12673 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012674#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012675 "no (library version too old)"
12676#elif defined(OPENSSL_NO_TLSEXT)
12677 "no (disabled via OPENSSL_NO_TLSEXT)"
12678#else
12679 "yes"
12680#endif
12681 "", ptr);
12682
12683 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
12684#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
12685 "yes"
12686#else
12687#ifdef OPENSSL_NO_TLSEXT
12688 "no (because of OPENSSL_NO_TLSEXT)"
12689#else
12690 "no (version might be too old, 0.9.8f min needed)"
12691#endif
12692#endif
12693 "", ptr);
12694
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020012695 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
12696 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
12697 if (methodVersions[i].option)
12698 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010012699
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012700 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010012701}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012702
Willy Tarreau80713382018-11-26 10:19:54 +010012703INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020012704
Emeric Brun46591952012-05-18 15:47:34 +020012705
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012706#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012707void ssl_free_engines(void) {
12708 struct ssl_engine_list *wl, *wlb;
12709 /* free up engine list */
12710 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
12711 ENGINE_finish(wl->e);
12712 ENGINE_free(wl->e);
12713 LIST_DEL(&wl->list);
12714 free(wl);
12715 }
12716}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012717#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020012718
Remi Gacogned3a23c32015-05-28 16:39:47 +020012719#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000012720void ssl_free_dh(void) {
12721 if (local_dh_1024) {
12722 DH_free(local_dh_1024);
12723 local_dh_1024 = NULL;
12724 }
12725 if (local_dh_2048) {
12726 DH_free(local_dh_2048);
12727 local_dh_2048 = NULL;
12728 }
12729 if (local_dh_4096) {
12730 DH_free(local_dh_4096);
12731 local_dh_4096 = NULL;
12732 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020012733 if (global_dh) {
12734 DH_free(global_dh);
12735 global_dh = NULL;
12736 }
Grant Zhang872f9c22017-01-21 01:10:18 +000012737}
12738#endif
12739
12740__attribute__((destructor))
12741static void __ssl_sock_deinit(void)
12742{
12743#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020012744 if (ssl_ctx_lru_tree) {
12745 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010012746 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020012747 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020012748#endif
12749
Willy Tarreau5db847a2019-05-09 14:13:35 +020012750#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020012751 ERR_remove_state(0);
12752 ERR_free_strings();
12753
12754 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080012755#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020012756
Willy Tarreau5db847a2019-05-09 14:13:35 +020012757#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020012758 CRYPTO_cleanup_all_ex_data();
12759#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020012760 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020012761}
12762
12763
Emeric Brun46591952012-05-18 15:47:34 +020012764/*
12765 * Local variables:
12766 * c-indent-level: 8
12767 * c-basic-offset: 8
12768 * End:
12769 */