blob: 4374788192db9b850cdb5dd8698655be0c425aa7 [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
William Lallemand2954c472020-03-06 21:54:13 +010060#include <ebpttree.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020061#include <ebsttree.h>
62
William Lallemand32af2032016-10-29 18:09:35 +020063#include <types/applet.h>
64#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020065#include <types/global.h>
66#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020067#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020068
Willy Tarreau7875d092012-09-10 08:20:03 +020069#include <proto/acl.h>
70#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020071#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020072#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020073#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020074#include <proto/fd.h>
75#include <proto/freq_ctr.h>
76#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020077#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020078#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010079#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020080#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020081#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020082#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020083#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020084#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020085#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020086#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020087#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020088#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020089#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010090#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020091
Willy Tarreau9356dac2019-05-10 09:22:53 +020092/* ***** READ THIS before adding code here! *****
93 *
94 * Due to API incompatibilities between multiple OpenSSL versions and their
95 * derivatives, it's often tempting to add macros to (re-)define certain
96 * symbols. Please do not do this here, and do it in common/openssl-compat.h
97 * exclusively so that the whole code consistently uses the same macros.
98 *
99 * Whenever possible if a macro is missing in certain versions, it's better
100 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
101 */
102
Willy Tarreau518cedd2014-02-17 15:43:01 +0100103/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200104#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100105#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100106#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200107#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
108
Emeric Brunf282a812012-09-21 15:27:54 +0200109/* bits 0xFFFF0000 are reserved to store verify errors */
110
111/* Verify errors macros */
112#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
113#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
114#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
115
116#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
117#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
118#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200119
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200120/* ssl_methods flags for ssl options */
121#define MC_SSL_O_ALL 0x0000
122#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
123#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
124#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
125#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200126#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200127
William Lallemand3af48e72020-02-03 17:15:52 +0100128/* file to guess during file loading */
129#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
130#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
131#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
132#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
133#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
William Lallemand4c5adbf2020-02-24 14:23:22 +0100134#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
William Lallemand3af48e72020-02-03 17:15:52 +0100135
William Lallemand4c5adbf2020-02-24 14:23:22 +0100136#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
William Lallemand3af48e72020-02-03 17:15:52 +0100137
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200138/* ssl_methods versions */
139enum {
140 CONF_TLSV_NONE = 0,
141 CONF_TLSV_MIN = 1,
142 CONF_SSLV3 = 1,
143 CONF_TLSV10 = 2,
144 CONF_TLSV11 = 3,
145 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200146 CONF_TLSV13 = 5,
147 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200148};
149
Emeric Brun850efd52014-01-29 12:24:34 +0100150/* server and bind verify method, it uses a global value as default */
151enum {
152 SSL_SOCK_VERIFY_DEFAULT = 0,
153 SSL_SOCK_VERIFY_REQUIRED = 1,
154 SSL_SOCK_VERIFY_OPTIONAL = 2,
155 SSL_SOCK_VERIFY_NONE = 3,
156};
157
Willy Tarreau71b734c2014-01-28 15:19:44 +0100158int sslconns = 0;
159int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100160static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100161int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200162
William Lallemande0f3fd52020-02-25 14:53:06 +0100163static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +0100164static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert);
William Lallemande0f3fd52020-02-25 14:53:06 +0100165
Willy Tarreauef934602016-12-22 23:12:01 +0100166static struct {
167 char *crt_base; /* base directory path for certificates */
168 char *ca_base; /* base directory path for CAs and CRLs */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100169 char *issuers_chain_path; /* from "issuers-chain-path" */
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +0200170 int skip_self_issued_ca;
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100171
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000172 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100173
174 char *listen_default_ciphers;
175 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200176#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200177 char *listen_default_ciphersuites;
178 char *connect_default_ciphersuites;
179#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100180 int listen_default_ssloptions;
181 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200182 struct tls_version_filter listen_default_sslmethods;
183 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100184
185 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
186 unsigned int life_time; /* SSL session lifetime in seconds */
187 unsigned int max_record; /* SSL max record size */
188 unsigned int default_dh_param; /* SSL maximum DH parameter size */
189 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100190 int capture_cipherlist; /* Size of the cipherlist buffer. */
William Lallemand3af48e72020-02-03 17:15:52 +0100191 int extra_files; /* which files not defined in the configuration file are we looking for */
Willy Tarreauef934602016-12-22 23:12:01 +0100192} global_ssl = {
193#ifdef LISTEN_DEFAULT_CIPHERS
194 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
195#endif
196#ifdef CONNECT_DEFAULT_CIPHERS
197 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
198#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200199#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200200#ifdef LISTEN_DEFAULT_CIPHERSUITES
201 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
202#endif
203#ifdef CONNECT_DEFAULT_CIPHERSUITES
204 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
205#endif
206#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100207 .listen_default_ssloptions = BC_SSL_O_NONE,
208 .connect_default_ssloptions = SRV_SSL_O_NONE,
209
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200210 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
211 .listen_default_sslmethods.min = CONF_TLSV_NONE,
212 .listen_default_sslmethods.max = CONF_TLSV_NONE,
213 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
214 .connect_default_sslmethods.min = CONF_TLSV_NONE,
215 .connect_default_sslmethods.max = CONF_TLSV_NONE,
216
Willy Tarreauef934602016-12-22 23:12:01 +0100217#ifdef DEFAULT_SSL_MAX_RECORD
218 .max_record = DEFAULT_SSL_MAX_RECORD,
219#endif
220 .default_dh_param = SSL_DEFAULT_DH_PARAM,
221 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100222 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100223 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100224};
225
Olivier Houcharda8955d52019-04-07 22:00:38 +0200226static BIO_METHOD *ha_meth;
227
Olivier Houchard66ab4982019-02-26 18:37:15 +0100228struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200229 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100230 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200231 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200232 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100233 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200234 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100235 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100236 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100237 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100238 int sent_early_data; /* Amount of early data we sent so far */
239
Olivier Houchard66ab4982019-02-26 18:37:15 +0100240};
241
242DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
243
Olivier Houchardea8dd942019-05-20 14:02:16 +0200244static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200245static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200246
Olivier Houcharda8955d52019-04-07 22:00:38 +0200247/* Methods to implement OpenSSL BIO */
248static int ha_ssl_write(BIO *h, const char *buf, int num)
249{
250 struct buffer tmpbuf;
251 struct ssl_sock_ctx *ctx;
252 int ret;
253
254 ctx = BIO_get_data(h);
255 tmpbuf.size = num;
256 tmpbuf.area = (void *)(uintptr_t)buf;
257 tmpbuf.data = num;
258 tmpbuf.head = 0;
259 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200260 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200261 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200262 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200263 } else if (ret == 0)
264 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200265 return ret;
266}
267
268static int ha_ssl_gets(BIO *h, char *buf, int size)
269{
270
271 return 0;
272}
273
274static int ha_ssl_puts(BIO *h, const char *str)
275{
276
277 return ha_ssl_write(h, str, strlen(str));
278}
279
280static int ha_ssl_read(BIO *h, char *buf, int size)
281{
282 struct buffer tmpbuf;
283 struct ssl_sock_ctx *ctx;
284 int ret;
285
286 ctx = BIO_get_data(h);
287 tmpbuf.size = size;
288 tmpbuf.area = buf;
289 tmpbuf.data = 0;
290 tmpbuf.head = 0;
291 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200292 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200293 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200294 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200295 } else if (ret == 0)
296 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200297
298 return ret;
299}
300
301static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
302{
303 int ret = 0;
304 switch (cmd) {
305 case BIO_CTRL_DUP:
306 case BIO_CTRL_FLUSH:
307 ret = 1;
308 break;
309 }
310 return ret;
311}
312
313static int ha_ssl_new(BIO *h)
314{
315 BIO_set_init(h, 1);
316 BIO_set_data(h, NULL);
317 BIO_clear_flags(h, ~0);
318 return 1;
319}
320
321static int ha_ssl_free(BIO *data)
322{
323
324 return 1;
325}
326
327
Willy Tarreau5db847a2019-05-09 14:13:35 +0200328#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100329
Emeric Brun821bb9b2017-06-15 16:37:39 +0200330static HA_RWLOCK_T *ssl_rwlocks;
331
332
333unsigned long ssl_id_function(void)
334{
335 return (unsigned long)tid;
336}
337
338void ssl_locking_function(int mode, int n, const char * file, int line)
339{
340 if (mode & CRYPTO_LOCK) {
341 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100342 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200343 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100344 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200345 }
346 else {
347 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100348 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200349 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100350 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200351 }
352}
353
354static int ssl_locking_init(void)
355{
356 int i;
357
358 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
359 if (!ssl_rwlocks)
360 return -1;
361
362 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100363 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200364
365 CRYPTO_set_id_callback(ssl_id_function);
366 CRYPTO_set_locking_callback(ssl_locking_function);
367
368 return 0;
369}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100370
Emeric Brun821bb9b2017-06-15 16:37:39 +0200371#endif
372
William Lallemand150bfa82019-09-19 17:12:49 +0200373__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200374
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100375/* Uncommitted CKCH transaction */
376
377static struct {
378 struct ckch_store *new_ckchs;
379 struct ckch_store *old_ckchs;
380 char *path;
381} ckchs_transaction;
382
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200383/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100384 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200385 */
386struct cafile_entry {
387 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200388 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200389 struct ebmb_node node;
390 char path[0];
391};
392
393static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
394
395static X509_STORE* ssl_store_get0_locations_file(char *path)
396{
397 struct ebmb_node *eb;
398
399 eb = ebst_lookup(&cafile_tree, path);
400 if (eb) {
401 struct cafile_entry *ca_e;
402 ca_e = ebmb_entry(eb, struct cafile_entry, node);
403 return ca_e->ca_store;
404 }
405 return NULL;
406}
407
408static int ssl_store_load_locations_file(char *path)
409{
410 if (ssl_store_get0_locations_file(path) == NULL) {
411 struct cafile_entry *ca_e;
412 X509_STORE *store = X509_STORE_new();
413 if (X509_STORE_load_locations(store, path, NULL)) {
414 int pathlen;
415 pathlen = strlen(path);
416 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
417 if (ca_e) {
418 memcpy(ca_e->path, path, pathlen + 1);
419 ca_e->ca_store = store;
420 ebst_insert(&cafile_tree, &ca_e->node);
421 return 1;
422 }
423 }
424 X509_STORE_free(store);
425 return 0;
426 }
427 return 1;
428}
429
430/* mimic what X509_STORE_load_locations do with store_ctx */
431static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
432{
433 X509_STORE *store;
434 store = ssl_store_get0_locations_file(path);
435 if (store_ctx && store) {
436 int i;
437 X509_OBJECT *obj;
438 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
439 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
440 obj = sk_X509_OBJECT_value(objs, i);
441 switch (X509_OBJECT_get_type(obj)) {
442 case X509_LU_X509:
443 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
444 break;
445 case X509_LU_CRL:
446 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
447 break;
448 default:
449 break;
450 }
451 }
452 return 1;
453 }
454 return 0;
455}
456
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500457/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200458static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
459{
460 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
461 return ssl_set_cert_crl_file(store_ctx, path);
462}
463
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200464/*
465 Extract CA_list from CA_file already in tree.
466 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
467 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
468*/
469static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
470{
471 struct ebmb_node *eb;
472 struct cafile_entry *ca_e;
473
474 eb = ebst_lookup(&cafile_tree, path);
475 if (!eb)
476 return NULL;
477 ca_e = ebmb_entry(eb, struct cafile_entry, node);
478
479 if (ca_e->ca_list == NULL) {
480 int i;
481 unsigned long key;
482 struct eb_root ca_name_tree = EB_ROOT;
483 struct eb64_node *node, *back;
484 struct {
485 struct eb64_node node;
486 X509_NAME *xname;
487 } *ca_name;
488 STACK_OF(X509_OBJECT) *objs;
489 STACK_OF(X509_NAME) *skn;
490 X509 *x;
491 X509_NAME *xn;
492
493 skn = sk_X509_NAME_new_null();
494 /* take x509 from cafile_tree */
495 objs = X509_STORE_get0_objects(ca_e->ca_store);
496 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
497 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
498 if (!x)
499 continue;
500 xn = X509_get_subject_name(x);
501 if (!xn)
502 continue;
503 /* Check for duplicates. */
504 key = X509_NAME_hash(xn);
505 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
506 node && ca_name == NULL;
507 node = eb64_next(node)) {
508 ca_name = container_of(node, typeof(*ca_name), node);
509 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
510 ca_name = NULL;
511 }
512 /* find a duplicate */
513 if (ca_name)
514 continue;
515 ca_name = calloc(1, sizeof *ca_name);
516 xn = X509_NAME_dup(xn);
517 if (!ca_name ||
518 !xn ||
519 !sk_X509_NAME_push(skn, xn)) {
520 free(ca_name);
521 X509_NAME_free(xn);
522 sk_X509_NAME_pop_free(skn, X509_NAME_free);
523 sk_X509_NAME_free(skn);
524 skn = NULL;
525 break;
526 }
527 ca_name->node.key = key;
528 ca_name->xname = xn;
529 eb64_insert(&ca_name_tree, &ca_name->node);
530 }
531 ca_e->ca_list = skn;
532 /* remove temporary ca_name tree */
533 node = eb64_first(&ca_name_tree);
534 while (node) {
535 ca_name = container_of(node, typeof(*ca_name), node);
536 back = eb64_next(node);
537 eb64_delete(node);
538 free(ca_name);
539 node = back;
540 }
541 }
542 return ca_e->ca_list;
543}
544
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100545/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100546struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100547 unsigned long long int xxh64;
548 unsigned char ciphersuite_len;
549 char ciphersuite[0];
550};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100551struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100552static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200553static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100554
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200555#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
556struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
557#endif
558
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200559#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000560static unsigned int openssl_engines_initialized;
561struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
562struct ssl_engine_list {
563 struct list list;
564 ENGINE *e;
565};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200566#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000567
Remi Gacogne8de54152014-07-15 11:36:40 +0200568#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200569static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200570static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200571static DH *local_dh_1024 = NULL;
572static DH *local_dh_2048 = NULL;
573static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100574static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200575#endif /* OPENSSL_NO_DH */
576
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100577#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200578/* X509V3 Extensions that will be added on generated certificates */
579#define X509V3_EXT_SIZE 5
580static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
581 "basicConstraints",
582 "nsComment",
583 "subjectKeyIdentifier",
584 "authorityKeyIdentifier",
585 "keyUsage",
586};
587static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
588 "CA:FALSE",
589 "\"OpenSSL Generated Certificate\"",
590 "hash",
591 "keyid,issuer:always",
592 "nonRepudiation,digitalSignature,keyEncipherment"
593};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200594/* LRU cache to store generated certificate */
595static struct lru64_head *ssl_ctx_lru_tree = NULL;
596static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200597static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100598__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200599
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200600#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
601
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100602static struct ssl_bind_kw ssl_bind_kws[];
603
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200604#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500605/* The order here matters for picking a default context,
606 * keep the most common keytype at the bottom of the list
607 */
608const char *SSL_SOCK_KEYTYPE_NAMES[] = {
609 "dsa",
610 "ecdsa",
611 "rsa"
612};
613#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100614#else
615#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500616#endif
617
William Lallemandc3cd35f2017-11-28 11:04:43 +0100618static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100619static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
620
621#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
622
623#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
624 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
625
626#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
627 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200628
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100629/*
630 * This function gives the detail of the SSL error. It is used only
631 * if the debug mode and the verbose mode are activated. It dump all
632 * the SSL error until the stack was empty.
633 */
634static forceinline void ssl_sock_dump_errors(struct connection *conn)
635{
636 unsigned long ret;
637
638 if (unlikely(global.mode & MODE_DEBUG)) {
639 while(1) {
640 ret = ERR_get_error();
641 if (ret == 0)
642 return;
643 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200644 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100645 ERR_func_error_string(ret), ERR_reason_error_string(ret));
646 }
647 }
648}
649
yanbzhube2774d2015-12-10 15:07:30 -0500650
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200651#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000652static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
653{
654 int err_code = ERR_ABORT;
655 ENGINE *engine;
656 struct ssl_engine_list *el;
657
658 /* grab the structural reference to the engine */
659 engine = ENGINE_by_id(engine_id);
660 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100661 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000662 goto fail_get;
663 }
664
665 if (!ENGINE_init(engine)) {
666 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100667 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000668 goto fail_init;
669 }
670
671 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100672 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000673 goto fail_set_method;
674 }
675
676 el = calloc(1, sizeof(*el));
677 el->e = engine;
678 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100679 nb_engines++;
680 if (global_ssl.async)
681 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000682 return 0;
683
684fail_set_method:
685 /* release the functional reference from ENGINE_init() */
686 ENGINE_finish(engine);
687
688fail_init:
689 /* release the structural reference from ENGINE_by_id() */
690 ENGINE_free(engine);
691
692fail_get:
693 return err_code;
694}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200695#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000696
Willy Tarreau5db847a2019-05-09 14:13:35 +0200697#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200698/*
699 * openssl async fd handler
700 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200701void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000702{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200703 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000704
Emeric Brun3854e012017-05-17 20:42:48 +0200705 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000706 * to poll this fd until it is requested
707 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000708 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000709 fd_cant_recv(fd);
710
711 /* crypto engine is available, let's notify the associated
712 * connection that it can pursue its processing.
713 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200714 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000715}
716
Emeric Brun3854e012017-05-17 20:42:48 +0200717/*
718 * openssl async delayed SSL_free handler
719 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200720void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000721{
722 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200723 OSSL_ASYNC_FD all_fd[32];
724 size_t num_all_fds = 0;
725 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000726
Emeric Brun3854e012017-05-17 20:42:48 +0200727 /* We suppose that the async job for a same SSL *
728 * are serialized. So if we are awake it is
729 * because the running job has just finished
730 * and we can remove all async fds safely
731 */
732 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
733 if (num_all_fds > 32) {
734 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
735 return;
736 }
737
738 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
739 for (i=0 ; i < num_all_fds ; i++)
740 fd_remove(all_fd[i]);
741
742 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000743 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100744 _HA_ATOMIC_SUB(&sslconns, 1);
745 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000746}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000747/*
Emeric Brun3854e012017-05-17 20:42:48 +0200748 * function used to manage a returned SSL_ERROR_WANT_ASYNC
749 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000750 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200751static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000752{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100753 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200754 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200755 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000756 size_t num_add_fds = 0;
757 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200758 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000759
760 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
761 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200762 if (num_add_fds > 32 || num_del_fds > 32) {
763 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 +0000764 return;
765 }
766
Emeric Brun3854e012017-05-17 20:42:48 +0200767 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000768
Emeric Brun3854e012017-05-17 20:42:48 +0200769 /* We remove unused fds from the fdtab */
770 for (i=0 ; i < num_del_fds ; i++)
771 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000772
Emeric Brun3854e012017-05-17 20:42:48 +0200773 /* We add new fds to the fdtab */
774 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200775 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000776 }
777
Emeric Brun3854e012017-05-17 20:42:48 +0200778 num_add_fds = 0;
779 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
780 if (num_add_fds > 32) {
781 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
782 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000783 }
Emeric Brun3854e012017-05-17 20:42:48 +0200784
785 /* We activate the polling for all known async fds */
786 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000787 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200788 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000789 /* To ensure that the fd cache won't be used
790 * We'll prefer to catch a real RD event
791 * because handling an EAGAIN on this fd will
792 * result in a context switch and also
793 * some engines uses a fd in blocking mode.
794 */
795 fd_cant_recv(add_fd[i]);
796 }
Emeric Brun3854e012017-05-17 20:42:48 +0200797
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000798}
799#endif
800
William Lallemand104a7a62019-10-14 14:14:59 +0200801#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200802/*
803 * This function returns the number of seconds elapsed
804 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
805 * date presented un ASN1_GENERALIZEDTIME.
806 *
807 * In parsing error case, it returns -1.
808 */
809static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
810{
811 long epoch;
812 char *p, *end;
813 const unsigned short month_offset[12] = {
814 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
815 };
816 int year, month;
817
818 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
819
820 p = (char *)d->data;
821 end = p + d->length;
822
823 if (end - p < 4) return -1;
824 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
825 p += 4;
826 if (end - p < 2) return -1;
827 month = 10 * (p[0] - '0') + p[1] - '0';
828 if (month < 1 || month > 12) return -1;
829 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
830 We consider leap years and the current month (<marsh or not) */
831 epoch = ( ((year - 1970) * 365)
832 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
833 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
834 + month_offset[month-1]
835 ) * 24 * 60 * 60;
836 p += 2;
837 if (end - p < 2) return -1;
838 /* Add the number of seconds of completed days of current month */
839 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
840 p += 2;
841 if (end - p < 2) return -1;
842 /* Add the completed hours of the current day */
843 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
844 p += 2;
845 if (end - p < 2) return -1;
846 /* Add the completed minutes of the current hour */
847 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
848 p += 2;
849 if (p == end) return -1;
850 /* Test if there is available seconds */
851 if (p[0] < '0' || p[0] > '9')
852 goto nosec;
853 if (end - p < 2) return -1;
854 /* Add the seconds of the current minute */
855 epoch += 10 * (p[0] - '0') + p[1] - '0';
856 p += 2;
857 if (p == end) return -1;
858 /* Ignore seconds float part if present */
859 if (p[0] == '.') {
860 do {
861 if (++p == end) return -1;
862 } while (p[0] >= '0' && p[0] <= '9');
863 }
864
865nosec:
866 if (p[0] == 'Z') {
867 if (end - p != 1) return -1;
868 return epoch;
869 }
870 else if (p[0] == '+') {
871 if (end - p != 5) return -1;
872 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700873 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 +0200874 }
875 else if (p[0] == '-') {
876 if (end - p != 5) return -1;
877 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700878 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 +0200879 }
880
881 return -1;
882}
883
William Lallemand104a7a62019-10-14 14:14:59 +0200884/*
885 * struct alignment works here such that the key.key is the same as key_data
886 * Do not change the placement of key_data
887 */
888struct certificate_ocsp {
889 struct ebmb_node key;
890 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
891 struct buffer response;
892 long expire;
893};
894
895struct ocsp_cbk_arg {
896 int is_single;
897 int single_kt;
898 union {
899 struct certificate_ocsp *s_ocsp;
900 /*
901 * m_ocsp will have multiple entries dependent on key type
902 * Entry 0 - DSA
903 * Entry 1 - ECDSA
904 * Entry 2 - RSA
905 */
906 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
907 };
908};
909
Emeric Brun1d3865b2014-06-20 15:37:32 +0200910static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200911
912/* This function starts to check if the OCSP response (in DER format) contained
913 * in chunk 'ocsp_response' is valid (else exits on error).
914 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
915 * contained in the OCSP Response and exits on error if no match.
916 * If it's a valid OCSP Response:
917 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
918 * pointed by 'ocsp'.
919 * If 'ocsp' is NULL, the function looks up into the OCSP response's
920 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
921 * from the response) and exits on error if not found. Finally, If an OCSP response is
922 * already present in the container, it will be overwritten.
923 *
924 * Note: OCSP response containing more than one OCSP Single response is not
925 * considered valid.
926 *
927 * Returns 0 on success, 1 in error case.
928 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200929static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
930 struct certificate_ocsp *ocsp,
931 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200932{
933 OCSP_RESPONSE *resp;
934 OCSP_BASICRESP *bs = NULL;
935 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200936 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200937 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200938 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200939 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200940 int reason;
941 int ret = 1;
942
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200943 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
944 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200945 if (!resp) {
946 memprintf(err, "Unable to parse OCSP response");
947 goto out;
948 }
949
950 rc = OCSP_response_status(resp);
951 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
952 memprintf(err, "OCSP response status not successful");
953 goto out;
954 }
955
956 bs = OCSP_response_get1_basic(resp);
957 if (!bs) {
958 memprintf(err, "Failed to get basic response from OCSP Response");
959 goto out;
960 }
961
962 count_sr = OCSP_resp_count(bs);
963 if (count_sr > 1) {
964 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
965 goto out;
966 }
967
968 sr = OCSP_resp_get0(bs, 0);
969 if (!sr) {
970 memprintf(err, "Failed to get OCSP single response");
971 goto out;
972 }
973
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200974 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
975
Emeric Brun4147b2e2014-06-16 18:36:30 +0200976 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200977 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200978 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200979 goto out;
980 }
981
Emeric Brun13a6b482014-06-20 15:44:34 +0200982 if (!nextupd) {
983 memprintf(err, "OCSP single response: missing nextupdate");
984 goto out;
985 }
986
Emeric Brunc8b27b62014-06-19 14:16:17 +0200987 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200988 if (!rc) {
989 memprintf(err, "OCSP single response: no longer valid.");
990 goto out;
991 }
992
993 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200994 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200995 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
996 goto out;
997 }
998 }
999
1000 if (!ocsp) {
1001 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
1002 unsigned char *p;
1003
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001004 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001005 if (!rc) {
1006 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
1007 goto out;
1008 }
1009
1010 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
1011 memprintf(err, "OCSP single response: Certificate ID too long");
1012 goto out;
1013 }
1014
1015 p = key;
1016 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001017 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001018 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1019 if (!ocsp) {
1020 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1021 goto out;
1022 }
1023 }
1024
1025 /* According to comments on "chunk_dup", the
1026 previous chunk buffer will be freed */
1027 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1028 memprintf(err, "OCSP response: Memory allocation error");
1029 goto out;
1030 }
1031
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001032 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1033
Emeric Brun4147b2e2014-06-16 18:36:30 +02001034 ret = 0;
1035out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001036 ERR_clear_error();
1037
Emeric Brun4147b2e2014-06-16 18:36:30 +02001038 if (bs)
1039 OCSP_BASICRESP_free(bs);
1040
1041 if (resp)
1042 OCSP_RESPONSE_free(resp);
1043
1044 return ret;
1045}
1046/*
1047 * External function use to update the OCSP response in the OCSP response's
1048 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1049 * to update in DER format.
1050 *
1051 * Returns 0 on success, 1 in error case.
1052 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001053int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001054{
1055 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1056}
1057
William Lallemand4a660132019-10-14 14:51:41 +02001058#endif
1059
1060#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001061/*
1062 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001063 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001064 *
1065 * Returns 0 on success, 1 in error case.
1066 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001067static 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 +02001068{
1069 int fd = -1;
1070 int r = 0;
1071 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001072 struct buffer *ocsp_response;
1073 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001074
William Lallemand3b5f3602019-10-16 18:05:05 +02001075 if (buf) {
1076 int i, j;
1077 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001078
William Lallemand3b5f3602019-10-16 18:05:05 +02001079 /* remove \r and \n from the payload */
1080 for (i = 0, j = 0; buf[i]; i++) {
1081 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001082 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001083 buf[j++] = buf[i];
1084 }
1085 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001086
William Lallemand3b5f3602019-10-16 18:05:05 +02001087 ret = base64dec(buf, j, trash.area, trash.size);
1088 if (ret < 0) {
1089 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001090 goto end;
1091 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001092 trash.data = ret;
1093 src = &trash;
1094 } else {
1095 fd = open(ocsp_path, O_RDONLY);
1096 if (fd == -1) {
1097 memprintf(err, "Error opening OCSP response file");
1098 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001099 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001100
1101 trash.data = 0;
1102 while (trash.data < trash.size) {
1103 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1104 if (r < 0) {
1105 if (errno == EINTR)
1106 continue;
1107
1108 memprintf(err, "Error reading OCSP response from file");
1109 goto end;
1110 }
1111 else if (r == 0) {
1112 break;
1113 }
1114 trash.data += r;
1115 }
1116 close(fd);
1117 fd = -1;
1118 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001119 }
1120
William Lallemand3b5f3602019-10-16 18:05:05 +02001121 ocsp_response = calloc(1, sizeof(*ocsp_response));
1122 if (!chunk_dup(ocsp_response, src)) {
1123 free(ocsp_response);
1124 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001125 goto end;
1126 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001127 /* no error, fill ckch with new context, old context must be free */
1128 if (ckch->ocsp_response) {
1129 free(ckch->ocsp_response->area);
1130 ckch->ocsp_response->area = NULL;
1131 free(ckch->ocsp_response);
1132 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001133 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001134 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001135end:
1136 if (fd != -1)
1137 close(fd);
1138
1139 return ret;
1140}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001141#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001142
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001143#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1144static 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)
1145{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001146 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001147 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001148 struct connection *conn;
1149 int head;
1150 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001151 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001152
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001153 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001154 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001155 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1156
1157 keys = ref->tlskeys;
1158 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001159
1160 if (enc) {
1161 memcpy(key_name, keys[head].name, 16);
1162
1163 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001164 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001165
Emeric Brun9e754772019-01-10 17:51:55 +01001166 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001167
Emeric Brun9e754772019-01-10 17:51:55 +01001168 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1169 goto end;
1170
Willy Tarreau9356dac2019-05-10 09:22:53 +02001171 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001172 ret = 1;
1173 }
1174 else if (ref->key_size_bits == 256 ) {
1175
1176 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1177 goto end;
1178
Willy Tarreau9356dac2019-05-10 09:22:53 +02001179 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001180 ret = 1;
1181 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001182 } else {
1183 for (i = 0; i < TLS_TICKETS_NO; i++) {
1184 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1185 goto found;
1186 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001187 ret = 0;
1188 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001189
Christopher Faulet16f45c82018-02-16 11:23:49 +01001190 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001191 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001192 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 +01001193 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1194 goto end;
1195 /* 2 for key renewal, 1 if current key is still valid */
1196 ret = i ? 2 : 1;
1197 }
1198 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001199 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 +01001200 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1201 goto end;
1202 /* 2 for key renewal, 1 if current key is still valid */
1203 ret = i ? 2 : 1;
1204 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001205 }
Emeric Brun9e754772019-01-10 17:51:55 +01001206
Christopher Faulet16f45c82018-02-16 11:23:49 +01001207 end:
1208 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1209 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001210}
1211
1212struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1213{
1214 struct tls_keys_ref *ref;
1215
1216 list_for_each_entry(ref, &tlskeys_reference, list)
1217 if (ref->filename && strcmp(filename, ref->filename) == 0)
1218 return ref;
1219 return NULL;
1220}
1221
1222struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1223{
1224 struct tls_keys_ref *ref;
1225
1226 list_for_each_entry(ref, &tlskeys_reference, list)
1227 if (ref->unique_id == unique_id)
1228 return ref;
1229 return NULL;
1230}
1231
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001232/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001233 * match existing ones, this function returns -1
1234 * else it returns 0 on success.
1235 */
1236int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001237 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001238{
Emeric Brun9e754772019-01-10 17:51:55 +01001239 if (ref->key_size_bits == 128) {
1240 if (tlskey->data != sizeof(struct tls_sess_key_128))
1241 return -1;
1242 }
1243 else if (ref->key_size_bits == 256) {
1244 if (tlskey->data != sizeof(struct tls_sess_key_256))
1245 return -1;
1246 }
1247 else
1248 return -1;
1249
Christopher Faulet16f45c82018-02-16 11:23:49 +01001250 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001251 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1252 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001253 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1254 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001255
1256 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001257}
1258
Willy Tarreau83061a82018-07-13 11:56:34 +02001259int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001260{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001261 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1262
1263 if(!ref) {
1264 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1265 return 1;
1266 }
Emeric Brun9e754772019-01-10 17:51:55 +01001267 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1268 memprintf(err, "Invalid key size");
1269 return 1;
1270 }
1271
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001272 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001273}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001274
1275/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001276 * automatic ids. It's called just after the basic checks. It returns
1277 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001278 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001279static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001280{
1281 int i = 0;
1282 struct tls_keys_ref *ref, *ref2, *ref3;
1283 struct list tkr = LIST_HEAD_INIT(tkr);
1284
1285 list_for_each_entry(ref, &tlskeys_reference, list) {
1286 if (ref->unique_id == -1) {
1287 /* Look for the first free id. */
1288 while (1) {
1289 list_for_each_entry(ref2, &tlskeys_reference, list) {
1290 if (ref2->unique_id == i) {
1291 i++;
1292 break;
1293 }
1294 }
1295 if (&ref2->list == &tlskeys_reference)
1296 break;
1297 }
1298
1299 /* Uses the unique id and increment it for the next entry. */
1300 ref->unique_id = i;
1301 i++;
1302 }
1303 }
1304
1305 /* This sort the reference list by id. */
1306 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1307 LIST_DEL(&ref->list);
1308 list_for_each_entry(ref3, &tkr, list) {
1309 if (ref->unique_id < ref3->unique_id) {
1310 LIST_ADDQ(&ref3->list, &ref->list);
1311 break;
1312 }
1313 }
1314 if (&ref3->list == &tkr)
1315 LIST_ADDQ(&tkr, &ref->list);
1316 }
1317
1318 /* swap root */
1319 LIST_ADD(&tkr, &tlskeys_reference);
1320 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001321 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001322}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001323#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1324
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001325#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001326int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1327{
1328 switch (evp_keytype) {
1329 case EVP_PKEY_RSA:
1330 return 2;
1331 case EVP_PKEY_DSA:
1332 return 0;
1333 case EVP_PKEY_EC:
1334 return 1;
1335 }
1336
1337 return -1;
1338}
1339
Emeric Brun4147b2e2014-06-16 18:36:30 +02001340/*
1341 * Callback used to set OCSP status extension content in server hello.
1342 */
1343int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1344{
yanbzhube2774d2015-12-10 15:07:30 -05001345 struct certificate_ocsp *ocsp;
1346 struct ocsp_cbk_arg *ocsp_arg;
1347 char *ssl_buf;
1348 EVP_PKEY *ssl_pkey;
1349 int key_type;
1350 int index;
1351
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001352 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001353
1354 ssl_pkey = SSL_get_privatekey(ssl);
1355 if (!ssl_pkey)
1356 return SSL_TLSEXT_ERR_NOACK;
1357
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001358 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001359
1360 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1361 ocsp = ocsp_arg->s_ocsp;
1362 else {
1363 /* For multiple certs per context, we have to find the correct OCSP response based on
1364 * the certificate type
1365 */
1366 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1367
1368 if (index < 0)
1369 return SSL_TLSEXT_ERR_NOACK;
1370
1371 ocsp = ocsp_arg->m_ocsp[index];
1372
1373 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001374
1375 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001376 !ocsp->response.area ||
1377 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001378 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001379 return SSL_TLSEXT_ERR_NOACK;
1380
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001381 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001382 if (!ssl_buf)
1383 return SSL_TLSEXT_ERR_NOACK;
1384
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001385 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1386 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001387
1388 return SSL_TLSEXT_ERR_OK;
1389}
1390
William Lallemand4a660132019-10-14 14:51:41 +02001391#endif
1392
1393#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001394/*
1395 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001396 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1397 * status extension, the issuer's certificate is mandatory. It should be
1398 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001399 *
William Lallemand246c0242019-10-11 08:59:13 +02001400 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1401 * OCSP response. If file is empty or content is not a valid OCSP response,
1402 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1403 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001404 *
1405 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001406 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001407 */
William Lallemand4a660132019-10-14 14:51:41 +02001408#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001409static 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 +02001410{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001411 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001412 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001413 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001414 struct certificate_ocsp *ocsp = NULL, *iocsp;
1415 char *warn = NULL;
1416 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001417 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001418
Emeric Brun4147b2e2014-06-16 18:36:30 +02001419
William Lallemand246c0242019-10-11 08:59:13 +02001420 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001421 if (!x)
1422 goto out;
1423
William Lallemand246c0242019-10-11 08:59:13 +02001424 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001425 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1426 if (chain) {
1427 /* check if one of the certificate of the chain is the issuer */
1428 for (i = 0; i < sk_X509_num(chain); i++) {
1429 X509 *ti = sk_X509_value(chain, i);
1430 if (X509_check_issued(ti, x) == X509_V_OK) {
1431 issuer = ti;
1432 break;
1433 }
1434 }
1435 }
William Lallemand246c0242019-10-11 08:59:13 +02001436 if (!issuer)
1437 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001438
1439 cid = OCSP_cert_to_id(0, x, issuer);
1440 if (!cid)
1441 goto out;
1442
1443 i = i2d_OCSP_CERTID(cid, NULL);
1444 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1445 goto out;
1446
Vincent Bernat02779b62016-04-03 13:48:43 +02001447 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001448 if (!ocsp)
1449 goto out;
1450
1451 p = ocsp->key_data;
1452 i2d_OCSP_CERTID(cid, &p);
1453
1454 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1455 if (iocsp == ocsp)
1456 ocsp = NULL;
1457
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001458#ifndef SSL_CTX_get_tlsext_status_cb
1459# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1460 *cb = (void (*) (void))ctx->tlsext_status_cb;
1461#endif
1462 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1463
1464 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001465 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001466 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001467
1468 cb_arg->is_single = 1;
1469 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001470
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001471 pkey = X509_get_pubkey(x);
1472 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1473 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001474
1475 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1476 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1477 } else {
1478 /*
1479 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1480 * Update that cb_arg with the new cert's staple
1481 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001482 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001483 struct certificate_ocsp *tmp_ocsp;
1484 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001485 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001486 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001487
1488#ifdef SSL_CTX_get_tlsext_status_arg
1489 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1490#else
1491 cb_arg = ctx->tlsext_status_arg;
1492#endif
yanbzhube2774d2015-12-10 15:07:30 -05001493
1494 /*
1495 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1496 * the order of operations below matter, take care when changing it
1497 */
1498 tmp_ocsp = cb_arg->s_ocsp;
1499 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1500 cb_arg->s_ocsp = NULL;
1501 cb_arg->m_ocsp[index] = tmp_ocsp;
1502 cb_arg->is_single = 0;
1503 cb_arg->single_kt = 0;
1504
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001505 pkey = X509_get_pubkey(x);
1506 key_type = EVP_PKEY_base_id(pkey);
1507 EVP_PKEY_free(pkey);
1508
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001509 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001510 if (index >= 0 && !cb_arg->m_ocsp[index])
1511 cb_arg->m_ocsp[index] = iocsp;
1512
1513 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001514
1515 ret = 0;
1516
1517 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001518 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001519 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001520 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001521 }
1522
1523out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001524 if (cid)
1525 OCSP_CERTID_free(cid);
1526
1527 if (ocsp)
1528 free(ocsp);
1529
1530 if (warn)
1531 free(warn);
1532
Emeric Brun4147b2e2014-06-16 18:36:30 +02001533 return ret;
1534}
William Lallemand4a660132019-10-14 14:51:41 +02001535#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001536static 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 +02001537{
William Lallemand4a660132019-10-14 14:51:41 +02001538 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 +02001539}
1540#endif
1541
William Lallemand4a660132019-10-14 14:51:41 +02001542#endif
1543
1544
Willy Tarreau5db847a2019-05-09 14:13:35 +02001545#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001546
1547#define CT_EXTENSION_TYPE 18
1548
1549static int sctl_ex_index = -1;
1550
1551/*
1552 * Try to parse Signed Certificate Timestamp List structure. This function
1553 * makes only basic test if the data seems like SCTL. No signature validation
1554 * is performed.
1555 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001556static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001557{
1558 int ret = 1;
1559 int len, pos, sct_len;
1560 unsigned char *data;
1561
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001562 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001563 goto out;
1564
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001565 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001566 len = (data[0] << 8) | data[1];
1567
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001568 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001569 goto out;
1570
1571 data = data + 2;
1572 pos = 0;
1573 while (pos < len) {
1574 if (len - pos < 2)
1575 goto out;
1576
1577 sct_len = (data[pos] << 8) | data[pos + 1];
1578 if (pos + sct_len + 2 > len)
1579 goto out;
1580
1581 pos += sct_len + 2;
1582 }
1583
1584 ret = 0;
1585
1586out:
1587 return ret;
1588}
1589
William Lallemand0dfae6c2019-10-16 18:06:58 +02001590/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1591 * It fills the ckch->sctl buffer
1592 * return 0 on success or != 0 on failure */
1593static 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 +01001594{
1595 int fd = -1;
1596 int r = 0;
1597 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001598 struct buffer tmp;
1599 struct buffer *src;
1600 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001601
William Lallemand0dfae6c2019-10-16 18:06:58 +02001602 if (buf) {
1603 tmp.area = buf;
1604 tmp.data = strlen(buf);
1605 tmp.size = tmp.data + 1;
1606 src = &tmp;
1607 } else {
1608 fd = open(sctl_path, O_RDONLY);
1609 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001610 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001611
1612 trash.data = 0;
1613 while (trash.data < trash.size) {
1614 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1615 if (r < 0) {
1616 if (errno == EINTR)
1617 continue;
1618 goto end;
1619 }
1620 else if (r == 0) {
1621 break;
1622 }
1623 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001624 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001625 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001626 }
1627
William Lallemand0dfae6c2019-10-16 18:06:58 +02001628 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001629 if (ret)
1630 goto end;
1631
William Lallemand0dfae6c2019-10-16 18:06:58 +02001632 sctl = calloc(1, sizeof(*sctl));
1633 if (!chunk_dup(sctl, src)) {
1634 free(sctl);
1635 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001636 goto end;
1637 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001638 /* no error, fill ckch with new context, old context must be free */
1639 if (ckch->sctl) {
1640 free(ckch->sctl->area);
1641 ckch->sctl->area = NULL;
1642 free(ckch->sctl);
1643 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001644 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001645 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001646end:
1647 if (fd != -1)
1648 close(fd);
1649
1650 return ret;
1651}
1652
1653int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1654{
Willy Tarreau83061a82018-07-13 11:56:34 +02001655 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001656
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001657 *out = (unsigned char *) sctl->area;
1658 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001659
1660 return 1;
1661}
1662
1663int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1664{
1665 return 1;
1666}
1667
William Lallemanda17f4112019-10-10 15:16:44 +02001668static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001669{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001670 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001671
William Lallemanda17f4112019-10-10 15:16:44 +02001672 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 +01001673 goto out;
1674
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001675 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1676
1677 ret = 0;
1678
1679out:
1680 return ret;
1681}
1682
1683#endif
1684
Emeric Brune1f38db2012-09-03 20:36:47 +02001685void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1686{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001687 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001688 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001689 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001690 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001691
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001692#ifndef SSL_OP_NO_RENEGOTIATION
1693 /* Please note that BoringSSL defines this macro to zero so don't
1694 * change this to #if and do not assign a default value to this macro!
1695 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001696 if (where & SSL_CB_HANDSHAKE_START) {
1697 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001698 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 +02001699 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001700 conn->err_code = CO_ER_SSL_RENEG;
1701 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001702 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001703#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001704
1705 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001706 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001707 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001708 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001709 consider that the buffering was activated,
1710 so we rise the output buffer size from 4k
1711 to 16k */
1712 write_bio = SSL_get_wbio(ssl);
1713 if (write_bio != SSL_get_rbio(ssl)) {
1714 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001715 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001716 }
1717 }
1718 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001719}
1720
Emeric Brune64aef12012-09-21 13:15:06 +02001721/* Callback is called for each certificate of the chain during a verify
1722 ok is set to 1 if preverify detect no error on current certificate.
1723 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001724int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001725{
1726 SSL *ssl;
1727 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001728 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001729 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001730
1731 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001732 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001733
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001734 ctx = conn->xprt_ctx;
1735
1736 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001737
Emeric Brun81c00f02012-09-21 14:31:21 +02001738 if (ok) /* no errors */
1739 return ok;
1740
1741 depth = X509_STORE_CTX_get_error_depth(x_store);
1742 err = X509_STORE_CTX_get_error(x_store);
1743
1744 /* check if CA error needs to be ignored */
1745 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001746 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1747 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1748 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001749 }
1750
Willy Tarreau731248f2020-02-04 14:02:02 +01001751 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001752 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001753 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001754 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001755 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001756
Willy Tarreau20879a02012-12-03 16:32:10 +01001757 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001758 return 0;
1759 }
1760
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001761 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1762 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001763
Emeric Brun81c00f02012-09-21 14:31:21 +02001764 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001765 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001766 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001767 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001768 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001769 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001770
Willy Tarreau20879a02012-12-03 16:32:10 +01001771 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001772 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001773}
1774
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001775static inline
1776void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001777 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001778{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001779 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001780 unsigned char *msg;
1781 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001782 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001783
1784 /* This function is called for "from client" and "to server"
1785 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001786 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001787 */
1788
1789 /* "write_p" is set to 0 is the bytes are received messages,
1790 * otherwise it is set to 1.
1791 */
1792 if (write_p != 0)
1793 return;
1794
1795 /* content_type contains the type of message received or sent
1796 * according with the SSL/TLS protocol spec. This message is
1797 * encoded with one byte. The value 256 (two bytes) is used
1798 * for designing the SSL/TLS record layer. According with the
1799 * rfc6101, the expected message (other than 256) are:
1800 * - change_cipher_spec(20)
1801 * - alert(21)
1802 * - handshake(22)
1803 * - application_data(23)
1804 * - (255)
1805 * We are interessed by the handshake and specially the client
1806 * hello.
1807 */
1808 if (content_type != 22)
1809 return;
1810
1811 /* The message length is at least 4 bytes, containing the
1812 * message type and the message length.
1813 */
1814 if (len < 4)
1815 return;
1816
1817 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001818 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001819 * - hello_request(0)
1820 * - client_hello(1)
1821 * - server_hello(2)
1822 * - certificate(11)
1823 * - server_key_exchange (12)
1824 * - certificate_request(13)
1825 * - server_hello_done(14)
1826 * We are interested by the client hello.
1827 */
1828 msg = (unsigned char *)buf;
1829 if (msg[0] != 1)
1830 return;
1831
1832 /* Next three bytes are the length of the message. The total length
1833 * must be this decoded length + 4. If the length given as argument
1834 * is not the same, we abort the protocol dissector.
1835 */
1836 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1837 if (len < rec_len + 4)
1838 return;
1839 msg += 4;
1840 end = msg + rec_len;
1841 if (end < msg)
1842 return;
1843
1844 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1845 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001846 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1847 */
1848 msg += 1 + 1 + 4 + 28;
1849 if (msg > end)
1850 return;
1851
1852 /* Next, is session id:
1853 * if present, we have to jump by length + 1 for the size information
1854 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001855 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001856 if (msg[0] > 0)
1857 msg += msg[0];
1858 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001859 if (msg > end)
1860 return;
1861
1862 /* Next two bytes are the ciphersuite length. */
1863 if (msg + 2 > end)
1864 return;
1865 rec_len = (msg[0] << 8) + msg[1];
1866 msg += 2;
1867 if (msg + rec_len > end || msg + rec_len < msg)
1868 return;
1869
Willy Tarreaubafbe012017-11-24 17:34:44 +01001870 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001871 if (!capture)
1872 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001873 /* Compute the xxh64 of the ciphersuite. */
1874 capture->xxh64 = XXH64(msg, rec_len, 0);
1875
1876 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001877 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1878 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001879 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001880
1881 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001882}
1883
Emeric Brun29f037d2014-04-25 19:05:36 +02001884/* Callback is called for ssl protocol analyse */
1885void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1886{
Emeric Brun29f037d2014-04-25 19:05:36 +02001887#ifdef TLS1_RT_HEARTBEAT
1888 /* test heartbeat received (write_p is set to 0
1889 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001890 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001891 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001892 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001893 const unsigned char *p = buf;
1894 unsigned int payload;
1895
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001896 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001897
1898 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1899 if (*p != TLS1_HB_REQUEST)
1900 return;
1901
Willy Tarreauaeed6722014-04-25 23:59:58 +02001902 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001903 goto kill_it;
1904
1905 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001906 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001907 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001908 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001909 /* We have a clear heartbleed attack (CVE-2014-0160), the
1910 * advertised payload is larger than the advertised packet
1911 * length, so we have garbage in the buffer between the
1912 * payload and the end of the buffer (p+len). We can't know
1913 * if the SSL stack is patched, and we don't know if we can
1914 * safely wipe out the area between p+3+len and payload.
1915 * So instead, we prevent the response from being sent by
1916 * setting the max_send_fragment to 0 and we report an SSL
1917 * error, which will kill this connection. It will be reported
1918 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001919 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1920 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001921 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001922 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1923 return;
1924 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001925#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001926 if (global_ssl.capture_cipherlist > 0)
1927 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001928}
1929
Bernard Spil13c53f82018-02-15 13:34:58 +01001930#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001931static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1932 const unsigned char *in, unsigned int inlen,
1933 void *arg)
1934{
1935 struct server *srv = arg;
1936
1937 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1938 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1939 return SSL_TLSEXT_ERR_OK;
1940 return SSL_TLSEXT_ERR_NOACK;
1941}
1942#endif
1943
1944#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001945/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001946 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001947 */
1948static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1949 unsigned int *len, void *arg)
1950{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001951 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001952
1953 *data = (const unsigned char *)conf->npn_str;
1954 *len = conf->npn_len;
1955 return SSL_TLSEXT_ERR_OK;
1956}
1957#endif
1958
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001959#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001960/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001961 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001962 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001963static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1964 unsigned char *outlen,
1965 const unsigned char *server,
1966 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001967{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001968 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001969
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001970 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1971 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1972 return SSL_TLSEXT_ERR_NOACK;
1973 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001974 return SSL_TLSEXT_ERR_OK;
1975}
1976#endif
1977
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001978#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001979#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001980
Christopher Faulet30548802015-06-11 13:39:32 +02001981/* Create a X509 certificate with the specified servername and serial. This
1982 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001983static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001984ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001985{
Christopher Faulet7969a332015-10-09 11:15:03 +02001986 X509 *cacert = bind_conf->ca_sign_cert;
1987 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001988 SSL_CTX *ssl_ctx = NULL;
1989 X509 *newcrt = NULL;
1990 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001991 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001992 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001993 X509_NAME *name;
1994 const EVP_MD *digest;
1995 X509V3_CTX ctx;
1996 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001997 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001998
Christopher Faulet48a83322017-07-28 16:56:09 +02001999 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02002000#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002001 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
2002#else
2003 tmp_ssl = SSL_new(bind_conf->default_ctx);
2004 if (tmp_ssl)
2005 pkey = SSL_get_privatekey(tmp_ssl);
2006#endif
2007 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002008 goto mkcert_error;
2009
2010 /* Create the certificate */
2011 if (!(newcrt = X509_new()))
2012 goto mkcert_error;
2013
2014 /* Set version number for the certificate (X509v3) and the serial
2015 * number */
2016 if (X509_set_version(newcrt, 2L) != 1)
2017 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002018 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002019
2020 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002021 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2022 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002023 goto mkcert_error;
2024
2025 /* set public key in the certificate */
2026 if (X509_set_pubkey(newcrt, pkey) != 1)
2027 goto mkcert_error;
2028
2029 /* Set issuer name from the CA */
2030 if (!(name = X509_get_subject_name(cacert)))
2031 goto mkcert_error;
2032 if (X509_set_issuer_name(newcrt, name) != 1)
2033 goto mkcert_error;
2034
2035 /* Set the subject name using the same, but the CN */
2036 name = X509_NAME_dup(name);
2037 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2038 (const unsigned char *)servername,
2039 -1, -1, 0) != 1) {
2040 X509_NAME_free(name);
2041 goto mkcert_error;
2042 }
2043 if (X509_set_subject_name(newcrt, name) != 1) {
2044 X509_NAME_free(name);
2045 goto mkcert_error;
2046 }
2047 X509_NAME_free(name);
2048
2049 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002050 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002051 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2052 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2053 X509_EXTENSION *ext;
2054
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002055 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002056 goto mkcert_error;
2057 if (!X509_add_ext(newcrt, ext, -1)) {
2058 X509_EXTENSION_free(ext);
2059 goto mkcert_error;
2060 }
2061 X509_EXTENSION_free(ext);
2062 }
2063
2064 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002065
2066 key_type = EVP_PKEY_base_id(capkey);
2067
2068 if (key_type == EVP_PKEY_DSA)
2069 digest = EVP_sha1();
2070 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002071 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002072 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002073 digest = EVP_sha256();
2074 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002075#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002076 int nid;
2077
2078 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2079 goto mkcert_error;
2080 if (!(digest = EVP_get_digestbynid(nid)))
2081 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002082#else
2083 goto mkcert_error;
2084#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002085 }
2086
Christopher Faulet31af49d2015-06-09 17:29:50 +02002087 if (!(X509_sign(newcrt, capkey, digest)))
2088 goto mkcert_error;
2089
2090 /* Create and set the new SSL_CTX */
2091 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2092 goto mkcert_error;
2093 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2094 goto mkcert_error;
2095 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2096 goto mkcert_error;
2097 if (!SSL_CTX_check_private_key(ssl_ctx))
2098 goto mkcert_error;
2099
2100 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002101
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002102#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002103 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002104#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002105#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2106 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002107 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002108 EC_KEY *ecc;
2109 int nid;
2110
2111 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2112 goto end;
2113 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2114 goto end;
2115 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2116 EC_KEY_free(ecc);
2117 }
2118#endif
2119 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002120 return ssl_ctx;
2121
2122 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002123 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002124 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002125 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2126 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002127 return NULL;
2128}
2129
Christopher Faulet7969a332015-10-09 11:15:03 +02002130SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002131ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002132{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002133 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002134 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002135
Olivier Houchard66ab4982019-02-26 18:37:15 +01002136 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002137}
2138
Christopher Faulet30548802015-06-11 13:39:32 +02002139/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002140 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002141SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002142ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002143{
2144 struct lru64 *lru = NULL;
2145
2146 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002147 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002148 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002149 if (lru && lru->domain) {
2150 if (ssl)
2151 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002152 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002153 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002154 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002155 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002156 }
2157 return NULL;
2158}
2159
Emeric Brun821bb9b2017-06-15 16:37:39 +02002160/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2161 * function is not thread-safe, it should only be used to check if a certificate
2162 * exists in the lru cache (with no warranty it will not be removed by another
2163 * thread). It is kept for backward compatibility. */
2164SSL_CTX *
2165ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2166{
2167 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2168}
2169
Christopher Fauletd2cab922015-07-28 16:03:47 +02002170/* Set a certificate int the LRU cache used to store generated
2171 * certificate. Return 0 on success, otherwise -1 */
2172int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002173ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002174{
2175 struct lru64 *lru = NULL;
2176
2177 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002178 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002179 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002180 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002181 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002182 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002183 }
Christopher Faulet30548802015-06-11 13:39:32 +02002184 if (lru->domain && lru->data)
2185 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002186 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002187 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002188 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002189 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002190 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002191}
2192
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002193/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002194unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002195ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002196{
2197 return XXH32(data, len, ssl_ctx_lru_seed);
2198}
2199
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002200/* Generate a cert and immediately assign it to the SSL session so that the cert's
2201 * refcount is maintained regardless of the cert's presence in the LRU cache.
2202 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002203static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002204ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002205{
2206 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002207 SSL_CTX *ssl_ctx = NULL;
2208 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002209 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002210
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002211 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002212 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002213 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002214 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002215 if (lru && lru->domain)
2216 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002217 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002218 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002219 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002220 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002221 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002222 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002223 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002224 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002225 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002226 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002227 SSL_set_SSL_CTX(ssl, ssl_ctx);
2228 /* No LRU cache, this CTX will be released as soon as the session dies */
2229 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002230 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002231 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002232 return 0;
2233}
2234static int
2235ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2236{
2237 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002238 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002239
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002240 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002241 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002242 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002243 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002244 }
2245 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002246}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002247#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002248
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002249#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002250typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2251
2252static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002253{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002254#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002255 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002256 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2257#endif
2258}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002259static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2260 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002261 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2262}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002263static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002264#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002265 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002266 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2267#endif
2268}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002269static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002270#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002271 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002272 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2273#endif
2274}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002275/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002276static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2277/* Unusable in this context. */
2278static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2279static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2280static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2281static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2282static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002283#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002284typedef enum { SET_MIN, SET_MAX } set_context_func;
2285
2286static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2287 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002288 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2289}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002290static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2291 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2292 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2293}
2294static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2295 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002296 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2297}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002298static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2299 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2300 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2301}
2302static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2303 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002304 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2305}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002306static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2307 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2308 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2309}
2310static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2311 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002312 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2313}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002314static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2315 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2316 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2317}
2318static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002319#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002320 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002321 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2322#endif
2323}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002324static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2325#if SSL_OP_NO_TLSv1_3
2326 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2327 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002328#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002329}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002330#endif
2331static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2332static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002333
2334static struct {
2335 int option;
2336 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002337 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2338 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002339 const char *name;
2340} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002341 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2342 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2343 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2344 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2345 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2346 {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 +02002347};
2348
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002349static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2350{
2351 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2352 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2353 SSL_set_SSL_CTX(ssl, ctx);
2354}
2355
Willy Tarreau5db847a2019-05-09 14:13:35 +02002356#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002357
2358static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2359{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002360 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002361 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002362
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002363 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2364 return SSL_TLSEXT_ERR_OK;
2365 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002366}
2367
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002368#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2370{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002371 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002372#else
2373static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2374{
2375#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002376 struct connection *conn;
2377 struct bind_conf *s;
2378 const uint8_t *extension_data;
2379 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002380 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002381
2382 char *wildp = NULL;
2383 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002384 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002385 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002386 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002387 int i;
2388
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002389 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002390 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002391
Olivier Houchard9679ac92017-10-27 14:58:08 +02002392 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002393 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002394#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002395 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2396 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002397#else
2398 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2399#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002400 /*
2401 * The server_name extension was given too much extensibility when it
2402 * was written, so parsing the normal case is a bit complex.
2403 */
2404 size_t len;
2405 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002406 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002407 /* Extract the length of the supplied list of names. */
2408 len = (*extension_data++) << 8;
2409 len |= *extension_data++;
2410 if (len + 2 != extension_len)
2411 goto abort;
2412 /*
2413 * The list in practice only has a single element, so we only consider
2414 * the first one.
2415 */
2416 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2417 goto abort;
2418 extension_len = len - 1;
2419 /* Now we can finally pull out the byte array with the actual hostname. */
2420 if (extension_len <= 2)
2421 goto abort;
2422 len = (*extension_data++) << 8;
2423 len |= *extension_data++;
2424 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2425 || memchr(extension_data, 0, len) != NULL)
2426 goto abort;
2427 servername = extension_data;
2428 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002430#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2431 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002432 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002433 }
2434#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002435 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002436 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002437 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002438 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002439 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002440 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002441 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002442 goto abort;
2443 }
2444
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002445 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002446#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002447 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002448#else
2449 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2450#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002451 uint8_t sign;
2452 size_t len;
2453 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002454 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002455 len = (*extension_data++) << 8;
2456 len |= *extension_data++;
2457 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002458 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002459 if (len % 2 != 0)
2460 goto abort;
2461 for (; len > 0; len -= 2) {
2462 extension_data++; /* hash */
2463 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002464 switch (sign) {
2465 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002466 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002467 break;
2468 case TLSEXT_signature_ecdsa:
2469 has_ecdsa_sig = 1;
2470 break;
2471 default:
2472 continue;
2473 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002474 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002475 break;
2476 }
2477 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002478 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002479 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002480 }
2481 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002482 const SSL_CIPHER *cipher;
2483 size_t len;
2484 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002485 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002486#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002487 len = ctx->cipher_suites_len;
2488 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002489#else
2490 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2491#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002492 if (len % 2 != 0)
2493 goto abort;
2494 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002495#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002496 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002497 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002498#else
2499 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2500#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002501 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002502 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002503 break;
2504 }
2505 }
2506 }
2507
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002508 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002509 trash.area[i] = tolower(servername[i]);
2510 if (!wildp && (trash.area[i] == '.'))
2511 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002512 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002513 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002514
William Lallemand150bfa82019-09-19 17:12:49 +02002515 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002516
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002517 for (i = 0; i < 2; i++) {
2518 if (i == 0) /* lookup in full qualified names */
2519 node = ebst_lookup(&s->sni_ctx, trash.area);
2520 else if (i == 1 && wildp) /* lookup in wildcards names */
2521 node = ebst_lookup(&s->sni_w_ctx, wildp);
2522 else
2523 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002524 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002525 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002526 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002527 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002528 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002529 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002530 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002531 break;
2532 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002533 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002534 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002535 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002536 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002537 if (!node_anonymous)
2538 node_anonymous = n;
2539 break;
2540 }
2541 }
2542 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002543 /* select by key_signature priority order */
2544 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2545 : ((has_rsa_sig && node_rsa) ? node_rsa
2546 : (node_anonymous ? node_anonymous
2547 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2548 : node_rsa /* no rsa signature case (far far away) */
2549 )));
2550 if (node) {
2551 /* switch ctx */
2552 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2553 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002554 if (conf) {
2555 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2556 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2557 if (conf->early_data)
2558 allow_early = 1;
2559 }
William Lallemand02010472019-10-18 11:02:19 +02002560 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002561 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002562 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002563 }
William Lallemand150bfa82019-09-19 17:12:49 +02002564
William Lallemand02010472019-10-18 11:02:19 +02002565 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002566#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002567 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002568 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002569 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002570 }
2571#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002572 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002573 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002574 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002575 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002576 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002577 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002578allow_early:
2579#ifdef OPENSSL_IS_BORINGSSL
2580 if (allow_early)
2581 SSL_set_early_data_enabled(ssl, 1);
2582#else
2583 if (!allow_early)
2584 SSL_set_max_early_data(ssl, 0);
2585#endif
2586 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002587 abort:
2588 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2589 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002590#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002591 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002592#else
2593 *al = SSL_AD_UNRECOGNIZED_NAME;
2594 return 0;
2595#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002596}
2597
2598#else /* OPENSSL_IS_BORINGSSL */
2599
Emeric Brunfc0421f2012-09-07 17:30:07 +02002600/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2601 * warning when no match is found, which implies the default (first) cert
2602 * will keep being used.
2603 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002604static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002605{
2606 const char *servername;
2607 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002608 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002609 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002610 int i;
2611 (void)al; /* shut gcc stupid warning */
2612
2613 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002614 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002615#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002616 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2617 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002618#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002619 if (s->strict_sni)
2620 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002621 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002622 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002623 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002624 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002625 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002626
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002627 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002628 if (!servername[i])
2629 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002630 trash.area[i] = tolower(servername[i]);
2631 if (!wildp && (trash.area[i] == '.'))
2632 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002633 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002634 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002635
William Lallemand150bfa82019-09-19 17:12:49 +02002636 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002637 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002638 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002639 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2640 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002641 if (!container_of(n, struct sni_ctx, name)->neg) {
2642 node = n;
2643 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002644 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002645 }
2646 if (!node && wildp) {
2647 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002648 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2649 /* lookup a not neg filter */
2650 if (!container_of(n, struct sni_ctx, name)->neg) {
2651 node = n;
2652 break;
2653 }
2654 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002655 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002656 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002657#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002658 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2659 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002660 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002661 return SSL_TLSEXT_ERR_OK;
2662 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002663#endif
William Lallemand21724f02019-11-04 17:56:13 +01002664 if (s->strict_sni) {
2665 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002666 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002667 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002668 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002669 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002670 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002671 }
2672
2673 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002674 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002675 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002676 return SSL_TLSEXT_ERR_OK;
2677}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002678#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002679#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2680
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002681#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002682
2683static DH * ssl_get_dh_1024(void)
2684{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002685 static unsigned char dh1024_p[]={
2686 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2687 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2688 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2689 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2690 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2691 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2692 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2693 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2694 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2695 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2696 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2697 };
2698 static unsigned char dh1024_g[]={
2699 0x02,
2700 };
2701
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002702 BIGNUM *p;
2703 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002704 DH *dh = DH_new();
2705 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002706 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2707 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002708
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002709 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002710 DH_free(dh);
2711 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002712 } else {
2713 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002714 }
2715 }
2716 return dh;
2717}
2718
2719static DH *ssl_get_dh_2048(void)
2720{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002721 static unsigned char dh2048_p[]={
2722 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2723 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2724 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2725 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2726 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2727 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2728 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2729 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2730 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2731 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2732 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2733 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2734 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2735 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2736 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2737 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2738 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2739 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2740 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2741 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2742 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2743 0xB7,0x1F,0x77,0xF3,
2744 };
2745 static unsigned char dh2048_g[]={
2746 0x02,
2747 };
2748
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002749 BIGNUM *p;
2750 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002751 DH *dh = DH_new();
2752 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002753 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2754 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002755
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002756 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002757 DH_free(dh);
2758 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002759 } else {
2760 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002761 }
2762 }
2763 return dh;
2764}
2765
2766static DH *ssl_get_dh_4096(void)
2767{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002768 static unsigned char dh4096_p[]={
2769 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2770 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2771 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2772 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2773 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2774 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2775 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2776 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2777 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2778 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2779 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2780 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2781 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2782 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2783 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2784 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2785 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2786 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2787 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2788 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2789 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2790 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2791 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2792 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2793 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2794 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2795 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2796 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2797 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2798 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2799 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2800 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2801 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2802 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2803 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2804 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2805 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2806 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2807 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2808 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2809 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2810 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2811 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002812 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002813 static unsigned char dh4096_g[]={
2814 0x02,
2815 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002816
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002817 BIGNUM *p;
2818 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002819 DH *dh = DH_new();
2820 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002821 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2822 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002823
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002824 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002825 DH_free(dh);
2826 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002827 } else {
2828 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002829 }
2830 }
2831 return dh;
2832}
2833
2834/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002835 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002836static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2837{
2838 DH *dh = NULL;
2839 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002840 int type;
2841
2842 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002843
2844 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2845 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2846 */
2847 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2848 keylen = EVP_PKEY_bits(pkey);
2849 }
2850
Willy Tarreauef934602016-12-22 23:12:01 +01002851 if (keylen > global_ssl.default_dh_param) {
2852 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002853 }
2854
Remi Gacogned3a341a2015-05-29 16:26:17 +02002855 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002856 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002857 }
2858 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002859 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002860 }
2861 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002862 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002863 }
2864
2865 return dh;
2866}
2867
Remi Gacogne47783ef2015-05-29 15:53:22 +02002868static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002869{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002870 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002871 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002872
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002873 if (in == NULL)
2874 goto end;
2875
Remi Gacogne47783ef2015-05-29 15:53:22 +02002876 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002877 goto end;
2878
Remi Gacogne47783ef2015-05-29 15:53:22 +02002879 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2880
2881end:
2882 if (in)
2883 BIO_free(in);
2884
Emeric Brune1b4ed42018-08-16 15:14:12 +02002885 ERR_clear_error();
2886
Remi Gacogne47783ef2015-05-29 15:53:22 +02002887 return dh;
2888}
2889
2890int ssl_sock_load_global_dh_param_from_file(const char *filename)
2891{
2892 global_dh = ssl_sock_get_dh_from_file(filename);
2893
2894 if (global_dh) {
2895 return 0;
2896 }
2897
2898 return -1;
2899}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002900#endif
2901
William Lallemand82b21bb2020-04-10 10:26:27 +02002902/* release ssl bind conf */
2903void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
2904{
2905 if (conf) {
2906#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2907 free(conf->npn_str);
2908 conf->npn_str = NULL;
2909#endif
2910#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2911 free(conf->alpn_str);
2912 conf->alpn_str = NULL;
2913#endif
2914 free(conf->ca_file);
2915 conf->ca_file = NULL;
2916 free(conf->ca_verify_file);
2917 conf->ca_verify_file = NULL;
2918 free(conf->crl_file);
2919 conf->crl_file = NULL;
2920 free(conf->ciphers);
2921 conf->ciphers = NULL;
2922#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
2923 free(conf->ciphersuites);
2924 conf->ciphersuites = NULL;
2925#endif
2926 free(conf->curves);
2927 conf->curves = NULL;
2928 free(conf->ecdhe);
2929 conf->ecdhe = NULL;
2930 }
2931}
2932
William Lallemandd9d5d1b2020-04-09 16:31:05 +02002933/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
2934/* The caller must use the lock of the bind_conf if used with inserted SNIs */
2935static void ckch_inst_free(struct ckch_inst *inst)
2936{
2937 struct sni_ctx *sni, *sni_s;
2938
2939 if (inst == NULL)
2940 return;
2941
2942 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
2943 SSL_CTX_free(sni->ctx);
2944 LIST_DEL(&sni->by_ckch_inst);
2945 ebmb_delete(&sni->name);
2946 free(sni);
2947 }
2948 LIST_DEL(&inst->by_ckchs);
2949 LIST_DEL(&inst->by_crtlist_entry);
2950 free(inst);
2951}
2952
William Lallemand9117de92019-10-04 00:29:42 +02002953/* Alloc and init a ckch_inst */
2954static struct ckch_inst *ckch_inst_new()
2955{
2956 struct ckch_inst *ckch_inst;
2957
2958 ckch_inst = calloc(1, sizeof *ckch_inst);
William Lallemand9cef2e22020-04-09 16:25:10 +02002959 if (!ckch_inst)
2960 return NULL;
2961
2962 LIST_INIT(&ckch_inst->sni_ctx);
2963 LIST_INIT(&ckch_inst->by_ckchs);
2964 LIST_INIT(&ckch_inst->by_crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02002965
2966 return ckch_inst;
2967}
2968
William Lallemand82b21bb2020-04-10 10:26:27 +02002969/* free sni filters */
2970static void crtlist_free_filters(char **args)
2971{
2972 int i;
2973
2974 if (!args)
2975 return;
2976
2977 for (i = 0; args[i]; i++)
2978 free(args[i]);
2979
2980 free(args);
2981}
2982
2983/* Alloc and duplicate a char ** array */
2984static char **crtlist_dup_filters(char **args, int fcount)
2985{
2986 char **dst;
2987 int i;
2988
2989 if (fcount == 0)
2990 return NULL;
2991
2992 dst = calloc(fcount + 1, sizeof(*dst));
2993 if (!dst)
2994 return NULL;
2995
2996 for (i = 0; i < fcount; i++) {
2997 dst[i] = strdup(args[i]);
2998 if (!dst[i])
2999 goto error;
3000 }
3001 return dst;
3002
3003error:
3004 crtlist_free_filters(dst);
3005 return NULL;
3006}
3007
William Lallemande718dfb2020-04-10 11:09:25 +02003008/*
3009 * Detach and free a crtlist_entry.
3010 * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst
3011 */
3012static void crtlist_entry_free(struct crtlist_entry *entry)
3013{
3014 struct ckch_inst *inst, *inst_s;
3015
3016 if (entry == NULL)
3017 return;
3018
3019 ebpt_delete(&entry->node);
3020 LIST_DEL(&entry->by_crtlist);
3021 LIST_DEL(&entry->by_ckch_store);
3022 crtlist_free_filters(entry->filters);
3023 ssl_sock_free_ssl_conf(entry->ssl_conf);
3024 free(entry->ssl_conf);
3025 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
3026 ckch_inst_free(inst);
3027 }
3028 free(entry);
3029}
3030
3031/*
3032 * Allocate and initialize a crtlist_entry
3033 */
3034static struct crtlist_entry *crtlist_entry_new()
3035{
3036 struct crtlist_entry *entry;
3037
3038 entry = calloc(1, sizeof(*entry));
3039 if (entry == NULL)
3040 return NULL;
3041
3042 LIST_INIT(&entry->ckch_inst);
3043
3044 /* initialize the nodes so we can LIST_DEL in any cases */
3045 LIST_INIT(&entry->by_crtlist);
3046 LIST_INIT(&entry->by_ckch_store);
3047
3048 return entry;
3049}
3050
William Lallemand82b21bb2020-04-10 10:26:27 +02003051/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
3052static void crtlist_free(struct crtlist *crtlist)
3053{
3054 struct crtlist_entry *entry, *s_entry;
3055
3056 if (crtlist == NULL)
3057 return;
3058
3059 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
3060 crtlist_entry_free(entry);
3061 }
3062 ebmb_delete(&crtlist->node);
3063 free(crtlist);
3064}
3065
3066/* Alloc and initialize a struct crtlist
3067 * <filename> is the key of the ebmb_node
3068 * <unique> initialize the list of entries to be unique (1) or not (0)
3069 */
3070static struct crtlist *crtlist_new(const char *filename, int unique)
3071{
3072 struct crtlist *newlist;
3073
3074 newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1);
3075 if (newlist == NULL)
3076 return NULL;
3077
3078 memcpy(newlist->node.key, filename, strlen(filename) + 1);
3079 if (unique)
3080 newlist->entries = EB_ROOT_UNIQUE;
3081 else
3082 newlist->entries = EB_ROOT;
3083
3084 LIST_INIT(&newlist->ord_entries);
3085
3086 return newlist;
3087}
William Lallemand9117de92019-10-04 00:29:42 +02003088
3089/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02003090static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02003091 struct bind_conf *s, struct ssl_bind_conf *conf,
3092 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003093{
3094 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003095 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003096
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003097 if (*name == '!') {
3098 neg = 1;
3099 name++;
3100 }
3101 if (*name == '*') {
3102 wild = 1;
3103 name++;
3104 }
3105 /* !* filter is a nop */
3106 if (neg && wild)
3107 return order;
3108 if (*name) {
3109 int j, len;
3110 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003111 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003113 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02003114 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003115 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003116
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003117 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02003118 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02003119 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003120 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02003121 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003122 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003123 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003124 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003125 sc->order = order++;
3126 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02003127 sc->wild = wild;
3128 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01003129 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02003130 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003131 }
3132 return order;
3133}
3134
William Lallemand6af03992019-07-23 15:00:54 +02003135/*
William Lallemand1d29c742019-10-04 00:53:29 +02003136 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
3137 * This function can't return an error.
3138 *
3139 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
3140 */
3141static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
3142{
3143
3144 struct sni_ctx *sc0, *sc0b, *sc1;
3145 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01003146 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02003147
3148 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
3149
3150 /* ignore if sc0 was already inserted in a tree */
3151 if (sc0->name.node.leaf_p)
3152 continue;
3153
3154 /* Check for duplicates. */
3155 if (sc0->wild)
3156 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
3157 else
3158 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
3159
3160 for (; node; node = ebmb_next_dup(node)) {
3161 sc1 = ebmb_entry(node, struct sni_ctx, name);
3162 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
3163 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
3164 /* it's a duplicate, we should remove and free it */
3165 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02003166 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02003167 free(sc0);
3168 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02003169 break;
William Lallemand1d29c742019-10-04 00:53:29 +02003170 }
3171 }
3172
3173 /* if duplicate, ignore the insertion */
3174 if (!sc0)
3175 continue;
3176
3177 if (sc0->wild)
3178 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3179 else
3180 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003181
3182 /* replace the default_ctx if required with the first ctx */
3183 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02003184 SSL_CTX_free(bind_conf->default_ctx);
3185 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01003186 bind_conf->default_ctx = sc0->ctx;
3187 def = 1;
3188 }
William Lallemand1d29c742019-10-04 00:53:29 +02003189 }
3190}
3191
3192/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003193 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003194 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003195struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003196
William Lallemand2954c472020-03-06 21:54:13 +01003197/* tree of crtlist (crt-list/directory) */
3198static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003199
Emeric Brun7a883362019-10-17 13:27:40 +02003200/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003201 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003202 * DH parameter is loaded into the SSL_CTX and if there is no
3203 * DH parameter available in ckchs nor in global, the default
3204 * DH parameters are applied on the SSL_CTX.
3205 * Returns a bitfield containing the flags:
3206 * ERR_FATAL in any fatal error case
3207 * ERR_ALERT if a reason of the error is availabine in err
3208 * ERR_WARN if a warning is available into err
3209 * The value 0 means there is no error nor warning and
3210 * the operation succeed.
3211 */
William Lallemandfa892222019-07-23 16:06:08 +02003212#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003213static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3214 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003215{
Emeric Brun7a883362019-10-17 13:27:40 +02003216 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003217 DH *dh = NULL;
3218
William Lallemanda8c73742019-07-31 18:31:34 +02003219 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003220 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003221 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3222 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3223 err && *err ? *err : "", path);
3224#if defined(SSL_CTX_set_dh_auto)
3225 SSL_CTX_set_dh_auto(ctx, 1);
3226 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3227 err && *err ? *err : "");
3228#else
3229 memprintf(err, "%s, DH ciphers won't be available.\n",
3230 err && *err ? *err : "");
3231#endif
3232 ret |= ERR_WARN;
3233 goto end;
3234 }
William Lallemandfa892222019-07-23 16:06:08 +02003235
3236 if (ssl_dh_ptr_index >= 0) {
3237 /* store a pointer to the DH params to avoid complaining about
3238 ssl-default-dh-param not being set for this SSL_CTX */
3239 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3240 }
3241 }
3242 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003243 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3244 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3245 err && *err ? *err : "", path);
3246#if defined(SSL_CTX_set_dh_auto)
3247 SSL_CTX_set_dh_auto(ctx, 1);
3248 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3249 err && *err ? *err : "");
3250#else
3251 memprintf(err, "%s, DH ciphers won't be available.\n",
3252 err && *err ? *err : "");
3253#endif
3254 ret |= ERR_WARN;
3255 goto end;
3256 }
William Lallemandfa892222019-07-23 16:06:08 +02003257 }
3258 else {
3259 /* Clear openssl global errors stack */
3260 ERR_clear_error();
3261
3262 if (global_ssl.default_dh_param <= 1024) {
3263 /* we are limited to DH parameter of 1024 bits anyway */
3264 if (local_dh_1024 == NULL)
3265 local_dh_1024 = ssl_get_dh_1024();
3266
Emeric Brun7a883362019-10-17 13:27:40 +02003267 if (local_dh_1024 == NULL) {
3268 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3269 err && *err ? *err : "", path);
3270 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003271 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003272 }
William Lallemandfa892222019-07-23 16:06:08 +02003273
Emeric Bruna9363eb2019-10-17 14:53:03 +02003274 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3275 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3276 err && *err ? *err : "", path);
3277#if defined(SSL_CTX_set_dh_auto)
3278 SSL_CTX_set_dh_auto(ctx, 1);
3279 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3280 err && *err ? *err : "");
3281#else
3282 memprintf(err, "%s, DH ciphers won't be available.\n",
3283 err && *err ? *err : "");
3284#endif
3285 ret |= ERR_WARN;
3286 goto end;
3287 }
William Lallemandfa892222019-07-23 16:06:08 +02003288 }
3289 else {
3290 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3291 }
William Lallemandfa892222019-07-23 16:06:08 +02003292 }
3293
3294end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003295 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003296 return ret;
3297}
3298#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003299
yanbzhu488a4d22015-12-01 15:16:07 -05003300/* Frees the contents of a cert_key_and_chain
3301 */
3302static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3303{
yanbzhu488a4d22015-12-01 15:16:07 -05003304 if (!ckch)
3305 return;
3306
3307 /* Free the certificate and set pointer to NULL */
3308 if (ckch->cert)
3309 X509_free(ckch->cert);
3310 ckch->cert = NULL;
3311
3312 /* Free the key and set pointer to NULL */
3313 if (ckch->key)
3314 EVP_PKEY_free(ckch->key);
3315 ckch->key = NULL;
3316
3317 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003318 if (ckch->chain)
3319 sk_X509_pop_free(ckch->chain, X509_free);
3320 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003321
William Lallemand455af502019-10-17 18:04:45 +02003322 if (ckch->dh)
3323 DH_free(ckch->dh);
3324 ckch->dh = NULL;
3325
3326 if (ckch->sctl) {
3327 free(ckch->sctl->area);
3328 ckch->sctl->area = NULL;
3329 free(ckch->sctl);
3330 ckch->sctl = NULL;
3331 }
3332
3333 if (ckch->ocsp_response) {
3334 free(ckch->ocsp_response->area);
3335 ckch->ocsp_response->area = NULL;
3336 free(ckch->ocsp_response);
3337 ckch->ocsp_response = NULL;
3338 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003339
3340 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003341 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003342 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003343}
3344
William Lallemand8d0f8932019-10-17 18:03:58 +02003345/*
3346 *
3347 * This function copy a cert_key_and_chain in memory
3348 *
3349 * It's used to try to apply changes on a ckch before committing them, because
3350 * most of the time it's not possible to revert those changes
3351 *
3352 * Return a the dst or NULL
3353 */
3354static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3355 struct cert_key_and_chain *dst)
3356{
3357 if (src->cert) {
3358 dst->cert = src->cert;
3359 X509_up_ref(src->cert);
3360 }
3361
3362 if (src->key) {
3363 dst->key = src->key;
3364 EVP_PKEY_up_ref(src->key);
3365 }
3366
3367 if (src->chain) {
3368 dst->chain = X509_chain_up_ref(src->chain);
3369 }
3370
3371 if (src->dh) {
3372 DH_up_ref(src->dh);
3373 dst->dh = src->dh;
3374 }
3375
3376 if (src->sctl) {
3377 struct buffer *sctl;
3378
3379 sctl = calloc(1, sizeof(*sctl));
3380 if (!chunk_dup(sctl, src->sctl)) {
3381 free(sctl);
3382 sctl = NULL;
3383 goto error;
3384 }
3385 dst->sctl = sctl;
3386 }
3387
3388 if (src->ocsp_response) {
3389 struct buffer *ocsp_response;
3390
3391 ocsp_response = calloc(1, sizeof(*ocsp_response));
3392 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3393 free(ocsp_response);
3394 ocsp_response = NULL;
3395 goto error;
3396 }
3397 dst->ocsp_response = ocsp_response;
3398 }
3399
3400 if (src->ocsp_issuer) {
3401 X509_up_ref(src->ocsp_issuer);
3402 dst->ocsp_issuer = src->ocsp_issuer;
3403 }
3404
3405 return dst;
3406
3407error:
3408
3409 /* free everything */
3410 ssl_sock_free_cert_key_and_chain_contents(dst);
3411
3412 return NULL;
3413}
3414
3415
yanbzhu488a4d22015-12-01 15:16:07 -05003416/* checks if a key and cert exists in the ckch
3417 */
William Lallemand1633e392019-09-30 12:58:13 +02003418#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003419static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3420{
3421 return (ckch->cert != NULL && ckch->key != NULL);
3422}
William Lallemand1633e392019-09-30 12:58:13 +02003423#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003424
William Lallemandf9568fc2019-10-16 18:27:58 +02003425/*
3426 * return 0 on success or != 0 on failure
3427 */
3428static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3429{
3430 int ret = 1;
3431 BIO *in = NULL;
3432 X509 *issuer;
3433
3434 if (buf) {
3435 /* reading from a buffer */
3436 in = BIO_new_mem_buf(buf, -1);
3437 if (in == NULL) {
3438 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3439 goto end;
3440 }
3441
3442 } else {
3443 /* reading from a file */
3444 in = BIO_new(BIO_s_file());
3445 if (in == NULL)
3446 goto end;
3447
3448 if (BIO_read_filename(in, path) <= 0)
3449 goto end;
3450 }
3451
3452 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3453 if (!issuer) {
3454 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003455 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003456 goto end;
3457 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003458 /* no error, fill ckch with new context, old context must be free */
3459 if (ckch->ocsp_issuer)
3460 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003461 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003462 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003463
3464end:
3465
3466 ERR_clear_error();
3467 if (in)
3468 BIO_free(in);
3469
3470 return ret;
3471}
3472
William Lallemand96a9c972019-10-17 11:56:17 +02003473
3474/*
3475 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003476 * The PEM must contain at least a Certificate,
3477 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003478 *
William Lallemand96a9c972019-10-17 11:56:17 +02003479 * If it failed you should not attempt to use the ckch but free it.
3480 *
3481 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003482 */
William Lallemand96a9c972019-10-17 11:56:17 +02003483static 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 -05003484{
William Lallemandf11365b2019-09-19 14:25:58 +02003485 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003486 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003487 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003488 X509 *cert = NULL;
3489 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003490 DH *dh = NULL;
3491 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003492
3493 if (buf) {
3494 /* reading from a buffer */
3495 in = BIO_new_mem_buf(buf, -1);
3496 if (in == NULL) {
3497 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3498 goto end;
3499 }
yanbzhu488a4d22015-12-01 15:16:07 -05003500
William Lallemand96a9c972019-10-17 11:56:17 +02003501 } else {
3502 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003503 in = BIO_new(BIO_s_file());
William Lallemand7fd01b32020-04-07 14:16:32 +02003504 if (in == NULL) {
3505 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
William Lallemandf11365b2019-09-19 14:25:58 +02003506 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003507 }
yanbzhu488a4d22015-12-01 15:16:07 -05003508
William Lallemand7fd01b32020-04-07 14:16:32 +02003509 if (BIO_read_filename(in, path) <= 0) {
3510 memprintf(err, "%scannot open the file '%s'.\n",
3511 err && *err ? *err : "", path);
William Lallemandf11365b2019-09-19 14:25:58 +02003512 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003513 }
William Lallemandf11365b2019-09-19 14:25:58 +02003514 }
yanbzhu488a4d22015-12-01 15:16:07 -05003515
yanbzhu488a4d22015-12-01 15:16:07 -05003516 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003517 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003518 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003519
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003520#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003521 /* Seek back to beginning of file */
3522 if (BIO_reset(in) == -1) {
3523 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3524 err && *err ? *err : "", path);
3525 goto end;
3526 }
3527
William Lallemand96a9c972019-10-17 11:56:17 +02003528 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3529 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003530#endif
William Lallemandfa892222019-07-23 16:06:08 +02003531
Willy Tarreaubb137a82016-04-06 19:02:38 +02003532 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003533 if (BIO_reset(in) == -1) {
3534 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3535 err && *err ? *err : "", path);
3536 goto end;
3537 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003538
3539 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003540 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3541 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003542 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003543 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003544 goto end;
3545 }
3546
William Lallemand96a9c972019-10-17 11:56:17 +02003547 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003548 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3549 if (chain == NULL)
3550 chain = sk_X509_new_null();
3551 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003552 X509_free(ca);
3553 goto end;
3554 }
3555 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003556
yanbzhu488a4d22015-12-01 15:16:07 -05003557 ret = ERR_get_error();
3558 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3559 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003560 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003561 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003562 }
3563
William Lallemand75b15f72020-01-23 10:56:05 +01003564 /* once it loaded the PEM, it should remove everything else in the ckch */
3565 if (ckch->ocsp_response) {
3566 free(ckch->ocsp_response->area);
3567 ckch->ocsp_response->area = NULL;
3568 free(ckch->ocsp_response);
3569 ckch->ocsp_response = NULL;
3570 }
3571
3572 if (ckch->sctl) {
3573 free(ckch->sctl->area);
3574 ckch->sctl->area = NULL;
3575 free(ckch->sctl);
3576 ckch->sctl = NULL;
3577 }
3578
3579 if (ckch->ocsp_issuer) {
3580 X509_free(ckch->ocsp_issuer);
3581 ckch->ocsp_issuer = NULL;
3582 }
3583
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003584 /* no error, fill ckch with new context, old context will be free at end: */
3585 SWAP(ckch->key, key);
3586 SWAP(ckch->dh, dh);
3587 SWAP(ckch->cert, cert);
3588 SWAP(ckch->chain, chain);
3589
William Lallemand246c0242019-10-11 08:59:13 +02003590 ret = 0;
3591
William Lallemand96a9c972019-10-17 11:56:17 +02003592end:
William Lallemand246c0242019-10-11 08:59:13 +02003593
3594 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003595 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003596 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003597 if (key)
3598 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003599 if (dh)
3600 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003601 if (cert)
3602 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003603 if (chain)
3604 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003605
William Lallemand96a9c972019-10-17 11:56:17 +02003606 return ret;
3607}
3608
3609/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003610 * Try to load a private key file from a <path> or a buffer <buf>
3611 *
3612 * If it failed you should not attempt to use the ckch but free it.
3613 *
3614 * Return 0 on success or != 0 on failure
3615 */
3616static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3617{
3618 BIO *in = NULL;
3619 int ret = 1;
3620 EVP_PKEY *key = NULL;
3621
3622 if (buf) {
3623 /* reading from a buffer */
3624 in = BIO_new_mem_buf(buf, -1);
3625 if (in == NULL) {
3626 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3627 goto end;
3628 }
3629
3630 } else {
3631 /* reading from a file */
3632 in = BIO_new(BIO_s_file());
3633 if (in == NULL)
3634 goto end;
3635
3636 if (BIO_read_filename(in, path) <= 0)
3637 goto end;
3638 }
3639
3640 /* Read Private Key */
3641 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3642 if (key == NULL) {
3643 memprintf(err, "%sunable to load private key from file '%s'.\n",
3644 err && *err ? *err : "", path);
3645 goto end;
3646 }
3647
3648 ret = 0;
3649
3650 SWAP(ckch->key, key);
3651
3652end:
3653
3654 ERR_clear_error();
3655 if (in)
3656 BIO_free(in);
3657 if (key)
3658 EVP_PKEY_free(key);
3659
3660 return ret;
3661}
3662
3663/*
William Lallemand96a9c972019-10-17 11:56:17 +02003664 * Try to load in a ckch every files related to a ckch.
3665 * (PEM, sctl, ocsp, issuer etc.)
3666 *
3667 * This function is only used to load files during the configuration parsing,
3668 * it is not used with the CLI.
3669 *
3670 * This allows us to carry the contents of the file without having to read the
3671 * file multiple times. The caller must call
3672 * ssl_sock_free_cert_key_and_chain_contents.
3673 *
3674 * returns:
3675 * 0 on Success
3676 * 1 on SSL Failure
3677 */
3678static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3679{
3680 int ret = 1;
3681
3682 /* try to load the PEM */
3683 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3684 goto end;
3685 }
3686
William Lallemand4c5adbf2020-02-24 14:23:22 +01003687 /* try to load an external private key if it wasn't in the PEM */
3688 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3689 char fp[MAXPATHLEN+1];
3690 struct stat st;
3691
3692 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3693 if (stat(fp, &st) == 0) {
3694 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3695 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3696 err && *err ? *err : "", fp);
3697 goto end;
3698 }
3699 }
3700 }
3701
3702 if (ckch->key == NULL) {
3703 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3704 goto end;
3705 }
3706
3707 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3708 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3709 err && *err ? *err : "", path);
3710 goto end;
3711 }
3712
William Lallemanda17f4112019-10-10 15:16:44 +02003713#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3714 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003715 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003716 char fp[MAXPATHLEN+1];
3717 struct stat st;
3718
3719 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3720 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003721 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003722 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003723 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003724 ret = 1;
3725 goto end;
3726 }
3727 }
3728 }
3729#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003730
William Lallemand246c0242019-10-11 08:59:13 +02003731 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003732 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003733 char fp[MAXPATHLEN+1];
3734 struct stat st;
3735
3736 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3737 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003738 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003739 ret = 1;
3740 goto end;
3741 }
3742 }
3743 }
3744
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003745#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003746 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003747 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003748 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003749 struct stat st;
3750 char fp[MAXPATHLEN+1];
3751
3752 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3753 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003754 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003755 ret = 1;
3756 goto end;
3757 }
3758
3759 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003760 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003761 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003762 ret = 1;
3763 goto end;
3764 }
William Lallemand246c0242019-10-11 08:59:13 +02003765 }
3766 }
3767 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003768#endif
William Lallemand246c0242019-10-11 08:59:13 +02003769
yanbzhu488a4d22015-12-01 15:16:07 -05003770 ret = 0;
3771
3772end:
3773
3774 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003775
3776 /* Something went wrong in one of the reads */
3777 if (ret != 0)
3778 ssl_sock_free_cert_key_and_chain_contents(ckch);
3779
3780 return ret;
3781}
3782
3783/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003784 * Returns a bitfield containing the flags:
3785 * ERR_FATAL in any fatal error case
3786 * ERR_ALERT if the reason of the error is available in err
3787 * ERR_WARN if a warning is available into err
3788 * The value 0 means there is no error nor warning and
3789 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003790 */
3791static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3792{
Emeric Bruna96b5822019-10-17 13:25:14 +02003793 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003794 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003795
yanbzhu488a4d22015-12-01 15:16:07 -05003796 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3797 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3798 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003799 errcode |= ERR_ALERT | ERR_FATAL;
3800 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003801 }
3802
3803 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3804 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3805 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003806 errcode |= ERR_ALERT | ERR_FATAL;
3807 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003808 }
3809
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003810 if (ckch->chain) {
3811 find_chain = ckch->chain;
3812 } else {
3813 /* Find Certificate Chain in global */
3814 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003815 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003816 if (issuer)
3817 find_chain = issuer->chain;
3818 }
William Lallemand85888572020-02-27 14:48:35 +01003819
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003820 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003821 if (find_chain) {
3822 int i;
3823 X509 *ca;
3824 for (i = 0; i < sk_X509_num(find_chain); i++) {
3825 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003826 /* skip self issued (Root CA) */
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +02003827 if (global_ssl.skip_self_issued_ca && !X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca)))
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003828 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003829 /*
3830 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3831 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3832 */
3833 X509_up_ref(ca);
3834 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3835 X509_free(ca);
3836 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3837 err && *err ? *err : "", path);
3838 errcode |= ERR_ALERT | ERR_FATAL;
3839 goto end;
3840 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003841 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003842 }
yanbzhu488a4d22015-12-01 15:16:07 -05003843
William Lallemandfa892222019-07-23 16:06:08 +02003844#ifndef OPENSSL_NO_DH
3845 /* store a NULL pointer to indicate we have not yet loaded
3846 a custom DH param file */
3847 if (ssl_dh_ptr_index >= 0) {
3848 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3849 }
3850
Emeric Brun7a883362019-10-17 13:27:40 +02003851 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3852 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003853 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3854 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003855 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003856 }
3857#endif
3858
William Lallemanda17f4112019-10-10 15:16:44 +02003859#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3860 if (sctl_ex_index >= 0 && ckch->sctl) {
3861 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3862 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003863 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003864 errcode |= ERR_ALERT | ERR_FATAL;
3865 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003866 }
3867 }
3868#endif
3869
William Lallemand4a660132019-10-14 14:51:41 +02003870#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003871 /* Load OCSP Info into context */
3872 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003873 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003874 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",
3875 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003876 errcode |= ERR_ALERT | ERR_FATAL;
3877 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003878 }
3879 }
William Lallemand246c0242019-10-11 08:59:13 +02003880#endif
3881
Emeric Bruna96b5822019-10-17 13:25:14 +02003882 end:
3883 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003884}
3885
William Lallemandc4ecddf2019-07-31 16:50:08 +02003886#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003887
William Lallemand28a8fce2019-10-04 17:36:55 +02003888static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003889{
3890 struct sni_keytype *s_kt = NULL;
3891 struct ebmb_node *node;
3892 int i;
3893
3894 for (i = 0; i < trash.size; i++) {
3895 if (!str[i])
3896 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003897 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003898 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003899 trash.area[i] = 0;
3900 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003901 if (!node) {
3902 /* CN not found in tree */
3903 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3904 /* Using memcpy here instead of strncpy.
3905 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3906 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3907 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003908 if (!s_kt)
3909 return -1;
3910
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003911 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003912 s_kt->keytypes = 0;
3913 ebst_insert(sni_keytypes, &s_kt->name);
3914 } else {
3915 /* CN found in tree */
3916 s_kt = container_of(node, struct sni_keytype, name);
3917 }
3918
3919 /* Mark that this CN has the keytype of key_index via keytypes mask */
3920 s_kt->keytypes |= 1<<key_index;
3921
William Lallemand28a8fce2019-10-04 17:36:55 +02003922 return 0;
3923
yanbzhu08ce6ab2015-12-02 13:01:29 -05003924}
3925
William Lallemandc4ecddf2019-07-31 16:50:08 +02003926#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003927/*
William Lallemandba1c33f2020-04-08 17:55:45 +02003928 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
William Lallemand8c1cdde2019-10-18 10:58:14 +02003929 */
William Lallemandba1c33f2020-04-08 17:55:45 +02003930static void ckch_store_free(struct ckch_store *store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003931{
William Lallemandba1c33f2020-04-08 17:55:45 +02003932 struct ckch_inst *inst, *inst_s;
3933
3934 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003935 return;
3936
William Lallemandba1c33f2020-04-08 17:55:45 +02003937#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L
3938 if (store->multi) {
William Lallemand8c1cdde2019-10-18 10:58:14 +02003939 int n;
3940
William Lallemandba1c33f2020-04-08 17:55:45 +02003941 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3942 ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003943 } else
3944#endif
3945 {
William Lallemandba1c33f2020-04-08 17:55:45 +02003946 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003947 }
3948
William Lallemandba1c33f2020-04-08 17:55:45 +02003949 free(store->ckch);
3950 store->ckch = NULL;
3951
3952 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
3953 ckch_inst_free(inst);
3954 }
3955 ebmb_delete(&store->node);
3956 free(store);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003957}
3958
William Lallemand8a874e42020-04-09 10:32:53 +02003959/*
3960 * create and initialize a ckch_store
3961 * <path> is the key name
3962 * <nmemb> is the number of store->ckch objects to allocate
3963 *
3964 * Return a ckch_store or NULL upon failure.
3965 */
3966static struct ckch_store *ckch_store_new(const char *filename, int nmemb)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003967{
William Lallemand8a874e42020-04-09 10:32:53 +02003968 struct ckch_store *store;
William Lallemand8c1cdde2019-10-18 10:58:14 +02003969 int pathlen;
3970
William Lallemand8a874e42020-04-09 10:32:53 +02003971 pathlen = strlen(filename);
3972 store = calloc(1, sizeof(*store) + pathlen + 1);
3973 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003974 return NULL;
William Lallemand8a874e42020-04-09 10:32:53 +02003975
3976 if (nmemb > 1)
3977 store->multi = 1;
3978 else
3979 store->multi = 0;
3980
3981 memcpy(store->path, filename, pathlen + 1);
3982
3983 LIST_INIT(&store->ckch_inst);
3984 LIST_INIT(&store->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003985
William Lallemand8a874e42020-04-09 10:32:53 +02003986 store->ckch = calloc(nmemb, sizeof(*store->ckch));
3987 if (!store->ckch)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003988 goto error;
3989
William Lallemand8a874e42020-04-09 10:32:53 +02003990 return store;
3991error:
3992 ckch_store_free(store);
3993 return NULL;
3994}
3995
3996/* allocate and duplicate a ckch_store
3997 * Return a new ckch_store or NULL */
3998static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3999{
4000 struct ckch_store *dst;
4001
4002 dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1);
4003
William Lallemand8c1cdde2019-10-18 10:58:14 +02004004#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4005 if (src->multi) {
4006 int n;
4007
4008 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4009 if (&src->ckch[n]) {
4010 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
4011 goto error;
4012 }
4013 }
4014 } else
4015#endif
4016 {
4017 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
4018 goto error;
4019 }
4020
4021 return dst;
4022
4023error:
William Lallemandba1c33f2020-04-08 17:55:45 +02004024 ckch_store_free(dst);
William Lallemand8c1cdde2019-10-18 10:58:14 +02004025
4026 return NULL;
4027}
William Lallemandc4ecddf2019-07-31 16:50:08 +02004028
William Lallemand36b84632019-07-18 19:28:17 +02004029/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004030 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02004031 */
William Lallemande3af8fb2019-10-08 11:36:53 +02004032static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02004033{
4034 struct ebmb_node *eb;
4035
William Lallemande3af8fb2019-10-08 11:36:53 +02004036 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02004037 if (!eb)
4038 return NULL;
4039
William Lallemande3af8fb2019-10-08 11:36:53 +02004040 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02004041}
4042
4043/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004044 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02004045 */
William Lallemande3af8fb2019-10-08 11:36:53 +02004046static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02004047{
William Lallemande3af8fb2019-10-08 11:36:53 +02004048 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02004049
William Lallemand8a874e42020-04-09 10:32:53 +02004050 ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1);
William Lallemande3af8fb2019-10-08 11:36:53 +02004051 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02004052 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
4053 goto end;
4054 }
William Lallemand36b84632019-07-18 19:28:17 +02004055 if (!multi) {
4056
William Lallemand96a9c972019-10-17 11:56:17 +02004057 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004058 goto end;
4059
William Lallemande3af8fb2019-10-08 11:36:53 +02004060 /* insert into the ckchs tree */
4061 memcpy(ckchs->path, path, strlen(path) + 1);
4062 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004063 } else {
4064 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02004065#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4066 char fp[MAXPATHLEN+1] = {0};
4067 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02004068
4069 /* Load all possible certs and keys */
4070 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4071 struct stat buf;
4072 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
4073 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02004074 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004075 goto end;
4076 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02004077 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02004078 }
4079 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02004080#endif
William Lallemand36b84632019-07-18 19:28:17 +02004081
4082 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02004083 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02004084 goto end;
4085 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004086 /* insert into the ckchs tree */
4087 memcpy(ckchs->path, path, strlen(path) + 1);
4088 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004089 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004090 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004091
William Lallemand36b84632019-07-18 19:28:17 +02004092end:
William Lallemandba1c33f2020-04-08 17:55:45 +02004093 ckch_store_free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02004094
4095 return NULL;
4096}
4097
William Lallemandc4ecddf2019-07-31 16:50:08 +02004098#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4099
William Lallemand36b84632019-07-18 19:28:17 +02004100/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004101 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02004102 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05004103 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
4104 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08004105 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05004106 *
Emeric Brun054563d2019-10-17 13:16:58 +02004107 * Returns a bitfield containing the flags:
4108 * ERR_FATAL in any fatal error case
4109 * ERR_ALERT if the reason of the error is available in err
4110 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02004111 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05004112 */
Emeric Brun054563d2019-10-17 13:16:58 +02004113static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4114 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4115 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004116{
William Lallemand36b84632019-07-18 19:28:17 +02004117 int i = 0, n = 0;
4118 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02004119 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004120 struct ebmb_node *node;
4121 struct ebmb_node *next;
4122 /* Array of SSL_CTX pointers corresponding to each possible combo
4123 * of keytypes
4124 */
4125 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02004126 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004127 X509_NAME *xname = NULL;
4128 char *str = NULL;
4129#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4130 STACK_OF(GENERAL_NAME) *names = NULL;
4131#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02004132 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004133
Emeric Brun054563d2019-10-17 13:16:58 +02004134 *ckchi = NULL;
4135
William Lallemande3af8fb2019-10-08 11:36:53 +02004136 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02004137 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4138 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004139 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004140 }
4141
4142 ckch_inst = ckch_inst_new();
4143 if (!ckch_inst) {
4144 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4145 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004146 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004147 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004148 }
4149
William Lallemande3af8fb2019-10-08 11:36:53 +02004150 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004151
yanbzhu08ce6ab2015-12-02 13:01:29 -05004152 /* Process each ckch and update keytypes for each CN/SAN
4153 * for example, if CN/SAN www.a.com is associated with
4154 * certs with keytype 0 and 2, then at the end of the loop,
4155 * www.a.com will have:
4156 * keyindex = 0 | 1 | 4 = 5
4157 */
4158 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004159 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004160
4161 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4162 continue;
4163
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004164 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004165 for (i = 0; i < fcount; i++) {
4166 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4167 if (ret < 0) {
4168 memprintf(err, "%sunable to allocate SSL context.\n",
4169 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004170 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004171 goto end;
4172 }
4173 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004174 } else {
4175 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4176 * so the line that contains logic is marked via comments
4177 */
4178 xname = X509_get_subject_name(certs_and_keys[n].cert);
4179 i = -1;
4180 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4181 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004182 ASN1_STRING *value;
4183 value = X509_NAME_ENTRY_get_data(entry);
4184 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004185 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004186 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004187
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004188 OPENSSL_free(str);
4189 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004190 if (ret < 0) {
4191 memprintf(err, "%sunable to allocate SSL context.\n",
4192 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004193 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004194 goto end;
4195 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004196 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004197 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004198
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004199 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004200#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004201 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4202 if (names) {
4203 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4204 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004205
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004206 if (name->type == GEN_DNS) {
4207 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4208 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004209 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004210
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004211 OPENSSL_free(str);
4212 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004213 if (ret < 0) {
4214 memprintf(err, "%sunable to allocate SSL context.\n",
4215 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004216 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004217 goto end;
4218 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004219 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004220 }
4221 }
4222 }
4223 }
4224#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4225 }
4226
4227 /* If no files found, return error */
4228 if (eb_is_empty(&sni_keytypes_map)) {
4229 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4230 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004231 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004232 goto end;
4233 }
4234
4235 /* We now have a map of CN/SAN to keytypes that are loaded in
4236 * Iterate through the map to create the SSL_CTX's (if needed)
4237 * and add each CTX to the SNI tree
4238 *
4239 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004240 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004241 * combination is denoted by the key in the map. Each key
4242 * has a value between 1 and 2^n - 1. Conveniently, the array
4243 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4244 * entry in the array to correspond to the unique combo (key)
4245 * associated with i. This unique key combo (i) will be associated
4246 * with combos[i-1]
4247 */
4248
4249 node = ebmb_first(&sni_keytypes_map);
4250 while (node) {
4251 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004252 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004253 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004254
4255 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4256 i = container_of(node, struct sni_keytype, name)->keytypes;
4257 cur_ctx = key_combos[i-1].ctx;
4258
4259 if (cur_ctx == NULL) {
4260 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004261 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004262 if (cur_ctx == NULL) {
4263 memprintf(err, "%sunable to allocate SSL context.\n",
4264 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004265 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004266 goto end;
4267 }
4268
yanbzhube2774d2015-12-10 15:07:30 -05004269 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004270 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4271 if (i & (1<<n)) {
4272 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004273 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004274 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4275 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004276 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004277 }
4278 }
4279
yanbzhu08ce6ab2015-12-02 13:01:29 -05004280 /* Update key_combos */
4281 key_combos[i-1].ctx = cur_ctx;
4282 }
4283
4284 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004285
William Lallemand1d29c742019-10-04 00:53:29 +02004286 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 +02004287 kinfo, str, key_combos[i-1].order);
4288 if (key_combos[i-1].order < 0) {
4289 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004290 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004291 goto end;
4292 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004293 node = ebmb_next(node);
4294 }
4295
4296
4297 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4298 if (!bind_conf->default_ctx) {
4299 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4300 if (key_combos[i].ctx) {
4301 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004302 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004303 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004304 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004305 break;
4306 }
4307 }
4308 }
4309
William Lallemand614ca0d2019-10-07 13:52:11 +02004310 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004311 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004312 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004313
yanbzhu08ce6ab2015-12-02 13:01:29 -05004314end:
4315
4316 if (names)
4317 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4318
yanbzhu08ce6ab2015-12-02 13:01:29 -05004319 node = ebmb_first(&sni_keytypes_map);
4320 while (node) {
4321 next = ebmb_next(node);
4322 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004323 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004324 node = next;
4325 }
4326
William Lallemand02e19a52020-04-08 16:11:26 +02004327 /* we need to free the ctx since we incremented the refcount where it's used */
4328 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4329 if (key_combos[i].ctx)
4330 SSL_CTX_free(key_combos[i].ctx);
4331 }
4332
Emeric Brun054563d2019-10-17 13:16:58 +02004333 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004334 if (ckch_inst->is_default) {
4335 SSL_CTX_free(bind_conf->default_ctx);
4336 bind_conf->default_ctx = NULL;
4337 }
4338
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004339 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004340 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004341 }
4342
Emeric Brun054563d2019-10-17 13:16:58 +02004343 *ckchi = ckch_inst;
4344 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004345}
4346#else
4347/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004348static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4349 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4350 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004351{
4352 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4353 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004354 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004355}
4356
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004357#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004358
William Lallemand614ca0d2019-10-07 13:52:11 +02004359/*
4360 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004361 *
4362 * Returns a bitfield containing the flags:
4363 * ERR_FATAL in any fatal error case
4364 * ERR_ALERT if the reason of the error is available in err
4365 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004366 */
Emeric Brun054563d2019-10-17 13:16:58 +02004367static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4368 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004369{
William Lallemandc9402072019-05-15 15:33:54 +02004370 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004371 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004372 int order = 0;
4373 X509_NAME *xname;
4374 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004375 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004376 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004377#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4378 STACK_OF(GENERAL_NAME) *names;
4379#endif
William Lallemand36b84632019-07-18 19:28:17 +02004380 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004381 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004382 int errcode = 0;
4383
4384 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004385
William Lallemande3af8fb2019-10-08 11:36:53 +02004386 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004387 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004388
William Lallemande3af8fb2019-10-08 11:36:53 +02004389 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004390
William Lallemandc9402072019-05-15 15:33:54 +02004391 ctx = SSL_CTX_new(SSLv23_server_method());
4392 if (!ctx) {
4393 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4394 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004395 errcode |= ERR_ALERT | ERR_FATAL;
4396 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004397 }
4398
Emeric Bruna96b5822019-10-17 13:25:14 +02004399 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4400 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004401 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004402
4403 ckch_inst = ckch_inst_new();
4404 if (!ckch_inst) {
4405 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4406 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004407 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004408 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004409 }
4410
William Lallemand36b84632019-07-18 19:28:17 +02004411 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004412 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004413 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004414 switch(EVP_PKEY_base_id(pkey)) {
4415 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004416 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004417 break;
4418 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004419 kinfo.sig = TLSEXT_signature_ecdsa;
4420 break;
4421 case EVP_PKEY_DSA:
4422 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004423 break;
4424 }
4425 EVP_PKEY_free(pkey);
4426 }
4427
Emeric Brun50bcecc2013-04-22 13:05:23 +02004428 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004429 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004430 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 +02004431 if (order < 0) {
4432 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004433 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004434 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004435 }
4436 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004437 }
4438 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004439#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004440 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004441 if (names) {
4442 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4443 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4444 if (name->type == GEN_DNS) {
4445 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004446 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004447 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004448 if (order < 0) {
4449 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004450 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004451 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004452 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004453 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004454 }
4455 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004456 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004457 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004458#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004459 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004460 i = -1;
4461 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4462 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004463 ASN1_STRING *value;
4464
4465 value = X509_NAME_ENTRY_get_data(entry);
4466 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004467 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004468 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004469 if (order < 0) {
4470 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004471 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004472 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004473 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004474 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004475 }
4476 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004477 /* we must not free the SSL_CTX anymore below, since it's already in
4478 * the tree, so it will be discovered and cleaned in time.
4479 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004480
Emeric Brunfc0421f2012-09-07 17:30:07 +02004481#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004482 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004483 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4484 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004485 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004486 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004487 }
4488#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004489 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004490 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004491 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004492 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004493 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004494 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004495
William Lallemand9117de92019-10-04 00:29:42 +02004496 /* everything succeed, the ckch instance can be used */
4497 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004498 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004499 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004500
William Lallemand02e19a52020-04-08 16:11:26 +02004501 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4502
Emeric Brun054563d2019-10-17 13:16:58 +02004503 *ckchi = ckch_inst;
4504 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004505
4506error:
4507 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004508 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004509 if (ckch_inst->is_default)
4510 SSL_CTX_free(ctx);
4511
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004512 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004513 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004514 }
William Lallemandd9199372019-10-04 15:37:05 +02004515 SSL_CTX_free(ctx);
4516
Emeric Brun054563d2019-10-17 13:16:58 +02004517 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004518}
4519
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004520/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004521static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4522 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004523 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004524{
Emeric Brun054563d2019-10-17 13:16:58 +02004525 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004526
4527 /* we found the ckchs in the tree, we can use it directly */
4528 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004529 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 +02004530 else
William Lallemand24bde432020-03-09 16:48:43 +01004531 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 +02004532
Emeric Brun054563d2019-10-17 13:16:58 +02004533 if (errcode & ERR_CODE)
4534 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004535
William Lallemand24bde432020-03-09 16:48:43 +01004536 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004537
4538 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004539 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004540 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004541}
4542
William Lallemand6be66ec2020-03-06 22:26:32 +01004543
William Lallemand4c68bba2020-03-30 18:45:10 +02004544
4545
4546/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4547 * done once. Zero is returned if the operation fails. No error is returned
4548 * if the random is said as not implemented, because we expect that openssl
4549 * will use another method once needed.
4550 */
4551static int ssl_initialize_random()
4552{
4553 unsigned char random;
4554 static int random_initialized = 0;
4555
4556 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4557 random_initialized = 1;
4558
4559 return random_initialized;
4560}
4561
William Lallemandec2d4932020-04-09 13:44:21 +02004562
William Lallemand6be66ec2020-03-06 22:26:32 +01004563/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4564 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4565 *
4566 * This function tries to open and store certificate files.
4567 */
4568static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004569{
William Lallemand6be66ec2020-03-06 22:26:32 +01004570 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004571 struct dirent **de_list;
4572 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004573 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004574 char *end;
4575 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004576 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004577 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004578#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004579 int is_bundle;
4580 int j;
4581#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004582
William Lallemandec2d4932020-04-09 13:44:21 +02004583 dir = crtlist_new(path, 1);
William Lallemand6be66ec2020-03-06 22:26:32 +01004584 if (dir == NULL) {
4585 memprintf(err, "not enough memory");
4586 return ERR_ALERT | ERR_FATAL;
4587 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004588
William Lallemand2d232c22020-03-06 22:12:35 +01004589 n = scandir(path, &de_list, 0, alphasort);
4590 if (n < 0) {
4591 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4592 err && *err ? *err : "", path, strerror(errno));
4593 cfgerr |= ERR_ALERT | ERR_FATAL;
4594 }
4595 else {
4596 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004597 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004598 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004599
William Lallemand2d232c22020-03-06 22:12:35 +01004600 end = strrchr(de->d_name, '.');
4601 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4602 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004603
William Lallemanda64593c2020-03-17 20:11:41 +01004604 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4605 if (stat(fp, &buf) != 0) {
4606 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4607 err && *err ? *err : "", fp, strerror(errno));
4608 cfgerr |= ERR_ALERT | ERR_FATAL;
4609 goto ignore_entry;
4610 }
4611 if (!S_ISREG(buf.st_mode))
4612 goto ignore_entry;
4613
William Lallemande718dfb2020-04-10 11:09:25 +02004614 entry = crtlist_entry_new();
William Lallemand6be66ec2020-03-06 22:26:32 +01004615 if (entry == NULL) {
4616 memprintf(err, "not enough memory '%s'", fp);
4617 cfgerr |= ERR_ALERT | ERR_FATAL;
4618 goto ignore_entry;
4619 }
4620
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004621#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004622 is_bundle = 0;
4623 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004624
William Lallemand2d232c22020-03-06 22:12:35 +01004625 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4626 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4627 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4628 is_bundle = 1;
4629 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004630 }
William Lallemand2d232c22020-03-06 22:12:35 +01004631 }
yanbzhu63ea8462015-12-09 13:35:14 -05004632
William Lallemand2d232c22020-03-06 22:12:35 +01004633 if (is_bundle) {
4634 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004635
William Lallemand2d232c22020-03-06 22:12:35 +01004636 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004637
William Lallemand2d232c22020-03-06 22:12:35 +01004638 /* increment i and free de until we get to a non-bundle cert
4639 * Note here that we look at de_list[i + 1] before freeing de
4640 * this is important since ignore_entry will free de. This also
4641 * guarantees that de->d_name continues to hold the same prefix.
4642 */
4643 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4644 free(de);
4645 i++;
4646 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004647 }
William Lallemand2d232c22020-03-06 22:12:35 +01004648
4649 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004650 ckchs = ckchs_lookup(fp);
4651 if (ckchs == NULL)
4652 ckchs = ckchs_load_cert_file(fp, 1, err);
4653 if (ckchs == NULL) {
4654 free(de);
4655 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004656 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004657 goto end;
4658 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004659 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004660 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004661 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004662 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004663 ebpt_insert(&dir->entries, &entry->node);
4664
William Lallemand2d232c22020-03-06 22:12:35 +01004665 /* Successfully processed the bundle */
4666 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004667 }
William Lallemand2d232c22020-03-06 22:12:35 +01004668 }
yanbzhu63ea8462015-12-09 13:35:14 -05004669
4670#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004671 ckchs = ckchs_lookup(fp);
4672 if (ckchs == NULL)
4673 ckchs = ckchs_load_cert_file(fp, 0, err);
4674 if (ckchs == NULL) {
4675 free(de);
4676 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004677 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004678 goto end;
4679 }
4680 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004681 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004682 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004683 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004684 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004685
yanbzhu08ce6ab2015-12-02 13:01:29 -05004686ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004687 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004688 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004689end:
William Lallemand2d232c22020-03-06 22:12:35 +01004690 free(de_list);
4691 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004692
4693 if (cfgerr & ERR_CODE) {
4694 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004695 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004696 } else {
4697 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004698 }
William Lallemand2d232c22020-03-06 22:12:35 +01004699 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004700
William Lallemand2d232c22020-03-06 22:12:35 +01004701}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004702
William Lallemand97b08102020-04-01 10:14:00 +02004703/*
4704 * Read a single crt-list line. /!\ alter the <line> string.
4705 * Fill <crt_path> and <crtlist_entry>
4706 * <crtlist_entry> must be alloc and free by the caller
4707 * <crtlist_entry->ssl_conf> is alloc by the function
4708 * <crtlist_entry->filters> is alloc by the function
4709 * <crt_path> is a ptr in <line>
4710 * Return an error code
4711 */
4712static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4713{
4714 int cfgerr = 0;
4715 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4716 char *end;
4717 char *args[MAX_CRT_ARGS + 1];
4718 struct ssl_bind_conf *ssl_conf = NULL;
4719
4720 if (!line || !crt_path || !entry)
4721 return ERR_ALERT | ERR_FATAL;
4722
4723 end = line + strlen(line);
4724 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4725 /* Check if we reached the limit and the last char is not \n.
4726 * Watch out for the last line without the terminating '\n'!
4727 */
4728 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4729 linenum, file, CRT_LINESIZE-1);
4730 cfgerr |= ERR_ALERT | ERR_FATAL;
4731 goto error;
4732 }
4733 arg = 0;
4734 newarg = 1;
4735 while (*line) {
4736 if (isspace((unsigned char)*line)) {
4737 newarg = 1;
4738 *line = 0;
4739 } else if (*line == '[') {
4740 if (ssl_b) {
4741 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4742 cfgerr |= ERR_ALERT | ERR_FATAL;
4743 goto error;
4744 }
4745 if (!arg) {
4746 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4747 cfgerr |= ERR_ALERT | ERR_FATAL;
4748 goto error;
4749 }
4750 ssl_b = arg;
4751 newarg = 1;
4752 *line = 0;
4753 } else if (*line == ']') {
4754 if (ssl_e) {
4755 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4756 cfgerr |= ERR_ALERT | ERR_FATAL;
4757 goto error;
4758 }
4759 if (!ssl_b) {
4760 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4761 cfgerr |= ERR_ALERT | ERR_FATAL;
4762 goto error;
4763 }
4764 ssl_e = arg;
4765 newarg = 1;
4766 *line = 0;
4767 } else if (newarg) {
4768 if (arg == MAX_CRT_ARGS) {
4769 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4770 cfgerr |= ERR_ALERT | ERR_FATAL;
4771 goto error;
4772 }
4773 newarg = 0;
4774 args[arg++] = line;
4775 }
4776 line++;
4777 }
4778 args[arg++] = line;
4779
4780 /* empty line */
4781 if (!*args[0]) {
4782 cfgerr |= ERR_NONE;
4783 goto error;
4784 }
4785
4786 *crt_path = args[0];
4787
William Lallemand1b2988b2020-04-10 17:20:45 +02004788 if (ssl_b) {
4789 ssl_conf = calloc(1, sizeof *ssl_conf);
4790 if (!ssl_conf) {
4791 memprintf(err, "not enough memory!");
4792 cfgerr |= ERR_ALERT | ERR_FATAL;
4793 goto error;
4794 }
William Lallemand97b08102020-04-01 10:14:00 +02004795 }
4796 cur_arg = ssl_b ? ssl_b : 1;
4797 while (cur_arg < ssl_e) {
4798 newarg = 0;
4799 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4800 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4801 newarg = 1;
4802 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4803 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4804 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4805 args[cur_arg], linenum, file);
4806 cfgerr |= ERR_ALERT | ERR_FATAL;
4807 goto error;
4808 }
4809 cur_arg += 1 + ssl_bind_kws[i].skip;
4810 break;
4811 }
4812 }
4813 if (!cfgerr && !newarg) {
4814 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4815 args[cur_arg], linenum, file);
4816 cfgerr |= ERR_ALERT | ERR_FATAL;
4817 goto error;
4818 }
4819 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004820 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004821 entry->ssl_conf = ssl_conf;
4822 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4823 entry->fcount = arg - cur_arg - 1;
4824
4825 return cfgerr;
4826
4827error:
4828 crtlist_free_filters(entry->filters);
4829 entry->filters = NULL;
4830 ssl_sock_free_ssl_conf(entry->ssl_conf);
4831 free(entry->ssl_conf);
4832 entry->ssl_conf = NULL;
4833 return cfgerr;
4834}
4835
William Lallemand2954c472020-03-06 21:54:13 +01004836/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4837 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4838 *
4839 * This function tries to open and store certificate files.
4840 */
4841static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4842{
4843 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004844 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004845 char thisline[CRT_LINESIZE];
4846 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004847 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004848 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004849 int linenum = 0;
4850 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004851
Willy Tarreauad1731d2013-04-02 17:35:58 +02004852 if ((f = fopen(file, "r")) == NULL) {
4853 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004854 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004855 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004856
William Lallemandec2d4932020-04-09 13:44:21 +02004857 newlist = crtlist_new(file, 0);
William Lallemand2954c472020-03-06 21:54:13 +01004858 if (newlist == NULL) {
4859 memprintf(err, "Not enough memory!");
4860 cfgerr |= ERR_ALERT | ERR_FATAL;
4861 goto error;
4862 }
William Lallemand2954c472020-03-06 21:54:13 +01004863
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004864 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004865 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004866 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004867 char *crt_path;
William Lallemand2954c472020-03-06 21:54:13 +01004868 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004869
4870 linenum++;
4871 end = line + strlen(line);
4872 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4873 /* Check if we reached the limit and the last char is not \n.
4874 * Watch out for the last line without the terminating '\n'!
4875 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004876 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4877 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004878 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004879 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004880 }
William Lallemand97b08102020-04-01 10:14:00 +02004881
4882 if (*line == '#' || *line == '\n' || *line == '\r')
4883 continue;
4884
William Lallemande718dfb2020-04-10 11:09:25 +02004885 entry = crtlist_entry_new();
William Lallemand97b08102020-04-01 10:14:00 +02004886 if (entry == NULL) {
4887 memprintf(err, "Not enough memory!");
4888 cfgerr |= ERR_ALERT | ERR_FATAL;
4889 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004890 }
William Lallemand97b08102020-04-01 10:14:00 +02004891
4892 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4893 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004894 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004895 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004896
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004897 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004898 if (!crt_path || !*crt_path) {
William Lallemande718dfb2020-04-10 11:09:25 +02004899 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004900 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004901 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004902 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004903
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004904 if (*crt_path != '/' && global_ssl.crt_base) {
4905 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4906 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4907 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004908 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004909 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004910 }
4911 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4912 crt_path = path;
4913 }
4914
William Lallemand2954c472020-03-06 21:54:13 +01004915 /* Look for a ckch_store or create one */
4916 ckchs = ckchs_lookup(crt_path);
4917 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004918 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004919 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004920 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004921 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004922 }
William Lallemand909086e2020-03-17 16:53:27 +01004923 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004924 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004925
William Lallemand97b08102020-04-01 10:14:00 +02004926 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004927 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004928
William Lallemand2954c472020-03-06 21:54:13 +01004929 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004930 entry->crtlist = newlist;
William Lallemand2954c472020-03-06 21:54:13 +01004931 ebpt_insert(&newlist->entries, &entry->node);
4932 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004933 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004934
4935 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004936 }
William Lallemand2954c472020-03-06 21:54:13 +01004937 if (cfgerr & ERR_CODE)
4938 goto error;
4939
William Lallemandc69f02d2020-04-06 19:07:03 +02004940 newlist->linecount = linenum;
4941
William Lallemand2954c472020-03-06 21:54:13 +01004942 fclose(f);
4943 *crtlist = newlist;
4944
4945 return cfgerr;
4946error:
William Lallemande718dfb2020-04-10 11:09:25 +02004947 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004948
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004949 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004950 crtlist_free(newlist);
4951 return cfgerr;
4952}
4953
4954/* Load a crt-list file, this is done in 2 parts:
4955 * - store the content of the file in a crtlist structure with crtlist_entry structures
4956 * - generate the instances by iterating on entries in the crtlist struct
4957 *
4958 * Nothing is locked there, this function is used in the configuration parser.
4959 *
4960 * Returns a set of ERR_* flags possibly with an error in <err>.
4961 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004962int 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 +01004963{
4964 struct crtlist *crtlist = NULL;
4965 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004966 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004967 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004968 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004969 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004970
William Lallemand79d31ec2020-03-25 15:10:49 +01004971 bind_conf_node = malloc(sizeof(*bind_conf_node));
4972 if (!bind_conf_node) {
4973 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4974 cfgerr |= ERR_FATAL | ERR_ALERT;
4975 goto error;
4976 }
4977 bind_conf_node->next = NULL;
4978 bind_conf_node->bind_conf = bind_conf;
4979
William Lallemand41ca9302020-04-08 13:15:18 +02004980 /* strip trailing slashes, including first one */
4981 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4982 *end = 0;
4983
William Lallemand2954c472020-03-06 21:54:13 +01004984 /* look for an existing crtlist or create one */
4985 eb = ebst_lookup(&crtlists_tree, file);
4986 if (eb) {
4987 crtlist = ebmb_entry(eb, struct crtlist, node);
4988 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004989 /* load a crt-list OR a directory */
4990 if (dir)
4991 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4992 else
4993 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4994
William Lallemand2954c472020-03-06 21:54:13 +01004995 if (!(cfgerr & ERR_CODE))
4996 ebst_insert(&crtlists_tree, &crtlist->node);
4997 }
4998
4999 if (cfgerr & ERR_CODE) {
5000 cfgerr |= ERR_FATAL | ERR_ALERT;
5001 goto error;
5002 }
5003
5004 /* generates ckch instance from the crtlist_entry */
5005 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5006 struct ckch_store *store;
5007 struct ckch_inst *ckch_inst = NULL;
5008
5009 store = entry->node.key;
5010 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
5011 if (cfgerr & ERR_CODE) {
5012 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
5013 goto error;
5014 }
William Lallemand49398312020-03-30 17:01:33 +02005015 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02005016 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01005017 }
William Lallemand2954c472020-03-06 21:54:13 +01005018
William Lallemand79d31ec2020-03-25 15:10:49 +01005019 /* add the bind_conf to the list */
5020 bind_conf_node->next = crtlist->bind_conf;
5021 crtlist->bind_conf = bind_conf_node;
5022
William Lallemand2954c472020-03-06 21:54:13 +01005023 return cfgerr;
5024error:
5025 {
William Lallemand49398312020-03-30 17:01:33 +02005026 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01005027 struct ckch_inst *inst, *s_inst;
5028
William Lallemand49398312020-03-30 17:01:33 +02005029 lastentry = entry; /* which entry we tried to generate last */
5030 if (lastentry) {
5031 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5032 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
5033 break;
5034
5035 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01005036
William Lallemand49398312020-03-30 17:01:33 +02005037 /* this was not generated for this bind_conf, skip */
5038 if (inst->bind_conf != bind_conf)
5039 continue;
5040
William Lallemandd9d5d1b2020-04-09 16:31:05 +02005041 /* free the sni_ctx and instance */
5042 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02005043 }
William Lallemand2954c472020-03-06 21:54:13 +01005044 }
William Lallemand2954c472020-03-06 21:54:13 +01005045 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005046 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005047 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005048 return cfgerr;
5049}
5050
William Lallemand06b22a82020-03-16 14:45:55 +01005051/* Returns a set of ERR_* flags possibly with an error in <err>. */
5052int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5053{
5054 struct stat buf;
5055 char fp[MAXPATHLEN+1];
5056 int cfgerr = 0;
5057 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005058 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005059
5060 if ((ckchs = ckchs_lookup(path))) {
5061 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005062 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005063 }
5064 if (stat(path, &buf) == 0) {
5065 if (S_ISDIR(buf.st_mode) == 0) {
5066 ckchs = ckchs_load_cert_file(path, 0, err);
5067 if (!ckchs)
5068 return ERR_ALERT | ERR_FATAL;
5069
William Lallemand24bde432020-03-09 16:48:43 +01005070 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005071 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005072 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005073 }
5074 } else {
5075 /* stat failed, could be a bundle */
5076 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5077 /* try to load a bundle if it is permitted */
5078 ckchs = ckchs_load_cert_file(path, 1, err);
5079 if (!ckchs)
5080 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005081 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005082 } else {
5083 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5084 err && *err ? *err : "", fp, strerror(errno));
5085 cfgerr |= ERR_ALERT | ERR_FATAL;
5086 }
5087 }
5088
5089 return cfgerr;
5090}
5091
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005092/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005093static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005094ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005095{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005096 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005097 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005098 SSL_OP_ALL | /* all known workarounds for bugs */
5099 SSL_OP_NO_SSLv2 |
5100 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005101 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005102 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005103 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005104 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005105 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005106 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005107 SSL_MODE_ENABLE_PARTIAL_WRITE |
5108 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005109 SSL_MODE_RELEASE_BUFFERS |
5110 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005111 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005112 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005113 int flags = MC_SSL_O_ALL;
5114 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005115
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005116 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005117 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005118
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005119 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005120 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5121 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5122 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005123 else
5124 flags = conf_ssl_methods->flags;
5125
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005126 min = conf_ssl_methods->min;
5127 max = conf_ssl_methods->max;
5128 /* start with TLSv10 to remove SSLv3 per default */
5129 if (!min && (!max || max >= CONF_TLSV10))
5130 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005131 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005132 if (min)
5133 flags |= (methodVersions[min].flag - 1);
5134 if (max)
5135 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005136 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005137 min = max = CONF_TLSV_NONE;
5138 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005139 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005140 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005141 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005142 if (min) {
5143 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005144 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5145 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5146 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5147 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005148 hole = 0;
5149 }
5150 max = i;
5151 }
5152 else {
5153 min = max = i;
5154 }
5155 }
5156 else {
5157 if (min)
5158 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005159 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005160 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005161 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5162 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005163 cfgerr += 1;
5164 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005165 /* save real min/max in bind_conf */
5166 conf_ssl_methods->min = min;
5167 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005168
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005169#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005170 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005171 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005172 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005173 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005174 else
5175 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5176 if (flags & methodVersions[i].flag)
5177 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005178#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005179 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005180 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5181 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005182#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005183
5184 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5185 options |= SSL_OP_NO_TICKET;
5186 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5187 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005188
5189#ifdef SSL_OP_NO_RENEGOTIATION
5190 options |= SSL_OP_NO_RENEGOTIATION;
5191#endif
5192
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005193 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005194
Willy Tarreau5db847a2019-05-09 14:13:35 +02005195#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005196 if (global_ssl.async)
5197 mode |= SSL_MODE_ASYNC;
5198#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005199 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005200 if (global_ssl.life_time)
5201 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005202
5203#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5204#ifdef OPENSSL_IS_BORINGSSL
5205 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5206 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005207#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005208 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005209 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005210 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5211 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005212#else
5213 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005214#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005215 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005216#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005217 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005218}
5219
William Lallemand4f45bb92017-10-30 20:08:51 +01005220
5221static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5222{
5223 if (first == block) {
5224 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5225 if (first->len > 0)
5226 sh_ssl_sess_tree_delete(sh_ssl_sess);
5227 }
5228}
5229
5230/* return first block from sh_ssl_sess */
5231static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5232{
5233 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5234
5235}
5236
5237/* store a session into the cache
5238 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5239 * data: asn1 encoded session
5240 * data_len: asn1 encoded session length
5241 * Returns 1 id session was stored (else 0)
5242 */
5243static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5244{
5245 struct shared_block *first;
5246 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5247
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005248 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005249 if (!first) {
5250 /* Could not retrieve enough free blocks to store that session */
5251 return 0;
5252 }
5253
5254 /* STORE the key in the first elem */
5255 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5256 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5257 first->len = sizeof(struct sh_ssl_sess_hdr);
5258
5259 /* it returns the already existing node
5260 or current node if none, never returns null */
5261 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5262 if (oldsh_ssl_sess != sh_ssl_sess) {
5263 /* NOTE: Row couldn't be in use because we lock read & write function */
5264 /* release the reserved row */
5265 shctx_row_dec_hot(ssl_shctx, first);
5266 /* replace the previous session already in the tree */
5267 sh_ssl_sess = oldsh_ssl_sess;
5268 /* ignore the previous session data, only use the header */
5269 first = sh_ssl_sess_first_block(sh_ssl_sess);
5270 shctx_row_inc_hot(ssl_shctx, first);
5271 first->len = sizeof(struct sh_ssl_sess_hdr);
5272 }
5273
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005274 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005275 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005276 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005277 }
5278
5279 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005280
5281 return 1;
5282}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005283
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005284/* SSL callback used when a new session is created while connecting to a server */
5285static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5286{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005287 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005288 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005289
Willy Tarreau07d94e42018-09-20 10:57:52 +02005290 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005291
Olivier Houcharde6060c52017-11-16 17:42:52 +01005292 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5293 int len;
5294 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005295
Olivier Houcharde6060c52017-11-16 17:42:52 +01005296 len = i2d_SSL_SESSION(sess, NULL);
5297 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5298 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5299 } else {
5300 free(s->ssl_ctx.reused_sess[tid].ptr);
5301 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5302 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5303 }
5304 if (s->ssl_ctx.reused_sess[tid].ptr) {
5305 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5306 &ptr);
5307 }
5308 } else {
5309 free(s->ssl_ctx.reused_sess[tid].ptr);
5310 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5311 }
5312
5313 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005314}
5315
Olivier Houcharde6060c52017-11-16 17:42:52 +01005316
William Lallemanded0b5ad2017-10-30 19:36:36 +01005317/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005318int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005319{
5320 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5321 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5322 unsigned char *p;
5323 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005324 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005325 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005326
5327 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005328 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005329 * note: SSL_SESSION_set1_id is using
5330 * a memcpy so we need to use a different pointer
5331 * than sid_data or sid_ctx_data to avoid valgrind
5332 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005333 */
5334
5335 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005336
5337 /* copy value in an other buffer */
5338 memcpy(encid, sid_data, sid_length);
5339
5340 /* pad with 0 */
5341 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5342 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5343
5344 /* force length to zero to avoid ASN1 encoding */
5345 SSL_SESSION_set1_id(sess, encid, 0);
5346
5347 /* force length to zero to avoid ASN1 encoding */
5348 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005349
5350 /* check if buffer is large enough for the ASN1 encoded session */
5351 data_len = i2d_SSL_SESSION(sess, NULL);
5352 if (data_len > SHSESS_MAX_DATA_LEN)
5353 goto err;
5354
5355 p = encsess;
5356
5357 /* process ASN1 session encoding before the lock */
5358 i2d_SSL_SESSION(sess, &p);
5359
William Lallemanded0b5ad2017-10-30 19:36:36 +01005360
William Lallemanda3c77cf2017-10-30 23:44:40 +01005361 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005362 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005363 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005364 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005365err:
5366 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005367 SSL_SESSION_set1_id(sess, encid, sid_length);
5368 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005369
5370 return 0; /* do not increment session reference count */
5371}
5372
5373/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005374SSL_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 +01005375{
William Lallemand4f45bb92017-10-30 20:08:51 +01005376 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005377 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5378 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005379 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005380 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005381
5382 global.shctx_lookups++;
5383
5384 /* allow the session to be freed automatically by openssl */
5385 *do_copy = 0;
5386
5387 /* tree key is zeros padded sessionid */
5388 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5389 memcpy(tmpkey, key, key_len);
5390 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5391 key = tmpkey;
5392 }
5393
5394 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005395 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005396
5397 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005398 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5399 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005400 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005401 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005402 global.shctx_misses++;
5403 return NULL;
5404 }
5405
William Lallemand4f45bb92017-10-30 20:08:51 +01005406 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5407 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005408
William Lallemand4f45bb92017-10-30 20:08:51 +01005409 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 +01005410
William Lallemanda3c77cf2017-10-30 23:44:40 +01005411 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005412
5413 /* decode ASN1 session */
5414 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005415 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005416 /* Reset session id and session id contenxt */
5417 if (sess) {
5418 SSL_SESSION_set1_id(sess, key, key_len);
5419 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5420 }
5421
5422 return sess;
5423}
5424
William Lallemand4f45bb92017-10-30 20:08:51 +01005425
William Lallemanded0b5ad2017-10-30 19:36:36 +01005426/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005427void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005428{
William Lallemand4f45bb92017-10-30 20:08:51 +01005429 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005430 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5431 unsigned int sid_length;
5432 const unsigned char *sid_data;
5433 (void)ctx;
5434
5435 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5436 /* tree key is zeros padded sessionid */
5437 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5438 memcpy(tmpkey, sid_data, sid_length);
5439 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5440 sid_data = tmpkey;
5441 }
5442
William Lallemanda3c77cf2017-10-30 23:44:40 +01005443 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005444
5445 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005446 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5447 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005448 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005449 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005450 }
5451
5452 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005453 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005454}
5455
5456/* Set session cache mode to server and disable openssl internal cache.
5457 * Set shared cache callbacks on an ssl context.
5458 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005459void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005460{
5461 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5462
5463 if (!ssl_shctx) {
5464 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5465 return;
5466 }
5467
5468 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5469 SSL_SESS_CACHE_NO_INTERNAL |
5470 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5471
5472 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005473 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5474 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5475 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005476}
5477
William Lallemand8b453912019-11-21 15:48:10 +01005478/*
5479 * This function applies the SSL configuration on a SSL_CTX
5480 * It returns an error code and fills the <err> buffer
5481 */
5482int 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 +01005483{
5484 struct proxy *curproxy = bind_conf->frontend;
5485 int cfgerr = 0;
5486 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005487 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005488 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005489#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005490 const char *conf_ciphersuites;
5491#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005492 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005493
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005494 if (ssl_conf) {
5495 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5496 int i, min, max;
5497 int flags = MC_SSL_O_ALL;
5498
5499 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005500 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5501 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005502 if (min)
5503 flags |= (methodVersions[min].flag - 1);
5504 if (max)
5505 flags |= ~((methodVersions[max].flag << 1) - 1);
5506 min = max = CONF_TLSV_NONE;
5507 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5508 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5509 if (min)
5510 max = i;
5511 else
5512 min = max = i;
5513 }
5514 /* save real min/max */
5515 conf_ssl_methods->min = min;
5516 conf_ssl_methods->max = max;
5517 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005518 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5519 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005520 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005521 }
5522 }
5523
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005524 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005525 case SSL_SOCK_VERIFY_NONE:
5526 verify = SSL_VERIFY_NONE;
5527 break;
5528 case SSL_SOCK_VERIFY_OPTIONAL:
5529 verify = SSL_VERIFY_PEER;
5530 break;
5531 case SSL_SOCK_VERIFY_REQUIRED:
5532 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5533 break;
5534 }
5535 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5536 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005537 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 +01005538 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 +01005539 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 +01005540 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005541 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005542 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005543 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005544 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005545 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005546 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005547 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5548 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5549 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5550 cfgerr |= ERR_ALERT | ERR_FATAL;
5551 }
5552 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005553 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005554 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 +02005555 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005556 }
Emeric Brun850efd52014-01-29 12:24:34 +01005557 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005558 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5559 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005560 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005561 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005562#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005563 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005564 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5565
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005566 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005567 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5568 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005569 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005570 }
Emeric Brun561e5742012-10-02 15:20:55 +02005571 else {
5572 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5573 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005574 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005575#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005576 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005577 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005578#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005579 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005580 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005581 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5582 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005583 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005584 }
5585 }
5586#endif
5587
William Lallemand4f45bb92017-10-30 20:08:51 +01005588 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005589 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5590 if (conf_ciphers &&
5591 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005592 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5593 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005594 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005595 }
5596
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005597#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005598 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5599 if (conf_ciphersuites &&
5600 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005601 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5602 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005603 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005604 }
5605#endif
5606
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005607#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005608 /* If tune.ssl.default-dh-param has not been set,
5609 neither has ssl-default-dh-file and no static DH
5610 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005611 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005612 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005613 (ssl_dh_ptr_index == -1 ||
5614 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005615 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5616 const SSL_CIPHER * cipher = NULL;
5617 char cipher_description[128];
5618 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5619 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5620 which is not ephemeral DH. */
5621 const char dhe_description[] = " Kx=DH ";
5622 const char dhe_export_description[] = " Kx=DH(";
5623 int idx = 0;
5624 int dhe_found = 0;
5625 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005626
Remi Gacogne23d5d372014-10-10 17:04:26 +02005627 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005628
Remi Gacogne23d5d372014-10-10 17:04:26 +02005629 if (ssl) {
5630 ciphers = SSL_get_ciphers(ssl);
5631
5632 if (ciphers) {
5633 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5634 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5635 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5636 if (strstr(cipher_description, dhe_description) != NULL ||
5637 strstr(cipher_description, dhe_export_description) != NULL) {
5638 dhe_found = 1;
5639 break;
5640 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005641 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005642 }
5643 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005644 SSL_free(ssl);
5645 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005646 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005647
Lukas Tribus90132722014-08-18 00:56:33 +02005648 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005649 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",
5650 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005651 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005652 }
5653
Willy Tarreauef934602016-12-22 23:12:01 +01005654 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005655 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005656
Willy Tarreauef934602016-12-22 23:12:01 +01005657 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005658 if (local_dh_1024 == NULL) {
5659 local_dh_1024 = ssl_get_dh_1024();
5660 }
Willy Tarreauef934602016-12-22 23:12:01 +01005661 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005662 if (local_dh_2048 == NULL) {
5663 local_dh_2048 = ssl_get_dh_2048();
5664 }
Willy Tarreauef934602016-12-22 23:12:01 +01005665 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005666 if (local_dh_4096 == NULL) {
5667 local_dh_4096 = ssl_get_dh_4096();
5668 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005669 }
5670 }
5671 }
5672#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005673
Emeric Brunfc0421f2012-09-07 17:30:07 +02005674 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005675#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005676 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005677#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005678
Bernard Spil13c53f82018-02-15 13:34:58 +01005679#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005680 ssl_conf_cur = NULL;
5681 if (ssl_conf && ssl_conf->npn_str)
5682 ssl_conf_cur = ssl_conf;
5683 else if (bind_conf->ssl_conf.npn_str)
5684 ssl_conf_cur = &bind_conf->ssl_conf;
5685 if (ssl_conf_cur)
5686 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005687#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005688#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005689 ssl_conf_cur = NULL;
5690 if (ssl_conf && ssl_conf->alpn_str)
5691 ssl_conf_cur = ssl_conf;
5692 else if (bind_conf->ssl_conf.alpn_str)
5693 ssl_conf_cur = &bind_conf->ssl_conf;
5694 if (ssl_conf_cur)
5695 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005696#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005697#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005698 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5699 if (conf_curves) {
5700 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005701 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5702 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005703 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005704 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005705 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005706 }
5707#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005708#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005709 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005710 int i;
5711 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005712#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005713 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005714 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5715 NULL);
5716
5717 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005718 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005719 return cfgerr;
5720 }
5721#else
5722 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5723 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5724 ECDHE_DEFAULT_CURVE);
5725#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005726
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005727 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005728 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005729 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5730 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005731 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005732 }
5733 else {
5734 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5735 EC_KEY_free(ecdh);
5736 }
5737 }
5738#endif
5739
Emeric Brunfc0421f2012-09-07 17:30:07 +02005740 return cfgerr;
5741}
5742
Evan Broderbe554312013-06-27 00:05:25 -07005743static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5744{
5745 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5746 size_t prefixlen, suffixlen;
5747
5748 /* Trivial case */
5749 if (strcmp(pattern, hostname) == 0)
5750 return 1;
5751
Evan Broderbe554312013-06-27 00:05:25 -07005752 /* The rest of this logic is based on RFC 6125, section 6.4.3
5753 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5754
Emeric Bruna848dae2013-10-08 11:27:28 +02005755 pattern_wildcard = NULL;
5756 pattern_left_label_end = pattern;
5757 while (*pattern_left_label_end != '.') {
5758 switch (*pattern_left_label_end) {
5759 case 0:
5760 /* End of label not found */
5761 return 0;
5762 case '*':
5763 /* If there is more than one wildcards */
5764 if (pattern_wildcard)
5765 return 0;
5766 pattern_wildcard = pattern_left_label_end;
5767 break;
5768 }
5769 pattern_left_label_end++;
5770 }
5771
5772 /* If it's not trivial and there is no wildcard, it can't
5773 * match */
5774 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005775 return 0;
5776
5777 /* Make sure all labels match except the leftmost */
5778 hostname_left_label_end = strchr(hostname, '.');
5779 if (!hostname_left_label_end
5780 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5781 return 0;
5782
5783 /* Make sure the leftmost label of the hostname is long enough
5784 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005785 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005786 return 0;
5787
5788 /* Finally compare the string on either side of the
5789 * wildcard */
5790 prefixlen = pattern_wildcard - pattern;
5791 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005792 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5793 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005794 return 0;
5795
5796 return 1;
5797}
5798
5799static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5800{
5801 SSL *ssl;
5802 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005803 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005804 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005805 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005806
5807 int depth;
5808 X509 *cert;
5809 STACK_OF(GENERAL_NAME) *alt_names;
5810 int i;
5811 X509_NAME *cert_subject;
5812 char *str;
5813
5814 if (ok == 0)
5815 return ok;
5816
5817 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005818 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005819 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005820
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005821 /* We're checking if the provided hostnames match the desired one. The
5822 * desired hostname comes from the SNI we presented if any, or if not
5823 * provided then it may have been explicitly stated using a "verifyhost"
5824 * directive. If neither is set, we don't care about the name so the
5825 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005826 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005827 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005828 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005829 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005830 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005831 if (!servername)
5832 return ok;
5833 }
Evan Broderbe554312013-06-27 00:05:25 -07005834
5835 /* We only need to verify the CN on the actual server cert,
5836 * not the indirect CAs */
5837 depth = X509_STORE_CTX_get_error_depth(ctx);
5838 if (depth != 0)
5839 return ok;
5840
5841 /* At this point, the cert is *not* OK unless we can find a
5842 * hostname match */
5843 ok = 0;
5844
5845 cert = X509_STORE_CTX_get_current_cert(ctx);
5846 /* It seems like this might happen if verify peer isn't set */
5847 if (!cert)
5848 return ok;
5849
5850 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5851 if (alt_names) {
5852 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5853 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5854 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005855#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005856 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5857#else
Evan Broderbe554312013-06-27 00:05:25 -07005858 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005859#endif
Evan Broderbe554312013-06-27 00:05:25 -07005860 ok = ssl_sock_srv_hostcheck(str, servername);
5861 OPENSSL_free(str);
5862 }
5863 }
5864 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005865 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005866 }
5867
5868 cert_subject = X509_get_subject_name(cert);
5869 i = -1;
5870 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5871 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005872 ASN1_STRING *value;
5873 value = X509_NAME_ENTRY_get_data(entry);
5874 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005875 ok = ssl_sock_srv_hostcheck(str, servername);
5876 OPENSSL_free(str);
5877 }
5878 }
5879
Willy Tarreau71d058c2017-07-26 20:09:56 +02005880 /* report the mismatch and indicate if SNI was used or not */
5881 if (!ok && !conn->err_code)
5882 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005883 return ok;
5884}
5885
Emeric Brun94324a42012-10-11 14:00:19 +02005886/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005887int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005888{
Willy Tarreau03209342016-12-22 17:08:28 +01005889 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005890 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005891 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005892 SSL_OP_ALL | /* all known workarounds for bugs */
5893 SSL_OP_NO_SSLv2 |
5894 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005895 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005896 SSL_MODE_ENABLE_PARTIAL_WRITE |
5897 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005898 SSL_MODE_RELEASE_BUFFERS |
5899 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005900 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005901 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005902 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005903 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005904 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005905
Thierry Fournier383085f2013-01-24 14:15:43 +01005906 /* Make sure openssl opens /dev/urandom before the chroot */
5907 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005908 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005909 cfgerr++;
5910 }
5911
Willy Tarreaufce03112015-01-15 21:32:40 +01005912 /* Automatic memory computations need to know we use SSL there */
5913 global.ssl_used_backend = 1;
5914
5915 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005916 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005917 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005918 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5919 curproxy->id, srv->id,
5920 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005921 cfgerr++;
5922 return cfgerr;
5923 }
5924 }
Emeric Brun94324a42012-10-11 14:00:19 +02005925 if (srv->use_ssl)
5926 srv->xprt = &ssl_sock;
5927 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005928 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005929
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005930 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005931 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005932 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5933 proxy_type_str(curproxy), curproxy->id,
5934 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005935 cfgerr++;
5936 return cfgerr;
5937 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005938
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005939 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005940 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5941 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5942 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005943 else
5944 flags = conf_ssl_methods->flags;
5945
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005946 /* Real min and max should be determinate with configuration and openssl's capabilities */
5947 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005948 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005949 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005950 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005951
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005952 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005953 min = max = CONF_TLSV_NONE;
5954 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005955 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005956 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005957 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005958 if (min) {
5959 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005960 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5961 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5962 proxy_type_str(curproxy), curproxy->id, srv->id,
5963 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005964 hole = 0;
5965 }
5966 max = i;
5967 }
5968 else {
5969 min = max = i;
5970 }
5971 }
5972 else {
5973 if (min)
5974 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005975 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005976 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005977 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5978 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005979 cfgerr += 1;
5980 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005981
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005982#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005983 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005984 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005985 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005986 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005987 else
5988 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5989 if (flags & methodVersions[i].flag)
5990 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005991#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005992 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005993 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5994 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005995#endif
5996
5997 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5998 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005999 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006000
Willy Tarreau5db847a2019-05-09 14:13:35 +02006001#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006002 if (global_ssl.async)
6003 mode |= SSL_MODE_ASYNC;
6004#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01006005 SSL_CTX_set_mode(ctx, mode);
6006 srv->ssl_ctx.ctx = ctx;
6007
Emeric Bruna7aa3092012-10-26 12:58:00 +02006008 if (srv->ssl_ctx.client_crt) {
6009 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 +01006010 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
6011 proxy_type_str(curproxy), curproxy->id,
6012 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006013 cfgerr++;
6014 }
6015 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 +01006016 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
6017 proxy_type_str(curproxy), curproxy->id,
6018 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006019 cfgerr++;
6020 }
6021 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006022 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
6023 proxy_type_str(curproxy), curproxy->id,
6024 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006025 cfgerr++;
6026 }
6027 }
Emeric Brun94324a42012-10-11 14:00:19 +02006028
Emeric Brun850efd52014-01-29 12:24:34 +01006029 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
6030 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01006031 switch (srv->ssl_ctx.verify) {
6032 case SSL_SOCK_VERIFY_NONE:
6033 verify = SSL_VERIFY_NONE;
6034 break;
6035 case SSL_SOCK_VERIFY_REQUIRED:
6036 verify = SSL_VERIFY_PEER;
6037 break;
6038 }
Evan Broderbe554312013-06-27 00:05:25 -07006039 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01006040 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02006041 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01006042 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02006043 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02006044 /* set CAfile to verify */
6045 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6046 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006047 curproxy->id, srv->id,
6048 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006049 cfgerr++;
6050 }
6051 }
Emeric Brun850efd52014-01-29 12:24:34 +01006052 else {
6053 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006054 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",
6055 curproxy->id, srv->id,
6056 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006057 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006058 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6059 curproxy->id, srv->id,
6060 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006061 cfgerr++;
6062 }
Emeric Brunef42d922012-10-11 16:11:36 +02006063#ifdef X509_V_FLAG_CRL_CHECK
6064 if (srv->ssl_ctx.crl_file) {
6065 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6066
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006067 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006068 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6069 curproxy->id, srv->id,
6070 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006071 cfgerr++;
6072 }
6073 else {
6074 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6075 }
6076 }
6077#endif
6078 }
6079
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006080 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6081 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6082 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006083 if (srv->ssl_ctx.ciphers &&
6084 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006085 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6086 curproxy->id, srv->id,
6087 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006088 cfgerr++;
6089 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006090
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006091#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006092 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006093 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006094 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6095 curproxy->id, srv->id,
6096 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6097 cfgerr++;
6098 }
6099#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006100#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6101 if (srv->ssl_ctx.npn_str)
6102 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6103#endif
6104#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6105 if (srv->ssl_ctx.alpn_str)
6106 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6107#endif
6108
Emeric Brun94324a42012-10-11 14:00:19 +02006109
6110 return cfgerr;
6111}
6112
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006113/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006114 * be NULL, in which case nothing is done. Returns the number of errors
6115 * encountered.
6116 */
Willy Tarreau03209342016-12-22 17:08:28 +01006117int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006118{
6119 struct ebmb_node *node;
6120 struct sni_ctx *sni;
6121 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006122 int errcode = 0;
6123 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006124
Willy Tarreaufce03112015-01-15 21:32:40 +01006125 /* Automatic memory computations need to know we use SSL there */
6126 global.ssl_used_frontend = 1;
6127
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006128 /* Make sure openssl opens /dev/urandom before the chroot */
6129 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006130 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006131 err++;
6132 }
6133 /* Create initial_ctx used to start the ssl connection before do switchctx */
6134 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006135 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006136 /* It should not be necessary to call this function, but it's
6137 necessary first to check and move all initialisation related
6138 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006139 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006140 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006141 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006142 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006143
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006144 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006145 while (node) {
6146 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006147 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6148 /* only initialize the CTX on its first occurrence and
6149 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006150 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006151 node = ebmb_next(node);
6152 }
6153
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006154 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006155 while (node) {
6156 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006157 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006158 /* only initialize the CTX on its first occurrence and
6159 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006160 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6161 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006162 node = ebmb_next(node);
6163 }
William Lallemand8b453912019-11-21 15:48:10 +01006164
6165 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006166 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006167 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006168 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006169 err++;
6170 }
6171
6172 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006173 return err;
6174}
6175
Willy Tarreau55d37912016-12-21 23:38:39 +01006176/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6177 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6178 * alerts are directly emitted since the rest of the stack does it below.
6179 */
6180int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6181{
6182 struct proxy *px = bind_conf->frontend;
6183 int alloc_ctx;
6184 int err;
6185
6186 if (!bind_conf->is_ssl) {
6187 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006188 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6189 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006190 }
6191 return 0;
6192 }
6193 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006194 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006195 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6196 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006197 }
6198 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006199 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6200 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006201 return -1;
6202 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006203 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006204 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006205 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006206 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006207 sizeof(*sh_ssl_sess_tree),
6208 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006209 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006210 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6211 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");
6212 else
6213 ha_alert("Unable to allocate SSL session cache.\n");
6214 return -1;
6215 }
6216 /* free block callback */
6217 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6218 /* init the root tree within the extra space */
6219 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6220 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006221 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006222 err = 0;
6223 /* initialize all certificate contexts */
6224 err += ssl_sock_prepare_all_ctx(bind_conf);
6225
6226 /* initialize CA variables if the certificates generation is enabled */
6227 err += ssl_sock_load_ca(bind_conf);
6228
6229 return -err;
6230}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006231
6232/* release ssl context allocated for servers. */
6233void ssl_sock_free_srv_ctx(struct server *srv)
6234{
Olivier Houchardc7566002018-11-20 23:33:50 +01006235#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6236 if (srv->ssl_ctx.alpn_str)
6237 free(srv->ssl_ctx.alpn_str);
6238#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006239#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006240 if (srv->ssl_ctx.npn_str)
6241 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006242#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006243 if (srv->ssl_ctx.ctx)
6244 SSL_CTX_free(srv->ssl_ctx.ctx);
6245}
6246
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006247/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006248 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6249 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006250void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006251{
6252 struct ebmb_node *node, *back;
6253 struct sni_ctx *sni;
6254
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006255 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006256 while (node) {
6257 sni = ebmb_entry(node, struct sni_ctx, name);
6258 back = ebmb_next(node);
6259 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006260 SSL_CTX_free(sni->ctx);
6261 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006262 ssl_sock_free_ssl_conf(sni->conf);
6263 free(sni->conf);
6264 sni->conf = NULL;
6265 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006266 free(sni);
6267 node = back;
6268 }
6269
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006270 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006271 while (node) {
6272 sni = ebmb_entry(node, struct sni_ctx, name);
6273 back = ebmb_next(node);
6274 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006275 SSL_CTX_free(sni->ctx);
6276 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006277 ssl_sock_free_ssl_conf(sni->conf);
6278 free(sni->conf);
6279 sni->conf = NULL;
6280 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006281 free(sni);
6282 node = back;
6283 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006284 SSL_CTX_free(bind_conf->initial_ctx);
6285 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006286 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006287 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006288 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006289}
6290
Willy Tarreau795cdab2016-12-22 17:30:54 +01006291/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6292void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6293{
6294 ssl_sock_free_ca(bind_conf);
6295 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006296 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006297 free(bind_conf->ca_sign_file);
6298 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006299 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006300 free(bind_conf->keys_ref->filename);
6301 free(bind_conf->keys_ref->tlskeys);
6302 LIST_DEL(&bind_conf->keys_ref->list);
6303 free(bind_conf->keys_ref);
6304 }
6305 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006306 bind_conf->ca_sign_pass = NULL;
6307 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006308}
6309
Christopher Faulet31af49d2015-06-09 17:29:50 +02006310/* Load CA cert file and private key used to generate certificates */
6311int
Willy Tarreau03209342016-12-22 17:08:28 +01006312ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006313{
Willy Tarreau03209342016-12-22 17:08:28 +01006314 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006315 FILE *fp;
6316 X509 *cacert = NULL;
6317 EVP_PKEY *capkey = NULL;
6318 int err = 0;
6319
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006320 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006321 return err;
6322
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006323#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006324 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006325 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006326 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006327 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006328 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006329#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006330
Christopher Faulet31af49d2015-06-09 17:29:50 +02006331 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006332 ha_alert("Proxy '%s': cannot enable certificate generation, "
6333 "no CA certificate File configured at [%s:%d].\n",
6334 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006335 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006336 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006337
6338 /* read in the CA certificate */
6339 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006340 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6341 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006342 goto load_error;
6343 }
6344 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006345 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6346 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006347 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006348 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006349 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006350 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006351 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6352 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006353 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006354 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006355
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006356 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006357 bind_conf->ca_sign_cert = cacert;
6358 bind_conf->ca_sign_pkey = capkey;
6359 return err;
6360
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006361 read_error:
6362 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006363 if (capkey) EVP_PKEY_free(capkey);
6364 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006365 load_error:
6366 bind_conf->generate_certs = 0;
6367 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006368 return err;
6369}
6370
6371/* Release CA cert and private key used to generate certificated */
6372void
6373ssl_sock_free_ca(struct bind_conf *bind_conf)
6374{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006375 if (bind_conf->ca_sign_pkey)
6376 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6377 if (bind_conf->ca_sign_cert)
6378 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006379 bind_conf->ca_sign_pkey = NULL;
6380 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006381}
6382
Emeric Brun46591952012-05-18 15:47:34 +02006383/*
6384 * This function is called if SSL * context is not yet allocated. The function
6385 * is designed to be called before any other data-layer operation and sets the
6386 * handshake flag on the connection. It is safe to call it multiple times.
6387 * It returns 0 on success and -1 in error case.
6388 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006389static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006390{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006391 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006392 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006393 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006394 return 0;
6395
Willy Tarreau3c728722014-01-23 13:50:42 +01006396 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006397 return 0;
6398
Olivier Houchard66ab4982019-02-26 18:37:15 +01006399 ctx = pool_alloc(ssl_sock_ctx_pool);
6400 if (!ctx) {
6401 conn->err_code = CO_ER_SSL_NO_MEM;
6402 return -1;
6403 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006404 ctx->wait_event.tasklet = tasklet_new();
6405 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006406 conn->err_code = CO_ER_SSL_NO_MEM;
6407 pool_free(ssl_sock_ctx_pool, ctx);
6408 return -1;
6409 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006410 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6411 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006412 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006413 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006414 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006415 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006416 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006417 ctx->xprt_st = 0;
6418 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006419
6420 /* Only work with sockets for now, this should be adapted when we'll
6421 * add QUIC support.
6422 */
6423 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006424 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006425 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6426 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006427 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006428
Willy Tarreau20879a02012-12-03 16:32:10 +01006429 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6430 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006431 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006432 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006433
Emeric Brun46591952012-05-18 15:47:34 +02006434 /* If it is in client mode initiate SSL session
6435 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006436 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006437 int may_retry = 1;
6438
6439 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006440 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006441 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6442 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006443 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006444 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006445 goto retry_connect;
6446 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006447 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006448 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006449 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006450 ctx->bio = BIO_new(ha_meth);
6451 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006452 SSL_free(ctx->ssl);
6453 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006454 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006455 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006456 goto retry_connect;
6457 }
Emeric Brun55476152014-11-12 17:35:37 +01006458 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006459 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006460 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006461 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006462 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006463
Evan Broderbe554312013-06-27 00:05:25 -07006464 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006465 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6466 SSL_free(ctx->ssl);
6467 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006468 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006469 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006470 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006471 goto retry_connect;
6472 }
Emeric Brun55476152014-11-12 17:35:37 +01006473 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006474 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006475 }
6476
Olivier Houchard66ab4982019-02-26 18:37:15 +01006477 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006478 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6479 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6480 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 +01006481 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006482 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006483 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6484 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006485 } else if (sess) {
6486 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006487 }
6488 }
Evan Broderbe554312013-06-27 00:05:25 -07006489
Emeric Brun46591952012-05-18 15:47:34 +02006490 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006491 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006492
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006493 _HA_ATOMIC_ADD(&sslconns, 1);
6494 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006495 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006496 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006497 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006498 return 0;
6499 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006500 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006501 int may_retry = 1;
6502
6503 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006504 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006505 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6506 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006507 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006508 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006509 goto retry_accept;
6510 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006511 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006512 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006513 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006514 ctx->bio = BIO_new(ha_meth);
6515 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006516 SSL_free(ctx->ssl);
6517 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006518 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006519 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006520 goto retry_accept;
6521 }
Emeric Brun55476152014-11-12 17:35:37 +01006522 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006523 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006524 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006525 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006526 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006527
Emeric Brune1f38db2012-09-03 20:36:47 +02006528 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006529 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6530 SSL_free(ctx->ssl);
6531 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006532 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006533 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006534 goto retry_accept;
6535 }
Emeric Brun55476152014-11-12 17:35:37 +01006536 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006537 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006538 }
6539
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006540#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6541 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6542 b_alloc(&ctx->early_buf);
6543 SSL_set_max_early_data(ctx->ssl,
6544 /* Only allow early data if we managed to allocate
6545 * a buffer.
6546 */
6547 (!b_is_null(&ctx->early_buf)) ?
6548 global.tune.bufsize - global.tune.maxrewrite : 0);
6549 }
6550#endif
6551
Olivier Houchard66ab4982019-02-26 18:37:15 +01006552 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006553
Emeric Brun46591952012-05-18 15:47:34 +02006554 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006555 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006556#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006557 conn->flags |= CO_FL_EARLY_SSL_HS;
6558#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006559
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006560 _HA_ATOMIC_ADD(&sslconns, 1);
6561 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006562 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006563 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006564 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006565 return 0;
6566 }
6567 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006568 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006569err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006570 if (ctx && ctx->wait_event.tasklet)
6571 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006572 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006573 return -1;
6574}
6575
6576
6577/* This is the callback which is used when an SSL handshake is pending. It
6578 * updates the FD status if it wants some polling before being called again.
6579 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6580 * otherwise it returns non-zero and removes itself from the connection's
6581 * flags (the bit is provided in <flag> by the caller).
6582 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006583static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006584{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006585 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006586 int ret;
6587
Willy Tarreau3c728722014-01-23 13:50:42 +01006588 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006589 return 0;
6590
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006591 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006592 goto out_error;
6593
Willy Tarreau5db847a2019-05-09 14:13:35 +02006594#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006595 /*
6596 * Check if we have early data. If we do, we have to read them
6597 * before SSL_do_handshake() is called, And there's no way to
6598 * detect early data, except to try to read them
6599 */
6600 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006601 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006602
Olivier Houchard54907bb2019-12-19 15:02:39 +01006603 while (1) {
6604 ret = SSL_read_early_data(ctx->ssl,
6605 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6606 &read_data);
6607 if (ret == SSL_READ_EARLY_DATA_ERROR)
6608 goto check_error;
6609 if (read_data > 0) {
6610 conn->flags |= CO_FL_EARLY_DATA;
6611 b_add(&ctx->early_buf, read_data);
6612 }
6613 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6614 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6615 if (!b_data(&ctx->early_buf))
6616 b_free(&ctx->early_buf);
6617 break;
6618 }
6619 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006620 }
6621#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006622 /* If we use SSL_do_handshake to process a reneg initiated by
6623 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6624 * Usually SSL_write and SSL_read are used and process implicitly
6625 * the reneg handshake.
6626 * Here we use SSL_peek as a workaround for reneg.
6627 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006628 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006629 char c;
6630
Olivier Houchard66ab4982019-02-26 18:37:15 +01006631 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006632 if (ret <= 0) {
6633 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006634 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006635
Emeric Brun674b7432012-11-08 19:21:55 +01006636 if (ret == SSL_ERROR_WANT_WRITE) {
6637 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006638 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006639 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006640 return 0;
6641 }
6642 else if (ret == SSL_ERROR_WANT_READ) {
6643 /* handshake may have been completed but we have
6644 * no more data to read.
6645 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006646 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006647 ret = 1;
6648 goto reneg_ok;
6649 }
6650 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006651 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006652 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006653 return 0;
6654 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006655#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006656 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006657 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006658 return 0;
6659 }
6660#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006661 else if (ret == SSL_ERROR_SYSCALL) {
6662 /* if errno is null, then connection was successfully established */
6663 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6664 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006665 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006666#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6667 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006668 conn->err_code = CO_ER_SSL_HANDSHAKE;
6669#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006670 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006671#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006672 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006673 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006674 empty_handshake = state == TLS_ST_BEFORE;
6675#else
Lukas Tribus49799162019-07-08 14:29:15 +02006676 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6677 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006678#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006679 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006680 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006681 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006682 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6683 else
6684 conn->err_code = CO_ER_SSL_EMPTY;
6685 }
6686 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006687 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006688 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6689 else
6690 conn->err_code = CO_ER_SSL_ABORT;
6691 }
6692 }
6693 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006694 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006695 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006696 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006697 conn->err_code = CO_ER_SSL_HANDSHAKE;
6698 }
Lukas Tribus49799162019-07-08 14:29:15 +02006699#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006700 }
Emeric Brun674b7432012-11-08 19:21:55 +01006701 goto out_error;
6702 }
6703 else {
6704 /* Fail on all other handshake errors */
6705 /* Note: OpenSSL may leave unread bytes in the socket's
6706 * buffer, causing an RST to be emitted upon close() on
6707 * TCP sockets. We first try to drain possibly pending
6708 * data to avoid this as much as possible.
6709 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006710 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006711 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006712 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006713 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006714 goto out_error;
6715 }
6716 }
6717 /* read some data: consider handshake completed */
6718 goto reneg_ok;
6719 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006720 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006721check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006722 if (ret != 1) {
6723 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006724 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006725
6726 if (ret == SSL_ERROR_WANT_WRITE) {
6727 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006728 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006729 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006730 return 0;
6731 }
6732 else if (ret == SSL_ERROR_WANT_READ) {
6733 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006734 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006735 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6736 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006737 return 0;
6738 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006739#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006740 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006741 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006742 return 0;
6743 }
6744#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006745 else if (ret == SSL_ERROR_SYSCALL) {
6746 /* if errno is null, then connection was successfully established */
6747 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6748 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006749 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006750#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6751 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006752 conn->err_code = CO_ER_SSL_HANDSHAKE;
6753#else
6754 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006755#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006756 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006757 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006758 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006759#else
Lukas Tribus49799162019-07-08 14:29:15 +02006760 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6761 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006762#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006763 if (empty_handshake) {
6764 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006765 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006766 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6767 else
6768 conn->err_code = CO_ER_SSL_EMPTY;
6769 }
6770 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006771 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006772 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6773 else
6774 conn->err_code = CO_ER_SSL_ABORT;
6775 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006776 }
6777 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006778 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006779 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6780 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006781 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006782 }
Lukas Tribus49799162019-07-08 14:29:15 +02006783#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006784 }
Willy Tarreau89230192012-09-28 20:22:13 +02006785 goto out_error;
6786 }
Emeric Brun46591952012-05-18 15:47:34 +02006787 else {
6788 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006789 /* Note: OpenSSL may leave unread bytes in the socket's
6790 * buffer, causing an RST to be emitted upon close() on
6791 * TCP sockets. We first try to drain possibly pending
6792 * data to avoid this as much as possible.
6793 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006794 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006795 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006796 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006797 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006798 goto out_error;
6799 }
6800 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006801#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006802 else {
6803 /*
6804 * If the server refused the early data, we have to send a
6805 * 425 to the client, as we no longer have the data to sent
6806 * them again.
6807 */
6808 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006809 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006810 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6811 goto out_error;
6812 }
6813 }
6814 }
6815#endif
6816
Emeric Brun46591952012-05-18 15:47:34 +02006817
Emeric Brun674b7432012-11-08 19:21:55 +01006818reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006819
Willy Tarreau5db847a2019-05-09 14:13:35 +02006820#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006821 /* ASYNC engine API doesn't support moving read/write
6822 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006823 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006824 */
6825 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006826 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006827#endif
Emeric Brun46591952012-05-18 15:47:34 +02006828 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006829 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006830 if (objt_server(conn->target)) {
6831 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6832 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6833 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006834 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006835 else {
6836 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6837 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6838 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6839 }
Emeric Brun46591952012-05-18 15:47:34 +02006840 }
6841
6842 /* The connection is now established at both layers, it's time to leave */
6843 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6844 return 1;
6845
6846 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006847 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006848 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006849 ERR_clear_error();
6850
Emeric Brun9fa89732012-10-04 17:09:56 +02006851 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006852 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6853 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6854 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006855 }
6856
Emeric Brun46591952012-05-18 15:47:34 +02006857 /* Fail on all other handshake errors */
6858 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006859 if (!conn->err_code)
6860 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006861 return 0;
6862}
6863
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006864/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6865 * event subscriber <es> is not allowed to change from a previous call as long
6866 * as at least one event is still subscribed. The <event_type> must only be a
6867 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6868 * unless the transport layer was already released.
6869 */
6870static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006871{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006872 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006873
Olivier Houchard0ff28652019-06-24 18:57:39 +02006874 if (!ctx)
6875 return -1;
6876
Willy Tarreau113d52b2020-01-10 09:20:26 +01006877 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006878 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006879
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006880 ctx->subs = es;
6881 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006882
6883 /* we may have to subscribe to lower layers for new events */
6884 event_type &= ~ctx->wait_event.events;
6885 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6886 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006887 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006888}
6889
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006890/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6891 * The <es> pointer is not allowed to differ from the one passed to the
6892 * subscribe() call. It always returns zero.
6893 */
6894static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006895{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006896 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006897
Willy Tarreau113d52b2020-01-10 09:20:26 +01006898 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006899 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006900
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006901 es->events &= ~event_type;
6902 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006903 ctx->subs = NULL;
6904
6905 /* If we subscribed, and we're not doing the handshake,
6906 * then we subscribed because the upper layer asked for it,
6907 * as the upper layer is no longer interested, we can
6908 * unsubscribe too.
6909 */
6910 event_type &= ctx->wait_event.events;
6911 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6912 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006913
6914 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006915}
6916
Olivier Houchard2e055482019-05-27 19:50:12 +02006917/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6918 * Returns 0 on success, and non-zero on failure.
6919 */
6920static 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)
6921{
6922 struct ssl_sock_ctx *ctx = xprt_ctx;
6923
6924 if (oldxprt_ops != NULL)
6925 *oldxprt_ops = ctx->xprt;
6926 if (oldxprt_ctx != NULL)
6927 *oldxprt_ctx = ctx->xprt_ctx;
6928 ctx->xprt = toadd_ops;
6929 ctx->xprt_ctx = toadd_ctx;
6930 return 0;
6931}
6932
Olivier Houchard5149b592019-05-23 17:47:36 +02006933/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6934 * return 0, otherwise just call the remove_xprt method from the underlying
6935 * XPRT.
6936 */
6937static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6938{
6939 struct ssl_sock_ctx *ctx = xprt_ctx;
6940
6941 if (ctx->xprt_ctx == toremove_ctx) {
6942 ctx->xprt_ctx = newctx;
6943 ctx->xprt = newops;
6944 return 0;
6945 }
6946 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6947}
6948
Olivier Houchardea8dd942019-05-20 14:02:16 +02006949static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6950{
6951 struct ssl_sock_ctx *ctx = context;
6952
6953 /* First if we're doing an handshake, try that */
6954 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6955 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6956 /* If we had an error, or the handshake is done and I/O is available,
6957 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006958 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006959 * we can't be sure conn_fd_handler() will be called again.
6960 */
6961 if ((ctx->conn->flags & CO_FL_ERROR) ||
6962 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6963 int ret = 0;
6964 int woke = 0;
6965
6966 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006967 if (ctx->subs) {
6968 tasklet_wakeup(ctx->subs->tasklet);
6969 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006970 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006971 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006972 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006973
Olivier Houchardea8dd942019-05-20 14:02:16 +02006974 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006975 * upper layers know. If we have no mux, create it,
6976 * and once we have a mux, call its wake method if we didn't
6977 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006978 */
6979 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006980 if (!ctx->conn->mux)
6981 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006982 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6983 ctx->conn->mux->wake(ctx->conn);
6984 return NULL;
6985 }
6986 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006987#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6988 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006989 else if (b_data(&ctx->early_buf) && ctx->subs &&
6990 ctx->subs->events & SUB_RETRY_RECV) {
6991 tasklet_wakeup(ctx->subs->tasklet);
6992 ctx->subs->events &= ~SUB_RETRY_RECV;
6993 if (!ctx->subs->events)
6994 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006995 }
6996#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006997 return NULL;
6998}
6999
Emeric Brun46591952012-05-18 15:47:34 +02007000/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01007001 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02007002 * buffer wraps, in which case a second call may be performed. The connection's
7003 * flags are updated with whatever special event is detected (error, read0,
7004 * empty). The caller is responsible for taking care of those events and
7005 * avoiding the call if inappropriate. The function does not call the
7006 * connection's polling update function, so the caller is responsible for this.
7007 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007008static 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 +02007009{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007010 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02007011 ssize_t ret;
7012 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02007013
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007014 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007015 goto out_error;
7016
Olivier Houchard54907bb2019-12-19 15:02:39 +01007017#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
7018 if (b_data(&ctx->early_buf)) {
7019 try = b_contig_space(buf);
7020 if (try > b_data(&ctx->early_buf))
7021 try = b_data(&ctx->early_buf);
7022 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
7023 b_add(buf, try);
7024 b_del(&ctx->early_buf, try);
7025 if (b_data(&ctx->early_buf) == 0)
7026 b_free(&ctx->early_buf);
7027 return try;
7028 }
7029#endif
7030
Willy Tarreau911db9b2020-01-23 16:27:54 +01007031 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007032 /* a handshake was requested */
7033 return 0;
7034
Emeric Brun46591952012-05-18 15:47:34 +02007035 /* read the largest possible block. For this, we perform only one call
7036 * to recv() unless the buffer wraps and we exactly fill the first hunk,
7037 * in which case we accept to do it once again. A new attempt is made on
7038 * EINTR too.
7039 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01007040 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007041
Willy Tarreau591d4452018-06-15 17:21:00 +02007042 try = b_contig_space(buf);
7043 if (!try)
7044 break;
7045
Willy Tarreauabf08d92014-01-14 11:31:27 +01007046 if (try > count)
7047 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007048
Olivier Houchard66ab4982019-02-26 18:37:15 +01007049 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007050
Emeric Brune1f38db2012-09-03 20:36:47 +02007051 if (conn->flags & CO_FL_ERROR) {
7052 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007053 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007054 }
Emeric Brun46591952012-05-18 15:47:34 +02007055 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007056 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007057 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007058 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007059 }
Emeric Brun46591952012-05-18 15:47:34 +02007060 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007061 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007062 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007063 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007064 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007065 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007066#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007067 /* Async mode can be re-enabled, because we're leaving data state.*/
7068 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007069 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007070#endif
Emeric Brun46591952012-05-18 15:47:34 +02007071 break;
7072 }
7073 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007074 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007075 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7076 SUB_RETRY_RECV,
7077 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007078 /* handshake is running, and it may need to re-enable read */
7079 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007080#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007081 /* Async mode can be re-enabled, because we're leaving data state.*/
7082 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007083 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007084#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007085 break;
7086 }
Emeric Brun46591952012-05-18 15:47:34 +02007087 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007088 } else if (ret == SSL_ERROR_ZERO_RETURN)
7089 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007090 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7091 * stack before shutting down the connection for
7092 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007093 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7094 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007095 /* otherwise it's a real error */
7096 goto out_error;
7097 }
7098 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007099 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007100 return done;
7101
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007102 clear_ssl_error:
7103 /* Clear openssl global errors stack */
7104 ssl_sock_dump_errors(conn);
7105 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007106 read0:
7107 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007108 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007109
Emeric Brun46591952012-05-18 15:47:34 +02007110 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007111 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007112 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007113 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007114 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007115 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007116}
7117
7118
Willy Tarreau787db9a2018-06-14 18:31:46 +02007119/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7120 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7121 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007122 * Only one call to send() is performed, unless the buffer wraps, in which case
7123 * a second call may be performed. The connection's flags are updated with
7124 * whatever special event is detected (error, empty). The caller is responsible
7125 * for taking care of those events and avoiding the call if inappropriate. The
7126 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007127 * is responsible for this. The buffer's output is not adjusted, it's up to the
7128 * caller to take care of this. It's up to the caller to update the buffer's
7129 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007130 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007131static 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 +02007132{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007133 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007134 ssize_t ret;
7135 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007136
7137 done = 0;
7138
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007139 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007140 goto out_error;
7141
Willy Tarreau911db9b2020-01-23 16:27:54 +01007142 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007143 /* a handshake was requested */
7144 return 0;
7145
7146 /* send the largest possible block. For this we perform only one call
7147 * to send() unless the buffer wraps and we exactly fill the first hunk,
7148 * in which case we accept to do it once again.
7149 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007150 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007151#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007152 size_t written_data;
7153#endif
7154
Willy Tarreau787db9a2018-06-14 18:31:46 +02007155 try = b_contig_data(buf, done);
7156 if (try > count)
7157 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007158
Willy Tarreau7bed9452014-02-02 02:00:24 +01007159 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007160 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007161 global_ssl.max_record && try > global_ssl.max_record) {
7162 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007163 }
7164 else {
7165 /* we need to keep the information about the fact that
7166 * we're not limiting the upcoming send(), because if it
7167 * fails, we'll have to retry with at least as many data.
7168 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007169 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007170 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007171
Willy Tarreau5db847a2019-05-09 14:13:35 +02007172#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007173 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007174 unsigned int max_early;
7175
Olivier Houchard522eea72017-11-03 16:27:47 +01007176 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007177 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007178 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007179 if (SSL_get0_session(ctx->ssl))
7180 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007181 else
7182 max_early = 0;
7183 }
7184
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007185 if (try + ctx->sent_early_data > max_early) {
7186 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007187 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007188 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007189 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007190 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007191 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007192 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007193 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007194 if (ret == 1) {
7195 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007196 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007197 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007198 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007199 /* Initiate the handshake, now */
7200 tasklet_wakeup(ctx->wait_event.tasklet);
7201 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007202
Olivier Houchardc2aae742017-09-22 18:26:28 +02007203 }
7204
7205 } else
7206#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007207 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007208
Emeric Brune1f38db2012-09-03 20:36:47 +02007209 if (conn->flags & CO_FL_ERROR) {
7210 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007211 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007212 }
Emeric Brun46591952012-05-18 15:47:34 +02007213 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007214 /* A send succeeded, so we can consider ourself connected */
7215 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007216 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007217 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007218 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007219 }
7220 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007221 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007222
Emeric Brun46591952012-05-18 15:47:34 +02007223 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007224 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007225 /* handshake is running, and it may need to re-enable write */
7226 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007227 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007228#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007229 /* Async mode can be re-enabled, because we're leaving data state.*/
7230 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007231 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007232#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007233 break;
7234 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007235
Emeric Brun46591952012-05-18 15:47:34 +02007236 break;
7237 }
7238 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007239 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007240 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007241 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7242 SUB_RETRY_RECV,
7243 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007244#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007245 /* Async mode can be re-enabled, because we're leaving data state.*/
7246 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007247 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007248#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007249 break;
7250 }
Emeric Brun46591952012-05-18 15:47:34 +02007251 goto out_error;
7252 }
7253 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007254 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007255 return done;
7256
7257 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007258 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007259 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007260 ERR_clear_error();
7261
Emeric Brun46591952012-05-18 15:47:34 +02007262 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007263 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007264}
7265
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007266static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007267
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007268 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007269
Olivier Houchardea8dd942019-05-20 14:02:16 +02007270
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007271 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007272 if (ctx->wait_event.events != 0)
7273 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7274 ctx->wait_event.events,
7275 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007276 if (ctx->subs) {
7277 ctx->subs->events = 0;
7278 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007279 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007280
Olivier Houchard692c1d02019-05-23 18:41:47 +02007281 if (ctx->xprt->close)
7282 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007283#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007284 if (global_ssl.async) {
7285 OSSL_ASYNC_FD all_fd[32], afd;
7286 size_t num_all_fds = 0;
7287 int i;
7288
Olivier Houchard66ab4982019-02-26 18:37:15 +01007289 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007290 if (num_all_fds > 32) {
7291 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7292 return;
7293 }
7294
Olivier Houchard66ab4982019-02-26 18:37:15 +01007295 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007296
7297 /* If an async job is pending, we must try to
7298 to catch the end using polling before calling
7299 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007300 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007301 for (i=0 ; i < num_all_fds ; i++) {
7302 /* switch on an handler designed to
7303 * handle the SSL_free
7304 */
7305 afd = all_fd[i];
7306 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007307 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007308 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007309 /* To ensure that the fd cache won't be used
7310 * and we'll catch a real RD event.
7311 */
7312 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007313 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007314 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007315 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007316 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007317 return;
7318 }
Emeric Brun3854e012017-05-17 20:42:48 +02007319 /* Else we can remove the fds from the fdtab
7320 * and call SSL_free.
7321 * note: we do a fd_remove and not a delete
7322 * because the fd is owned by the engine.
7323 * the engine is responsible to close
7324 */
7325 for (i=0 ; i < num_all_fds ; i++)
7326 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007327 }
7328#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007329 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007330 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007331 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007332 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007333 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007334 }
Emeric Brun46591952012-05-18 15:47:34 +02007335}
7336
7337/* This function tries to perform a clean shutdown on an SSL connection, and in
7338 * any case, flags the connection as reusable if no handshake was in progress.
7339 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007340static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007341{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007342 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007343
Willy Tarreau911db9b2020-01-23 16:27:54 +01007344 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007345 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007346 if (!clean)
7347 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007348 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007349 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007350 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007351 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007352 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007353 ERR_clear_error();
7354 }
Emeric Brun46591952012-05-18 15:47:34 +02007355}
7356
William Lallemandd4f946c2019-12-05 10:26:40 +01007357/* fill a buffer with the algorithm and size of a public key */
7358static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007359{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007360 int bits = 0;
7361 int sig = TLSEXT_signature_anonymous;
7362 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007363 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007364
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007365 pkey = X509_get_pubkey(crt);
7366 if (pkey) {
7367 bits = EVP_PKEY_bits(pkey);
7368 switch(EVP_PKEY_base_id(pkey)) {
7369 case EVP_PKEY_RSA:
7370 sig = TLSEXT_signature_rsa;
7371 break;
7372 case EVP_PKEY_EC:
7373 sig = TLSEXT_signature_ecdsa;
7374 break;
7375 case EVP_PKEY_DSA:
7376 sig = TLSEXT_signature_dsa;
7377 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007378 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007379 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007380 }
7381
7382 switch(sig) {
7383 case TLSEXT_signature_rsa:
7384 len = chunk_printf(out, "RSA%d", bits);
7385 break;
7386 case TLSEXT_signature_ecdsa:
7387 len = chunk_printf(out, "EC%d", bits);
7388 break;
7389 case TLSEXT_signature_dsa:
7390 len = chunk_printf(out, "DSA%d", bits);
7391 break;
7392 default:
7393 return 0;
7394 }
7395 if (len < 0)
7396 return 0;
7397 return 1;
7398}
7399
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007400/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007401int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7402{
7403 struct ssl_sock_ctx *ctx;
7404 X509 *crt;
7405
7406 if (!ssl_sock_is_ssl(conn))
7407 return 0;
7408
7409 ctx = conn->xprt_ctx;
7410
7411 crt = SSL_get_certificate(ctx->ssl);
7412 if (!crt)
7413 return 0;
7414
7415 return cert_get_pkey_algo(crt, out);
7416}
7417
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007418/* used for ppv2 cert signature (can be used for logging) */
7419const char *ssl_sock_get_cert_sig(struct connection *conn)
7420{
Christopher Faulet82004142019-09-10 10:12:03 +02007421 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007422
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007423 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7424 X509 *crt;
7425
7426 if (!ssl_sock_is_ssl(conn))
7427 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007428 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007429 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007430 if (!crt)
7431 return NULL;
7432 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7433 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7434}
7435
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007436/* used for ppv2 authority */
7437const char *ssl_sock_get_sni(struct connection *conn)
7438{
7439#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007440 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007441
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007442 if (!ssl_sock_is_ssl(conn))
7443 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007444 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007445 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007446#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007447 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007448#endif
7449}
7450
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007451/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007452const char *ssl_sock_get_cipher_name(struct connection *conn)
7453{
Christopher Faulet82004142019-09-10 10:12:03 +02007454 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007455
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007456 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007457 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007458 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007459 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007460}
7461
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007462/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007463const char *ssl_sock_get_proto_version(struct connection *conn)
7464{
Christopher Faulet82004142019-09-10 10:12:03 +02007465 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007466
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007467 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007468 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007469 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007470 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007471}
7472
Willy Tarreau8d598402012-10-22 17:58:39 +02007473/* Extract a serial from a cert, and copy it to a chunk.
7474 * Returns 1 if serial is found and copied, 0 if no serial found and
7475 * -1 if output is not large enough.
7476 */
7477static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007478ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007479{
7480 ASN1_INTEGER *serial;
7481
7482 serial = X509_get_serialNumber(crt);
7483 if (!serial)
7484 return 0;
7485
7486 if (out->size < serial->length)
7487 return -1;
7488
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007489 memcpy(out->area, serial->data, serial->length);
7490 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007491 return 1;
7492}
7493
Emeric Brun43e79582014-10-29 19:03:26 +01007494/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007495 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7496 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007497 */
7498static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007499ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007500{
7501 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007502 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007503
7504 len =i2d_X509(crt, NULL);
7505 if (len <= 0)
7506 return 1;
7507
7508 if (out->size < len)
7509 return -1;
7510
7511 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007512 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007513 return 1;
7514}
7515
Emeric Brunce5ad802012-10-22 14:11:22 +02007516
Willy Tarreau83061a82018-07-13 11:56:34 +02007517/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007518 * Returns 1 if serial is found and copied, 0 if no valid time found
7519 * and -1 if output is not large enough.
7520 */
7521static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007522ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007523{
7524 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7525 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7526
7527 if (gentm->length < 12)
7528 return 0;
7529 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7530 return 0;
7531 if (out->size < gentm->length-2)
7532 return -1;
7533
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007534 memcpy(out->area, gentm->data+2, gentm->length-2);
7535 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007536 return 1;
7537 }
7538 else if (tm->type == V_ASN1_UTCTIME) {
7539 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7540
7541 if (utctm->length < 10)
7542 return 0;
7543 if (utctm->data[0] >= 0x35)
7544 return 0;
7545 if (out->size < utctm->length)
7546 return -1;
7547
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007548 memcpy(out->area, utctm->data, utctm->length);
7549 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007550 return 1;
7551 }
7552
7553 return 0;
7554}
7555
Emeric Brun87855892012-10-17 17:39:35 +02007556/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7557 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7558 */
7559static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007560ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7561 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007562{
7563 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007564 ASN1_OBJECT *obj;
7565 ASN1_STRING *data;
7566 const unsigned char *data_ptr;
7567 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007568 int i, j, n;
7569 int cur = 0;
7570 const char *s;
7571 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007572 int name_count;
7573
7574 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007575
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007576 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007577 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007578 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007579 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007580 else
7581 j = i;
7582
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007583 ne = X509_NAME_get_entry(a, j);
7584 obj = X509_NAME_ENTRY_get_object(ne);
7585 data = X509_NAME_ENTRY_get_data(ne);
7586 data_ptr = ASN1_STRING_get0_data(data);
7587 data_len = ASN1_STRING_length(data);
7588 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007589 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007590 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007591 s = tmp;
7592 }
7593
7594 if (chunk_strcasecmp(entry, s) != 0)
7595 continue;
7596
7597 if (pos < 0)
7598 cur--;
7599 else
7600 cur++;
7601
7602 if (cur != pos)
7603 continue;
7604
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007605 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007606 return -1;
7607
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007608 memcpy(out->area, data_ptr, data_len);
7609 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007610 return 1;
7611 }
7612
7613 return 0;
7614
William Lallemandd4f946c2019-12-05 10:26:40 +01007615}
7616
7617/*
7618 * Extract and format the DNS SAN extensions and copy result into a chuink
7619 * Return 0;
7620 */
7621#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7622static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7623{
7624 int i;
7625 char *str;
7626 STACK_OF(GENERAL_NAME) *names = NULL;
7627
7628 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7629 if (names) {
7630 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7631 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7632 if (i > 0)
7633 chunk_appendf(out, ", ");
7634 if (name->type == GEN_DNS) {
7635 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7636 chunk_appendf(out, "DNS:%s", str);
7637 OPENSSL_free(str);
7638 }
7639 }
7640 }
7641 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7642 }
7643 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007644}
William Lallemandd4f946c2019-12-05 10:26:40 +01007645#endif
Emeric Brun87855892012-10-17 17:39:35 +02007646
Elliot Otchet71f82972020-01-15 08:12:14 -05007647/*
7648 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7649 * Currently supports rfc2253 for returning LDAP V3 DNs.
7650 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7651 */
7652static int
7653ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7654{
7655 BIO *bio = NULL;
7656 int ret = 0;
7657 int data_len = 0;
7658
7659 if (chunk_strcmp(format, "rfc2253") == 0) {
7660 bio = BIO_new(BIO_s_mem());
7661 if (bio == NULL)
7662 goto out;
7663
7664 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7665 goto out;
7666
7667 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7668 goto out;
7669
7670 out->data = data_len;
7671
7672 ret = 1;
7673 }
7674out:
7675 if (bio)
7676 BIO_free(bio);
7677 return ret;
7678}
7679
Emeric Brun87855892012-10-17 17:39:35 +02007680/* Extract and format full DN from a X509_NAME and copy result into a chunk
7681 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7682 */
7683static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007684ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007685{
7686 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007687 ASN1_OBJECT *obj;
7688 ASN1_STRING *data;
7689 const unsigned char *data_ptr;
7690 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007691 int i, n, ln;
7692 int l = 0;
7693 const char *s;
7694 char *p;
7695 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007696 int name_count;
7697
7698
7699 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007700
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007701 out->data = 0;
7702 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007703 for (i = 0; i < name_count; i++) {
7704 ne = X509_NAME_get_entry(a, i);
7705 obj = X509_NAME_ENTRY_get_object(ne);
7706 data = X509_NAME_ENTRY_get_data(ne);
7707 data_ptr = ASN1_STRING_get0_data(data);
7708 data_len = ASN1_STRING_length(data);
7709 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007710 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007711 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007712 s = tmp;
7713 }
7714 ln = strlen(s);
7715
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007716 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007717 if (l > out->size)
7718 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007719 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007720
7721 *(p++)='/';
7722 memcpy(p, s, ln);
7723 p += ln;
7724 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007725 memcpy(p, data_ptr, data_len);
7726 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007727 }
7728
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007729 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007730 return 0;
7731
7732 return 1;
7733}
7734
Olivier Houchardab28a322018-12-21 19:45:40 +01007735void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7736{
7737#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007738 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007739
Olivier Houcharde488ea82019-06-28 14:10:33 +02007740 if (!ssl_sock_is_ssl(conn))
7741 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007742 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007743 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007744#endif
7745}
7746
Willy Tarreau119a4082016-12-22 21:58:38 +01007747/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7748 * to disable SNI.
7749 */
Willy Tarreau63076412015-07-10 11:33:32 +02007750void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7751{
7752#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007753 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007754
Willy Tarreau119a4082016-12-22 21:58:38 +01007755 char *prev_name;
7756
Willy Tarreau63076412015-07-10 11:33:32 +02007757 if (!ssl_sock_is_ssl(conn))
7758 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007759 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007760
Willy Tarreau119a4082016-12-22 21:58:38 +01007761 /* if the SNI changes, we must destroy the reusable context so that a
7762 * new connection will present a new SNI. As an optimization we could
7763 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7764 * server.
7765 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007766 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007767 if ((!prev_name && hostname) ||
7768 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007769 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007770
Olivier Houchard66ab4982019-02-26 18:37:15 +01007771 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007772#endif
7773}
7774
Emeric Brun0abf8362014-06-24 18:26:41 +02007775/* Extract peer certificate's common name into the chunk dest
7776 * Returns
7777 * the len of the extracted common name
7778 * or 0 if no CN found in DN
7779 * or -1 on error case (i.e. no peer certificate)
7780 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007781int ssl_sock_get_remote_common_name(struct connection *conn,
7782 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007783{
Christopher Faulet82004142019-09-10 10:12:03 +02007784 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007785 X509 *crt = NULL;
7786 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007787 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007788 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007789 .area = (char *)&find_cn,
7790 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007791 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007792 int result = -1;
David Safb76832014-05-08 23:42:08 -04007793
7794 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007795 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007796 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007797
7798 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007799 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007800 if (!crt)
7801 goto out;
7802
7803 name = X509_get_subject_name(crt);
7804 if (!name)
7805 goto out;
David Safb76832014-05-08 23:42:08 -04007806
Emeric Brun0abf8362014-06-24 18:26:41 +02007807 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7808out:
David Safb76832014-05-08 23:42:08 -04007809 if (crt)
7810 X509_free(crt);
7811
7812 return result;
7813}
7814
Dave McCowan328fb582014-07-30 10:39:13 -04007815/* returns 1 if client passed a certificate for this session, 0 if not */
7816int ssl_sock_get_cert_used_sess(struct connection *conn)
7817{
Christopher Faulet82004142019-09-10 10:12:03 +02007818 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007819 X509 *crt = NULL;
7820
7821 if (!ssl_sock_is_ssl(conn))
7822 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007823 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007824
7825 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007826 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007827 if (!crt)
7828 return 0;
7829
7830 X509_free(crt);
7831 return 1;
7832}
7833
7834/* returns 1 if client passed a certificate for this connection, 0 if not */
7835int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007836{
Christopher Faulet82004142019-09-10 10:12:03 +02007837 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007838
David Safb76832014-05-08 23:42:08 -04007839 if (!ssl_sock_is_ssl(conn))
7840 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007841 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007842 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007843}
7844
7845/* returns result from SSL verify */
7846unsigned int ssl_sock_get_verify_result(struct connection *conn)
7847{
Christopher Faulet82004142019-09-10 10:12:03 +02007848 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007849
David Safb76832014-05-08 23:42:08 -04007850 if (!ssl_sock_is_ssl(conn))
7851 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007852 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007853 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007854}
7855
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007856/* Returns the application layer protocol name in <str> and <len> when known.
7857 * Zero is returned if the protocol name was not found, otherwise non-zero is
7858 * returned. The string is allocated in the SSL context and doesn't have to be
7859 * freed by the caller. NPN is also checked if available since older versions
7860 * of openssl (1.0.1) which are more common in field only support this one.
7861 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007862static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007863{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007864#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7865 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007866 struct ssl_sock_ctx *ctx = xprt_ctx;
7867 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007868 return 0;
7869
7870 *str = NULL;
7871
7872#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007873 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007874 if (*str)
7875 return 1;
7876#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007877#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007878 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007879 if (*str)
7880 return 1;
7881#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007882#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007883 return 0;
7884}
7885
Willy Tarreau7875d092012-09-10 08:20:03 +02007886/***** Below are some sample fetching functions for ACL/patterns *****/
7887
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007888static int
7889smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7890{
7891 struct connection *conn;
7892
7893 conn = objt_conn(smp->sess->origin);
7894 if (!conn || conn->xprt != &ssl_sock)
7895 return 0;
7896
7897 smp->flags = 0;
7898 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007899#ifdef OPENSSL_IS_BORINGSSL
7900 {
7901 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7902 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7903 SSL_early_data_accepted(ctx->ssl));
7904 }
7905#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007906 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007907 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007908#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007909 return 1;
7910}
7911
Emeric Brune64aef12012-09-21 13:15:06 +02007912/* boolean, returns true if client cert was present */
7913static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007914smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007915{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007916 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007917 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007918
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007919 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007920 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007921 return 0;
7922
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007923 ctx = conn->xprt_ctx;
7924
Willy Tarreau911db9b2020-01-23 16:27:54 +01007925 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007926 smp->flags |= SMP_F_MAY_CHANGE;
7927 return 0;
7928 }
7929
7930 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007931 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007932 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007933
7934 return 1;
7935}
7936
Emeric Brun43e79582014-10-29 19:03:26 +01007937/* binary, returns a certificate in a binary chunk (der/raw).
7938 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7939 * should be use.
7940 */
7941static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007942smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007943{
7944 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7945 X509 *crt = NULL;
7946 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007947 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007948 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007949 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007950
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007951 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007952 if (!conn || conn->xprt != &ssl_sock)
7953 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007954 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007955
Willy Tarreau911db9b2020-01-23 16:27:54 +01007956 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007957 smp->flags |= SMP_F_MAY_CHANGE;
7958 return 0;
7959 }
7960
7961 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007962 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007963 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007964 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007965
7966 if (!crt)
7967 goto out;
7968
7969 smp_trash = get_trash_chunk();
7970 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7971 goto out;
7972
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007973 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007974 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007975 ret = 1;
7976out:
7977 /* SSL_get_peer_certificate, it increase X509 * ref count */
7978 if (cert_peer && crt)
7979 X509_free(crt);
7980 return ret;
7981}
7982
Emeric Brunba841a12014-04-30 17:05:08 +02007983/* binary, returns serial of certificate in a binary chunk.
7984 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7985 * should be use.
7986 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007987static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007988smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007989{
Emeric Brunba841a12014-04-30 17:05:08 +02007990 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007991 X509 *crt = NULL;
7992 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007993 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007994 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007995 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007996
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007997 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007998 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007999 return 0;
8000
Olivier Houchard66ab4982019-02-26 18:37:15 +01008001 ctx = conn->xprt_ctx;
8002
Willy Tarreau911db9b2020-01-23 16:27:54 +01008003 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02008004 smp->flags |= SMP_F_MAY_CHANGE;
8005 return 0;
8006 }
8007
Emeric Brunba841a12014-04-30 17:05:08 +02008008 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008009 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008010 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008011 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008012
Willy Tarreau8d598402012-10-22 17:58:39 +02008013 if (!crt)
8014 goto out;
8015
Willy Tarreau47ca5452012-12-23 20:22:19 +01008016 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02008017 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
8018 goto out;
8019
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008020 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008021 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02008022 ret = 1;
8023out:
Emeric Brunba841a12014-04-30 17:05:08 +02008024 /* SSL_get_peer_certificate, it increase X509 * ref count */
8025 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02008026 X509_free(crt);
8027 return ret;
8028}
Emeric Brune64aef12012-09-21 13:15:06 +02008029
Emeric Brunba841a12014-04-30 17:05:08 +02008030/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
8031 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8032 * should be use.
8033 */
James Votha051b4a2013-05-14 20:37:59 +02008034static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008035smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02008036{
Emeric Brunba841a12014-04-30 17:05:08 +02008037 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02008038 X509 *crt = NULL;
8039 const EVP_MD *digest;
8040 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01008041 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008042 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008043 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008044 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02008045
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008046 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008047 if (!conn || conn->xprt != &ssl_sock)
8048 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008049 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008050
Willy Tarreau911db9b2020-01-23 16:27:54 +01008051 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008052 smp->flags |= SMP_F_MAY_CHANGE;
8053 return 0;
8054 }
8055
Emeric Brunba841a12014-04-30 17:05:08 +02008056 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008057 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008058 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008059 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008060 if (!crt)
8061 goto out;
8062
8063 smp_trash = get_trash_chunk();
8064 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008065 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8066 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008067 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008068 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008069 ret = 1;
8070out:
Emeric Brunba841a12014-04-30 17:05:08 +02008071 /* SSL_get_peer_certificate, it increase X509 * ref count */
8072 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008073 X509_free(crt);
8074 return ret;
8075}
8076
Emeric Brunba841a12014-04-30 17:05:08 +02008077/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8078 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8079 * should be use.
8080 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008081static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008082smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008083{
Emeric Brunba841a12014-04-30 17:05:08 +02008084 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008085 X509 *crt = NULL;
8086 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008087 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008088 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008089 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008090
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008091 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008092 if (!conn || conn->xprt != &ssl_sock)
8093 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008094 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008095
Willy Tarreau911db9b2020-01-23 16:27:54 +01008096 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008097 smp->flags |= SMP_F_MAY_CHANGE;
8098 return 0;
8099 }
8100
Emeric Brunba841a12014-04-30 17:05:08 +02008101 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008102 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008103 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008104 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008105 if (!crt)
8106 goto out;
8107
Willy Tarreau47ca5452012-12-23 20:22:19 +01008108 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008109 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008110 goto out;
8111
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008112 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008113 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008114 ret = 1;
8115out:
Emeric Brunba841a12014-04-30 17:05:08 +02008116 /* SSL_get_peer_certificate, it increase X509 * ref count */
8117 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008118 X509_free(crt);
8119 return ret;
8120}
8121
Emeric Brunba841a12014-04-30 17:05:08 +02008122/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8123 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8124 * should be use.
8125 */
Emeric Brun87855892012-10-17 17:39:35 +02008126static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008127smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008128{
Emeric Brunba841a12014-04-30 17:05:08 +02008129 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008130 X509 *crt = NULL;
8131 X509_NAME *name;
8132 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008133 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008134 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008135 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008136
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008137 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008138 if (!conn || conn->xprt != &ssl_sock)
8139 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008140 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008141
Willy Tarreau911db9b2020-01-23 16:27:54 +01008142 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008143 smp->flags |= SMP_F_MAY_CHANGE;
8144 return 0;
8145 }
8146
Emeric Brunba841a12014-04-30 17:05:08 +02008147 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008148 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008149 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008150 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008151 if (!crt)
8152 goto out;
8153
8154 name = X509_get_issuer_name(crt);
8155 if (!name)
8156 goto out;
8157
Willy Tarreau47ca5452012-12-23 20:22:19 +01008158 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008159 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008160 int pos = 1;
8161
8162 if (args[1].type == ARGT_SINT)
8163 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008164
8165 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8166 goto out;
8167 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008168 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8169 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8170 goto out;
8171 }
Emeric Brun87855892012-10-17 17:39:35 +02008172 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8173 goto out;
8174
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008175 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008176 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008177 ret = 1;
8178out:
Emeric Brunba841a12014-04-30 17:05:08 +02008179 /* SSL_get_peer_certificate, it increase X509 * ref count */
8180 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008181 X509_free(crt);
8182 return ret;
8183}
8184
Emeric Brunba841a12014-04-30 17:05:08 +02008185/* string, returns notbefore date in ASN1_UTCTIME format.
8186 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8187 * should be use.
8188 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008189static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008190smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008191{
Emeric Brunba841a12014-04-30 17:05:08 +02008192 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008193 X509 *crt = NULL;
8194 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008195 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008196 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008197 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008198
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008199 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008200 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008201 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008202 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008203
Willy Tarreau911db9b2020-01-23 16:27:54 +01008204 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008205 smp->flags |= SMP_F_MAY_CHANGE;
8206 return 0;
8207 }
8208
Emeric Brunba841a12014-04-30 17:05:08 +02008209 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008210 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008211 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008212 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008213 if (!crt)
8214 goto out;
8215
Willy Tarreau47ca5452012-12-23 20:22:19 +01008216 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008217 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008218 goto out;
8219
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008220 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008221 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008222 ret = 1;
8223out:
Emeric Brunba841a12014-04-30 17:05:08 +02008224 /* SSL_get_peer_certificate, it increase X509 * ref count */
8225 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008226 X509_free(crt);
8227 return ret;
8228}
8229
Emeric Brunba841a12014-04-30 17:05:08 +02008230/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8231 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8232 * should be use.
8233 */
Emeric Brun87855892012-10-17 17:39:35 +02008234static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008235smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008236{
Emeric Brunba841a12014-04-30 17:05:08 +02008237 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008238 X509 *crt = NULL;
8239 X509_NAME *name;
8240 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008241 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008242 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008243 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008244
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008245 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008246 if (!conn || conn->xprt != &ssl_sock)
8247 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008248 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008249
Willy Tarreau911db9b2020-01-23 16:27:54 +01008250 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008251 smp->flags |= SMP_F_MAY_CHANGE;
8252 return 0;
8253 }
8254
Emeric Brunba841a12014-04-30 17:05:08 +02008255 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008256 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008257 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008258 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008259 if (!crt)
8260 goto out;
8261
8262 name = X509_get_subject_name(crt);
8263 if (!name)
8264 goto out;
8265
Willy Tarreau47ca5452012-12-23 20:22:19 +01008266 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008267 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008268 int pos = 1;
8269
8270 if (args[1].type == ARGT_SINT)
8271 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008272
8273 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8274 goto out;
8275 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008276 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8277 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8278 goto out;
8279 }
Emeric Brun87855892012-10-17 17:39:35 +02008280 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8281 goto out;
8282
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008283 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008284 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008285 ret = 1;
8286out:
Emeric Brunba841a12014-04-30 17:05:08 +02008287 /* SSL_get_peer_certificate, it increase X509 * ref count */
8288 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008289 X509_free(crt);
8290 return ret;
8291}
Emeric Brun9143d372012-12-20 15:44:16 +01008292
8293/* integer, returns true if current session use a client certificate */
8294static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008295smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008296{
8297 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008298 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008299 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008300
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008301 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008302 if (!conn || conn->xprt != &ssl_sock)
8303 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008304 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008305
Willy Tarreau911db9b2020-01-23 16:27:54 +01008306 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008307 smp->flags |= SMP_F_MAY_CHANGE;
8308 return 0;
8309 }
8310
8311 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008312 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008313 if (crt) {
8314 X509_free(crt);
8315 }
8316
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008317 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008318 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008319 return 1;
8320}
8321
Emeric Brunba841a12014-04-30 17:05:08 +02008322/* integer, returns the certificate version
8323 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8324 * should be use.
8325 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008326static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008327smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008328{
Emeric Brunba841a12014-04-30 17:05:08 +02008329 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008330 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008331 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008332 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008333
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008334 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008335 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008336 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008337 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008338
Willy Tarreau911db9b2020-01-23 16:27:54 +01008339 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008340 smp->flags |= SMP_F_MAY_CHANGE;
8341 return 0;
8342 }
8343
Emeric Brunba841a12014-04-30 17:05:08 +02008344 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008345 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008346 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008347 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008348 if (!crt)
8349 return 0;
8350
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008351 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008352 /* SSL_get_peer_certificate increase X509 * ref count */
8353 if (cert_peer)
8354 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008355 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008356
8357 return 1;
8358}
8359
Emeric Brunba841a12014-04-30 17:05:08 +02008360/* string, returns the certificate's signature algorithm.
8361 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8362 * should be use.
8363 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008364static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008365smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008366{
Emeric Brunba841a12014-04-30 17:05:08 +02008367 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008368 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008369 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008370 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008371 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008372 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008373
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008374 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008375 if (!conn || conn->xprt != &ssl_sock)
8376 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008377 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008378
Willy Tarreau911db9b2020-01-23 16:27:54 +01008379 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008380 smp->flags |= SMP_F_MAY_CHANGE;
8381 return 0;
8382 }
8383
Emeric Brunba841a12014-04-30 17:05:08 +02008384 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008385 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008386 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008387 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008388 if (!crt)
8389 return 0;
8390
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008391 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8392 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008393
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008394 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8395 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008396 /* SSL_get_peer_certificate increase X509 * ref count */
8397 if (cert_peer)
8398 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008399 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008400 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008401
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008402 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008403 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008404 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008405 /* SSL_get_peer_certificate increase X509 * ref count */
8406 if (cert_peer)
8407 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008408
8409 return 1;
8410}
8411
Emeric Brunba841a12014-04-30 17:05:08 +02008412/* string, returns the certificate's key algorithm.
8413 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8414 * should be use.
8415 */
Emeric Brun521a0112012-10-22 12:22:55 +02008416static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008417smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008418{
Emeric Brunba841a12014-04-30 17:05:08 +02008419 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008420 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008421 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008422 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008423 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008424 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008425
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008426 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008427 if (!conn || conn->xprt != &ssl_sock)
8428 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008429 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008430
Willy Tarreau911db9b2020-01-23 16:27:54 +01008431 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008432 smp->flags |= SMP_F_MAY_CHANGE;
8433 return 0;
8434 }
8435
Emeric Brunba841a12014-04-30 17:05:08 +02008436 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008437 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008438 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008439 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008440 if (!crt)
8441 return 0;
8442
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008443 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8444 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008445
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008446 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8447 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008448 /* SSL_get_peer_certificate increase X509 * ref count */
8449 if (cert_peer)
8450 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008451 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008452 }
Emeric Brun521a0112012-10-22 12:22:55 +02008453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008454 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008455 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008456 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008457 if (cert_peer)
8458 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008459
8460 return 1;
8461}
8462
Emeric Brun645ae792014-04-30 14:21:06 +02008463/* boolean, returns true if front conn. transport layer is SSL.
8464 * This function is also usable on backend conn if the fetch keyword 5th
8465 * char is 'b'.
8466 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008467static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008468smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008469{
Emeric Bruneb8def92018-02-19 15:59:48 +01008470 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8471 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008472
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008473 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008474 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008475 return 1;
8476}
8477
Emeric Brun2525b6b2012-10-18 15:59:43 +02008478/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008479static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008480smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008481{
8482#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008483 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008484 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008485
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008486 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008487 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008488 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008489 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008490 return 1;
8491#else
8492 return 0;
8493#endif
8494}
8495
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008496/* boolean, returns true if client session has been resumed.
8497 * This function is also usable on backend conn if the fetch keyword 5th
8498 * char is 'b'.
8499 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008500static int
8501smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8502{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008503 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8504 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008505 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008506
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008507
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008508 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008509 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008510 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008511 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008512 return 1;
8513}
8514
Emeric Brun645ae792014-04-30 14:21:06 +02008515/* string, returns the used cipher if front conn. transport layer is SSL.
8516 * This function is also usable on backend conn if the fetch keyword 5th
8517 * char is 'b'.
8518 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008519static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008520smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008521{
Emeric Bruneb8def92018-02-19 15:59:48 +01008522 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8523 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008524 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008525
Willy Tarreaube508f12016-03-10 11:47:01 +01008526 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008527 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008528 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008529 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008530
Olivier Houchard66ab4982019-02-26 18:37:15 +01008531 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008532 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008533 return 0;
8534
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008535 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008536 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008537 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008538
8539 return 1;
8540}
8541
Emeric Brun645ae792014-04-30 14:21:06 +02008542/* integer, returns the algoritm's keysize if front conn. transport layer
8543 * is SSL.
8544 * This function is also usable on backend conn if the fetch keyword 5th
8545 * char is 'b'.
8546 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008547static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008548smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008549{
Emeric Bruneb8def92018-02-19 15:59:48 +01008550 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8551 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008552 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008553 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008554
Emeric Brun589fcad2012-10-16 14:13:26 +02008555 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008556 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008557 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008558 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008559
Olivier Houchard66ab4982019-02-26 18:37:15 +01008560 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008561 return 0;
8562
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008563 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008564 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008565
8566 return 1;
8567}
8568
Emeric Brun645ae792014-04-30 14:21:06 +02008569/* integer, returns the used keysize if front conn. transport layer is SSL.
8570 * This function is also usable on backend conn if the fetch keyword 5th
8571 * char is 'b'.
8572 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008573static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008574smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008575{
Emeric Bruneb8def92018-02-19 15:59:48 +01008576 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8577 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008578 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008579
Emeric Brun589fcad2012-10-16 14:13:26 +02008580 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008581 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8582 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008583 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008584
Olivier Houchard66ab4982019-02-26 18:37:15 +01008585 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008586 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008587 return 0;
8588
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008589 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008590
8591 return 1;
8592}
8593
Bernard Spil13c53f82018-02-15 13:34:58 +01008594#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008595static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008596smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008597{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008598 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008599 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008600 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008601
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008602 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008603 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008604
Olivier Houchard6b77f492018-11-22 18:18:29 +01008605 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8606 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008607 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8608 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008609 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008610
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008611 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008612 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008613 (const unsigned char **)&smp->data.u.str.area,
8614 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008615
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008616 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008617 return 0;
8618
Willy Tarreau105599c2020-02-25 08:59:23 +01008619 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008620 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008621}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008622#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008623
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008624#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008625static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008626smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008627{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008628 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008629 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008630 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008631
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008632 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008633 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008634
Olivier Houchard6b77f492018-11-22 18:18:29 +01008635 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8636 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8637
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008638 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008639 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008640 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008641
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008642 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008643 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008644 (const unsigned char **)&smp->data.u.str.area,
8645 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008647 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008648 return 0;
8649
Willy Tarreau105599c2020-02-25 08:59:23 +01008650 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008651 return 1;
8652}
8653#endif
8654
Emeric Brun645ae792014-04-30 14:21:06 +02008655/* string, returns the used protocol if front conn. transport layer is SSL.
8656 * This function is also usable on backend conn if the fetch keyword 5th
8657 * char is 'b'.
8658 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008659static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008660smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008661{
Emeric Bruneb8def92018-02-19 15:59:48 +01008662 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8663 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008664 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008665
Emeric Brun589fcad2012-10-16 14:13:26 +02008666 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008667 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8668 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008669 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008670
Olivier Houchard66ab4982019-02-26 18:37:15 +01008671 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008672 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008673 return 0;
8674
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008675 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008676 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008677 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008678
8679 return 1;
8680}
8681
Willy Tarreau87b09662015-04-03 00:22:06 +02008682/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008683 * This function is also usable on backend conn if the fetch keyword 5th
8684 * char is 'b'.
8685 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008686#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008687static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008688smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008689{
Emeric Bruneb8def92018-02-19 15:59:48 +01008690 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8691 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008692 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008693 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008694 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008695
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008696 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008697 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008698
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008699 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8700 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008701 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008702
Olivier Houchard66ab4982019-02-26 18:37:15 +01008703 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008704 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008705 return 0;
8706
Willy Tarreau105599c2020-02-25 08:59:23 +01008707 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008708 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008709 return 0;
8710
Willy Tarreau105599c2020-02-25 08:59:23 +01008711 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008712 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008713}
Patrick Hemmer41966772018-04-28 19:15:48 -04008714#endif
8715
Emeric Brunfe68f682012-10-16 14:59:28 +02008716
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008717#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008718static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008719smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8720{
8721 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8722 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8723 struct buffer *data;
8724 struct ssl_sock_ctx *ctx;
8725
8726 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8727 return 0;
8728 ctx = conn->xprt_ctx;
8729
8730 data = get_trash_chunk();
8731 if (kw[7] == 'c')
8732 data->data = SSL_get_client_random(ctx->ssl,
8733 (unsigned char *) data->area,
8734 data->size);
8735 else
8736 data->data = SSL_get_server_random(ctx->ssl,
8737 (unsigned char *) data->area,
8738 data->size);
8739 if (!data->data)
8740 return 0;
8741
8742 smp->flags = 0;
8743 smp->data.type = SMP_T_BIN;
8744 smp->data.u.str = *data;
8745
8746 return 1;
8747}
8748
8749static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008750smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8751{
8752 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8753 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8754 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008755 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008756 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008757
8758 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8759 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008760 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008761
Olivier Houchard66ab4982019-02-26 18:37:15 +01008762 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008763 if (!ssl_sess)
8764 return 0;
8765
8766 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008767 data->data = SSL_SESSION_get_master_key(ssl_sess,
8768 (unsigned char *) data->area,
8769 data->size);
8770 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008771 return 0;
8772
8773 smp->flags = 0;
8774 smp->data.type = SMP_T_BIN;
8775 smp->data.u.str = *data;
8776
8777 return 1;
8778}
8779#endif
8780
Patrick Hemmer41966772018-04-28 19:15:48 -04008781#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008783smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008784{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008785 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008786 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008787
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008788 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008789 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008790
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008791 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008792 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8793 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008794 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008795
Olivier Houchard66ab4982019-02-26 18:37:15 +01008796 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008797 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008798 return 0;
8799
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008800 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008801 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008802}
Patrick Hemmer41966772018-04-28 19:15:48 -04008803#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008804
David Sc1ad52e2014-04-08 18:48:47 -04008805static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008806smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8807{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008808 struct connection *conn;
8809 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008810 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008811
8812 conn = objt_conn(smp->sess->origin);
8813 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8814 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008815 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008816
Olivier Houchard66ab4982019-02-26 18:37:15 +01008817 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008818 if (!capture)
8819 return 0;
8820
8821 smp->flags = SMP_F_CONST;
8822 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008823 smp->data.u.str.area = capture->ciphersuite;
8824 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008825 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008826}
8827
8828static int
8829smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8830{
Willy Tarreau83061a82018-07-13 11:56:34 +02008831 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008832
8833 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8834 return 0;
8835
8836 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008837 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008838 smp->data.type = SMP_T_BIN;
8839 smp->data.u.str = *data;
8840 return 1;
8841}
8842
8843static int
8844smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8845{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008846 struct connection *conn;
8847 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008848 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008849
8850 conn = objt_conn(smp->sess->origin);
8851 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8852 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008853 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008854
Olivier Houchard66ab4982019-02-26 18:37:15 +01008855 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008856 if (!capture)
8857 return 0;
8858
8859 smp->data.type = SMP_T_SINT;
8860 smp->data.u.sint = capture->xxh64;
8861 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008862}
8863
8864static int
8865smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8866{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008867#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008868 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008869 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008870
8871 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8872 return 0;
8873
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008874 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008875 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008876 const char *str;
8877 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008878 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008879 uint16_t id = (bin[0] << 8) | bin[1];
8880#if defined(OPENSSL_IS_BORINGSSL)
8881 cipher = SSL_get_cipher_by_value(id);
8882#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008883 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008884 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8885 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008886#endif
8887 str = SSL_CIPHER_get_name(cipher);
8888 if (!str || strcmp(str, "(NONE)") == 0)
8889 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008890 else
8891 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8892 }
8893 smp->data.type = SMP_T_STR;
8894 smp->data.u.str = *data;
8895 return 1;
8896#else
8897 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8898#endif
8899}
8900
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008901#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008902static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008903smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008904{
Emeric Bruneb8def92018-02-19 15:59:48 +01008905 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8906 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008907 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008908 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008909 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008910
8911 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008912 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8913 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008914 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008915
Willy Tarreau911db9b2020-01-23 16:27:54 +01008916 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008917 smp->flags |= SMP_F_MAY_CHANGE;
8918 return 0;
8919 }
8920
8921 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008922 if (!SSL_session_reused(ctx->ssl))
8923 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008924 finished_trash->area,
8925 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008926 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008927 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008928 finished_trash->area,
8929 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008930
8931 if (!finished_len)
8932 return 0;
8933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008934 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008935 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008936 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008937
8938 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008939}
Patrick Hemmer41966772018-04-28 19:15:48 -04008940#endif
David Sc1ad52e2014-04-08 18:48:47 -04008941
Emeric Brun2525b6b2012-10-18 15:59:43 +02008942/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008943static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008944smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008945{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008946 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008947 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008948
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008949 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008950 if (!conn || conn->xprt != &ssl_sock)
8951 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008952 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008953
Willy Tarreau911db9b2020-01-23 16:27:54 +01008954 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008955 smp->flags = SMP_F_MAY_CHANGE;
8956 return 0;
8957 }
8958
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008959 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008960 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008961 smp->flags = 0;
8962
8963 return 1;
8964}
8965
Emeric Brun2525b6b2012-10-18 15:59:43 +02008966/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008967static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008968smp_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 +02008969{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008970 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008971 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008972
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008973 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008974 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008975 return 0;
8976
Willy Tarreau911db9b2020-01-23 16:27:54 +01008977 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008978 smp->flags = SMP_F_MAY_CHANGE;
8979 return 0;
8980 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008981 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008982
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008983 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008984 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008985 smp->flags = 0;
8986
8987 return 1;
8988}
8989
Emeric Brun2525b6b2012-10-18 15:59:43 +02008990/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008991static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008992smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008993{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008994 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008995 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008996
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008997 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008998 if (!conn || conn->xprt != &ssl_sock)
8999 return 0;
9000
Willy Tarreau911db9b2020-01-23 16:27:54 +01009001 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02009002 smp->flags = SMP_F_MAY_CHANGE;
9003 return 0;
9004 }
9005
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009006 ctx = conn->xprt_ctx;
9007
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009008 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009009 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009010 smp->flags = 0;
9011
9012 return 1;
9013}
9014
Emeric Brun2525b6b2012-10-18 15:59:43 +02009015/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009016static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009017smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009018{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009019 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009020 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009021
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009022 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009023 if (!conn || conn->xprt != &ssl_sock)
9024 return 0;
9025
Willy Tarreau911db9b2020-01-23 16:27:54 +01009026 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009027 smp->flags = SMP_F_MAY_CHANGE;
9028 return 0;
9029 }
9030
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009031 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009032 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009033 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009034
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009035 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009036 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009037 smp->flags = 0;
9038
9039 return 1;
9040}
9041
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009042/* for ca-file and ca-verify-file */
9043static 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 +02009044{
9045 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009046 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009047 return ERR_ALERT | ERR_FATAL;
9048 }
9049
Willy Tarreauef934602016-12-22 23:12:01 +01009050 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009051 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009052 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009053 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009054
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009055 if (!ssl_store_load_locations_file(*ca_file_p)) {
9056 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009057 return ERR_ALERT | ERR_FATAL;
9058 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009059 return 0;
9060}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009061
9062/* parse the "ca-file" bind keyword */
9063static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9064{
9065 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9066}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009067static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9068{
9069 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9070}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009071
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009072/* parse the "ca-verify-file" bind keyword */
9073static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9074{
9075 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9076}
9077static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9078{
9079 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9080}
9081
Christopher Faulet31af49d2015-06-09 17:29:50 +02009082/* parse the "ca-sign-file" bind keyword */
9083static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9084{
9085 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009086 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009087 return ERR_ALERT | ERR_FATAL;
9088 }
9089
Willy Tarreauef934602016-12-22 23:12:01 +01009090 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9091 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009092 else
9093 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9094
9095 return 0;
9096}
9097
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009098/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009099static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9100{
9101 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009102 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009103 return ERR_ALERT | ERR_FATAL;
9104 }
9105 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9106 return 0;
9107}
9108
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009109/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009110static 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 +02009111{
9112 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009113 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009114 return ERR_ALERT | ERR_FATAL;
9115 }
9116
Emeric Brun76d88952012-10-05 15:47:31 +02009117 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009118 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009119 return 0;
9120}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009121static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9122{
9123 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9124}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009125
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009126#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009127/* parse the "ciphersuites" bind keyword */
9128static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9129{
9130 if (!*args[cur_arg + 1]) {
9131 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9132 return ERR_ALERT | ERR_FATAL;
9133 }
9134
9135 free(conf->ciphersuites);
9136 conf->ciphersuites = strdup(args[cur_arg + 1]);
9137 return 0;
9138}
9139static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9140{
9141 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9142}
9143#endif
9144
Willy Tarreaubbc91962019-10-16 16:42:19 +02009145/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009146static 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 +02009147{
Willy Tarreau38011032013-08-13 16:59:39 +02009148 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009149
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009150 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009151 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009152 return ERR_ALERT | ERR_FATAL;
9153 }
9154
Willy Tarreauef934602016-12-22 23:12:01 +01009155 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9156 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009157 memprintf(err, "'%s' : path too long", args[cur_arg]);
9158 return ERR_ALERT | ERR_FATAL;
9159 }
Willy Tarreauef934602016-12-22 23:12:01 +01009160 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009161 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009162 }
9163
Willy Tarreaubbc91962019-10-16 16:42:19 +02009164 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009165}
9166
Willy Tarreaubbc91962019-10-16 16:42:19 +02009167/* 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 +01009168static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9169{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009170 int err_code;
9171
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009172 if (!*args[cur_arg + 1]) {
9173 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9174 return ERR_ALERT | ERR_FATAL;
9175 }
9176
William Lallemand6be66ec2020-03-06 22:26:32 +01009177 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009178 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009179 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009180
Willy Tarreaubbc91962019-10-16 16:42:19 +02009181 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009182}
9183
Emeric Brunfb510ea2012-10-05 12:00:26 +02009184/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009185static 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 +02009186{
Emeric Brun051cdab2012-10-02 19:25:50 +02009187#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009188 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009189 return ERR_ALERT | ERR_FATAL;
9190#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009191 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009192 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009193 return ERR_ALERT | ERR_FATAL;
9194 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009195
Willy Tarreauef934602016-12-22 23:12:01 +01009196 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9197 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009198 else
9199 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009200
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009201 if (!ssl_store_load_locations_file(conf->crl_file)) {
9202 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9203 return ERR_ALERT | ERR_FATAL;
9204 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009205 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009206#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009207}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009208static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9209{
9210 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9211}
Emeric Brun2b58d042012-09-20 17:10:03 +02009212
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009213/* parse the "curves" bind keyword keyword */
9214static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9215{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009216#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009217 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009218 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009219 return ERR_ALERT | ERR_FATAL;
9220 }
9221 conf->curves = strdup(args[cur_arg + 1]);
9222 return 0;
9223#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009224 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009225 return ERR_ALERT | ERR_FATAL;
9226#endif
9227}
9228static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9229{
9230 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9231}
9232
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009233/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009234static 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 +02009235{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009236#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009237 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009238 return ERR_ALERT | ERR_FATAL;
9239#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009240 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 +02009241 return ERR_ALERT | ERR_FATAL;
9242#else
9243 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009244 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009245 return ERR_ALERT | ERR_FATAL;
9246 }
9247
9248 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009249
9250 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009251#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009252}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009253static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9254{
9255 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9256}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009257
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009258/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009259static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9260{
9261 int code;
9262 char *p = args[cur_arg + 1];
9263 unsigned long long *ignerr = &conf->crt_ignerr;
9264
9265 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009266 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009267 return ERR_ALERT | ERR_FATAL;
9268 }
9269
9270 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9271 ignerr = &conf->ca_ignerr;
9272
9273 if (strcmp(p, "all") == 0) {
9274 *ignerr = ~0ULL;
9275 return 0;
9276 }
9277
9278 while (p) {
9279 code = atoi(p);
9280 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009281 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9282 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009283 return ERR_ALERT | ERR_FATAL;
9284 }
9285 *ignerr |= 1ULL << code;
9286 p = strchr(p, ',');
9287 if (p)
9288 p++;
9289 }
9290
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009291 return 0;
9292}
9293
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009294/* parse tls_method_options "no-xxx" and "force-xxx" */
9295static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009296{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009297 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009298 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009299 p = strchr(arg, '-');
9300 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009301 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009302 p++;
9303 if (!strcmp(p, "sslv3"))
9304 v = CONF_SSLV3;
9305 else if (!strcmp(p, "tlsv10"))
9306 v = CONF_TLSV10;
9307 else if (!strcmp(p, "tlsv11"))
9308 v = CONF_TLSV11;
9309 else if (!strcmp(p, "tlsv12"))
9310 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009311 else if (!strcmp(p, "tlsv13"))
9312 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009313 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009314 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009315 if (!strncmp(arg, "no-", 3))
9316 methods->flags |= methodVersions[v].flag;
9317 else if (!strncmp(arg, "force-", 6))
9318 methods->min = methods->max = v;
9319 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009320 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009321 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009322 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009323 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009324 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009325}
9326
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009327static 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 +02009328{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009329 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009330}
9331
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009332static 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 +02009333{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009334 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9335}
9336
9337/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9338static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9339{
9340 uint16_t i, v = 0;
9341 char *argv = args[cur_arg + 1];
9342 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009343 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009344 return ERR_ALERT | ERR_FATAL;
9345 }
9346 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9347 if (!strcmp(argv, methodVersions[i].name))
9348 v = i;
9349 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009350 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009351 return ERR_ALERT | ERR_FATAL;
9352 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009353 if (!strcmp("ssl-min-ver", args[cur_arg]))
9354 methods->min = v;
9355 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9356 methods->max = v;
9357 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009358 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009359 return ERR_ALERT | ERR_FATAL;
9360 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009361 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009362}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009363
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009364static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9365{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009366#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009367 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 +02009368#endif
9369 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9370}
9371
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009372static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9373{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009374 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009375}
9376
9377static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9378{
9379 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9380}
9381
Emeric Brun2d0c4822012-10-02 13:45:20 +02009382/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009383static 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 +02009384{
Emeric Brun89675492012-10-05 13:48:26 +02009385 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009386 return 0;
9387}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009388
Olivier Houchardc2aae742017-09-22 18:26:28 +02009389/* parse the "allow-0rtt" bind keyword */
9390static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9391{
9392 conf->early_data = 1;
9393 return 0;
9394}
9395
9396static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9397{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009398 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009399 return 0;
9400}
9401
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009402/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009403static 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 +02009404{
Bernard Spil13c53f82018-02-15 13:34:58 +01009405#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009406 char *p1, *p2;
9407
9408 if (!*args[cur_arg + 1]) {
9409 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9410 return ERR_ALERT | ERR_FATAL;
9411 }
9412
9413 free(conf->npn_str);
9414
Willy Tarreau3724da12016-02-12 17:11:12 +01009415 /* the NPN string is built as a suite of (<len> <name>)*,
9416 * so we reuse each comma to store the next <len> and need
9417 * one more for the end of the string.
9418 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009419 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009420 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009421 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9422
9423 /* replace commas with the name length */
9424 p1 = conf->npn_str;
9425 p2 = p1 + 1;
9426 while (1) {
9427 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9428 if (!p2)
9429 p2 = p1 + 1 + strlen(p1 + 1);
9430
9431 if (p2 - (p1 + 1) > 255) {
9432 *p2 = '\0';
9433 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9434 return ERR_ALERT | ERR_FATAL;
9435 }
9436
9437 *p1 = p2 - (p1 + 1);
9438 p1 = p2;
9439
9440 if (!*p2)
9441 break;
9442
9443 *(p2++) = '\0';
9444 }
9445 return 0;
9446#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009447 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009448 return ERR_ALERT | ERR_FATAL;
9449#endif
9450}
9451
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009452static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9453{
9454 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9455}
9456
Willy Tarreauab861d32013-04-02 02:30:41 +02009457/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009458static 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 +02009459{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009460#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009461 char *p1, *p2;
9462
9463 if (!*args[cur_arg + 1]) {
9464 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9465 return ERR_ALERT | ERR_FATAL;
9466 }
9467
9468 free(conf->alpn_str);
9469
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009470 /* the ALPN string is built as a suite of (<len> <name>)*,
9471 * so we reuse each comma to store the next <len> and need
9472 * one more for the end of the string.
9473 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009474 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009475 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009476 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9477
9478 /* replace commas with the name length */
9479 p1 = conf->alpn_str;
9480 p2 = p1 + 1;
9481 while (1) {
9482 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9483 if (!p2)
9484 p2 = p1 + 1 + strlen(p1 + 1);
9485
9486 if (p2 - (p1 + 1) > 255) {
9487 *p2 = '\0';
9488 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9489 return ERR_ALERT | ERR_FATAL;
9490 }
9491
9492 *p1 = p2 - (p1 + 1);
9493 p1 = p2;
9494
9495 if (!*p2)
9496 break;
9497
9498 *(p2++) = '\0';
9499 }
9500 return 0;
9501#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009502 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009503 return ERR_ALERT | ERR_FATAL;
9504#endif
9505}
9506
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009507static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9508{
9509 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9510}
9511
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009512/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009513static 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 +02009514{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009515 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009516 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009517
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009518 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9519 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009520#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009521 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9522 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9523#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009524 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009525 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9526 if (!conf->ssl_conf.ssl_methods.min)
9527 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9528 if (!conf->ssl_conf.ssl_methods.max)
9529 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009530
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009531 return 0;
9532}
9533
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009534/* parse the "prefer-client-ciphers" bind keyword */
9535static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9536{
9537 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9538 return 0;
9539}
9540
Christopher Faulet31af49d2015-06-09 17:29:50 +02009541/* parse the "generate-certificates" bind keyword */
9542static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9543{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009544#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009545 conf->generate_certs = 1;
9546#else
9547 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9548 err && *err ? *err : "");
9549#endif
9550 return 0;
9551}
9552
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009553/* parse the "strict-sni" bind keyword */
9554static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9555{
9556 conf->strict_sni = 1;
9557 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009558}
9559
9560/* parse the "tls-ticket-keys" bind keyword */
9561static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9562{
9563#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009564 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009565 int i = 0;
9566 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009567 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009568
9569 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009570 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009571 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009572 }
9573
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009574 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009575 if (keys_ref) {
9576 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009577 conf->keys_ref = keys_ref;
9578 return 0;
9579 }
9580
Christopher Faulete566f3d2019-10-21 09:55:49 +02009581 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009582 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009583 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009584 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009585 }
9586
Emeric Brun9e754772019-01-10 17:51:55 +01009587 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009588 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009589 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009590 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009591 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009592
9593 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009594 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009595 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009596 }
9597
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009598 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009599 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009600 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009601 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009602 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009603
Emeric Brun9e754772019-01-10 17:51:55 +01009604 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009605 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9606 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009607 int dec_size;
9608
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009609 /* Strip newline characters from the end */
9610 if(thisline[len - 1] == '\n')
9611 thisline[--len] = 0;
9612
9613 if(thisline[len - 1] == '\r')
9614 thisline[--len] = 0;
9615
Emeric Brun9e754772019-01-10 17:51:55 +01009616 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9617 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009618 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009619 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009620 }
Emeric Brun9e754772019-01-10 17:51:55 +01009621 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9622 keys_ref->key_size_bits = 128;
9623 }
9624 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9625 keys_ref->key_size_bits = 256;
9626 }
9627 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9628 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9629 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009630 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009631 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009632 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009633 i++;
9634 }
9635
9636 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009637 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 +02009638 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009639 }
9640
9641 fclose(f);
9642
9643 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009644 i -= 2;
9645 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009646 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009647 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009648 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009649 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009650
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009651 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9652
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009653 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009654
9655 fail:
9656 if (f)
9657 fclose(f);
9658 if (keys_ref) {
9659 free(keys_ref->filename);
9660 free(keys_ref->tlskeys);
9661 free(keys_ref);
9662 }
9663 return ERR_ALERT | ERR_FATAL;
9664
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009665#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009666 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009667 return ERR_ALERT | ERR_FATAL;
9668#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009669}
9670
Emeric Brund94b3fe2012-09-20 18:23:56 +02009671/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009672static 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 +02009673{
9674 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009675 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009676 return ERR_ALERT | ERR_FATAL;
9677 }
9678
9679 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009680 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009681 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009682 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009683 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009684 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009685 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009686 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9687 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009688 return ERR_ALERT | ERR_FATAL;
9689 }
9690
9691 return 0;
9692}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009693static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9694{
9695 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9696}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009697
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009698/* parse the "no-ca-names" bind keyword */
9699static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9700{
9701 conf->no_ca_names = 1;
9702 return 0;
9703}
9704static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9705{
9706 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9707}
9708
Willy Tarreau92faadf2012-10-10 23:04:25 +02009709/************** "server" keywords ****************/
9710
Olivier Houchardc7566002018-11-20 23:33:50 +01009711/* parse the "npn" bind keyword */
9712static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9713{
9714#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9715 char *p1, *p2;
9716
9717 if (!*args[*cur_arg + 1]) {
9718 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9719 return ERR_ALERT | ERR_FATAL;
9720 }
9721
9722 free(newsrv->ssl_ctx.npn_str);
9723
9724 /* the NPN string is built as a suite of (<len> <name>)*,
9725 * so we reuse each comma to store the next <len> and need
9726 * one more for the end of the string.
9727 */
9728 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9729 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9730 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9731 newsrv->ssl_ctx.npn_len);
9732
9733 /* replace commas with the name length */
9734 p1 = newsrv->ssl_ctx.npn_str;
9735 p2 = p1 + 1;
9736 while (1) {
9737 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9738 newsrv->ssl_ctx.npn_len - (p1 + 1));
9739 if (!p2)
9740 p2 = p1 + 1 + strlen(p1 + 1);
9741
9742 if (p2 - (p1 + 1) > 255) {
9743 *p2 = '\0';
9744 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9745 return ERR_ALERT | ERR_FATAL;
9746 }
9747
9748 *p1 = p2 - (p1 + 1);
9749 p1 = p2;
9750
9751 if (!*p2)
9752 break;
9753
9754 *(p2++) = '\0';
9755 }
9756 return 0;
9757#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009758 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009759 return ERR_ALERT | ERR_FATAL;
9760#endif
9761}
9762
Olivier Houchard92150142018-12-21 19:47:01 +01009763/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009764static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9765{
9766#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9767 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009768 char **alpn_str;
9769 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009770
Olivier Houchard92150142018-12-21 19:47:01 +01009771 if (*args[*cur_arg] == 'c') {
9772 alpn_str = &newsrv->check.alpn_str;
9773 alpn_len = &newsrv->check.alpn_len;
9774 } else {
9775 alpn_str = &newsrv->ssl_ctx.alpn_str;
9776 alpn_len = &newsrv->ssl_ctx.alpn_len;
9777
9778 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009779 if (!*args[*cur_arg + 1]) {
9780 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9781 return ERR_ALERT | ERR_FATAL;
9782 }
9783
Olivier Houchard92150142018-12-21 19:47:01 +01009784 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009785
9786 /* the ALPN string is built as a suite of (<len> <name>)*,
9787 * so we reuse each comma to store the next <len> and need
9788 * one more for the end of the string.
9789 */
Olivier Houchard92150142018-12-21 19:47:01 +01009790 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9791 *alpn_str = calloc(1, *alpn_len + 1);
9792 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009793
9794 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009795 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009796 p2 = p1 + 1;
9797 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009798 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009799 if (!p2)
9800 p2 = p1 + 1 + strlen(p1 + 1);
9801
9802 if (p2 - (p1 + 1) > 255) {
9803 *p2 = '\0';
9804 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9805 return ERR_ALERT | ERR_FATAL;
9806 }
9807
9808 *p1 = p2 - (p1 + 1);
9809 p1 = p2;
9810
9811 if (!*p2)
9812 break;
9813
9814 *(p2++) = '\0';
9815 }
9816 return 0;
9817#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009818 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009819 return ERR_ALERT | ERR_FATAL;
9820#endif
9821}
9822
Emeric Brunef42d922012-10-11 16:11:36 +02009823/* parse the "ca-file" server keyword */
9824static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9825{
9826 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009827 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009828 return ERR_ALERT | ERR_FATAL;
9829 }
9830
Willy Tarreauef934602016-12-22 23:12:01 +01009831 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9832 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009833 else
9834 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9835
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009836 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9837 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9838 return ERR_ALERT | ERR_FATAL;
9839 }
Emeric Brunef42d922012-10-11 16:11:36 +02009840 return 0;
9841}
9842
Olivier Houchard9130a962017-10-17 17:33:43 +02009843/* parse the "check-sni" server keyword */
9844static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9845{
9846 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009847 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009848 return ERR_ALERT | ERR_FATAL;
9849 }
9850
9851 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9852 if (!newsrv->check.sni) {
9853 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9854 return ERR_ALERT | ERR_FATAL;
9855 }
9856 return 0;
9857
9858}
9859
Willy Tarreau92faadf2012-10-10 23:04:25 +02009860/* parse the "check-ssl" server keyword */
9861static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9862{
9863 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009864 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9865 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009866#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009867 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9868 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9869#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009870 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009871 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9872 if (!newsrv->ssl_ctx.methods.min)
9873 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9874 if (!newsrv->ssl_ctx.methods.max)
9875 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9876
Willy Tarreau92faadf2012-10-10 23:04:25 +02009877 return 0;
9878}
9879
9880/* parse the "ciphers" server keyword */
9881static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9882{
9883 if (!*args[*cur_arg + 1]) {
9884 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9885 return ERR_ALERT | ERR_FATAL;
9886 }
9887
9888 free(newsrv->ssl_ctx.ciphers);
9889 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9890 return 0;
9891}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009892
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009893#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009894/* parse the "ciphersuites" server keyword */
9895static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9896{
9897 if (!*args[*cur_arg + 1]) {
9898 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9899 return ERR_ALERT | ERR_FATAL;
9900 }
9901
9902 free(newsrv->ssl_ctx.ciphersuites);
9903 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9904 return 0;
9905}
9906#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009907
Emeric Brunef42d922012-10-11 16:11:36 +02009908/* parse the "crl-file" server keyword */
9909static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9910{
9911#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009912 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009913 return ERR_ALERT | ERR_FATAL;
9914#else
9915 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009916 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009917 return ERR_ALERT | ERR_FATAL;
9918 }
9919
Willy Tarreauef934602016-12-22 23:12:01 +01009920 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9921 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009922 else
9923 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9924
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009925 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9926 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9927 return ERR_ALERT | ERR_FATAL;
9928 }
Emeric Brunef42d922012-10-11 16:11:36 +02009929 return 0;
9930#endif
9931}
9932
Emeric Bruna7aa3092012-10-26 12:58:00 +02009933/* parse the "crt" server keyword */
9934static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9935{
9936 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009937 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009938 return ERR_ALERT | ERR_FATAL;
9939 }
9940
Willy Tarreauef934602016-12-22 23:12:01 +01009941 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009942 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009943 else
9944 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9945
9946 return 0;
9947}
Emeric Brunef42d922012-10-11 16:11:36 +02009948
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009949/* parse the "no-check-ssl" server keyword */
9950static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9951{
9952 newsrv->check.use_ssl = 0;
9953 free(newsrv->ssl_ctx.ciphers);
9954 newsrv->ssl_ctx.ciphers = NULL;
9955 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9956 return 0;
9957}
9958
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009959/* parse the "no-send-proxy-v2-ssl" server keyword */
9960static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9961{
9962 newsrv->pp_opts &= ~SRV_PP_V2;
9963 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9964 return 0;
9965}
9966
9967/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9968static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9969{
9970 newsrv->pp_opts &= ~SRV_PP_V2;
9971 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9972 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9973 return 0;
9974}
9975
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009976/* parse the "no-ssl" server keyword */
9977static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9978{
9979 newsrv->use_ssl = 0;
9980 free(newsrv->ssl_ctx.ciphers);
9981 newsrv->ssl_ctx.ciphers = NULL;
9982 return 0;
9983}
9984
Olivier Houchard522eea72017-11-03 16:27:47 +01009985/* parse the "allow-0rtt" server keyword */
9986static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9987{
9988 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9989 return 0;
9990}
9991
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009992/* parse the "no-ssl-reuse" server keyword */
9993static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9994{
9995 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9996 return 0;
9997}
9998
Emeric Brunf9c5c472012-10-11 15:28:34 +02009999/* parse the "no-tls-tickets" server keyword */
10000static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10001{
10002 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
10003 return 0;
10004}
David Safb76832014-05-08 23:42:08 -040010005/* parse the "send-proxy-v2-ssl" server keyword */
10006static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10007{
10008 newsrv->pp_opts |= SRV_PP_V2;
10009 newsrv->pp_opts |= SRV_PP_V2_SSL;
10010 return 0;
10011}
10012
10013/* parse the "send-proxy-v2-ssl-cn" server keyword */
10014static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10015{
10016 newsrv->pp_opts |= SRV_PP_V2;
10017 newsrv->pp_opts |= SRV_PP_V2_SSL;
10018 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
10019 return 0;
10020}
Emeric Brunf9c5c472012-10-11 15:28:34 +020010021
Willy Tarreau732eac42015-07-09 11:40:25 +020010022/* parse the "sni" server keyword */
10023static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10024{
10025#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
10026 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
10027 return ERR_ALERT | ERR_FATAL;
10028#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010029 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +020010030
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010031 arg = args[*cur_arg + 1];
10032 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +020010033 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
10034 return ERR_ALERT | ERR_FATAL;
10035 }
10036
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010037 free(newsrv->sni_expr);
10038 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010039
Willy Tarreau732eac42015-07-09 11:40:25 +020010040 return 0;
10041#endif
10042}
10043
Willy Tarreau92faadf2012-10-10 23:04:25 +020010044/* parse the "ssl" server keyword */
10045static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10046{
10047 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010048 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10049 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010050#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010051 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10052 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10053#endif
Jerome Magnin2e8d52f2020-04-22 11:40:18 +020010054 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
10055 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
10056
10057 if (!newsrv->ssl_ctx.methods.min)
10058 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
10059
10060 if (!newsrv->ssl_ctx.methods.max)
10061 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
10062
10063
Willy Tarreau92faadf2012-10-10 23:04:25 +020010064 return 0;
10065}
10066
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010067/* parse the "ssl-reuse" server keyword */
10068static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10069{
10070 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10071 return 0;
10072}
10073
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010074/* parse the "tls-tickets" server keyword */
10075static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10076{
10077 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10078 return 0;
10079}
10080
Emeric Brunef42d922012-10-11 16:11:36 +020010081/* parse the "verify" server keyword */
10082static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10083{
10084 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010085 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010086 return ERR_ALERT | ERR_FATAL;
10087 }
10088
10089 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010090 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010091 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010092 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010093 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010094 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10095 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010096 return ERR_ALERT | ERR_FATAL;
10097 }
10098
Evan Broderbe554312013-06-27 00:05:25 -070010099 return 0;
10100}
10101
10102/* parse the "verifyhost" server keyword */
10103static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10104{
10105 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010106 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010107 return ERR_ALERT | ERR_FATAL;
10108 }
10109
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010110 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010111 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10112
Emeric Brunef42d922012-10-11 16:11:36 +020010113 return 0;
10114}
10115
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010116/* parse the "ssl-default-bind-options" keyword in global section */
10117static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10118 struct proxy *defpx, const char *file, int line,
10119 char **err) {
10120 int i = 1;
10121
10122 if (*(args[i]) == 0) {
10123 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10124 return -1;
10125 }
10126 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010127 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010128 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010129 else if (!strcmp(args[i], "prefer-client-ciphers"))
10130 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010131 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10132 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10133 i++;
10134 else {
10135 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10136 return -1;
10137 }
10138 }
10139 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010140 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10141 return -1;
10142 }
10143 i++;
10144 }
10145 return 0;
10146}
10147
10148/* parse the "ssl-default-server-options" keyword in global section */
10149static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10150 struct proxy *defpx, const char *file, int line,
10151 char **err) {
10152 int i = 1;
10153
10154 if (*(args[i]) == 0) {
10155 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10156 return -1;
10157 }
10158 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010159 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010160 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010161 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10162 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10163 i++;
10164 else {
10165 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10166 return -1;
10167 }
10168 }
10169 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010170 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10171 return -1;
10172 }
10173 i++;
10174 }
10175 return 0;
10176}
10177
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010178/* parse the "ca-base" / "crt-base" keywords in global section.
10179 * Returns <0 on alert, >0 on warning, 0 on success.
10180 */
10181static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10182 struct proxy *defpx, const char *file, int line,
10183 char **err)
10184{
10185 char **target;
10186
Willy Tarreauef934602016-12-22 23:12:01 +010010187 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010188
10189 if (too_many_args(1, args, err, NULL))
10190 return -1;
10191
10192 if (*target) {
10193 memprintf(err, "'%s' already specified.", args[0]);
10194 return -1;
10195 }
10196
10197 if (*(args[1]) == 0) {
10198 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10199 return -1;
10200 }
10201 *target = strdup(args[1]);
10202 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010203}
10204
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020010205/* parse the "ssl-skip-self-issued-ca" keyword in global section. */
10206static int ssl_parse_skip_self_issued_ca(char **args, int section_type, struct proxy *curpx,
10207 struct proxy *defpx, const char *file, int line,
10208 char **err)
10209{
10210 global_ssl.skip_self_issued_ca = 1;
10211 return 0;
10212}
10213
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010214/* "issuers-chain-path" load chain certificate in global */
10215static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10216{
10217 X509 *ca;
10218 X509_NAME *name = NULL;
10219 ASN1_OCTET_STRING *skid = NULL;
10220 STACK_OF(X509) *chain = NULL;
10221 struct issuer_chain *issuer;
10222 struct eb64_node *node;
10223 char *path;
10224 u64 key;
10225 int ret = 0;
10226
10227 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10228 if (chain == NULL) {
10229 chain = sk_X509_new_null();
10230 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10231 name = X509_get_subject_name(ca);
10232 }
10233 if (!sk_X509_push(chain, ca)) {
10234 X509_free(ca);
10235 goto end;
10236 }
10237 }
10238 if (!chain) {
10239 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10240 goto end;
10241 }
10242 if (!skid) {
10243 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10244 goto end;
10245 }
10246 if (!name) {
10247 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10248 goto end;
10249 }
10250 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010251 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010252 issuer = container_of(node, typeof(*issuer), node);
10253 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10254 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10255 goto end;
10256 }
10257 }
10258 issuer = calloc(1, sizeof *issuer);
10259 path = strdup(fp);
10260 if (!issuer || !path) {
10261 free(issuer);
10262 free(path);
10263 goto end;
10264 }
10265 issuer->node.key = key;
10266 issuer->path = path;
10267 issuer->chain = chain;
10268 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010269 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010270 ret = 1;
10271 end:
10272 if (skid)
10273 ASN1_OCTET_STRING_free(skid);
10274 if (chain)
10275 sk_X509_pop_free(chain, X509_free);
10276 return ret;
10277}
10278
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010279static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010280{
10281 AUTHORITY_KEYID *akid;
10282 struct issuer_chain *issuer = NULL;
10283
10284 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10285 if (akid) {
10286 struct eb64_node *node;
10287 u64 hk;
10288 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10289 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10290 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10291 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10292 issuer = ti;
10293 break;
10294 }
10295 }
10296 AUTHORITY_KEYID_free(akid);
10297 }
10298 return issuer;
10299}
10300
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010301static void ssl_free_global_issuers(void)
10302{
10303 struct eb64_node *node, *back;
10304 struct issuer_chain *issuer;
10305
William Lallemande0f3fd52020-02-25 14:53:06 +010010306 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010307 while (node) {
10308 issuer = container_of(node, typeof(*issuer), node);
10309 back = eb64_next(node);
10310 eb64_delete(node);
10311 free(issuer->path);
10312 sk_X509_pop_free(issuer->chain, X509_free);
10313 free(issuer);
10314 node = back;
10315 }
10316}
10317
10318static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10319 struct proxy *defpx, const char *file, int line,
10320 char **err)
10321{
10322 char *path;
10323 struct dirent **de_list;
10324 int i, n;
10325 struct stat buf;
10326 char *end;
10327 char fp[MAXPATHLEN+1];
10328
10329 if (too_many_args(1, args, err, NULL))
10330 return -1;
10331
10332 path = args[1];
10333 if (*path == 0 || stat(path, &buf)) {
10334 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10335 err && *err ? *err : "", args[0]);
10336 return -1;
10337 }
10338 if (S_ISDIR(buf.st_mode) == 0) {
10339 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10340 err && *err ? *err : "", args[0], path);
10341 return -1;
10342 }
10343
10344 /* strip trailing slashes, including first one */
10345 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10346 *end = 0;
10347 /* path already parsed? */
10348 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10349 return 0;
10350 /* overwrite old issuers_chain_path */
10351 free(global_ssl.issuers_chain_path);
10352 global_ssl.issuers_chain_path = strdup(path);
10353 ssl_free_global_issuers();
10354
10355 n = scandir(path, &de_list, 0, alphasort);
10356 if (n < 0) {
10357 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10358 err && *err ? *err : "", args[0], path, strerror(errno));
10359 return -1;
10360 }
10361 for (i = 0; i < n; i++) {
10362 struct dirent *de = de_list[i];
10363 BIO *in = NULL;
10364 char *warn = NULL;
10365
10366 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10367 free(de);
10368 if (stat(fp, &buf) != 0) {
10369 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10370 goto next;
10371 }
10372 if (!S_ISREG(buf.st_mode))
10373 goto next;
10374
10375 in = BIO_new(BIO_s_file());
10376 if (in == NULL)
10377 goto next;
10378 if (BIO_read_filename(in, fp) <= 0)
10379 goto next;
10380 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10381 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010382 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010383 free(warn);
10384 warn = NULL;
10385 }
10386 next:
10387 if (in)
10388 BIO_free(in);
10389 }
10390 free(de_list);
10391
10392 return 0;
10393}
10394
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010395/* parse the "ssl-mode-async" keyword in global section.
10396 * Returns <0 on alert, >0 on warning, 0 on success.
10397 */
10398static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10399 struct proxy *defpx, const char *file, int line,
10400 char **err)
10401{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010402#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010403 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010404 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010405 return 0;
10406#else
10407 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10408 return -1;
10409#endif
10410}
10411
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010412#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010413static int ssl_check_async_engine_count(void) {
10414 int err_code = 0;
10415
Emeric Brun3854e012017-05-17 20:42:48 +020010416 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010417 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010418 err_code = ERR_ABORT;
10419 }
10420 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010421}
10422
Grant Zhang872f9c22017-01-21 01:10:18 +000010423/* parse the "ssl-engine" keyword in global section.
10424 * Returns <0 on alert, >0 on warning, 0 on success.
10425 */
10426static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10427 struct proxy *defpx, const char *file, int line,
10428 char **err)
10429{
10430 char *algo;
10431 int ret = -1;
10432
10433 if (*(args[1]) == 0) {
10434 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10435 return ret;
10436 }
10437
10438 if (*(args[2]) == 0) {
10439 /* if no list of algorithms is given, it defaults to ALL */
10440 algo = strdup("ALL");
10441 goto add_engine;
10442 }
10443
10444 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10445 if (strcmp(args[2], "algo") != 0) {
10446 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10447 return ret;
10448 }
10449
10450 if (*(args[3]) == 0) {
10451 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10452 return ret;
10453 }
10454 algo = strdup(args[3]);
10455
10456add_engine:
10457 if (ssl_init_single_engine(args[1], algo)==0) {
10458 openssl_engines_initialized++;
10459 ret = 0;
10460 }
10461 free(algo);
10462 return ret;
10463}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010464#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010465
Willy Tarreauf22e9682016-12-21 23:23:19 +010010466/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10467 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10468 */
10469static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10470 struct proxy *defpx, const char *file, int line,
10471 char **err)
10472{
10473 char **target;
10474
Willy Tarreauef934602016-12-22 23:12:01 +010010475 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010476
10477 if (too_many_args(1, args, err, NULL))
10478 return -1;
10479
10480 if (*(args[1]) == 0) {
10481 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10482 return -1;
10483 }
10484
10485 free(*target);
10486 *target = strdup(args[1]);
10487 return 0;
10488}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010489
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010490#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010491/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10492 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10493 */
10494static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10495 struct proxy *defpx, const char *file, int line,
10496 char **err)
10497{
10498 char **target;
10499
10500 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10501
10502 if (too_many_args(1, args, err, NULL))
10503 return -1;
10504
10505 if (*(args[1]) == 0) {
10506 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10507 return -1;
10508 }
10509
10510 free(*target);
10511 *target = strdup(args[1]);
10512 return 0;
10513}
10514#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010515
Willy Tarreau9ceda382016-12-21 23:13:03 +010010516/* parse various global tune.ssl settings consisting in positive integers.
10517 * Returns <0 on alert, >0 on warning, 0 on success.
10518 */
10519static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10520 struct proxy *defpx, const char *file, int line,
10521 char **err)
10522{
10523 int *target;
10524
10525 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10526 target = &global.tune.sslcachesize;
10527 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010528 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010529 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010530 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010531 else if (strcmp(args[0], "maxsslconn") == 0)
10532 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010533 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10534 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010535 else {
10536 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10537 return -1;
10538 }
10539
10540 if (too_many_args(1, args, err, NULL))
10541 return -1;
10542
10543 if (*(args[1]) == 0) {
10544 memprintf(err, "'%s' expects an integer argument.", args[0]);
10545 return -1;
10546 }
10547
10548 *target = atoi(args[1]);
10549 if (*target < 0) {
10550 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10551 return -1;
10552 }
10553 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010554}
10555
10556static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10557 struct proxy *defpx, const char *file, int line,
10558 char **err)
10559{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010560 int ret;
10561
10562 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10563 if (ret != 0)
10564 return ret;
10565
Willy Tarreaubafbe012017-11-24 17:34:44 +010010566 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010567 memprintf(err, "'%s' is already configured.", args[0]);
10568 return -1;
10569 }
10570
Willy Tarreaubafbe012017-11-24 17:34:44 +010010571 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10572 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010573 memprintf(err, "Out of memory error.");
10574 return -1;
10575 }
10576 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010577}
10578
10579/* parse "ssl.force-private-cache".
10580 * Returns <0 on alert, >0 on warning, 0 on success.
10581 */
10582static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10583 struct proxy *defpx, const char *file, int line,
10584 char **err)
10585{
10586 if (too_many_args(0, args, err, NULL))
10587 return -1;
10588
Willy Tarreauef934602016-12-22 23:12:01 +010010589 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010590 return 0;
10591}
10592
10593/* parse "ssl.lifetime".
10594 * Returns <0 on alert, >0 on warning, 0 on success.
10595 */
10596static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10597 struct proxy *defpx, const char *file, int line,
10598 char **err)
10599{
10600 const char *res;
10601
10602 if (too_many_args(1, args, err, NULL))
10603 return -1;
10604
10605 if (*(args[1]) == 0) {
10606 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10607 return -1;
10608 }
10609
Willy Tarreauef934602016-12-22 23:12:01 +010010610 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010611 if (res == PARSE_TIME_OVER) {
10612 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10613 args[1], args[0]);
10614 return -1;
10615 }
10616 else if (res == PARSE_TIME_UNDER) {
10617 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10618 args[1], args[0]);
10619 return -1;
10620 }
10621 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010622 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10623 return -1;
10624 }
10625 return 0;
10626}
10627
10628#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010629/* parse "ssl-dh-param-file".
10630 * Returns <0 on alert, >0 on warning, 0 on success.
10631 */
10632static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10633 struct proxy *defpx, const char *file, int line,
10634 char **err)
10635{
10636 if (too_many_args(1, args, err, NULL))
10637 return -1;
10638
10639 if (*(args[1]) == 0) {
10640 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10641 return -1;
10642 }
10643
10644 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10645 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10646 return -1;
10647 }
10648 return 0;
10649}
10650
Willy Tarreau9ceda382016-12-21 23:13:03 +010010651/* parse "ssl.default-dh-param".
10652 * Returns <0 on alert, >0 on warning, 0 on success.
10653 */
10654static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10655 struct proxy *defpx, const char *file, int line,
10656 char **err)
10657{
10658 if (too_many_args(1, args, err, NULL))
10659 return -1;
10660
10661 if (*(args[1]) == 0) {
10662 memprintf(err, "'%s' expects an integer argument.", args[0]);
10663 return -1;
10664 }
10665
Willy Tarreauef934602016-12-22 23:12:01 +010010666 global_ssl.default_dh_param = atoi(args[1]);
10667 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010668 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10669 return -1;
10670 }
10671 return 0;
10672}
10673#endif
10674
William Lallemand3af48e72020-02-03 17:15:52 +010010675
10676/*
10677 * parse "ssl-load-extra-files".
10678 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10679 */
10680static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10681 struct proxy *defpx, const char *file, int line,
10682 char **err)
10683{
10684 int i;
10685 int gf = SSL_GF_NONE;
10686
10687 if (*(args[1]) == 0)
10688 goto err_arg;
10689
10690 for (i = 1; *args[i]; i++) {
10691
10692 if (!strcmp("bundle", args[i])) {
10693 gf |= SSL_GF_BUNDLE;
10694
10695 } else if (!strcmp("sctl", args[i])) {
10696 gf |= SSL_GF_SCTL;
10697
10698 } else if (!strcmp("ocsp", args[i])){
10699 gf |= SSL_GF_OCSP;
10700
10701 } else if (!strcmp("issuer", args[i])){
10702 gf |= SSL_GF_OCSP_ISSUER;
10703
William Lallemand4c5adbf2020-02-24 14:23:22 +010010704 } else if (!strcmp("key", args[i])) {
10705 gf |= SSL_GF_KEY;
10706
William Lallemand3af48e72020-02-03 17:15:52 +010010707 } else if (!strcmp("none", args[i])) {
10708 if (gf != SSL_GF_NONE)
10709 goto err_alone;
10710 gf = SSL_GF_NONE;
10711 i++;
10712 break;
10713
10714 } else if (!strcmp("all", args[i])) {
10715 if (gf != SSL_GF_NONE)
10716 goto err_alone;
10717 gf = SSL_GF_ALL;
10718 i++;
10719 break;
10720 } else {
10721 goto err_arg;
10722 }
10723 }
10724 /* break from loop but there are still arguments */
10725 if (*args[i])
10726 goto err_alone;
10727
10728 global_ssl.extra_files = gf;
10729
10730 return 0;
10731
10732err_alone:
10733 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10734 return -1;
10735
10736err_arg:
10737 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10738 return -1;
10739}
10740
Willy Tarreau9ceda382016-12-21 23:13:03 +010010741
William Lallemand32af2032016-10-29 18:09:35 +020010742/* This function is used with TLS ticket keys management. It permits to browse
10743 * each reference. The variable <getnext> must contain the current node,
10744 * <end> point to the root node.
10745 */
10746#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10747static inline
10748struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10749{
10750 struct tls_keys_ref *ref = getnext;
10751
10752 while (1) {
10753
10754 /* Get next list entry. */
10755 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10756
10757 /* If the entry is the last of the list, return NULL. */
10758 if (&ref->list == end)
10759 return NULL;
10760
10761 return ref;
10762 }
10763}
10764
10765static inline
10766struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10767{
10768 int id;
10769 char *error;
10770
10771 /* If the reference starts by a '#', this is numeric id. */
10772 if (reference[0] == '#') {
10773 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10774 id = strtol(reference + 1, &error, 10);
10775 if (*error != '\0')
10776 return NULL;
10777
10778 /* Perform the unique id lookup. */
10779 return tlskeys_ref_lookupid(id);
10780 }
10781
10782 /* Perform the string lookup. */
10783 return tlskeys_ref_lookup(reference);
10784}
10785#endif
10786
10787
10788#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10789
10790static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10791
10792static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10793 return cli_io_handler_tlskeys_files(appctx);
10794}
10795
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010796/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10797 * (next index to be dumped), and cli.p0 (next key reference).
10798 */
William Lallemand32af2032016-10-29 18:09:35 +020010799static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10800
10801 struct stream_interface *si = appctx->owner;
10802
10803 switch (appctx->st2) {
10804 case STAT_ST_INIT:
10805 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010806 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010807 * later and restart at the state "STAT_ST_INIT".
10808 */
10809 chunk_reset(&trash);
10810
10811 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10812 chunk_appendf(&trash, "# id secret\n");
10813 else
10814 chunk_appendf(&trash, "# id (file)\n");
10815
Willy Tarreau06d80a92017-10-19 14:32:15 +020010816 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010817 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010818 return 0;
10819 }
10820
William Lallemand32af2032016-10-29 18:09:35 +020010821 /* Now, we start the browsing of the references lists.
10822 * Note that the following call to LIST_ELEM return bad pointer. The only
10823 * available field of this pointer is <list>. It is used with the function
10824 * tlskeys_list_get_next() for retruning the first available entry
10825 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010826 if (appctx->ctx.cli.p0 == NULL) {
10827 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10828 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010829 }
10830
10831 appctx->st2 = STAT_ST_LIST;
10832 /* fall through */
10833
10834 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010835 while (appctx->ctx.cli.p0) {
10836 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010837
10838 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010839 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010840 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010841
10842 if (appctx->ctx.cli.i1 == 0)
10843 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10844
William Lallemand32af2032016-10-29 18:09:35 +020010845 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010846 int head;
10847
10848 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10849 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010850 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010851 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010852
10853 chunk_reset(t2);
10854 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010855 if (ref->key_size_bits == 128) {
10856 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10857 sizeof(struct tls_sess_key_128),
10858 t2->area, t2->size);
10859 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10860 t2->area);
10861 }
10862 else if (ref->key_size_bits == 256) {
10863 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10864 sizeof(struct tls_sess_key_256),
10865 t2->area, t2->size);
10866 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10867 t2->area);
10868 }
10869 else {
10870 /* This case should never happen */
10871 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10872 }
William Lallemand32af2032016-10-29 18:09:35 +020010873
Willy Tarreau06d80a92017-10-19 14:32:15 +020010874 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010875 /* let's try again later from this stream. We add ourselves into
10876 * this stream's users so that it can remove us upon termination.
10877 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010878 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010879 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010880 return 0;
10881 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010882 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010883 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010884 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010885 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010886 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010887 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010888 /* let's try again later from this stream. We add ourselves into
10889 * this stream's users so that it can remove us upon termination.
10890 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010891 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010892 return 0;
10893 }
10894
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010895 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010896 break;
10897
10898 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010899 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010900 }
10901
10902 appctx->st2 = STAT_ST_FIN;
10903 /* fall through */
10904
10905 default:
10906 appctx->st2 = STAT_ST_FIN;
10907 return 1;
10908 }
10909 return 0;
10910}
10911
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010912/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010913static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010914{
William Lallemand32af2032016-10-29 18:09:35 +020010915 /* no parameter, shows only file list */
10916 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010917 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010918 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010919 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010920 }
10921
10922 if (args[2][0] == '*') {
10923 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010924 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010925 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010926 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010927 if (!appctx->ctx.cli.p0)
10928 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010929 }
William Lallemand32af2032016-10-29 18:09:35 +020010930 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010931 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010932}
10933
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010934static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010935{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010936 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010937 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010938
William Lallemand32af2032016-10-29 18:09:35 +020010939 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010940 if (!*args[3] || !*args[4])
10941 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 +020010942
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010943 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010944 if (!ref)
10945 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010946
Willy Tarreau1c913e42018-08-22 05:26:57 +020010947 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010948 if (ret < 0)
10949 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010950
Willy Tarreau1c913e42018-08-22 05:26:57 +020010951 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010952 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10953 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010954
Willy Tarreau9d008692019-08-09 11:21:01 +020010955 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010956}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010957#endif
William Lallemand32af2032016-10-29 18:09:35 +020010958
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010959/*
10960 * Take an ssl_bind_conf structure and append the configuration line used to
10961 * create it in the buffer
10962 */
10963static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10964{
10965 int space = 0;
10966
10967 if (conf == NULL)
10968 return;
10969
10970 chunk_appendf(buf, " [");
10971#ifdef OPENSSL_NPN_NEGOTIATED
10972 if (conf->npn_str) {
10973 int len = conf->npn_len;
10974 char *ptr = conf->npn_str;
10975 int comma = 0;
10976
10977 if (space) chunk_appendf(buf, " ");
10978 chunk_appendf(buf, "npn ");
10979 while (len) {
10980 unsigned short size;
10981
10982 size = *ptr;
10983 ptr++;
10984 if (comma)
10985 chunk_memcat(buf, ",", 1);
10986 chunk_memcat(buf, ptr, size);
10987 ptr += size;
10988 len -= size + 1;
10989 comma = 1;
10990 }
10991 chunk_memcat(buf, "", 1); /* finish with a \0 */
10992 space++;
10993 }
10994#endif
10995#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
10996 if (conf->alpn_str) {
10997 int len = conf->alpn_len;
10998 char *ptr = conf->alpn_str;
10999 int comma = 0;
11000
11001 if (space) chunk_appendf(buf, " ");
11002 chunk_appendf(buf, "alpn ");
11003 while (len) {
11004 unsigned short size;
11005
11006 size = *ptr;
11007 ptr++;
11008 if (comma)
11009 chunk_memcat(buf, ",", 1);
11010 chunk_memcat(buf, ptr, size);
11011 ptr += size;
11012 len -= size + 1;
11013 comma = 1;
11014 }
11015 chunk_memcat(buf, "", 1); /* finish with a \0 */
11016 space++;
11017 }
11018#endif
11019 /* verify */
11020 {
11021 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
11022 if (space) chunk_appendf(buf, " ");
11023 chunk_appendf(buf, "verify none");
11024 space++;
11025 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
11026 if (space) chunk_appendf(buf, " ");
11027 chunk_appendf(buf, "verify optional");
11028 space++;
11029 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
11030 if (space) chunk_appendf(buf, " ");
11031 chunk_appendf(buf, "verify required");
11032 space++;
11033 }
11034 }
11035
11036 if (conf->no_ca_names) {
11037 if (space) chunk_appendf(buf, " ");
11038 chunk_appendf(buf, "no-ca-names");
11039 space++;
11040 }
11041
11042 if (conf->early_data) {
11043 if (space) chunk_appendf(buf, " ");
11044 chunk_appendf(buf, "allow-0rtt");
11045 space++;
11046 }
11047 if (conf->ca_file) {
11048 if (space) chunk_appendf(buf, " ");
11049 chunk_appendf(buf, "ca-file %s", conf->ca_file);
11050 space++;
11051 }
11052 if (conf->crl_file) {
11053 if (space) chunk_appendf(buf, " ");
11054 chunk_appendf(buf, "crl-file %s", conf->crl_file);
11055 space++;
11056 }
11057 if (conf->ciphers) {
11058 if (space) chunk_appendf(buf, " ");
11059 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11060 space++;
11061 }
11062#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11063 if (conf->ciphersuites) {
11064 if (space) chunk_appendf(buf, " ");
11065 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11066 space++;
11067 }
11068#endif
11069 if (conf->curves) {
11070 if (space) chunk_appendf(buf, " ");
11071 chunk_appendf(buf, "curves %s", conf->curves);
11072 space++;
11073 }
11074 if (conf->ecdhe) {
11075 if (space) chunk_appendf(buf, " ");
11076 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11077 space++;
11078 }
11079
11080 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11081 /* XXX: this part need to be revamp so we don't dump the default settings */
11082 if (conf->ssl_methods.min) {
11083 if (space) chunk_appendf(buf, " ");
11084 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11085 space++;
11086 }
11087
11088 if (conf->ssl_methods.max) {
11089 if (space) chunk_appendf(buf, " ");
11090 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11091 space++;
11092 }
11093
William Lallemand58a52222020-04-02 18:11:47 +020011094 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011095
11096 return;
11097}
11098
11099/* dump a list of filters */
11100static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11101{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011102 int i;
11103
11104 if (!entry->fcount)
11105 return;
11106
11107 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011108 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011109 }
11110 return;
11111}
11112
11113/* CLI IO handler for '(show|dump) ssl crt-list' */
11114static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11115{
11116 struct buffer *trash = alloc_trash_chunk();
11117 struct stream_interface *si = appctx->owner;
11118 struct ebmb_node *lnode;
11119
11120 if (trash == NULL)
11121 return 1;
11122
11123 /* dump the list of crt-lists */
11124 lnode = appctx->ctx.cli.p1;
11125 if (lnode == NULL)
11126 lnode = ebmb_first(&crtlists_tree);
11127 while (lnode) {
11128 chunk_appendf(trash, "%s\n", lnode->key);
11129 if (ci_putchk(si_ic(si), trash) == -1) {
11130 si_rx_room_blk(si);
11131 goto yield;
11132 }
11133 lnode = ebmb_next(lnode);
11134 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011135 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011136 return 1;
11137yield:
11138 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011139 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011140 return 0;
11141}
11142
11143/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11144static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11145{
11146 struct buffer *trash = alloc_trash_chunk();
11147 struct crtlist *crtlist;
11148 struct stream_interface *si = appctx->owner;
11149 struct crtlist_entry *entry;
11150
11151 if (trash == NULL)
11152 return 1;
11153
11154 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11155
11156 entry = appctx->ctx.cli.p1;
11157 if (entry == NULL) {
11158 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11159 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11160 if (ci_putchk(si_ic(si), trash) == -1) {
11161 si_rx_room_blk(si);
11162 goto yield;
11163 }
11164 }
11165
11166 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11167 struct ckch_store *store;
11168 const char *filename;
11169
11170 store = entry->node.key;
11171 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011172 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011173 if (appctx->ctx.cli.i0 == 's') /* show */
11174 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011175 dump_crtlist_sslconf(trash, entry->ssl_conf);
11176 dump_crtlist_filters(trash, entry);
11177 chunk_appendf(trash, "\n");
11178
11179 if (ci_putchk(si_ic(si), trash) == -1) {
11180 si_rx_room_blk(si);
11181 goto yield;
11182 }
11183 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011184 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011185 return 1;
11186yield:
11187 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011188 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011189 return 0;
11190}
11191
11192/* CLI argument parser for '(show|dump) ssl crt-list' */
11193static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11194{
11195 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011196 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011197 int mode;
11198
11199 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11200 return 1;
11201
11202 appctx->ctx.cli.p0 = NULL;
11203 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011204
William Lallemandc69f02d2020-04-06 19:07:03 +020011205 if (*args[3] && !strcmp(args[3], "-n")) {
11206 mode = 's';
11207 filename = args[4];
11208 } else {
11209 mode = 'd';
11210 filename = args[3];
11211 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011212
William Lallemandc69f02d2020-04-06 19:07:03 +020011213 if (mode == 's' && !*args[4])
11214 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11215
11216 if (filename && *filename) {
11217 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011218 if (lnode == NULL)
11219 return cli_err(appctx, "didn't find the specified filename\n");
11220
11221 appctx->ctx.cli.p0 = lnode;
11222 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11223 }
11224 appctx->ctx.cli.i0 = mode;
11225
11226 return 0;
11227}
William Lallemand44b35322019-10-17 16:28:40 +020011228
William Lallemande67c80b2020-03-25 14:42:37 +010011229/* release function of the "add ssl crt-list' command, free things and unlock
11230 the spinlock */
11231static void cli_release_add_crtlist(struct appctx *appctx)
11232{
11233 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11234
11235 if (appctx->st2 != SETCERT_ST_FIN) {
11236 struct ckch_inst *inst, *inst_s;
11237 /* upon error free the ckch_inst and everything inside */
11238 ebpt_delete(&entry->node);
11239 LIST_DEL(&entry->by_crtlist);
11240 LIST_DEL(&entry->by_ckch_store);
11241
11242 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +020011243 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011244 }
11245 crtlist_free_filters(entry->filters);
11246 ssl_sock_free_ssl_conf(entry->ssl_conf);
11247 free(entry->ssl_conf);
11248 free(entry);
11249 }
11250
11251 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11252}
11253
11254
11255/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11256 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11257 *
11258 * The logic is the same as the "commit ssl cert" command but without the
11259 * freeing of the old structures, because there are none.
11260 */
11261static int cli_io_handler_add_crtlist(struct appctx *appctx)
11262{
11263 struct bind_conf_list *bind_conf_node;
11264 struct stream_interface *si = appctx->owner;
11265 struct crtlist *crtlist = appctx->ctx.cli.p0;
11266 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11267 struct ckch_store *store = entry->node.key;
11268 struct buffer *trash = alloc_trash_chunk();
11269 struct ckch_inst *new_inst;
11270 char *err = NULL;
11271 int i = 0;
11272 int errcode = 0;
11273
11274 if (trash == NULL)
11275 goto error;
11276
11277 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11278 * created.
11279 */
11280 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11281 goto error;
11282
11283 while (1) {
11284 switch (appctx->st2) {
11285 case SETCERT_ST_INIT:
11286 /* This state just print the update message */
11287 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11288 if (ci_putchk(si_ic(si), trash) == -1) {
11289 si_rx_room_blk(si);
11290 goto yield;
11291 }
11292 appctx->st2 = SETCERT_ST_GEN;
11293 /* fallthrough */
11294 case SETCERT_ST_GEN:
11295 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11296 if (bind_conf_node == NULL)
11297 bind_conf_node = crtlist->bind_conf;
11298 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11299 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11300 struct sni_ctx *sni;
11301
11302 /* yield every 10 generations */
11303 if (i > 10) {
11304 appctx->ctx.cli.p2 = bind_conf_node;
11305 goto yield;
11306 }
11307
11308 /* we don't support multi-cert bundles, only simple ones */
11309 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11310 if (errcode & ERR_CODE)
11311 goto error;
11312
11313 /* we need to initialize the SSL_CTX generated */
11314 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11315 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11316 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11317 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11318 if (errcode & ERR_CODE)
11319 goto error;
11320 }
11321 }
11322 /* display one dot for each new instance */
11323 chunk_appendf(trash, ".");
11324 i++;
11325 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11326 }
11327 appctx->st2 = SETCERT_ST_INSERT;
11328 /* fallthrough */
11329 case SETCERT_ST_INSERT:
11330 /* insert SNIs in bind_conf */
11331 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11332 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11333 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11334 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11335 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011336 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011337 appctx->st2 = SETCERT_ST_FIN;
11338 goto end;
11339 }
11340 }
11341
11342end:
11343 chunk_appendf(trash, "\n");
11344 if (errcode & ERR_WARN)
11345 chunk_appendf(trash, "%s", err);
11346 chunk_appendf(trash, "Success!\n");
11347 if (ci_putchk(si_ic(si), trash) == -1)
11348 si_rx_room_blk(si);
11349 free_trash_chunk(trash);
11350 /* success: call the release function and don't come back */
11351 return 1;
11352yield:
11353 /* store the state */
11354 if (ci_putchk(si_ic(si), trash) == -1)
11355 si_rx_room_blk(si);
11356 free_trash_chunk(trash);
11357 si_rx_endp_more(si); /* let's come back later */
11358 return 0; /* should come back */
11359
11360error:
11361 /* spin unlock and free are done in the release function */
11362 if (trash) {
11363 chunk_appendf(trash, "\n%sFailed!\n", err);
11364 if (ci_putchk(si_ic(si), trash) == -1)
11365 si_rx_room_blk(si);
11366 free_trash_chunk(trash);
11367 }
11368 /* error: call the release function and don't come back */
11369 return 1;
11370}
11371
11372
11373/*
11374 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011375 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011376 */
11377static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11378{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011379 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011380 struct ckch_store *store;
11381 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011382 char path[MAXPATHLEN+1];
11383 char *crtlist_path;
11384 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011385 struct ebmb_node *eb;
11386 struct ebpt_node *inserted;
11387 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011388 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011389
11390 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11391 return 1;
11392
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011393 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011394 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11395
11396 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011397
11398 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11399 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11400
11401 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11402 if (!eb) {
11403 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11404 goto error;
11405 }
11406 crtlist = ebmb_entry(eb, struct crtlist, node);
11407
William Lallemande718dfb2020-04-10 11:09:25 +020011408 entry = crtlist_entry_new();
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011409 if (entry == NULL) {
11410 memprintf(&err, "Not enough memory!");
11411 goto error;
11412 }
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011413
11414 if (payload) {
11415 char *lf;
11416
11417 lf = strrchr(payload, '\n');
11418 if (lf) {
11419 memprintf(&err, "only one line of payload is supported!");
11420 goto error;
11421 }
11422 /* cert_path is filled here */
11423 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11424 if (cfgerr & ERR_CODE)
11425 goto error;
11426 } else {
11427 cert_path = args[4];
11428 }
11429
11430 if (!cert_path) {
11431 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11432 cfgerr |= ERR_ALERT | ERR_FATAL;
11433 goto error;
11434 }
11435
William Lallemand916d0b52020-04-21 18:29:12 +020011436 if (eb_gettag(crtlist->entries.b[EB_RGHT])) {
11437 char *slash;
11438
11439 slash = strrchr(cert_path, '/');
11440 if (!slash) {
11441 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
11442 goto error;
11443 }
11444 /* temporary replace / by 0 to do an strcmp */
11445 *slash = '\0';
11446 if (strcmp(cert_path, (char*)crtlist->node.key) != 0) {
11447 *slash = '/';
11448 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
11449 goto error;
11450 }
11451 *slash = '/';
11452 }
11453
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011454 if (*cert_path != '/' && global_ssl.crt_base) {
11455 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11456 memprintf(&err, "'%s' : path too long", cert_path);
11457 cfgerr |= ERR_ALERT | ERR_FATAL;
11458 goto error;
11459 }
11460 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11461 cert_path = path;
11462 }
11463
William Lallemande67c80b2020-03-25 14:42:37 +010011464 store = ckchs_lookup(cert_path);
11465 if (store == NULL) {
11466 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11467 goto error;
11468 }
William Lallemand36ccc392020-04-08 10:57:24 +020011469 if (store->multi) {
11470 memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path);
11471 goto error;
11472 }
William Lallemande67c80b2020-03-25 14:42:37 +010011473 if (store->ckch == NULL || store->ckch->cert == NULL) {
11474 memprintf(&err, "certificate '%s' is empty!", cert_path);
11475 goto error;
11476 }
11477
William Lallemande67c80b2020-03-25 14:42:37 +010011478 /* check if it's possible to insert this new crtlist_entry */
11479 entry->node.key = store;
11480 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11481 if (inserted != &entry->node) {
11482 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011483 goto error;
11484 }
11485
William Lallemandb74d5642020-04-21 16:54:19 +020011486 /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */
11487 if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) {
11488 memprintf(&err, "this is a directory, SSL configuration and filters are not allowed");
11489 goto error;
11490 }
11491
William Lallemande67c80b2020-03-25 14:42:37 +010011492 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
William Lallemande67c80b2020-03-25 14:42:37 +010011493 entry->crtlist = crtlist;
11494 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11495
11496 appctx->st2 = SETCERT_ST_INIT;
11497 appctx->ctx.cli.p0 = crtlist;
11498 appctx->ctx.cli.p1 = entry;
11499
11500 /* unlock is done in the release handler */
11501 return 0;
11502
11503error:
William Lallemande718dfb2020-04-10 11:09:25 +020011504 crtlist_entry_free(entry);
William Lallemande67c80b2020-03-25 14:42:37 +010011505 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11506 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11507 return cli_dynerr(appctx, err);
11508}
11509
William Lallemand0a9b9412020-04-06 17:43:05 +020011510/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11511static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11512{
11513 struct ckch_store *store;
11514 char *err = NULL;
11515 char *crtlist_path, *cert_path;
11516 struct ebmb_node *ebmb;
11517 struct ebpt_node *ebpt;
11518 struct crtlist *crtlist;
11519 struct crtlist_entry *entry = NULL;
11520 struct ckch_inst *inst, *inst_s;
11521 int linenum = 0;
11522 char *colons;
11523
11524 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11525 return 1;
11526
11527 if (!*args[3] || !*args[4])
11528 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11529
William Lallemand463b5242020-04-08 10:30:44 +020011530 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11531 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11532
William Lallemand0a9b9412020-04-06 17:43:05 +020011533 crtlist_path = args[3];
11534 cert_path = args[4];
11535
11536 colons = strchr(cert_path, ':');
11537 if (colons) {
11538 char *endptr;
11539
11540 linenum = strtol(colons + 1, &endptr, 10);
11541 if (colons + 1 == endptr || *endptr != '\0') {
11542 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11543 goto error;
11544 }
11545 *colons = '\0';
11546 }
11547 /* look for crtlist */
11548 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11549 if (!ebmb) {
11550 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11551 goto error;
11552 }
11553 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11554
11555 /* look for store */
11556 store = ckchs_lookup(cert_path);
11557 if (store == NULL) {
11558 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11559 goto error;
11560 }
William Lallemand36ccc392020-04-08 10:57:24 +020011561 if (store->multi) {
11562 memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path);
11563 goto error;
11564 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011565 if (store->ckch == NULL || store->ckch->cert == NULL) {
11566 memprintf(&err, "certificate '%s' is empty!", cert_path);
11567 goto error;
11568 }
11569
11570 ebpt = ebpt_lookup(&crtlist->entries, store);
11571 if (!ebpt) {
11572 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11573 goto error;
11574 }
11575
11576 /* list the line number of entries for errors in err, and select the right ebpt */
11577 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11578 struct crtlist_entry *tmp;
11579
11580 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11581 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11582
11583 /* select the entry we wanted */
11584 if (linenum == 0 || tmp->linenum == linenum) {
11585 if (!entry)
11586 entry = tmp;
11587 }
11588 }
11589
11590 /* we didn't found the specified entry */
11591 if (!entry) {
11592 memprintf(&err, "found a certificate '%s' but the line number is incorrect, please specify a correct line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11593 goto error;
11594 }
11595
11596 /* we didn't specified a line number but there were several entries */
11597 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11598 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11599 goto error;
11600 }
11601
11602 /* upon error free the ckch_inst and everything inside */
11603
11604 ebpt_delete(&entry->node);
11605 LIST_DEL(&entry->by_crtlist);
11606 LIST_DEL(&entry->by_ckch_store);
11607
11608 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11609 struct sni_ctx *sni, *sni_s;
11610
11611 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11612 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11613 ebmb_delete(&sni->name);
11614 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011615 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011616 free(sni);
11617 }
11618 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11619 LIST_DEL(&inst->by_ckchs);
11620 free(inst);
11621 }
11622
11623 crtlist_free_filters(entry->filters);
11624 ssl_sock_free_ssl_conf(entry->ssl_conf);
11625 free(entry->ssl_conf);
11626 free(entry);
11627
11628 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11629 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11630 return cli_dynmsg(appctx, LOG_NOTICE, err);
11631
11632error:
11633 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11634 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11635 return cli_dynerr(appctx, err);
11636}
William Lallemande67c80b2020-03-25 14:42:37 +010011637
William Lallemand44b35322019-10-17 16:28:40 +020011638/* Type of SSL payloads that can be updated over the CLI */
11639
11640enum {
11641 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011642 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011643#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011644 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011645#endif
William Lallemand44b35322019-10-17 16:28:40 +020011646 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011647#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011648 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011649#endif
William Lallemand44b35322019-10-17 16:28:40 +020011650 CERT_TYPE_MAX,
11651};
11652
11653struct {
11654 const char *ext;
11655 int type;
11656 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11657 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011658} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011659 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011660 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011661#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011662 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011663#endif
11664#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011665 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011666#endif
William Lallemand44b35322019-10-17 16:28:40 +020011667 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011668 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011669};
11670
William Lallemand8f840d72019-10-23 10:53:05 +020011671
William Lallemandd4f946c2019-12-05 10:26:40 +010011672/* release function of the `show ssl cert' command */
11673static void cli_release_show_cert(struct appctx *appctx)
11674{
11675 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11676}
11677
11678/* IO handler of "show ssl cert <filename>" */
11679static int cli_io_handler_show_cert(struct appctx *appctx)
11680{
11681 struct buffer *trash = alloc_trash_chunk();
11682 struct ebmb_node *node;
11683 struct stream_interface *si = appctx->owner;
11684 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011685
11686 if (trash == NULL)
11687 return 1;
11688
11689 if (!appctx->ctx.ssl.old_ckchs) {
11690 if (ckchs_transaction.old_ckchs) {
11691 ckchs = ckchs_transaction.old_ckchs;
11692 chunk_appendf(trash, "# transaction\n");
11693 if (!ckchs->multi) {
11694 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011695#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011696 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011697 int n;
11698
William Lallemandd4f946c2019-12-05 10:26:40 +010011699 chunk_appendf(trash, "*%s:", ckchs->path);
11700 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11701 if (ckchs->ckch[n].cert)
11702 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11703 }
11704 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011705#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011706 }
11707 }
11708 }
11709
11710 if (!appctx->ctx.cli.p0) {
11711 chunk_appendf(trash, "# filename\n");
11712 node = ebmb_first(&ckchs_tree);
11713 } else {
11714 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11715 }
11716 while (node) {
11717 ckchs = ebmb_entry(node, struct ckch_store, node);
11718 if (!ckchs->multi) {
11719 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011720#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011721 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011722 int n;
11723
William Lallemandd4f946c2019-12-05 10:26:40 +010011724 chunk_appendf(trash, "%s:", ckchs->path);
11725 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11726 if (ckchs->ckch[n].cert)
11727 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11728 }
11729 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011730#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011731 }
11732
11733 node = ebmb_next(node);
11734 if (ci_putchk(si_ic(si), trash) == -1) {
11735 si_rx_room_blk(si);
11736 goto yield;
11737 }
11738 }
11739
11740 appctx->ctx.cli.p0 = NULL;
11741 free_trash_chunk(trash);
11742 return 1;
11743yield:
11744
11745 free_trash_chunk(trash);
11746 appctx->ctx.cli.p0 = ckchs;
11747 return 0; /* should come back */
11748}
11749
11750/* IO handler of the details "show ssl cert <filename>" */
11751static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11752{
11753 struct stream_interface *si = appctx->owner;
11754 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11755 struct buffer *out = alloc_trash_chunk();
11756 struct buffer *tmp = alloc_trash_chunk();
11757 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011758 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011759 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011760 int write = -1;
11761 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011762 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011763
11764 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011765 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011766
11767 if (!ckchs->multi) {
11768 chunk_appendf(out, "Filename: ");
11769 if (ckchs == ckchs_transaction.new_ckchs)
11770 chunk_appendf(out, "*");
11771 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011772
William Lallemand59c16fc2020-03-19 20:26:02 +010011773 chunk_appendf(out, "Status: ");
11774 if (ckchs->ckch->cert == NULL)
11775 chunk_appendf(out, "Empty\n");
11776 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11777 chunk_appendf(out, "Unused\n");
11778 else
11779 chunk_appendf(out, "Used\n");
11780
William Lallemandea987ed2020-03-19 16:48:33 +010011781 if (ckchs->ckch->cert == NULL)
11782 goto end;
11783
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011784 chain = ckchs->ckch->chain;
11785 if (chain == NULL) {
11786 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011787 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011788 if (issuer) {
11789 chain = issuer->chain;
11790 chunk_appendf(out, "Chain Filename: ");
11791 chunk_appendf(out, "%s\n", issuer->path);
11792 }
11793 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011794 chunk_appendf(out, "Serial: ");
11795 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11796 goto end;
11797 dump_binary(out, tmp->area, tmp->data);
11798 chunk_appendf(out, "\n");
11799
11800 chunk_appendf(out, "notBefore: ");
11801 chunk_reset(tmp);
11802 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11803 goto end;
11804 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11805 goto end;
11806 write = BIO_read(bio, tmp->area, tmp->size-1);
11807 tmp->area[write] = '\0';
11808 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011809 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011810 chunk_appendf(out, "%s\n", tmp->area);
11811
11812 chunk_appendf(out, "notAfter: ");
11813 chunk_reset(tmp);
11814 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11815 goto end;
11816 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11817 goto end;
11818 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11819 goto end;
11820 tmp->area[write] = '\0';
11821 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011822 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011823 chunk_appendf(out, "%s\n", tmp->area);
11824
William Lallemandd4f946c2019-12-05 10:26:40 +010011825#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11826 chunk_appendf(out, "Subject Alternative Name: ");
11827 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11828 goto end;
11829 *(out->area + out->data) = '\0';
11830 chunk_appendf(out, "\n");
11831#endif
11832 chunk_reset(tmp);
11833 chunk_appendf(out, "Algorithm: ");
11834 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11835 goto end;
11836 chunk_appendf(out, "%s\n", tmp->area);
11837
11838 chunk_reset(tmp);
11839 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011840 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011841 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011842 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011843 dump_binary(out, tmp->area, tmp->data);
11844 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011845
William Lallemanda90e5932020-02-25 14:07:58 +010011846 chunk_appendf(out, "Subject: ");
11847 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11848 goto end;
11849 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11850 goto end;
11851 *(tmp->area + tmp->data) = '\0';
11852 chunk_appendf(out, "%s\n", tmp->area);
11853
11854 chunk_appendf(out, "Issuer: ");
11855 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11856 goto end;
11857 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11858 goto end;
11859 *(tmp->area + tmp->data) = '\0';
11860 chunk_appendf(out, "%s\n", tmp->area);
11861
William Lallemand35f4a9d2020-02-25 11:56:32 +010011862 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011863 for (i = 0; i < sk_X509_num(chain); i++) {
11864 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011865
William Lallemandbb7288a2020-02-25 14:04:33 +010011866 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011867 if ((name = X509_get_subject_name(ca)) == NULL)
11868 goto end;
11869 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11870 goto end;
11871 *(tmp->area + tmp->data) = '\0';
11872 chunk_appendf(out, "%s\n", tmp->area);
11873
William Lallemandbb7288a2020-02-25 14:04:33 +010011874 chunk_appendf(out, "Chain Issuer: ");
11875 if ((name = X509_get_issuer_name(ca)) == NULL)
11876 goto end;
11877 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11878 goto end;
11879 *(tmp->area + tmp->data) = '\0';
11880 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011881 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011882 }
11883
William Lallemandea987ed2020-03-19 16:48:33 +010011884end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011885 if (ci_putchk(si_ic(si), out) == -1) {
11886 si_rx_room_blk(si);
11887 goto yield;
11888 }
11889
William Lallemand18eeb8e2020-03-20 14:42:36 +010011890end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011891 if (bio)
11892 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011893 free_trash_chunk(tmp);
11894 free_trash_chunk(out);
11895 return 1;
11896yield:
11897 free_trash_chunk(tmp);
11898 free_trash_chunk(out);
11899 return 0; /* should come back */
11900}
11901
11902/* parsing function for 'show ssl cert [certfile]' */
11903static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11904{
11905 struct ckch_store *ckchs;
11906
11907 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11908 return cli_err(appctx, "Can't allocate memory!\n");
11909
11910 /* The operations on the CKCH architecture are locked so we can
11911 * manipulate ckch_store and ckch_inst */
11912 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11913 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11914
11915 /* check if there is a certificate to lookup */
11916 if (*args[3]) {
11917 if (*args[3] == '*') {
11918 if (!ckchs_transaction.new_ckchs)
11919 goto error;
11920
11921 ckchs = ckchs_transaction.new_ckchs;
11922
11923 if (strcmp(args[3] + 1, ckchs->path))
11924 goto error;
11925
11926 } else {
11927 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11928 goto error;
11929
11930 }
11931
11932 if (ckchs->multi)
11933 goto error;
11934
11935 appctx->ctx.cli.p0 = ckchs;
11936 /* use the IO handler that shows details */
11937 appctx->io_handler = cli_io_handler_show_cert_detail;
11938 }
11939
11940 return 0;
11941
11942error:
11943 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11944 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11945}
11946
William Lallemand430413e2019-10-28 14:30:47 +010011947/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011948static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011949{
11950 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011951
William Lallemand430413e2019-10-28 14:30:47 +010011952 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011953
William Lallemand430413e2019-10-28 14:30:47 +010011954 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011955 /* 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 +010011956 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011957
William Lallemandbeea2a42019-10-30 17:45:33 +010011958 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020011959 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011960 }
11961}
11962
11963
11964/*
11965 * This function tries to create the new ckch_inst and their SNIs
11966 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011967static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011968{
11969 struct stream_interface *si = appctx->owner;
11970 int y = 0;
11971 char *err = NULL;
11972 int errcode = 0;
11973 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11974 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011975 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011976 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011977 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011978
William Lallemand33cc76f2019-10-31 11:43:45 +010011979 if (trash == NULL)
11980 goto error;
11981
William Lallemand8f840d72019-10-23 10:53:05 +020011982 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11983 goto error;
11984
William Lallemand430413e2019-10-28 14:30:47 +010011985 while (1) {
11986 switch (appctx->st2) {
11987 case SETCERT_ST_INIT:
11988 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011989 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010011990 if (ci_putchk(si_ic(si), trash) == -1) {
11991 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020011992 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010011993 }
11994 appctx->st2 = SETCERT_ST_GEN;
11995 /* fallthrough */
11996 case SETCERT_ST_GEN:
11997 /*
11998 * This state generates the ckch instances with their
11999 * sni_ctxs and SSL_CTX.
12000 *
William Lallemand430413e2019-10-28 14:30:47 +010012001 * Since the SSL_CTX generation can be CPU consumer, we
12002 * yield every 10 instances.
12003 */
William Lallemand8f840d72019-10-23 10:53:05 +020012004
William Lallemandbeea2a42019-10-30 17:45:33 +010012005 old_ckchs = appctx->ctx.ssl.old_ckchs;
12006 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012007
William Lallemandbeea2a42019-10-30 17:45:33 +010012008 if (!new_ckchs)
12009 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020012010
William Lallemandbeea2a42019-10-30 17:45:33 +010012011 /* get the next ckchi to regenerate */
12012 ckchi = appctx->ctx.ssl.next_ckchi;
12013 /* we didn't start yet, set it to the first elem */
12014 if (ckchi == NULL)
12015 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020012016
William Lallemandbeea2a42019-10-30 17:45:33 +010012017 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
12018 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
12019 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010012020 char **sni_filter = NULL;
12021 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020012022
William Lallemandbeea2a42019-10-30 17:45:33 +010012023 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
12024 if (y >= 10) {
12025 /* save the next ckchi to compute */
12026 appctx->ctx.ssl.next_ckchi = ckchi;
12027 goto yield;
12028 }
William Lallemandcaa16192020-04-08 16:29:15 +020012029
12030 if (ckchi->crtlist_entry) {
12031 sni_filter = ckchi->crtlist_entry->filters;
12032 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010012033 }
William Lallemand38df1c82019-12-04 15:39:35 +010012034
William Lallemandbeea2a42019-10-30 17:45:33 +010012035 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010012036 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 +010012037 else
William Lallemand38df1c82019-12-04 15:39:35 +010012038 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
12039
William Lallemandbeea2a42019-10-30 17:45:33 +010012040 if (errcode & ERR_CODE)
12041 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020012042
William Lallemand21724f02019-11-04 17:56:13 +010012043 /* if the previous ckchi was used as the default */
12044 if (ckchi->is_default)
12045 new_inst->is_default = 1;
12046
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012047 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010012048 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
12049 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012050 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 +010012051 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
12052 if (errcode & ERR_CODE)
12053 goto error;
12054 }
12055 }
12056
12057
William Lallemandbeea2a42019-10-30 17:45:33 +010012058 /* display one dot per new instance */
12059 chunk_appendf(trash, ".");
12060 /* link the new ckch_inst to the duplicate */
12061 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
12062 y++;
12063 }
William Lallemand430413e2019-10-28 14:30:47 +010012064 appctx->st2 = SETCERT_ST_INSERT;
12065 /* fallthrough */
12066 case SETCERT_ST_INSERT:
12067 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012068
William Lallemandbeea2a42019-10-30 17:45:33 +010012069 old_ckchs = appctx->ctx.ssl.old_ckchs;
12070 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012071
William Lallemandbeea2a42019-10-30 17:45:33 +010012072 if (!new_ckchs)
12073 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012074
William Lallemand90afe902020-03-30 19:29:45 +020012075 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12076 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12077 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12078 ebpt_delete(&entry->node);
12079 /* change the ptr and reinsert the node */
12080 entry->node.key = new_ckchs;
12081 ebpt_insert(&entry->crtlist->entries, &entry->node);
12082 }
12083
William Lallemand21724f02019-11-04 17:56:13 +010012084 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012085 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12086 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12087 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12088 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12089 }
William Lallemand8f840d72019-10-23 10:53:05 +020012090
William Lallemandbeea2a42019-10-30 17:45:33 +010012091 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12092 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemandd5e93772020-04-09 17:12:16 +020012093 struct bind_conf *bind_conf = ckchi->bind_conf;
12094
12095 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020012096 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020012097 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010012098 }
William Lallemand8f840d72019-10-23 10:53:05 +020012099
William Lallemandbeea2a42019-10-30 17:45:33 +010012100 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020012101 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010012102 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012103 appctx->st2 = SETCERT_ST_FIN;
12104 /* fallthrough */
12105 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012106 /* we achieved the transaction, we can set everything to NULL */
12107 free(ckchs_transaction.path);
12108 ckchs_transaction.path = NULL;
12109 ckchs_transaction.new_ckchs = NULL;
12110 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012111 goto end;
12112 }
William Lallemand8f840d72019-10-23 10:53:05 +020012113 }
William Lallemand430413e2019-10-28 14:30:47 +010012114end:
William Lallemand8f840d72019-10-23 10:53:05 +020012115
William Lallemanded442432019-11-21 16:41:07 +010012116 chunk_appendf(trash, "\n");
12117 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012118 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012119 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012120 if (ci_putchk(si_ic(si), trash) == -1)
12121 si_rx_room_blk(si);
12122 free_trash_chunk(trash);
12123 /* success: call the release function and don't come back */
12124 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012125yield:
12126 /* store the state */
12127 if (ci_putchk(si_ic(si), trash) == -1)
12128 si_rx_room_blk(si);
12129 free_trash_chunk(trash);
12130 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012131 return 0; /* should come back */
12132
12133error:
12134 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012135 if (trash) {
12136 chunk_appendf(trash, "\n%sFailed!\n", err);
12137 if (ci_putchk(si_ic(si), trash) == -1)
12138 si_rx_room_blk(si);
12139 free_trash_chunk(trash);
12140 }
William Lallemand430413e2019-10-28 14:30:47 +010012141 /* error: call the release function and don't come back */
12142 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012143}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012144
12145/*
12146 * Parsing function of 'commit ssl cert'
12147 */
12148static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12149{
12150 char *err = NULL;
12151
William Lallemand230662a2019-12-03 13:32:54 +010012152 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12153 return 1;
12154
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012155 if (!*args[3])
12156 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12157
12158 /* The operations on the CKCH architecture are locked so we can
12159 * manipulate ckch_store and ckch_inst */
12160 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12161 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12162
12163 if (!ckchs_transaction.path) {
12164 memprintf(&err, "No ongoing transaction! !\n");
12165 goto error;
12166 }
12167
12168 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12169 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12170 goto error;
12171 }
12172
William Lallemand4c5adbf2020-02-24 14:23:22 +010012173#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12174 if (ckchs_transaction.new_ckchs->multi) {
12175 int n;
12176
12177 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12178 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)) {
12179 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12180 goto error;
12181 }
12182 }
12183 } else
12184#endif
12185 {
12186 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12187 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12188 goto error;
12189 }
12190 }
12191
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012192 /* init the appctx structure */
12193 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012194 appctx->ctx.ssl.next_ckchi = NULL;
12195 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12196 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12197
12198 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12199 return 0;
12200
12201error:
12202
12203 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12204 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12205
12206 return cli_dynerr(appctx, err);
12207}
12208
William Lallemand8f840d72019-10-23 10:53:05 +020012209/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012210 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012211 */
William Lallemand150bfa82019-09-19 17:12:49 +020012212static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12213{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012214 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012215 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012216 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012217 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012218 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012219 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012220 char *end;
12221 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012222 struct cert_key_and_chain *ckch;
12223 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012224
William Lallemand230662a2019-12-03 13:32:54 +010012225 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12226 return 1;
12227
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012228 if ((buf = alloc_trash_chunk()) == NULL)
12229 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012230
12231 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012232 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012233
12234 /* The operations on the CKCH architecture are locked so we can
12235 * manipulate ckch_store and ckch_inst */
12236 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12237 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12238
William Lallemand8f840d72019-10-23 10:53:05 +020012239 if (!chunk_strcpy(buf, args[3])) {
12240 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12241 errcode |= ERR_ALERT | ERR_FATAL;
12242 goto end;
12243 }
12244
William Lallemand44b35322019-10-17 16:28:40 +020012245 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012246 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012247 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012248 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12249 *end = '\0';
12250 type = cert_exts[i].type;
12251 break;
12252 }
12253 }
12254
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012255 appctx->ctx.ssl.old_ckchs = NULL;
12256 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012257
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012258 /* if there is an ongoing transaction */
12259 if (ckchs_transaction.path) {
12260 /* 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 +020012261#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012262 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012263 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012264 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012265
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012266 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012267 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012268 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012269 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012270 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12271 bundle = j; /* keep the type of certificate so we insert it at the right place */
12272 *end = '\0'; /* it's a bundle let's end the string*/
12273 break;
12274 }
William Lallemand150bfa82019-09-19 17:12:49 +020012275 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012276 if (bundle < 0) {
12277 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);
12278 errcode |= ERR_ALERT | ERR_FATAL;
12279 goto end;
12280 }
12281 }
12282#endif
12283
12284 /* if there is an ongoing transaction, check if this is the same file */
12285 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12286 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12287 errcode |= ERR_ALERT | ERR_FATAL;
12288 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012289 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012290
12291 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12292
12293 } else {
12294 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12295
12296 /* lookup for the certificate in the tree:
12297 * check if this is used as a bundle AND as a unique certificate */
12298 for (i = 0; i < 2; i++) {
12299
12300 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12301 /* only the bundle name is in the tree and you should
12302 * never update a bundle name, only a filename */
12303 if (bundle < 0 && find_ckchs[i]->multi) {
12304 /* we tried to look for a non-bundle and we found a bundle */
12305 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12306 err ? err : "", args[3], args[3]);
12307 errcode |= ERR_ALERT | ERR_FATAL;
12308 goto end;
12309 }
William Lallemand3246d942019-11-04 14:02:11 +010012310 /* If we want a bundle but this is not a bundle
12311 * example: When you try to update <file>.rsa, but
12312 * <file> is a regular file */
12313 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12314 find_ckchs[i] = NULL;
12315 break;
12316 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012317 }
12318#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12319 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012320 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012321 int j;
12322
12323 /* check if it was used in a bundle by removing the
12324 * .dsa/.rsa/.ecdsa at the end of the filename */
12325 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012326 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012327 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12328 bundle = j; /* keep the type of certificate so we insert it at the right place */
12329 *end = '\0'; /* it's a bundle let's end the string*/
12330 break;
12331 }
12332 }
William Lallemand37031b82019-11-04 13:38:53 +010012333 if (bundle < 0) /* we didn't find a bundle extension */
12334 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012335 }
William Lallemand963b2e72019-10-14 11:38:36 +020012336#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012337 /* bundles are not supported here, so we don't need to lookup again */
12338 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012339#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012340 }
12341
12342 if (find_ckchs[0] && find_ckchs[1]) {
12343 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",
12344 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12345 errcode |= ERR_ALERT | ERR_FATAL;
12346 goto end;
12347 }
12348
12349 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012350 }
12351
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012352 if (!appctx->ctx.ssl.old_ckchs) {
12353 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012354 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012355 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012356 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012357 }
12358
William Lallemand8a7fdf02019-11-04 10:59:32 +010012359 if (!appctx->ctx.ssl.path) {
12360 /* this is a new transaction, set the path of the transaction */
12361 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12362 if (!appctx->ctx.ssl.path) {
12363 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12364 errcode |= ERR_ALERT | ERR_FATAL;
12365 goto end;
12366 }
12367 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012368
12369 old_ckchs = appctx->ctx.ssl.old_ckchs;
12370
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012371 /* duplicate the ckch store */
12372 new_ckchs = ckchs_dup(old_ckchs);
12373 if (!new_ckchs) {
12374 memprintf(&err, "%sCannot allocate memory!\n",
12375 err ? err : "");
12376 errcode |= ERR_ALERT | ERR_FATAL;
12377 goto end;
12378 }
12379
12380 if (!new_ckchs->multi)
12381 ckch = new_ckchs->ckch;
12382 else
12383 ckch = &new_ckchs->ckch[bundle];
12384
12385 /* appply the change on the duplicate */
12386 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12387 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12388 errcode |= ERR_ALERT | ERR_FATAL;
12389 goto end;
12390 }
12391
12392 appctx->ctx.ssl.new_ckchs = new_ckchs;
12393
12394 /* we succeed, we can save the ckchs in the transaction */
12395
12396 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012397 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012398 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12399 ckchs_transaction.path = appctx->ctx.ssl.path;
12400 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12401 } else {
12402 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12403
12404 }
12405
12406 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020012407 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012408
12409 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12410
12411
William Lallemand8f840d72019-10-23 10:53:05 +020012412 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012413
William Lallemand8f840d72019-10-23 10:53:05 +020012414end:
12415 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012416
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012417 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012418
William Lallemandba1c33f2020-04-08 17:55:45 +020012419 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012420 appctx->ctx.ssl.new_ckchs = NULL;
12421
12422 appctx->ctx.ssl.old_ckchs = NULL;
12423
12424 free(appctx->ctx.ssl.path);
12425 appctx->ctx.ssl.path = NULL;
12426
12427 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012428 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012429 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012430
12431 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12432 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012433 }
William Lallemand8f840d72019-10-23 10:53:05 +020012434 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012435}
12436
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012437/* parsing function of 'abort ssl cert' */
12438static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12439{
12440 char *err = NULL;
12441
William Lallemand230662a2019-12-03 13:32:54 +010012442 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12443 return 1;
12444
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012445 if (!*args[3])
12446 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12447
12448 /* The operations on the CKCH architecture are locked so we can
12449 * manipulate ckch_store and ckch_inst */
12450 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12451 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12452
12453 if (!ckchs_transaction.path) {
12454 memprintf(&err, "No ongoing transaction!\n");
12455 goto error;
12456 }
12457
12458 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12459 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12460 goto error;
12461 }
12462
12463 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020012464 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012465 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020012466 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012467 ckchs_transaction.old_ckchs = NULL;
12468 free(ckchs_transaction.path);
12469 ckchs_transaction.path = NULL;
12470
12471 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12472
12473 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12474 return cli_dynmsg(appctx, LOG_NOTICE, err);
12475
12476error:
12477 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12478
12479 return cli_dynerr(appctx, err);
12480}
12481
William Lallemandea987ed2020-03-19 16:48:33 +010012482/* parsing function of 'new ssl cert' */
12483static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12484{
12485 struct ckch_store *store;
12486 char *err = NULL;
12487 char *path;
12488
12489 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12490 return 1;
12491
12492 if (!*args[3])
12493 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12494
12495 path = args[3];
12496
12497 /* The operations on the CKCH architecture are locked so we can
12498 * manipulate ckch_store and ckch_inst */
12499 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12500 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12501
12502 store = ckchs_lookup(path);
12503 if (store != NULL) {
12504 memprintf(&err, "Certificate '%s' already exists!\n", path);
12505 store = NULL; /* we don't want to free it */
12506 goto error;
12507 }
William Lallemand8a874e42020-04-09 10:32:53 +020012508 /* we won't support multi-certificate bundle here */
12509 store = ckch_store_new(path, 1);
William Lallemandea987ed2020-03-19 16:48:33 +010012510 if (!store) {
12511 memprintf(&err, "unable to allocate memory.\n");
12512 goto error;
12513 }
William Lallemandea987ed2020-03-19 16:48:33 +010012514
12515 /* insert into the ckchs tree */
William Lallemandea987ed2020-03-19 16:48:33 +010012516 ebst_insert(&ckchs_tree, &store->node);
12517 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12518
12519 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12520 return cli_dynmsg(appctx, LOG_NOTICE, err);
12521error:
12522 free(store);
12523 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12524 return cli_dynerr(appctx, err);
12525}
12526
William Lallemand419e6342020-04-08 12:05:39 +020012527/* parsing function of 'del ssl cert' */
12528static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12529{
12530 struct ckch_store *store;
12531 char *err = NULL;
12532 char *filename;
12533
12534 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12535 return 1;
12536
12537 if (!*args[3])
12538 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12539
12540 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12541 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12542
12543 filename = args[3];
12544
12545 store = ckchs_lookup(filename);
12546 if (store == NULL) {
12547 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12548 goto error;
12549 }
12550 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12551 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12552 goto error;
12553 }
12554
12555 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020012556 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020012557
12558 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12559
12560 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12561 return cli_dynmsg(appctx, LOG_NOTICE, err);
12562
12563error:
12564 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12565 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12566 return cli_dynerr(appctx, err);
12567}
12568
12569
12570
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012571static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012572{
12573#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12574 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012575 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012576
12577 if (!payload)
12578 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012579
12580 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012581 if (!*payload)
12582 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012583
12584 /* remove \r and \n from the payload */
12585 for (i = 0, j = 0; payload[i]; i++) {
12586 if (payload[i] == '\r' || payload[i] == '\n')
12587 continue;
12588 payload[j++] = payload[i];
12589 }
12590 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012591
Willy Tarreau1c913e42018-08-22 05:26:57 +020012592 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012593 if (ret < 0)
12594 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012595
Willy Tarreau1c913e42018-08-22 05:26:57 +020012596 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012597 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012598 if (err)
12599 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12600 else
12601 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012602 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012603
12604 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012605#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012606 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 +020012607#endif
12608
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012609}
12610
Willy Tarreau86a394e2019-05-09 14:15:32 +020012611#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012612static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12613{
12614 switch (arg->type) {
12615 case ARGT_STR:
12616 smp->data.type = SMP_T_STR;
12617 smp->data.u.str = arg->data.str;
12618 return 1;
12619 case ARGT_VAR:
12620 if (!vars_get_by_desc(&arg->data.var, smp))
12621 return 0;
12622 if (!sample_casts[smp->data.type][SMP_T_STR])
12623 return 0;
12624 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12625 return 0;
12626 return 1;
12627 default:
12628 return 0;
12629 }
12630}
12631
12632static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12633 const char *file, int line, char **err)
12634{
12635 switch(args[0].data.sint) {
12636 case 128:
12637 case 192:
12638 case 256:
12639 break;
12640 default:
12641 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12642 return 0;
12643 }
12644 /* Try to decode a variable. */
12645 vars_check_arg(&args[1], NULL);
12646 vars_check_arg(&args[2], NULL);
12647 vars_check_arg(&args[3], NULL);
12648 return 1;
12649}
12650
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012651/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012652static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12653{
12654 struct sample nonce, key, aead_tag;
12655 struct buffer *smp_trash, *smp_trash_alloc;
12656 EVP_CIPHER_CTX *ctx;
12657 int dec_size, ret;
12658
12659 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12660 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12661 return 0;
12662
12663 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12664 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12665 return 0;
12666
12667 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12668 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12669 return 0;
12670
12671 smp_trash = get_trash_chunk();
12672 smp_trash_alloc = alloc_trash_chunk();
12673 if (!smp_trash_alloc)
12674 return 0;
12675
12676 ctx = EVP_CIPHER_CTX_new();
12677
12678 if (!ctx)
12679 goto err;
12680
12681 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12682 if (dec_size < 0)
12683 goto err;
12684 smp_trash->data = dec_size;
12685
12686 /* Set cipher type and mode */
12687 switch(arg_p[0].data.sint) {
12688 case 128:
12689 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12690 break;
12691 case 192:
12692 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12693 break;
12694 case 256:
12695 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12696 break;
12697 }
12698
12699 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12700
12701 /* Initialise IV */
12702 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12703 goto err;
12704
12705 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12706 if (dec_size < 0)
12707 goto err;
12708 smp_trash->data = dec_size;
12709
12710 /* Initialise key */
12711 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12712 goto err;
12713
12714 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12715 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12716 goto err;
12717
12718 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12719 if (dec_size < 0)
12720 goto err;
12721 smp_trash_alloc->data = dec_size;
12722 dec_size = smp_trash->data;
12723
12724 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12725 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12726
12727 if (ret <= 0)
12728 goto err;
12729
12730 smp->data.u.str.data = dec_size + smp_trash->data;
12731 smp->data.u.str.area = smp_trash->area;
12732 smp->data.type = SMP_T_BIN;
12733 smp->flags &= ~SMP_F_CONST;
12734 free_trash_chunk(smp_trash_alloc);
12735 return 1;
12736
12737err:
12738 free_trash_chunk(smp_trash_alloc);
12739 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012740}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012741# endif
William Lallemand32af2032016-10-29 18:09:35 +020012742
Elliot Otchet71f82972020-01-15 08:12:14 -050012743/* Argument validation functions */
12744
12745/* This function is used to validate the arguments passed to any "x_dn" ssl
12746 * keywords. These keywords support specifying a third parameter that must be
12747 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12748 */
12749int val_dnfmt(struct arg *arg, char **err_msg)
12750{
12751 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12752 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12753 return 0;
12754 }
12755 return 1;
12756}
12757
William Lallemand32af2032016-10-29 18:09:35 +020012758/* register cli keywords */
12759static struct cli_kw_list cli_kws = {{ },{
12760#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12761 { { "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 +020012762 { { "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 +020012763#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012764 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012765 { { "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 +010012766 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12767 { { "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 +010012768 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
William Lallemand419e6342020-04-08 12:05:39 +020012769 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
William Lallemandd4f946c2019-12-05 10:26:40 +010012770 { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a <certfile>", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert },
William Lallemande67c80b2020-03-25 14:42:37 +010012771 { { "add", "ssl", "crt-list", NULL }, "add ssl crt-list <filename> <certfile> [options] : add a line <certfile> to a crt-list <filename>", cli_parse_add_crtlist, cli_io_handler_add_crtlist, cli_release_add_crtlist },
William Lallemand0a9b9412020-04-06 17:43:05 +020012772 { { "del", "ssl", "crt-list", NULL }, "del ssl crt-list <filename> <certfile[:line]> : delete a line <certfile> in a crt-list <filename>", cli_parse_del_crtlist, NULL, NULL },
William Lallemandc69f02d2020-04-06 19:07:03 +020012773 { { "show", "ssl", "crt-list", NULL }, "show ssl crt-list [-n] [<filename>] : show the list of crt-lists or the content of a crt-list <filename>", cli_parse_dump_crtlist, cli_io_handler_dump_crtlist, NULL },
William Lallemand32af2032016-10-29 18:09:35 +020012774 { { NULL }, NULL, NULL, NULL }
12775}};
12776
Willy Tarreau0108d902018-11-25 19:14:37 +010012777INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012778
Willy Tarreau7875d092012-09-10 08:20:03 +020012779/* Note: must not be declared <const> as its list will be overwritten.
12780 * Please take care of keeping this list alphabetically sorted.
12781 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012782static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012783 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012784 { "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 +010012785#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012786 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012787#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012788 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012789#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12790 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12791#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012792 { "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 +020012793 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012794 { "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 +020012795 { "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 +020012796#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012797 { "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 -040012798#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012799#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012800 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12801 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012802 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12803#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012804 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12805 { "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 +010012806 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012807 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012808 { "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 +020012809 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12810 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12811 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12812 { "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 -050012813 { "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 +020012814 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12815 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012816 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012817 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12818 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012819 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012820 { "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 +020012821 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12822 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12823 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12824 { "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 -050012825 { "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 +020012826 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012827 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012828 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012829 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012830 { "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 +010012831 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012832 { "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 +020012833 { "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 +010012834 { "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 +020012835 { "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 +010012836#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012837 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012838#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012839#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012840 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012841#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012842 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012843#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012844 { "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 -040012845#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012846 { "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 +020012847#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012848 { "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 -040012849#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012850#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012851 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12852 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012853 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12854#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012855#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012856 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012857#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012858 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12859 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12860 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12861 { "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 +020012862 { NULL, NULL, 0, 0, 0 },
12863}};
12864
Willy Tarreau0108d902018-11-25 19:14:37 +010012865INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12866
Willy Tarreau7875d092012-09-10 08:20:03 +020012867/* Note: must not be declared <const> as its list will be overwritten.
12868 * Please take care of keeping this list alphabetically sorted.
12869 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012870static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012871 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12872 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012873 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012874}};
12875
Willy Tarreau0108d902018-11-25 19:14:37 +010012876INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12877
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012878/* Note: must not be declared <const> as its list will be overwritten.
12879 * Please take care of keeping this list alphabetically sorted, doing so helps
12880 * all code contributors.
12881 * Optional keywords are also declared with a NULL ->parse() function so that
12882 * the config parser can report an appropriate error when a known keyword was
12883 * not enabled.
12884 */
William Lallemand557823f2020-04-01 17:42:47 +020012885
12886/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12887 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012888static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012889 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012890 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012891 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12892 { "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 +010012893 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012894#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012895 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12896#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012897 { "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 +010012898 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012899 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012900 { "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 +010012901 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012902 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12903 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012904 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12905 { NULL, NULL, 0 },
12906};
12907
Willy Tarreau0108d902018-11-25 19:14:37 +010012908/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12909
Willy Tarreau51fb7652012-09-18 18:24:39 +020012910static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012911 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012912 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012913 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12914 { "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 +020012915 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12916 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12917 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12918 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012919#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012920 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12921#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012922 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012923 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012924 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012925 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12926 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12927 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12928 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12929 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12930 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12931 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012932 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012933 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012934 { "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 +020012935 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12936 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12937 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12938 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012939 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012940 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12941 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012942 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12943 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012944 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12945 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12946 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12947 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12948 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012949 { NULL, NULL, 0 },
12950}};
Emeric Brun46591952012-05-18 15:47:34 +020012951
Willy Tarreau0108d902018-11-25 19:14:37 +010012952INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12953
Willy Tarreau92faadf2012-10-10 23:04:25 +020012954/* Note: must not be declared <const> as its list will be overwritten.
12955 * Please take care of keeping this list alphabetically sorted, doing so helps
12956 * all code contributors.
12957 * Optional keywords are also declared with a NULL ->parse() function so that
12958 * the config parser can report an appropriate error when a known keyword was
12959 * not enabled.
12960 */
12961static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012962 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012963 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012964 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012965 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012966 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012967 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12968 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012969#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012970 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12971#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012972 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12973 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12974 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12975 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12976 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12977 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12978 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12979 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12980 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12981 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12982 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12983 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12984 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12985 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12986 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12987 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12988 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12989 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012990 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012991 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12992 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12993 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12994 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12995 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12996 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12997 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12998 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12999 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
13000 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020013001 { NULL, NULL, 0, 0 },
13002}};
13003
Willy Tarreau0108d902018-11-25 19:14:37 +010013004INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
13005
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013006static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010013007 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
13008 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013009 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010013010 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013011 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
13012 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010013013#ifndef OPENSSL_NO_DH
13014 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
13015#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013016 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013017#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013018 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013019#endif
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020013020 { CFG_GLOBAL, "ssl-skip-self-issued-ca", ssl_parse_skip_self_issued_ca },
Willy Tarreau9ceda382016-12-21 23:13:03 +010013021 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
13022#ifndef OPENSSL_NO_DH
13023 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
13024#endif
13025 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
13026 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
13027 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
13028 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010013029 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010013030 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
13031 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013032#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013033 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
13034 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
13035#endif
William Lallemand3af48e72020-02-03 17:15:52 +010013036 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013037 { 0, NULL, NULL },
13038}};
13039
Willy Tarreau0108d902018-11-25 19:14:37 +010013040INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
13041
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010013042/* Note: must not be declared <const> as its list will be overwritten */
13043static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020013044#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010013045 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
13046#endif
13047 { NULL, NULL, 0, 0, 0 },
13048}};
13049
13050INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
13051
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020013052/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010013053static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020013054 .snd_buf = ssl_sock_from_buf,
13055 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010013056 .subscribe = ssl_subscribe,
13057 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020013058 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020013059 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020013060 .rcv_pipe = NULL,
13061 .snd_pipe = NULL,
13062 .shutr = NULL,
13063 .shutw = ssl_sock_shutw,
13064 .close = ssl_sock_close,
13065 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010013066 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010013067 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010013068 .prepare_srv = ssl_sock_prepare_srv_ctx,
13069 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013070 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013071 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013072};
13073
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013074enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13075 struct session *sess, struct stream *s, int flags)
13076{
13077 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013078 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013079
13080 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013081 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013082
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013083 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013084 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013085 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013086 s->req.flags |= CF_READ_NULL;
13087 return ACT_RET_YIELD;
13088 }
13089 }
13090 return (ACT_RET_CONT);
13091}
13092
13093static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13094{
13095 rule->action_ptr = ssl_action_wait_for_hs;
13096
13097 return ACT_RET_PRS_OK;
13098}
13099
13100static struct action_kw_list http_req_actions = {ILH, {
13101 { "wait-for-handshake", ssl_parse_wait_for_hs },
13102 { /* END */ }
13103}};
13104
Willy Tarreau0108d902018-11-25 19:14:37 +010013105INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13106
Willy Tarreau5db847a2019-05-09 14:13:35 +020013107#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013108
13109static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13110{
13111 if (ptr) {
13112 chunk_destroy(ptr);
13113 free(ptr);
13114 }
13115}
13116
13117#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013118static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13119{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013120 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013121}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013122
Emeric Brun46591952012-05-18 15:47:34 +020013123__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013124static void __ssl_sock_init(void)
13125{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013126#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013127 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013128 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013129#endif
Emeric Brun46591952012-05-18 15:47:34 +020013130
Willy Tarreauef934602016-12-22 23:12:01 +010013131 if (global_ssl.listen_default_ciphers)
13132 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13133 if (global_ssl.connect_default_ciphers)
13134 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013135#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013136 if (global_ssl.listen_default_ciphersuites)
13137 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13138 if (global_ssl.connect_default_ciphersuites)
13139 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13140#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013141
Willy Tarreau13e14102016-12-22 20:25:26 +010013142 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013143#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013144 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013145#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013146#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013147 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013148 n = sk_SSL_COMP_num(cm);
13149 while (n--) {
13150 (void) sk_SSL_COMP_pop(cm);
13151 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013152#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013153
Willy Tarreau5db847a2019-05-09 14:13:35 +020013154#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013155 ssl_locking_init();
13156#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013157#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013158 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13159#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013160 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013161 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 +020013162#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013163 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013164 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013165#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013166#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13167 hap_register_post_check(tlskeys_finalize_config);
13168#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013169
13170 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13171 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13172
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013173 hap_register_post_deinit(ssl_free_global_issuers);
13174
Willy Tarreau80713382018-11-26 10:19:54 +010013175#ifndef OPENSSL_NO_DH
13176 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13177 hap_register_post_deinit(ssl_free_dh);
13178#endif
13179#ifndef OPENSSL_NO_ENGINE
13180 hap_register_post_deinit(ssl_free_engines);
13181#endif
13182 /* Load SSL string for the verbose & debug mode. */
13183 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013184 ha_meth = BIO_meth_new(0x666, "ha methods");
13185 BIO_meth_set_write(ha_meth, ha_ssl_write);
13186 BIO_meth_set_read(ha_meth, ha_ssl_read);
13187 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13188 BIO_meth_set_create(ha_meth, ha_ssl_new);
13189 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13190 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13191 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013192
13193 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013194}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013195
Willy Tarreau80713382018-11-26 10:19:54 +010013196/* Compute and register the version string */
13197static void ssl_register_build_options()
13198{
13199 char *ptr = NULL;
13200 int i;
13201
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013202 memprintf(&ptr, "Built with OpenSSL version : "
13203#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013204 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013205#else /* OPENSSL_IS_BORINGSSL */
13206 OPENSSL_VERSION_TEXT
13207 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013208 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013209 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013210#endif
13211 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013212#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013213 "no (library version too old)"
13214#elif defined(OPENSSL_NO_TLSEXT)
13215 "no (disabled via OPENSSL_NO_TLSEXT)"
13216#else
13217 "yes"
13218#endif
13219 "", ptr);
13220
13221 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13222#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13223 "yes"
13224#else
13225#ifdef OPENSSL_NO_TLSEXT
13226 "no (because of OPENSSL_NO_TLSEXT)"
13227#else
13228 "no (version might be too old, 0.9.8f min needed)"
13229#endif
13230#endif
13231 "", ptr);
13232
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013233 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13234 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13235 if (methodVersions[i].option)
13236 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013237
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013238 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013239}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013240
Willy Tarreau80713382018-11-26 10:19:54 +010013241INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013242
Emeric Brun46591952012-05-18 15:47:34 +020013243
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013244#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013245void ssl_free_engines(void) {
13246 struct ssl_engine_list *wl, *wlb;
13247 /* free up engine list */
13248 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13249 ENGINE_finish(wl->e);
13250 ENGINE_free(wl->e);
13251 LIST_DEL(&wl->list);
13252 free(wl);
13253 }
13254}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013255#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013256
Remi Gacogned3a23c32015-05-28 16:39:47 +020013257#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013258void ssl_free_dh(void) {
13259 if (local_dh_1024) {
13260 DH_free(local_dh_1024);
13261 local_dh_1024 = NULL;
13262 }
13263 if (local_dh_2048) {
13264 DH_free(local_dh_2048);
13265 local_dh_2048 = NULL;
13266 }
13267 if (local_dh_4096) {
13268 DH_free(local_dh_4096);
13269 local_dh_4096 = NULL;
13270 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013271 if (global_dh) {
13272 DH_free(global_dh);
13273 global_dh = NULL;
13274 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013275}
13276#endif
13277
13278__attribute__((destructor))
13279static void __ssl_sock_deinit(void)
13280{
13281#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013282 if (ssl_ctx_lru_tree) {
13283 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013284 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013285 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013286#endif
13287
Willy Tarreau5db847a2019-05-09 14:13:35 +020013288#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013289 ERR_remove_state(0);
13290 ERR_free_strings();
13291
13292 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013293#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013294
Willy Tarreau5db847a2019-05-09 14:13:35 +020013295#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013296 CRYPTO_cleanup_all_ex_data();
13297#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013298 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013299}
13300
13301
Emeric Brun46591952012-05-18 15:47:34 +020013302/*
13303 * Local variables:
13304 * c-indent-level: 8
13305 * c-basic-offset: 8
13306 * End:
13307 */