blob: dfaea05b7bf127ab34ed8a2974c6af97bd0784a6 [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
Jerome Magninb203ff62020-04-03 15:28:22 +0200180#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
181 char *listen_default_curves;
182#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100183 int listen_default_ssloptions;
184 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200185 struct tls_version_filter listen_default_sslmethods;
186 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100187
188 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
189 unsigned int life_time; /* SSL session lifetime in seconds */
190 unsigned int max_record; /* SSL max record size */
191 unsigned int default_dh_param; /* SSL maximum DH parameter size */
192 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100193 int capture_cipherlist; /* Size of the cipherlist buffer. */
William Lallemand3af48e72020-02-03 17:15:52 +0100194 int extra_files; /* which files not defined in the configuration file are we looking for */
Willy Tarreauef934602016-12-22 23:12:01 +0100195} global_ssl = {
196#ifdef LISTEN_DEFAULT_CIPHERS
197 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
198#endif
199#ifdef CONNECT_DEFAULT_CIPHERS
200 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
201#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200202#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200203#ifdef LISTEN_DEFAULT_CIPHERSUITES
204 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
205#endif
206#ifdef CONNECT_DEFAULT_CIPHERSUITES
207 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
208#endif
209#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100210 .listen_default_ssloptions = BC_SSL_O_NONE,
211 .connect_default_ssloptions = SRV_SSL_O_NONE,
212
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200213 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
214 .listen_default_sslmethods.min = CONF_TLSV_NONE,
215 .listen_default_sslmethods.max = CONF_TLSV_NONE,
216 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
217 .connect_default_sslmethods.min = CONF_TLSV_NONE,
218 .connect_default_sslmethods.max = CONF_TLSV_NONE,
219
Willy Tarreauef934602016-12-22 23:12:01 +0100220#ifdef DEFAULT_SSL_MAX_RECORD
221 .max_record = DEFAULT_SSL_MAX_RECORD,
222#endif
223 .default_dh_param = SSL_DEFAULT_DH_PARAM,
224 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100225 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100226 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100227};
228
Olivier Houcharda8955d52019-04-07 22:00:38 +0200229static BIO_METHOD *ha_meth;
230
Olivier Houchard66ab4982019-02-26 18:37:15 +0100231struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200232 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100233 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200234 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200235 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100236 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200237 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100238 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100239 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100240 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100241 int sent_early_data; /* Amount of early data we sent so far */
242
Olivier Houchard66ab4982019-02-26 18:37:15 +0100243};
244
245DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
246
Olivier Houchardea8dd942019-05-20 14:02:16 +0200247static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200248static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200249
Olivier Houcharda8955d52019-04-07 22:00:38 +0200250/* Methods to implement OpenSSL BIO */
251static int ha_ssl_write(BIO *h, const char *buf, int num)
252{
253 struct buffer tmpbuf;
254 struct ssl_sock_ctx *ctx;
255 int ret;
256
257 ctx = BIO_get_data(h);
258 tmpbuf.size = num;
259 tmpbuf.area = (void *)(uintptr_t)buf;
260 tmpbuf.data = num;
261 tmpbuf.head = 0;
262 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200263 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200264 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200265 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200266 } else if (ret == 0)
267 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200268 return ret;
269}
270
271static int ha_ssl_gets(BIO *h, char *buf, int size)
272{
273
274 return 0;
275}
276
277static int ha_ssl_puts(BIO *h, const char *str)
278{
279
280 return ha_ssl_write(h, str, strlen(str));
281}
282
283static int ha_ssl_read(BIO *h, char *buf, int size)
284{
285 struct buffer tmpbuf;
286 struct ssl_sock_ctx *ctx;
287 int ret;
288
289 ctx = BIO_get_data(h);
290 tmpbuf.size = size;
291 tmpbuf.area = buf;
292 tmpbuf.data = 0;
293 tmpbuf.head = 0;
294 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200295 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200296 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200297 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200298 } else if (ret == 0)
299 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200300
301 return ret;
302}
303
304static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
305{
306 int ret = 0;
307 switch (cmd) {
308 case BIO_CTRL_DUP:
309 case BIO_CTRL_FLUSH:
310 ret = 1;
311 break;
312 }
313 return ret;
314}
315
316static int ha_ssl_new(BIO *h)
317{
318 BIO_set_init(h, 1);
319 BIO_set_data(h, NULL);
320 BIO_clear_flags(h, ~0);
321 return 1;
322}
323
324static int ha_ssl_free(BIO *data)
325{
326
327 return 1;
328}
329
330
Willy Tarreau5db847a2019-05-09 14:13:35 +0200331#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100332
Emeric Brun821bb9b2017-06-15 16:37:39 +0200333static HA_RWLOCK_T *ssl_rwlocks;
334
335
336unsigned long ssl_id_function(void)
337{
338 return (unsigned long)tid;
339}
340
341void ssl_locking_function(int mode, int n, const char * file, int line)
342{
343 if (mode & CRYPTO_LOCK) {
344 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200346 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100347 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200348 }
349 else {
350 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100351 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200352 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100353 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200354 }
355}
356
357static int ssl_locking_init(void)
358{
359 int i;
360
361 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
362 if (!ssl_rwlocks)
363 return -1;
364
365 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100366 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200367
368 CRYPTO_set_id_callback(ssl_id_function);
369 CRYPTO_set_locking_callback(ssl_locking_function);
370
371 return 0;
372}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100373
Emeric Brun821bb9b2017-06-15 16:37:39 +0200374#endif
375
William Lallemand150bfa82019-09-19 17:12:49 +0200376__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200377
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100378/* Uncommitted CKCH transaction */
379
380static struct {
381 struct ckch_store *new_ckchs;
382 struct ckch_store *old_ckchs;
383 char *path;
384} ckchs_transaction;
385
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200386/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100387 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200388 */
389struct cafile_entry {
390 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200391 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200392 struct ebmb_node node;
393 char path[0];
394};
395
396static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
397
398static X509_STORE* ssl_store_get0_locations_file(char *path)
399{
400 struct ebmb_node *eb;
401
402 eb = ebst_lookup(&cafile_tree, path);
403 if (eb) {
404 struct cafile_entry *ca_e;
405 ca_e = ebmb_entry(eb, struct cafile_entry, node);
406 return ca_e->ca_store;
407 }
408 return NULL;
409}
410
411static int ssl_store_load_locations_file(char *path)
412{
413 if (ssl_store_get0_locations_file(path) == NULL) {
414 struct cafile_entry *ca_e;
415 X509_STORE *store = X509_STORE_new();
416 if (X509_STORE_load_locations(store, path, NULL)) {
417 int pathlen;
418 pathlen = strlen(path);
419 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
420 if (ca_e) {
421 memcpy(ca_e->path, path, pathlen + 1);
422 ca_e->ca_store = store;
423 ebst_insert(&cafile_tree, &ca_e->node);
424 return 1;
425 }
426 }
427 X509_STORE_free(store);
428 return 0;
429 }
430 return 1;
431}
432
433/* mimic what X509_STORE_load_locations do with store_ctx */
434static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
435{
436 X509_STORE *store;
437 store = ssl_store_get0_locations_file(path);
438 if (store_ctx && store) {
439 int i;
440 X509_OBJECT *obj;
441 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
442 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
443 obj = sk_X509_OBJECT_value(objs, i);
444 switch (X509_OBJECT_get_type(obj)) {
445 case X509_LU_X509:
446 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
447 break;
448 case X509_LU_CRL:
449 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
450 break;
451 default:
452 break;
453 }
454 }
455 return 1;
456 }
457 return 0;
458}
459
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500460/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200461static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
462{
463 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
464 return ssl_set_cert_crl_file(store_ctx, path);
465}
466
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200467/*
468 Extract CA_list from CA_file already in tree.
469 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
470 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
471*/
472static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
473{
474 struct ebmb_node *eb;
475 struct cafile_entry *ca_e;
476
477 eb = ebst_lookup(&cafile_tree, path);
478 if (!eb)
479 return NULL;
480 ca_e = ebmb_entry(eb, struct cafile_entry, node);
481
482 if (ca_e->ca_list == NULL) {
483 int i;
484 unsigned long key;
485 struct eb_root ca_name_tree = EB_ROOT;
486 struct eb64_node *node, *back;
487 struct {
488 struct eb64_node node;
489 X509_NAME *xname;
490 } *ca_name;
491 STACK_OF(X509_OBJECT) *objs;
492 STACK_OF(X509_NAME) *skn;
493 X509 *x;
494 X509_NAME *xn;
495
496 skn = sk_X509_NAME_new_null();
497 /* take x509 from cafile_tree */
498 objs = X509_STORE_get0_objects(ca_e->ca_store);
499 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
500 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
501 if (!x)
502 continue;
503 xn = X509_get_subject_name(x);
504 if (!xn)
505 continue;
506 /* Check for duplicates. */
507 key = X509_NAME_hash(xn);
508 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
509 node && ca_name == NULL;
510 node = eb64_next(node)) {
511 ca_name = container_of(node, typeof(*ca_name), node);
512 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
513 ca_name = NULL;
514 }
515 /* find a duplicate */
516 if (ca_name)
517 continue;
518 ca_name = calloc(1, sizeof *ca_name);
519 xn = X509_NAME_dup(xn);
520 if (!ca_name ||
521 !xn ||
522 !sk_X509_NAME_push(skn, xn)) {
523 free(ca_name);
524 X509_NAME_free(xn);
525 sk_X509_NAME_pop_free(skn, X509_NAME_free);
526 sk_X509_NAME_free(skn);
527 skn = NULL;
528 break;
529 }
530 ca_name->node.key = key;
531 ca_name->xname = xn;
532 eb64_insert(&ca_name_tree, &ca_name->node);
533 }
534 ca_e->ca_list = skn;
535 /* remove temporary ca_name tree */
536 node = eb64_first(&ca_name_tree);
537 while (node) {
538 ca_name = container_of(node, typeof(*ca_name), node);
539 back = eb64_next(node);
540 eb64_delete(node);
541 free(ca_name);
542 node = back;
543 }
544 }
545 return ca_e->ca_list;
546}
547
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100548/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100549struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100550 unsigned long long int xxh64;
551 unsigned char ciphersuite_len;
552 char ciphersuite[0];
553};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100554struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100555static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200556static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100557
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200558#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
559struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
560#endif
561
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200562#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000563static unsigned int openssl_engines_initialized;
564struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
565struct ssl_engine_list {
566 struct list list;
567 ENGINE *e;
568};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200569#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000570
Remi Gacogne8de54152014-07-15 11:36:40 +0200571#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200572static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200573static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200574static DH *local_dh_1024 = NULL;
575static DH *local_dh_2048 = NULL;
576static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100577static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200578#endif /* OPENSSL_NO_DH */
579
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100580#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200581/* X509V3 Extensions that will be added on generated certificates */
582#define X509V3_EXT_SIZE 5
583static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
584 "basicConstraints",
585 "nsComment",
586 "subjectKeyIdentifier",
587 "authorityKeyIdentifier",
588 "keyUsage",
589};
590static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
591 "CA:FALSE",
592 "\"OpenSSL Generated Certificate\"",
593 "hash",
594 "keyid,issuer:always",
595 "nonRepudiation,digitalSignature,keyEncipherment"
596};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200597/* LRU cache to store generated certificate */
598static struct lru64_head *ssl_ctx_lru_tree = NULL;
599static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200600static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100601__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200602
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200603#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
604
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100605static struct ssl_bind_kw ssl_bind_kws[];
606
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200607#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500608/* The order here matters for picking a default context,
609 * keep the most common keytype at the bottom of the list
610 */
611const char *SSL_SOCK_KEYTYPE_NAMES[] = {
612 "dsa",
613 "ecdsa",
614 "rsa"
615};
616#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100617#else
618#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500619#endif
620
William Lallemandc3cd35f2017-11-28 11:04:43 +0100621static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100622static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
623
624#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
625
626#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
627 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
628
629#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
630 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200631
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100632/*
633 * This function gives the detail of the SSL error. It is used only
634 * if the debug mode and the verbose mode are activated. It dump all
635 * the SSL error until the stack was empty.
636 */
637static forceinline void ssl_sock_dump_errors(struct connection *conn)
638{
639 unsigned long ret;
640
641 if (unlikely(global.mode & MODE_DEBUG)) {
642 while(1) {
643 ret = ERR_get_error();
644 if (ret == 0)
645 return;
646 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200647 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100648 ERR_func_error_string(ret), ERR_reason_error_string(ret));
649 }
650 }
651}
652
yanbzhube2774d2015-12-10 15:07:30 -0500653
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200654#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000655static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
656{
657 int err_code = ERR_ABORT;
658 ENGINE *engine;
659 struct ssl_engine_list *el;
660
661 /* grab the structural reference to the engine */
662 engine = ENGINE_by_id(engine_id);
663 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100664 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000665 goto fail_get;
666 }
667
668 if (!ENGINE_init(engine)) {
669 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100670 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000671 goto fail_init;
672 }
673
674 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100675 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000676 goto fail_set_method;
677 }
678
679 el = calloc(1, sizeof(*el));
680 el->e = engine;
681 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100682 nb_engines++;
683 if (global_ssl.async)
684 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000685 return 0;
686
687fail_set_method:
688 /* release the functional reference from ENGINE_init() */
689 ENGINE_finish(engine);
690
691fail_init:
692 /* release the structural reference from ENGINE_by_id() */
693 ENGINE_free(engine);
694
695fail_get:
696 return err_code;
697}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200698#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000699
Willy Tarreau5db847a2019-05-09 14:13:35 +0200700#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200701/*
702 * openssl async fd handler
703 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200704void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000705{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200706 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000707
Emeric Brun3854e012017-05-17 20:42:48 +0200708 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000709 * to poll this fd until it is requested
710 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000711 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000712 fd_cant_recv(fd);
713
714 /* crypto engine is available, let's notify the associated
715 * connection that it can pursue its processing.
716 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200717 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000718}
719
Emeric Brun3854e012017-05-17 20:42:48 +0200720/*
721 * openssl async delayed SSL_free handler
722 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200723void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000724{
725 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200726 OSSL_ASYNC_FD all_fd[32];
727 size_t num_all_fds = 0;
728 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000729
Emeric Brun3854e012017-05-17 20:42:48 +0200730 /* We suppose that the async job for a same SSL *
731 * are serialized. So if we are awake it is
732 * because the running job has just finished
733 * and we can remove all async fds safely
734 */
735 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
736 if (num_all_fds > 32) {
737 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
738 return;
739 }
740
741 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
742 for (i=0 ; i < num_all_fds ; i++)
743 fd_remove(all_fd[i]);
744
745 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000746 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100747 _HA_ATOMIC_SUB(&sslconns, 1);
748 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000749}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000750/*
Emeric Brun3854e012017-05-17 20:42:48 +0200751 * function used to manage a returned SSL_ERROR_WANT_ASYNC
752 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000753 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200754static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000755{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100756 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200757 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200758 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000759 size_t num_add_fds = 0;
760 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200761 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000762
763 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
764 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200765 if (num_add_fds > 32 || num_del_fds > 32) {
766 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 +0000767 return;
768 }
769
Emeric Brun3854e012017-05-17 20:42:48 +0200770 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000771
Emeric Brun3854e012017-05-17 20:42:48 +0200772 /* We remove unused fds from the fdtab */
773 for (i=0 ; i < num_del_fds ; i++)
774 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000775
Emeric Brun3854e012017-05-17 20:42:48 +0200776 /* We add new fds to the fdtab */
777 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200778 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000779 }
780
Emeric Brun3854e012017-05-17 20:42:48 +0200781 num_add_fds = 0;
782 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
783 if (num_add_fds > 32) {
784 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
785 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000786 }
Emeric Brun3854e012017-05-17 20:42:48 +0200787
788 /* We activate the polling for all known async fds */
789 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000790 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200791 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000792 /* To ensure that the fd cache won't be used
793 * We'll prefer to catch a real RD event
794 * because handling an EAGAIN on this fd will
795 * result in a context switch and also
796 * some engines uses a fd in blocking mode.
797 */
798 fd_cant_recv(add_fd[i]);
799 }
Emeric Brun3854e012017-05-17 20:42:48 +0200800
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000801}
802#endif
803
William Lallemand104a7a62019-10-14 14:14:59 +0200804#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200805/*
806 * This function returns the number of seconds elapsed
807 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
808 * date presented un ASN1_GENERALIZEDTIME.
809 *
810 * In parsing error case, it returns -1.
811 */
812static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
813{
814 long epoch;
815 char *p, *end;
816 const unsigned short month_offset[12] = {
817 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
818 };
819 int year, month;
820
821 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
822
823 p = (char *)d->data;
824 end = p + d->length;
825
826 if (end - p < 4) return -1;
827 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
828 p += 4;
829 if (end - p < 2) return -1;
830 month = 10 * (p[0] - '0') + p[1] - '0';
831 if (month < 1 || month > 12) return -1;
832 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
833 We consider leap years and the current month (<marsh or not) */
834 epoch = ( ((year - 1970) * 365)
835 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
836 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
837 + month_offset[month-1]
838 ) * 24 * 60 * 60;
839 p += 2;
840 if (end - p < 2) return -1;
841 /* Add the number of seconds of completed days of current month */
842 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
843 p += 2;
844 if (end - p < 2) return -1;
845 /* Add the completed hours of the current day */
846 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
847 p += 2;
848 if (end - p < 2) return -1;
849 /* Add the completed minutes of the current hour */
850 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
851 p += 2;
852 if (p == end) return -1;
853 /* Test if there is available seconds */
854 if (p[0] < '0' || p[0] > '9')
855 goto nosec;
856 if (end - p < 2) return -1;
857 /* Add the seconds of the current minute */
858 epoch += 10 * (p[0] - '0') + p[1] - '0';
859 p += 2;
860 if (p == end) return -1;
861 /* Ignore seconds float part if present */
862 if (p[0] == '.') {
863 do {
864 if (++p == end) return -1;
865 } while (p[0] >= '0' && p[0] <= '9');
866 }
867
868nosec:
869 if (p[0] == 'Z') {
870 if (end - p != 1) return -1;
871 return epoch;
872 }
873 else if (p[0] == '+') {
874 if (end - p != 5) return -1;
875 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700876 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 +0200877 }
878 else if (p[0] == '-') {
879 if (end - p != 5) return -1;
880 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700881 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 +0200882 }
883
884 return -1;
885}
886
William Lallemand104a7a62019-10-14 14:14:59 +0200887/*
888 * struct alignment works here such that the key.key is the same as key_data
889 * Do not change the placement of key_data
890 */
891struct certificate_ocsp {
892 struct ebmb_node key;
893 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
894 struct buffer response;
895 long expire;
896};
897
898struct ocsp_cbk_arg {
899 int is_single;
900 int single_kt;
901 union {
902 struct certificate_ocsp *s_ocsp;
903 /*
904 * m_ocsp will have multiple entries dependent on key type
905 * Entry 0 - DSA
906 * Entry 1 - ECDSA
907 * Entry 2 - RSA
908 */
909 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
910 };
911};
912
Emeric Brun1d3865b2014-06-20 15:37:32 +0200913static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200914
915/* This function starts to check if the OCSP response (in DER format) contained
916 * in chunk 'ocsp_response' is valid (else exits on error).
917 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
918 * contained in the OCSP Response and exits on error if no match.
919 * If it's a valid OCSP Response:
920 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
921 * pointed by 'ocsp'.
922 * If 'ocsp' is NULL, the function looks up into the OCSP response's
923 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
924 * from the response) and exits on error if not found. Finally, If an OCSP response is
925 * already present in the container, it will be overwritten.
926 *
927 * Note: OCSP response containing more than one OCSP Single response is not
928 * considered valid.
929 *
930 * Returns 0 on success, 1 in error case.
931 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200932static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
933 struct certificate_ocsp *ocsp,
934 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200935{
936 OCSP_RESPONSE *resp;
937 OCSP_BASICRESP *bs = NULL;
938 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200939 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200940 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200941 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200942 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200943 int reason;
944 int ret = 1;
945
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200946 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
947 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200948 if (!resp) {
949 memprintf(err, "Unable to parse OCSP response");
950 goto out;
951 }
952
953 rc = OCSP_response_status(resp);
954 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
955 memprintf(err, "OCSP response status not successful");
956 goto out;
957 }
958
959 bs = OCSP_response_get1_basic(resp);
960 if (!bs) {
961 memprintf(err, "Failed to get basic response from OCSP Response");
962 goto out;
963 }
964
965 count_sr = OCSP_resp_count(bs);
966 if (count_sr > 1) {
967 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
968 goto out;
969 }
970
971 sr = OCSP_resp_get0(bs, 0);
972 if (!sr) {
973 memprintf(err, "Failed to get OCSP single response");
974 goto out;
975 }
976
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200977 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
978
Emeric Brun4147b2e2014-06-16 18:36:30 +0200979 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200980 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200981 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200982 goto out;
983 }
984
Emeric Brun13a6b482014-06-20 15:44:34 +0200985 if (!nextupd) {
986 memprintf(err, "OCSP single response: missing nextupdate");
987 goto out;
988 }
989
Emeric Brunc8b27b62014-06-19 14:16:17 +0200990 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200991 if (!rc) {
992 memprintf(err, "OCSP single response: no longer valid.");
993 goto out;
994 }
995
996 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200997 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200998 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
999 goto out;
1000 }
1001 }
1002
1003 if (!ocsp) {
1004 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
1005 unsigned char *p;
1006
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001007 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001008 if (!rc) {
1009 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
1010 goto out;
1011 }
1012
1013 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
1014 memprintf(err, "OCSP single response: Certificate ID too long");
1015 goto out;
1016 }
1017
1018 p = key;
1019 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001020 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001021 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1022 if (!ocsp) {
1023 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1024 goto out;
1025 }
1026 }
1027
1028 /* According to comments on "chunk_dup", the
1029 previous chunk buffer will be freed */
1030 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1031 memprintf(err, "OCSP response: Memory allocation error");
1032 goto out;
1033 }
1034
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001035 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1036
Emeric Brun4147b2e2014-06-16 18:36:30 +02001037 ret = 0;
1038out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001039 ERR_clear_error();
1040
Emeric Brun4147b2e2014-06-16 18:36:30 +02001041 if (bs)
1042 OCSP_BASICRESP_free(bs);
1043
1044 if (resp)
1045 OCSP_RESPONSE_free(resp);
1046
1047 return ret;
1048}
1049/*
1050 * External function use to update the OCSP response in the OCSP response's
1051 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1052 * to update in DER format.
1053 *
1054 * Returns 0 on success, 1 in error case.
1055 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001056int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001057{
1058 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1059}
1060
William Lallemand4a660132019-10-14 14:51:41 +02001061#endif
1062
1063#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001064/*
1065 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001066 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001067 *
1068 * Returns 0 on success, 1 in error case.
1069 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001070static 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 +02001071{
1072 int fd = -1;
1073 int r = 0;
1074 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001075 struct buffer *ocsp_response;
1076 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001077
William Lallemand3b5f3602019-10-16 18:05:05 +02001078 if (buf) {
1079 int i, j;
1080 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001081
William Lallemand3b5f3602019-10-16 18:05:05 +02001082 /* remove \r and \n from the payload */
1083 for (i = 0, j = 0; buf[i]; i++) {
1084 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001085 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001086 buf[j++] = buf[i];
1087 }
1088 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001089
William Lallemand3b5f3602019-10-16 18:05:05 +02001090 ret = base64dec(buf, j, trash.area, trash.size);
1091 if (ret < 0) {
1092 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001093 goto end;
1094 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001095 trash.data = ret;
1096 src = &trash;
1097 } else {
1098 fd = open(ocsp_path, O_RDONLY);
1099 if (fd == -1) {
1100 memprintf(err, "Error opening OCSP response file");
1101 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001102 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001103
1104 trash.data = 0;
1105 while (trash.data < trash.size) {
1106 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1107 if (r < 0) {
1108 if (errno == EINTR)
1109 continue;
1110
1111 memprintf(err, "Error reading OCSP response from file");
1112 goto end;
1113 }
1114 else if (r == 0) {
1115 break;
1116 }
1117 trash.data += r;
1118 }
1119 close(fd);
1120 fd = -1;
1121 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001122 }
1123
William Lallemand3b5f3602019-10-16 18:05:05 +02001124 ocsp_response = calloc(1, sizeof(*ocsp_response));
1125 if (!chunk_dup(ocsp_response, src)) {
1126 free(ocsp_response);
1127 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001128 goto end;
1129 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001130 /* no error, fill ckch with new context, old context must be free */
1131 if (ckch->ocsp_response) {
1132 free(ckch->ocsp_response->area);
1133 ckch->ocsp_response->area = NULL;
1134 free(ckch->ocsp_response);
1135 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001136 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001137 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001138end:
1139 if (fd != -1)
1140 close(fd);
1141
1142 return ret;
1143}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001144#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001145
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001146#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1147static 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)
1148{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001149 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001150 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001151 struct connection *conn;
1152 int head;
1153 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001154 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001155
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001156 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001157 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001158 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1159
1160 keys = ref->tlskeys;
1161 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001162
1163 if (enc) {
1164 memcpy(key_name, keys[head].name, 16);
1165
1166 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001167 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001168
Emeric Brun9e754772019-01-10 17:51:55 +01001169 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001170
Emeric Brun9e754772019-01-10 17:51:55 +01001171 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1172 goto end;
1173
Willy Tarreau9356dac2019-05-10 09:22:53 +02001174 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001175 ret = 1;
1176 }
1177 else if (ref->key_size_bits == 256 ) {
1178
1179 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1180 goto end;
1181
Willy Tarreau9356dac2019-05-10 09:22:53 +02001182 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001183 ret = 1;
1184 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001185 } else {
1186 for (i = 0; i < TLS_TICKETS_NO; i++) {
1187 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1188 goto found;
1189 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001190 ret = 0;
1191 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001192
Christopher Faulet16f45c82018-02-16 11:23:49 +01001193 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001194 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001195 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 +01001196 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1197 goto end;
1198 /* 2 for key renewal, 1 if current key is still valid */
1199 ret = i ? 2 : 1;
1200 }
1201 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001202 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 +01001203 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1204 goto end;
1205 /* 2 for key renewal, 1 if current key is still valid */
1206 ret = i ? 2 : 1;
1207 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001208 }
Emeric Brun9e754772019-01-10 17:51:55 +01001209
Christopher Faulet16f45c82018-02-16 11:23:49 +01001210 end:
1211 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1212 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001213}
1214
1215struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1216{
1217 struct tls_keys_ref *ref;
1218
1219 list_for_each_entry(ref, &tlskeys_reference, list)
1220 if (ref->filename && strcmp(filename, ref->filename) == 0)
1221 return ref;
1222 return NULL;
1223}
1224
1225struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1226{
1227 struct tls_keys_ref *ref;
1228
1229 list_for_each_entry(ref, &tlskeys_reference, list)
1230 if (ref->unique_id == unique_id)
1231 return ref;
1232 return NULL;
1233}
1234
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001235/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001236 * match existing ones, this function returns -1
1237 * else it returns 0 on success.
1238 */
1239int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001240 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001241{
Emeric Brun9e754772019-01-10 17:51:55 +01001242 if (ref->key_size_bits == 128) {
1243 if (tlskey->data != sizeof(struct tls_sess_key_128))
1244 return -1;
1245 }
1246 else if (ref->key_size_bits == 256) {
1247 if (tlskey->data != sizeof(struct tls_sess_key_256))
1248 return -1;
1249 }
1250 else
1251 return -1;
1252
Christopher Faulet16f45c82018-02-16 11:23:49 +01001253 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001254 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1255 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001256 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1257 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001258
1259 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001260}
1261
Willy Tarreau83061a82018-07-13 11:56:34 +02001262int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001263{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001264 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1265
1266 if(!ref) {
1267 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1268 return 1;
1269 }
Emeric Brun9e754772019-01-10 17:51:55 +01001270 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1271 memprintf(err, "Invalid key size");
1272 return 1;
1273 }
1274
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001275 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001276}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001277
1278/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001279 * automatic ids. It's called just after the basic checks. It returns
1280 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001281 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001282static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001283{
1284 int i = 0;
1285 struct tls_keys_ref *ref, *ref2, *ref3;
1286 struct list tkr = LIST_HEAD_INIT(tkr);
1287
1288 list_for_each_entry(ref, &tlskeys_reference, list) {
1289 if (ref->unique_id == -1) {
1290 /* Look for the first free id. */
1291 while (1) {
1292 list_for_each_entry(ref2, &tlskeys_reference, list) {
1293 if (ref2->unique_id == i) {
1294 i++;
1295 break;
1296 }
1297 }
1298 if (&ref2->list == &tlskeys_reference)
1299 break;
1300 }
1301
1302 /* Uses the unique id and increment it for the next entry. */
1303 ref->unique_id = i;
1304 i++;
1305 }
1306 }
1307
1308 /* This sort the reference list by id. */
1309 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1310 LIST_DEL(&ref->list);
1311 list_for_each_entry(ref3, &tkr, list) {
1312 if (ref->unique_id < ref3->unique_id) {
1313 LIST_ADDQ(&ref3->list, &ref->list);
1314 break;
1315 }
1316 }
1317 if (&ref3->list == &tkr)
1318 LIST_ADDQ(&tkr, &ref->list);
1319 }
1320
1321 /* swap root */
1322 LIST_ADD(&tkr, &tlskeys_reference);
1323 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001324 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001325}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001326#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1327
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001328#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001329int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1330{
1331 switch (evp_keytype) {
1332 case EVP_PKEY_RSA:
1333 return 2;
1334 case EVP_PKEY_DSA:
1335 return 0;
1336 case EVP_PKEY_EC:
1337 return 1;
1338 }
1339
1340 return -1;
1341}
1342
Emeric Brun4147b2e2014-06-16 18:36:30 +02001343/*
1344 * Callback used to set OCSP status extension content in server hello.
1345 */
1346int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1347{
yanbzhube2774d2015-12-10 15:07:30 -05001348 struct certificate_ocsp *ocsp;
1349 struct ocsp_cbk_arg *ocsp_arg;
1350 char *ssl_buf;
1351 EVP_PKEY *ssl_pkey;
1352 int key_type;
1353 int index;
1354
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001355 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001356
1357 ssl_pkey = SSL_get_privatekey(ssl);
1358 if (!ssl_pkey)
1359 return SSL_TLSEXT_ERR_NOACK;
1360
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001361 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001362
1363 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1364 ocsp = ocsp_arg->s_ocsp;
1365 else {
1366 /* For multiple certs per context, we have to find the correct OCSP response based on
1367 * the certificate type
1368 */
1369 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1370
1371 if (index < 0)
1372 return SSL_TLSEXT_ERR_NOACK;
1373
1374 ocsp = ocsp_arg->m_ocsp[index];
1375
1376 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001377
1378 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001379 !ocsp->response.area ||
1380 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001381 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001382 return SSL_TLSEXT_ERR_NOACK;
1383
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001384 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001385 if (!ssl_buf)
1386 return SSL_TLSEXT_ERR_NOACK;
1387
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001388 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1389 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001390
1391 return SSL_TLSEXT_ERR_OK;
1392}
1393
William Lallemand4a660132019-10-14 14:51:41 +02001394#endif
1395
1396#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001397/*
1398 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001399 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1400 * status extension, the issuer's certificate is mandatory. It should be
1401 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001402 *
William Lallemand246c0242019-10-11 08:59:13 +02001403 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1404 * OCSP response. If file is empty or content is not a valid OCSP response,
1405 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1406 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001407 *
1408 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001409 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001410 */
William Lallemand4a660132019-10-14 14:51:41 +02001411#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001412static 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 +02001413{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001414 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001415 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001416 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001417 struct certificate_ocsp *ocsp = NULL, *iocsp;
1418 char *warn = NULL;
1419 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001420 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001421
Emeric Brun4147b2e2014-06-16 18:36:30 +02001422
William Lallemand246c0242019-10-11 08:59:13 +02001423 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001424 if (!x)
1425 goto out;
1426
William Lallemand246c0242019-10-11 08:59:13 +02001427 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001428 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1429 if (chain) {
1430 /* check if one of the certificate of the chain is the issuer */
1431 for (i = 0; i < sk_X509_num(chain); i++) {
1432 X509 *ti = sk_X509_value(chain, i);
1433 if (X509_check_issued(ti, x) == X509_V_OK) {
1434 issuer = ti;
1435 break;
1436 }
1437 }
1438 }
William Lallemand246c0242019-10-11 08:59:13 +02001439 if (!issuer)
1440 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001441
1442 cid = OCSP_cert_to_id(0, x, issuer);
1443 if (!cid)
1444 goto out;
1445
1446 i = i2d_OCSP_CERTID(cid, NULL);
1447 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1448 goto out;
1449
Vincent Bernat02779b62016-04-03 13:48:43 +02001450 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001451 if (!ocsp)
1452 goto out;
1453
1454 p = ocsp->key_data;
1455 i2d_OCSP_CERTID(cid, &p);
1456
1457 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1458 if (iocsp == ocsp)
1459 ocsp = NULL;
1460
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001461#ifndef SSL_CTX_get_tlsext_status_cb
1462# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1463 *cb = (void (*) (void))ctx->tlsext_status_cb;
1464#endif
1465 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1466
1467 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001468 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001469 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001470
1471 cb_arg->is_single = 1;
1472 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001473
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001474 pkey = X509_get_pubkey(x);
1475 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1476 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001477
1478 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1479 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1480 } else {
1481 /*
1482 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1483 * Update that cb_arg with the new cert's staple
1484 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001485 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001486 struct certificate_ocsp *tmp_ocsp;
1487 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001488 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001489 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001490
1491#ifdef SSL_CTX_get_tlsext_status_arg
1492 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1493#else
1494 cb_arg = ctx->tlsext_status_arg;
1495#endif
yanbzhube2774d2015-12-10 15:07:30 -05001496
1497 /*
1498 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1499 * the order of operations below matter, take care when changing it
1500 */
1501 tmp_ocsp = cb_arg->s_ocsp;
1502 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1503 cb_arg->s_ocsp = NULL;
1504 cb_arg->m_ocsp[index] = tmp_ocsp;
1505 cb_arg->is_single = 0;
1506 cb_arg->single_kt = 0;
1507
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001508 pkey = X509_get_pubkey(x);
1509 key_type = EVP_PKEY_base_id(pkey);
1510 EVP_PKEY_free(pkey);
1511
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001512 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001513 if (index >= 0 && !cb_arg->m_ocsp[index])
1514 cb_arg->m_ocsp[index] = iocsp;
1515
1516 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001517
1518 ret = 0;
1519
1520 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001521 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001522 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001523 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001524 }
1525
1526out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001527 if (cid)
1528 OCSP_CERTID_free(cid);
1529
1530 if (ocsp)
1531 free(ocsp);
1532
1533 if (warn)
1534 free(warn);
1535
Emeric Brun4147b2e2014-06-16 18:36:30 +02001536 return ret;
1537}
William Lallemand4a660132019-10-14 14:51:41 +02001538#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001539static 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 +02001540{
William Lallemand4a660132019-10-14 14:51:41 +02001541 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 +02001542}
1543#endif
1544
William Lallemand4a660132019-10-14 14:51:41 +02001545#endif
1546
1547
Willy Tarreau5db847a2019-05-09 14:13:35 +02001548#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001549
1550#define CT_EXTENSION_TYPE 18
1551
1552static int sctl_ex_index = -1;
1553
1554/*
1555 * Try to parse Signed Certificate Timestamp List structure. This function
1556 * makes only basic test if the data seems like SCTL. No signature validation
1557 * is performed.
1558 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001559static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001560{
1561 int ret = 1;
1562 int len, pos, sct_len;
1563 unsigned char *data;
1564
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001565 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001566 goto out;
1567
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001568 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001569 len = (data[0] << 8) | data[1];
1570
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001571 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001572 goto out;
1573
1574 data = data + 2;
1575 pos = 0;
1576 while (pos < len) {
1577 if (len - pos < 2)
1578 goto out;
1579
1580 sct_len = (data[pos] << 8) | data[pos + 1];
1581 if (pos + sct_len + 2 > len)
1582 goto out;
1583
1584 pos += sct_len + 2;
1585 }
1586
1587 ret = 0;
1588
1589out:
1590 return ret;
1591}
1592
William Lallemand0dfae6c2019-10-16 18:06:58 +02001593/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1594 * It fills the ckch->sctl buffer
1595 * return 0 on success or != 0 on failure */
1596static 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 +01001597{
1598 int fd = -1;
1599 int r = 0;
1600 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001601 struct buffer tmp;
1602 struct buffer *src;
1603 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001604
William Lallemand0dfae6c2019-10-16 18:06:58 +02001605 if (buf) {
1606 tmp.area = buf;
1607 tmp.data = strlen(buf);
1608 tmp.size = tmp.data + 1;
1609 src = &tmp;
1610 } else {
1611 fd = open(sctl_path, O_RDONLY);
1612 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001613 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001614
1615 trash.data = 0;
1616 while (trash.data < trash.size) {
1617 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1618 if (r < 0) {
1619 if (errno == EINTR)
1620 continue;
1621 goto end;
1622 }
1623 else if (r == 0) {
1624 break;
1625 }
1626 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001627 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001628 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001629 }
1630
William Lallemand0dfae6c2019-10-16 18:06:58 +02001631 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001632 if (ret)
1633 goto end;
1634
William Lallemand0dfae6c2019-10-16 18:06:58 +02001635 sctl = calloc(1, sizeof(*sctl));
1636 if (!chunk_dup(sctl, src)) {
1637 free(sctl);
1638 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001639 goto end;
1640 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001641 /* no error, fill ckch with new context, old context must be free */
1642 if (ckch->sctl) {
1643 free(ckch->sctl->area);
1644 ckch->sctl->area = NULL;
1645 free(ckch->sctl);
1646 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001647 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001648 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001649end:
1650 if (fd != -1)
1651 close(fd);
1652
1653 return ret;
1654}
1655
1656int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1657{
Willy Tarreau83061a82018-07-13 11:56:34 +02001658 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001659
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001660 *out = (unsigned char *) sctl->area;
1661 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001662
1663 return 1;
1664}
1665
1666int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1667{
1668 return 1;
1669}
1670
William Lallemanda17f4112019-10-10 15:16:44 +02001671static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001672{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001673 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001674
William Lallemanda17f4112019-10-10 15:16:44 +02001675 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 +01001676 goto out;
1677
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001678 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1679
1680 ret = 0;
1681
1682out:
1683 return ret;
1684}
1685
1686#endif
1687
Emeric Brune1f38db2012-09-03 20:36:47 +02001688void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1689{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001690 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001691 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001692 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001693 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001694
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001695#ifndef SSL_OP_NO_RENEGOTIATION
1696 /* Please note that BoringSSL defines this macro to zero so don't
1697 * change this to #if and do not assign a default value to this macro!
1698 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001699 if (where & SSL_CB_HANDSHAKE_START) {
1700 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001701 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 +02001702 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001703 conn->err_code = CO_ER_SSL_RENEG;
1704 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001705 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001706#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001707
1708 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001709 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001710 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001711 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001712 consider that the buffering was activated,
1713 so we rise the output buffer size from 4k
1714 to 16k */
1715 write_bio = SSL_get_wbio(ssl);
1716 if (write_bio != SSL_get_rbio(ssl)) {
1717 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001718 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001719 }
1720 }
1721 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001722}
1723
Emeric Brune64aef12012-09-21 13:15:06 +02001724/* Callback is called for each certificate of the chain during a verify
1725 ok is set to 1 if preverify detect no error on current certificate.
1726 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001727int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001728{
1729 SSL *ssl;
1730 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001731 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001732 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001733
1734 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001735 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001736
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001737 ctx = conn->xprt_ctx;
1738
1739 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001740
Emeric Brun81c00f02012-09-21 14:31:21 +02001741 if (ok) /* no errors */
1742 return ok;
1743
1744 depth = X509_STORE_CTX_get_error_depth(x_store);
1745 err = X509_STORE_CTX_get_error(x_store);
1746
1747 /* check if CA error needs to be ignored */
1748 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001749 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1750 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1751 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001752 }
1753
Willy Tarreau731248f2020-02-04 14:02:02 +01001754 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001755 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001756 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001757 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001758 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001759
Willy Tarreau20879a02012-12-03 16:32:10 +01001760 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001761 return 0;
1762 }
1763
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001764 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1765 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001766
Emeric Brun81c00f02012-09-21 14:31:21 +02001767 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001768 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001769 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001770 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001771 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001772 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001773
Willy Tarreau20879a02012-12-03 16:32:10 +01001774 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001775 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001776}
1777
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001778static inline
1779void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001780 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001781{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001782 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001783 unsigned char *msg;
1784 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001785 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001786
1787 /* This function is called for "from client" and "to server"
1788 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001789 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001790 */
1791
1792 /* "write_p" is set to 0 is the bytes are received messages,
1793 * otherwise it is set to 1.
1794 */
1795 if (write_p != 0)
1796 return;
1797
1798 /* content_type contains the type of message received or sent
1799 * according with the SSL/TLS protocol spec. This message is
1800 * encoded with one byte. The value 256 (two bytes) is used
1801 * for designing the SSL/TLS record layer. According with the
1802 * rfc6101, the expected message (other than 256) are:
1803 * - change_cipher_spec(20)
1804 * - alert(21)
1805 * - handshake(22)
1806 * - application_data(23)
1807 * - (255)
1808 * We are interessed by the handshake and specially the client
1809 * hello.
1810 */
1811 if (content_type != 22)
1812 return;
1813
1814 /* The message length is at least 4 bytes, containing the
1815 * message type and the message length.
1816 */
1817 if (len < 4)
1818 return;
1819
1820 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001821 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001822 * - hello_request(0)
1823 * - client_hello(1)
1824 * - server_hello(2)
1825 * - certificate(11)
1826 * - server_key_exchange (12)
1827 * - certificate_request(13)
1828 * - server_hello_done(14)
1829 * We are interested by the client hello.
1830 */
1831 msg = (unsigned char *)buf;
1832 if (msg[0] != 1)
1833 return;
1834
1835 /* Next three bytes are the length of the message. The total length
1836 * must be this decoded length + 4. If the length given as argument
1837 * is not the same, we abort the protocol dissector.
1838 */
1839 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1840 if (len < rec_len + 4)
1841 return;
1842 msg += 4;
1843 end = msg + rec_len;
1844 if (end < msg)
1845 return;
1846
1847 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1848 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001849 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1850 */
1851 msg += 1 + 1 + 4 + 28;
1852 if (msg > end)
1853 return;
1854
1855 /* Next, is session id:
1856 * if present, we have to jump by length + 1 for the size information
1857 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001858 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001859 if (msg[0] > 0)
1860 msg += msg[0];
1861 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001862 if (msg > end)
1863 return;
1864
1865 /* Next two bytes are the ciphersuite length. */
1866 if (msg + 2 > end)
1867 return;
1868 rec_len = (msg[0] << 8) + msg[1];
1869 msg += 2;
1870 if (msg + rec_len > end || msg + rec_len < msg)
1871 return;
1872
Willy Tarreaubafbe012017-11-24 17:34:44 +01001873 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001874 if (!capture)
1875 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001876 /* Compute the xxh64 of the ciphersuite. */
1877 capture->xxh64 = XXH64(msg, rec_len, 0);
1878
1879 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001880 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1881 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001882 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001883
1884 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001885}
1886
Emeric Brun29f037d2014-04-25 19:05:36 +02001887/* Callback is called for ssl protocol analyse */
1888void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1889{
Emeric Brun29f037d2014-04-25 19:05:36 +02001890#ifdef TLS1_RT_HEARTBEAT
1891 /* test heartbeat received (write_p is set to 0
1892 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001893 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001894 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001895 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001896 const unsigned char *p = buf;
1897 unsigned int payload;
1898
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001899 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001900
1901 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1902 if (*p != TLS1_HB_REQUEST)
1903 return;
1904
Willy Tarreauaeed6722014-04-25 23:59:58 +02001905 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001906 goto kill_it;
1907
1908 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001909 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001910 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001911 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001912 /* We have a clear heartbleed attack (CVE-2014-0160), the
1913 * advertised payload is larger than the advertised packet
1914 * length, so we have garbage in the buffer between the
1915 * payload and the end of the buffer (p+len). We can't know
1916 * if the SSL stack is patched, and we don't know if we can
1917 * safely wipe out the area between p+3+len and payload.
1918 * So instead, we prevent the response from being sent by
1919 * setting the max_send_fragment to 0 and we report an SSL
1920 * error, which will kill this connection. It will be reported
1921 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001922 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1923 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001924 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001925 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1926 return;
1927 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001928#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001929 if (global_ssl.capture_cipherlist > 0)
1930 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001931}
1932
Bernard Spil13c53f82018-02-15 13:34:58 +01001933#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001934static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1935 const unsigned char *in, unsigned int inlen,
1936 void *arg)
1937{
1938 struct server *srv = arg;
1939
1940 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1941 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1942 return SSL_TLSEXT_ERR_OK;
1943 return SSL_TLSEXT_ERR_NOACK;
1944}
1945#endif
1946
1947#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001948/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001949 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001950 */
1951static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1952 unsigned int *len, void *arg)
1953{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001954 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001955
1956 *data = (const unsigned char *)conf->npn_str;
1957 *len = conf->npn_len;
1958 return SSL_TLSEXT_ERR_OK;
1959}
1960#endif
1961
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001962#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001963/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001964 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001965 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001966static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1967 unsigned char *outlen,
1968 const unsigned char *server,
1969 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001970{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001971 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001972
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001973 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1974 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1975 return SSL_TLSEXT_ERR_NOACK;
1976 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001977 return SSL_TLSEXT_ERR_OK;
1978}
1979#endif
1980
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001981#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001982#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001983
Christopher Faulet30548802015-06-11 13:39:32 +02001984/* Create a X509 certificate with the specified servername and serial. This
1985 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001986static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001987ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001988{
Christopher Faulet7969a332015-10-09 11:15:03 +02001989 X509 *cacert = bind_conf->ca_sign_cert;
1990 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001991 SSL_CTX *ssl_ctx = NULL;
1992 X509 *newcrt = NULL;
1993 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001994 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001995 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001996 X509_NAME *name;
1997 const EVP_MD *digest;
1998 X509V3_CTX ctx;
1999 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002000 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002001
Christopher Faulet48a83322017-07-28 16:56:09 +02002002 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02002003#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002004 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
2005#else
2006 tmp_ssl = SSL_new(bind_conf->default_ctx);
2007 if (tmp_ssl)
2008 pkey = SSL_get_privatekey(tmp_ssl);
2009#endif
2010 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002011 goto mkcert_error;
2012
2013 /* Create the certificate */
2014 if (!(newcrt = X509_new()))
2015 goto mkcert_error;
2016
2017 /* Set version number for the certificate (X509v3) and the serial
2018 * number */
2019 if (X509_set_version(newcrt, 2L) != 1)
2020 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002021 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002022
2023 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002024 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2025 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002026 goto mkcert_error;
2027
2028 /* set public key in the certificate */
2029 if (X509_set_pubkey(newcrt, pkey) != 1)
2030 goto mkcert_error;
2031
2032 /* Set issuer name from the CA */
2033 if (!(name = X509_get_subject_name(cacert)))
2034 goto mkcert_error;
2035 if (X509_set_issuer_name(newcrt, name) != 1)
2036 goto mkcert_error;
2037
2038 /* Set the subject name using the same, but the CN */
2039 name = X509_NAME_dup(name);
2040 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2041 (const unsigned char *)servername,
2042 -1, -1, 0) != 1) {
2043 X509_NAME_free(name);
2044 goto mkcert_error;
2045 }
2046 if (X509_set_subject_name(newcrt, name) != 1) {
2047 X509_NAME_free(name);
2048 goto mkcert_error;
2049 }
2050 X509_NAME_free(name);
2051
2052 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002053 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002054 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2055 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2056 X509_EXTENSION *ext;
2057
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002058 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002059 goto mkcert_error;
2060 if (!X509_add_ext(newcrt, ext, -1)) {
2061 X509_EXTENSION_free(ext);
2062 goto mkcert_error;
2063 }
2064 X509_EXTENSION_free(ext);
2065 }
2066
2067 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002068
2069 key_type = EVP_PKEY_base_id(capkey);
2070
2071 if (key_type == EVP_PKEY_DSA)
2072 digest = EVP_sha1();
2073 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002074 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002075 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002076 digest = EVP_sha256();
2077 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002078#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002079 int nid;
2080
2081 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2082 goto mkcert_error;
2083 if (!(digest = EVP_get_digestbynid(nid)))
2084 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002085#else
2086 goto mkcert_error;
2087#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002088 }
2089
Christopher Faulet31af49d2015-06-09 17:29:50 +02002090 if (!(X509_sign(newcrt, capkey, digest)))
2091 goto mkcert_error;
2092
2093 /* Create and set the new SSL_CTX */
2094 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2095 goto mkcert_error;
2096 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2097 goto mkcert_error;
2098 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2099 goto mkcert_error;
2100 if (!SSL_CTX_check_private_key(ssl_ctx))
2101 goto mkcert_error;
2102
2103 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002104
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002105#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002106 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002107#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002108#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2109 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002110 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002111 EC_KEY *ecc;
2112 int nid;
2113
2114 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2115 goto end;
2116 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2117 goto end;
2118 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2119 EC_KEY_free(ecc);
2120 }
2121#endif
2122 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002123 return ssl_ctx;
2124
2125 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002126 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002127 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002128 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2129 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002130 return NULL;
2131}
2132
Christopher Faulet7969a332015-10-09 11:15:03 +02002133SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002134ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002135{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002136 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002137 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002138
Olivier Houchard66ab4982019-02-26 18:37:15 +01002139 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002140}
2141
Christopher Faulet30548802015-06-11 13:39:32 +02002142/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002143 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002144SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002145ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002146{
2147 struct lru64 *lru = NULL;
2148
2149 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002150 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002151 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002152 if (lru && lru->domain) {
2153 if (ssl)
2154 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
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 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002157 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002158 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002159 }
2160 return NULL;
2161}
2162
Emeric Brun821bb9b2017-06-15 16:37:39 +02002163/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2164 * function is not thread-safe, it should only be used to check if a certificate
2165 * exists in the lru cache (with no warranty it will not be removed by another
2166 * thread). It is kept for backward compatibility. */
2167SSL_CTX *
2168ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2169{
2170 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2171}
2172
Christopher Fauletd2cab922015-07-28 16:03:47 +02002173/* Set a certificate int the LRU cache used to store generated
2174 * certificate. Return 0 on success, otherwise -1 */
2175int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002176ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002177{
2178 struct lru64 *lru = NULL;
2179
2180 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002181 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002182 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002183 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002184 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002185 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002186 }
Christopher Faulet30548802015-06-11 13:39:32 +02002187 if (lru->domain && lru->data)
2188 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002189 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002190 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002191 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002192 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002193 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002194}
2195
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002196/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002197unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002198ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002199{
2200 return XXH32(data, len, ssl_ctx_lru_seed);
2201}
2202
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002203/* Generate a cert and immediately assign it to the SSL session so that the cert's
2204 * refcount is maintained regardless of the cert's presence in the LRU cache.
2205 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002206static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002207ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002208{
2209 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002210 SSL_CTX *ssl_ctx = NULL;
2211 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002212 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002213
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002214 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002215 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002216 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002217 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002218 if (lru && lru->domain)
2219 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002220 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002221 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002222 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002223 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002224 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002225 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002226 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002227 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002228 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002229 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002230 SSL_set_SSL_CTX(ssl, ssl_ctx);
2231 /* No LRU cache, this CTX will be released as soon as the session dies */
2232 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002233 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002234 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002235 return 0;
2236}
2237static int
2238ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2239{
2240 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002241 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002242
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002243 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002244 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002245 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002246 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002247 }
2248 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002249}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002250#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002251
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002252#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002253typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2254
2255static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002256{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002257#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002258 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002259 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2260#endif
2261}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002262static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2263 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002264 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2265}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002266static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002267#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002268 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002269 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2270#endif
2271}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002272static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002273#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002274 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002275 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2276#endif
2277}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002278/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002279static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2280/* Unusable in this context. */
2281static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2282static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2283static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2284static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2285static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002286#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002287typedef enum { SET_MIN, SET_MAX } set_context_func;
2288
2289static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2290 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002291 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2292}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002293static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2294 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2295 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2296}
2297static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2298 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002299 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2300}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002301static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2302 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2303 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2304}
2305static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2306 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002307 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2308}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002309static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2310 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2311 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2312}
2313static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2314 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002315 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2316}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002317static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2318 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2319 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2320}
2321static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002322#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002323 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002324 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2325#endif
2326}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002327static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2328#if SSL_OP_NO_TLSv1_3
2329 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2330 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002331#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002332}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002333#endif
2334static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2335static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002336
2337static struct {
2338 int option;
2339 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002340 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2341 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002342 const char *name;
2343} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002344 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2345 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2346 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2347 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2348 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2349 {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 +02002350};
2351
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002352static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2353{
2354 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2355 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2356 SSL_set_SSL_CTX(ssl, ctx);
2357}
2358
Willy Tarreau5db847a2019-05-09 14:13:35 +02002359#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002360
2361static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2362{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002363 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002364 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002365
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002366 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2367 return SSL_TLSEXT_ERR_OK;
2368 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369}
2370
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002371#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002372static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2373{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002374 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002375#else
2376static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2377{
2378#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002379 struct connection *conn;
2380 struct bind_conf *s;
2381 const uint8_t *extension_data;
2382 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002383 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002384
2385 char *wildp = NULL;
2386 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002387 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002388 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002389 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002390 int i;
2391
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002392 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002393 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002394
Olivier Houchard9679ac92017-10-27 14:58:08 +02002395 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002396 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002397#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002398 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2399 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002400#else
2401 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2402#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002403 /*
2404 * The server_name extension was given too much extensibility when it
2405 * was written, so parsing the normal case is a bit complex.
2406 */
2407 size_t len;
2408 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002409 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002410 /* Extract the length of the supplied list of names. */
2411 len = (*extension_data++) << 8;
2412 len |= *extension_data++;
2413 if (len + 2 != extension_len)
2414 goto abort;
2415 /*
2416 * The list in practice only has a single element, so we only consider
2417 * the first one.
2418 */
2419 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2420 goto abort;
2421 extension_len = len - 1;
2422 /* Now we can finally pull out the byte array with the actual hostname. */
2423 if (extension_len <= 2)
2424 goto abort;
2425 len = (*extension_data++) << 8;
2426 len |= *extension_data++;
2427 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2428 || memchr(extension_data, 0, len) != NULL)
2429 goto abort;
2430 servername = extension_data;
2431 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002432 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002433#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2434 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002435 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002436 }
2437#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002439 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002440 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002441 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002442 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002443 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002444 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002445 goto abort;
2446 }
2447
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002448 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002449#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002450 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002451#else
2452 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2453#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002454 uint8_t sign;
2455 size_t len;
2456 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002457 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 len = (*extension_data++) << 8;
2459 len |= *extension_data++;
2460 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002461 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002462 if (len % 2 != 0)
2463 goto abort;
2464 for (; len > 0; len -= 2) {
2465 extension_data++; /* hash */
2466 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002467 switch (sign) {
2468 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002469 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002470 break;
2471 case TLSEXT_signature_ecdsa:
2472 has_ecdsa_sig = 1;
2473 break;
2474 default:
2475 continue;
2476 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002477 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002478 break;
2479 }
2480 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002481 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002482 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002483 }
2484 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002485 const SSL_CIPHER *cipher;
2486 size_t len;
2487 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002488 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002489#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002490 len = ctx->cipher_suites_len;
2491 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002492#else
2493 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2494#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002495 if (len % 2 != 0)
2496 goto abort;
2497 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002498#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002499 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002500 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002501#else
2502 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2503#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002504 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002505 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002506 break;
2507 }
2508 }
2509 }
2510
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002511 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 trash.area[i] = tolower(servername[i]);
2513 if (!wildp && (trash.area[i] == '.'))
2514 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002515 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002516 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002517
William Lallemand150bfa82019-09-19 17:12:49 +02002518 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002519
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002520 for (i = 0; i < 2; i++) {
2521 if (i == 0) /* lookup in full qualified names */
2522 node = ebst_lookup(&s->sni_ctx, trash.area);
2523 else if (i == 1 && wildp) /* lookup in wildcards names */
2524 node = ebst_lookup(&s->sni_w_ctx, wildp);
2525 else
2526 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002527 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002528 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002529 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002530 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002531 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002532 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002533 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002534 break;
2535 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002536 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002537 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002538 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002539 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002540 if (!node_anonymous)
2541 node_anonymous = n;
2542 break;
2543 }
2544 }
2545 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002546 /* select by key_signature priority order */
2547 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2548 : ((has_rsa_sig && node_rsa) ? node_rsa
2549 : (node_anonymous ? node_anonymous
2550 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2551 : node_rsa /* no rsa signature case (far far away) */
2552 )));
2553 if (node) {
2554 /* switch ctx */
2555 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2556 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002557 if (conf) {
2558 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2559 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2560 if (conf->early_data)
2561 allow_early = 1;
2562 }
William Lallemand02010472019-10-18 11:02:19 +02002563 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002564 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002565 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002566 }
William Lallemand150bfa82019-09-19 17:12:49 +02002567
William Lallemand02010472019-10-18 11:02:19 +02002568 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002569#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002570 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002571 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002572 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002573 }
2574#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002575 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002576 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002577 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002578 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002579 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002580 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002581allow_early:
2582#ifdef OPENSSL_IS_BORINGSSL
2583 if (allow_early)
2584 SSL_set_early_data_enabled(ssl, 1);
2585#else
2586 if (!allow_early)
2587 SSL_set_max_early_data(ssl, 0);
2588#endif
2589 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002590 abort:
2591 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2592 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002593#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002594 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002595#else
2596 *al = SSL_AD_UNRECOGNIZED_NAME;
2597 return 0;
2598#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002599}
2600
2601#else /* OPENSSL_IS_BORINGSSL */
2602
Emeric Brunfc0421f2012-09-07 17:30:07 +02002603/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2604 * warning when no match is found, which implies the default (first) cert
2605 * will keep being used.
2606 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002607static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002608{
2609 const char *servername;
2610 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002611 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002612 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002613 int i;
2614 (void)al; /* shut gcc stupid warning */
2615
2616 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002617 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002618#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002619 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2620 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002621#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002622 if (s->strict_sni)
2623 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002624 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002625 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002626 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002627 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002628 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002629
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002630 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002631 if (!servername[i])
2632 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 trash.area[i] = tolower(servername[i]);
2634 if (!wildp && (trash.area[i] == '.'))
2635 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002636 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002637 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002638
William Lallemand150bfa82019-09-19 17:12:49 +02002639 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002640 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002641 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002642 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2643 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002644 if (!container_of(n, struct sni_ctx, name)->neg) {
2645 node = n;
2646 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002647 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002648 }
2649 if (!node && wildp) {
2650 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002651 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2652 /* lookup a not neg filter */
2653 if (!container_of(n, struct sni_ctx, name)->neg) {
2654 node = n;
2655 break;
2656 }
2657 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002658 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002659 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002660#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002661 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2662 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002663 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002664 return SSL_TLSEXT_ERR_OK;
2665 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002666#endif
William Lallemand21724f02019-11-04 17:56:13 +01002667 if (s->strict_sni) {
2668 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002669 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002670 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002671 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002672 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002673 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002674 }
2675
2676 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002677 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002678 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002679 return SSL_TLSEXT_ERR_OK;
2680}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002681#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002682#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2683
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002684#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002685
2686static DH * ssl_get_dh_1024(void)
2687{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002688 static unsigned char dh1024_p[]={
2689 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2690 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2691 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2692 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2693 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2694 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2695 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2696 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2697 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2698 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2699 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2700 };
2701 static unsigned char dh1024_g[]={
2702 0x02,
2703 };
2704
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002705 BIGNUM *p;
2706 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002707 DH *dh = DH_new();
2708 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002709 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2710 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002711
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002712 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002713 DH_free(dh);
2714 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002715 } else {
2716 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002717 }
2718 }
2719 return dh;
2720}
2721
2722static DH *ssl_get_dh_2048(void)
2723{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002724 static unsigned char dh2048_p[]={
2725 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2726 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2727 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2728 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2729 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2730 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2731 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2732 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2733 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2734 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2735 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2736 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2737 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2738 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2739 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2740 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2741 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2742 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2743 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2744 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2745 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2746 0xB7,0x1F,0x77,0xF3,
2747 };
2748 static unsigned char dh2048_g[]={
2749 0x02,
2750 };
2751
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002752 BIGNUM *p;
2753 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002754 DH *dh = DH_new();
2755 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002756 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2757 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002758
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002759 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002760 DH_free(dh);
2761 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002762 } else {
2763 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002764 }
2765 }
2766 return dh;
2767}
2768
2769static DH *ssl_get_dh_4096(void)
2770{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002771 static unsigned char dh4096_p[]={
2772 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2773 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2774 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2775 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2776 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2777 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2778 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2779 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2780 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2781 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2782 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2783 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2784 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2785 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2786 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2787 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2788 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2789 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2790 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2791 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2792 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2793 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2794 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2795 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2796 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2797 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2798 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2799 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2800 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2801 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2802 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2803 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2804 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2805 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2806 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2807 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2808 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2809 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2810 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2811 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2812 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2813 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2814 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002815 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002816 static unsigned char dh4096_g[]={
2817 0x02,
2818 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002819
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002820 BIGNUM *p;
2821 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002822 DH *dh = DH_new();
2823 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002824 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2825 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002826
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002827 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002828 DH_free(dh);
2829 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002830 } else {
2831 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002832 }
2833 }
2834 return dh;
2835}
2836
2837/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002838 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002839static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2840{
2841 DH *dh = NULL;
2842 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002843 int type;
2844
2845 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002846
2847 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2848 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2849 */
2850 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2851 keylen = EVP_PKEY_bits(pkey);
2852 }
2853
Willy Tarreauef934602016-12-22 23:12:01 +01002854 if (keylen > global_ssl.default_dh_param) {
2855 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002856 }
2857
Remi Gacogned3a341a2015-05-29 16:26:17 +02002858 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002859 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002860 }
2861 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002862 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002863 }
2864 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002865 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002866 }
2867
2868 return dh;
2869}
2870
Remi Gacogne47783ef2015-05-29 15:53:22 +02002871static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002872{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002873 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002874 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002875
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002876 if (in == NULL)
2877 goto end;
2878
Remi Gacogne47783ef2015-05-29 15:53:22 +02002879 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002880 goto end;
2881
Remi Gacogne47783ef2015-05-29 15:53:22 +02002882 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2883
2884end:
2885 if (in)
2886 BIO_free(in);
2887
Emeric Brune1b4ed42018-08-16 15:14:12 +02002888 ERR_clear_error();
2889
Remi Gacogne47783ef2015-05-29 15:53:22 +02002890 return dh;
2891}
2892
2893int ssl_sock_load_global_dh_param_from_file(const char *filename)
2894{
2895 global_dh = ssl_sock_get_dh_from_file(filename);
2896
2897 if (global_dh) {
2898 return 0;
2899 }
2900
2901 return -1;
2902}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002903#endif
2904
William Lallemand82b21bb2020-04-10 10:26:27 +02002905/* release ssl bind conf */
2906void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
2907{
2908 if (conf) {
2909#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2910 free(conf->npn_str);
2911 conf->npn_str = NULL;
2912#endif
2913#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2914 free(conf->alpn_str);
2915 conf->alpn_str = NULL;
2916#endif
2917 free(conf->ca_file);
2918 conf->ca_file = NULL;
2919 free(conf->ca_verify_file);
2920 conf->ca_verify_file = NULL;
2921 free(conf->crl_file);
2922 conf->crl_file = NULL;
2923 free(conf->ciphers);
2924 conf->ciphers = NULL;
2925#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
2926 free(conf->ciphersuites);
2927 conf->ciphersuites = NULL;
2928#endif
2929 free(conf->curves);
2930 conf->curves = NULL;
2931 free(conf->ecdhe);
2932 conf->ecdhe = NULL;
2933 }
2934}
2935
William Lallemandd9d5d1b2020-04-09 16:31:05 +02002936/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
2937/* The caller must use the lock of the bind_conf if used with inserted SNIs */
2938static void ckch_inst_free(struct ckch_inst *inst)
2939{
2940 struct sni_ctx *sni, *sni_s;
2941
2942 if (inst == NULL)
2943 return;
2944
2945 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
2946 SSL_CTX_free(sni->ctx);
2947 LIST_DEL(&sni->by_ckch_inst);
2948 ebmb_delete(&sni->name);
2949 free(sni);
2950 }
2951 LIST_DEL(&inst->by_ckchs);
2952 LIST_DEL(&inst->by_crtlist_entry);
2953 free(inst);
2954}
2955
William Lallemand9117de92019-10-04 00:29:42 +02002956/* Alloc and init a ckch_inst */
2957static struct ckch_inst *ckch_inst_new()
2958{
2959 struct ckch_inst *ckch_inst;
2960
2961 ckch_inst = calloc(1, sizeof *ckch_inst);
William Lallemand9cef2e22020-04-09 16:25:10 +02002962 if (!ckch_inst)
2963 return NULL;
2964
2965 LIST_INIT(&ckch_inst->sni_ctx);
2966 LIST_INIT(&ckch_inst->by_ckchs);
2967 LIST_INIT(&ckch_inst->by_crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02002968
2969 return ckch_inst;
2970}
2971
William Lallemand82b21bb2020-04-10 10:26:27 +02002972/* free sni filters */
2973static void crtlist_free_filters(char **args)
2974{
2975 int i;
2976
2977 if (!args)
2978 return;
2979
2980 for (i = 0; args[i]; i++)
2981 free(args[i]);
2982
2983 free(args);
2984}
2985
2986/* Alloc and duplicate a char ** array */
2987static char **crtlist_dup_filters(char **args, int fcount)
2988{
2989 char **dst;
2990 int i;
2991
2992 if (fcount == 0)
2993 return NULL;
2994
2995 dst = calloc(fcount + 1, sizeof(*dst));
2996 if (!dst)
2997 return NULL;
2998
2999 for (i = 0; i < fcount; i++) {
3000 dst[i] = strdup(args[i]);
3001 if (!dst[i])
3002 goto error;
3003 }
3004 return dst;
3005
3006error:
3007 crtlist_free_filters(dst);
3008 return NULL;
3009}
3010
William Lallemande718dfb2020-04-10 11:09:25 +02003011/*
3012 * Detach and free a crtlist_entry.
3013 * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst
3014 */
3015static void crtlist_entry_free(struct crtlist_entry *entry)
3016{
3017 struct ckch_inst *inst, *inst_s;
3018
3019 if (entry == NULL)
3020 return;
3021
3022 ebpt_delete(&entry->node);
3023 LIST_DEL(&entry->by_crtlist);
3024 LIST_DEL(&entry->by_ckch_store);
3025 crtlist_free_filters(entry->filters);
3026 ssl_sock_free_ssl_conf(entry->ssl_conf);
3027 free(entry->ssl_conf);
3028 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
3029 ckch_inst_free(inst);
3030 }
3031 free(entry);
3032}
3033
3034/*
3035 * Allocate and initialize a crtlist_entry
3036 */
3037static struct crtlist_entry *crtlist_entry_new()
3038{
3039 struct crtlist_entry *entry;
3040
3041 entry = calloc(1, sizeof(*entry));
3042 if (entry == NULL)
3043 return NULL;
3044
3045 LIST_INIT(&entry->ckch_inst);
3046
3047 /* initialize the nodes so we can LIST_DEL in any cases */
3048 LIST_INIT(&entry->by_crtlist);
3049 LIST_INIT(&entry->by_ckch_store);
3050
3051 return entry;
3052}
3053
William Lallemand82b21bb2020-04-10 10:26:27 +02003054/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
3055static void crtlist_free(struct crtlist *crtlist)
3056{
3057 struct crtlist_entry *entry, *s_entry;
3058
3059 if (crtlist == NULL)
3060 return;
3061
3062 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
3063 crtlist_entry_free(entry);
3064 }
3065 ebmb_delete(&crtlist->node);
3066 free(crtlist);
3067}
3068
3069/* Alloc and initialize a struct crtlist
3070 * <filename> is the key of the ebmb_node
3071 * <unique> initialize the list of entries to be unique (1) or not (0)
3072 */
3073static struct crtlist *crtlist_new(const char *filename, int unique)
3074{
3075 struct crtlist *newlist;
3076
3077 newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1);
3078 if (newlist == NULL)
3079 return NULL;
3080
3081 memcpy(newlist->node.key, filename, strlen(filename) + 1);
3082 if (unique)
3083 newlist->entries = EB_ROOT_UNIQUE;
3084 else
3085 newlist->entries = EB_ROOT;
3086
3087 LIST_INIT(&newlist->ord_entries);
3088
3089 return newlist;
3090}
William Lallemand9117de92019-10-04 00:29:42 +02003091
3092/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02003093static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02003094 struct bind_conf *s, struct ssl_bind_conf *conf,
3095 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003096{
3097 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003098 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003099
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003100 if (*name == '!') {
3101 neg = 1;
3102 name++;
3103 }
3104 if (*name == '*') {
3105 wild = 1;
3106 name++;
3107 }
3108 /* !* filter is a nop */
3109 if (neg && wild)
3110 return order;
3111 if (*name) {
3112 int j, len;
3113 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003114 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003115 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003116 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02003117 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003118 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003119
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003120 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02003121 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02003122 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003123 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02003124 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003125 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003126 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003127 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003128 sc->order = order++;
3129 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02003130 sc->wild = wild;
3131 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01003132 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02003133 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003134 }
3135 return order;
3136}
3137
William Lallemand6af03992019-07-23 15:00:54 +02003138/*
William Lallemand1d29c742019-10-04 00:53:29 +02003139 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
3140 * This function can't return an error.
3141 *
3142 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
3143 */
3144static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
3145{
3146
3147 struct sni_ctx *sc0, *sc0b, *sc1;
3148 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01003149 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02003150
3151 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
3152
3153 /* ignore if sc0 was already inserted in a tree */
3154 if (sc0->name.node.leaf_p)
3155 continue;
3156
3157 /* Check for duplicates. */
3158 if (sc0->wild)
3159 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
3160 else
3161 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
3162
3163 for (; node; node = ebmb_next_dup(node)) {
3164 sc1 = ebmb_entry(node, struct sni_ctx, name);
3165 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
3166 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
3167 /* it's a duplicate, we should remove and free it */
3168 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02003169 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02003170 free(sc0);
3171 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02003172 break;
William Lallemand1d29c742019-10-04 00:53:29 +02003173 }
3174 }
3175
3176 /* if duplicate, ignore the insertion */
3177 if (!sc0)
3178 continue;
3179
3180 if (sc0->wild)
3181 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3182 else
3183 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003184
3185 /* replace the default_ctx if required with the first ctx */
3186 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02003187 SSL_CTX_free(bind_conf->default_ctx);
3188 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01003189 bind_conf->default_ctx = sc0->ctx;
3190 def = 1;
3191 }
William Lallemand1d29c742019-10-04 00:53:29 +02003192 }
3193}
3194
3195/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003196 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003197 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003198struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003199
William Lallemand2954c472020-03-06 21:54:13 +01003200/* tree of crtlist (crt-list/directory) */
3201static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003202
Emeric Brun7a883362019-10-17 13:27:40 +02003203/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003204 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003205 * DH parameter is loaded into the SSL_CTX and if there is no
3206 * DH parameter available in ckchs nor in global, the default
3207 * DH parameters are applied on the SSL_CTX.
3208 * Returns a bitfield containing the flags:
3209 * ERR_FATAL in any fatal error case
3210 * ERR_ALERT if a reason of the error is availabine in err
3211 * ERR_WARN if a warning is available into err
3212 * The value 0 means there is no error nor warning and
3213 * the operation succeed.
3214 */
William Lallemandfa892222019-07-23 16:06:08 +02003215#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003216static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3217 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003218{
Emeric Brun7a883362019-10-17 13:27:40 +02003219 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003220 DH *dh = NULL;
3221
William Lallemanda8c73742019-07-31 18:31:34 +02003222 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003223 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003224 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3225 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3226 err && *err ? *err : "", path);
3227#if defined(SSL_CTX_set_dh_auto)
3228 SSL_CTX_set_dh_auto(ctx, 1);
3229 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3230 err && *err ? *err : "");
3231#else
3232 memprintf(err, "%s, DH ciphers won't be available.\n",
3233 err && *err ? *err : "");
3234#endif
3235 ret |= ERR_WARN;
3236 goto end;
3237 }
William Lallemandfa892222019-07-23 16:06:08 +02003238
3239 if (ssl_dh_ptr_index >= 0) {
3240 /* store a pointer to the DH params to avoid complaining about
3241 ssl-default-dh-param not being set for this SSL_CTX */
3242 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3243 }
3244 }
3245 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003246 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3247 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3248 err && *err ? *err : "", path);
3249#if defined(SSL_CTX_set_dh_auto)
3250 SSL_CTX_set_dh_auto(ctx, 1);
3251 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3252 err && *err ? *err : "");
3253#else
3254 memprintf(err, "%s, DH ciphers won't be available.\n",
3255 err && *err ? *err : "");
3256#endif
3257 ret |= ERR_WARN;
3258 goto end;
3259 }
William Lallemandfa892222019-07-23 16:06:08 +02003260 }
3261 else {
3262 /* Clear openssl global errors stack */
3263 ERR_clear_error();
3264
3265 if (global_ssl.default_dh_param <= 1024) {
3266 /* we are limited to DH parameter of 1024 bits anyway */
3267 if (local_dh_1024 == NULL)
3268 local_dh_1024 = ssl_get_dh_1024();
3269
Emeric Brun7a883362019-10-17 13:27:40 +02003270 if (local_dh_1024 == NULL) {
3271 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3272 err && *err ? *err : "", path);
3273 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003274 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003275 }
William Lallemandfa892222019-07-23 16:06:08 +02003276
Emeric Bruna9363eb2019-10-17 14:53:03 +02003277 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3278 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3279 err && *err ? *err : "", path);
3280#if defined(SSL_CTX_set_dh_auto)
3281 SSL_CTX_set_dh_auto(ctx, 1);
3282 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3283 err && *err ? *err : "");
3284#else
3285 memprintf(err, "%s, DH ciphers won't be available.\n",
3286 err && *err ? *err : "");
3287#endif
3288 ret |= ERR_WARN;
3289 goto end;
3290 }
William Lallemandfa892222019-07-23 16:06:08 +02003291 }
3292 else {
3293 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3294 }
William Lallemandfa892222019-07-23 16:06:08 +02003295 }
3296
3297end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003298 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003299 return ret;
3300}
3301#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003302
yanbzhu488a4d22015-12-01 15:16:07 -05003303/* Frees the contents of a cert_key_and_chain
3304 */
3305static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3306{
yanbzhu488a4d22015-12-01 15:16:07 -05003307 if (!ckch)
3308 return;
3309
3310 /* Free the certificate and set pointer to NULL */
3311 if (ckch->cert)
3312 X509_free(ckch->cert);
3313 ckch->cert = NULL;
3314
3315 /* Free the key and set pointer to NULL */
3316 if (ckch->key)
3317 EVP_PKEY_free(ckch->key);
3318 ckch->key = NULL;
3319
3320 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003321 if (ckch->chain)
3322 sk_X509_pop_free(ckch->chain, X509_free);
3323 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003324
William Lallemand455af502019-10-17 18:04:45 +02003325 if (ckch->dh)
3326 DH_free(ckch->dh);
3327 ckch->dh = NULL;
3328
3329 if (ckch->sctl) {
3330 free(ckch->sctl->area);
3331 ckch->sctl->area = NULL;
3332 free(ckch->sctl);
3333 ckch->sctl = NULL;
3334 }
3335
3336 if (ckch->ocsp_response) {
3337 free(ckch->ocsp_response->area);
3338 ckch->ocsp_response->area = NULL;
3339 free(ckch->ocsp_response);
3340 ckch->ocsp_response = NULL;
3341 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003342
3343 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003344 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003345 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003346}
3347
William Lallemand8d0f8932019-10-17 18:03:58 +02003348/*
3349 *
3350 * This function copy a cert_key_and_chain in memory
3351 *
3352 * It's used to try to apply changes on a ckch before committing them, because
3353 * most of the time it's not possible to revert those changes
3354 *
3355 * Return a the dst or NULL
3356 */
3357static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3358 struct cert_key_and_chain *dst)
3359{
3360 if (src->cert) {
3361 dst->cert = src->cert;
3362 X509_up_ref(src->cert);
3363 }
3364
3365 if (src->key) {
3366 dst->key = src->key;
3367 EVP_PKEY_up_ref(src->key);
3368 }
3369
3370 if (src->chain) {
3371 dst->chain = X509_chain_up_ref(src->chain);
3372 }
3373
3374 if (src->dh) {
3375 DH_up_ref(src->dh);
3376 dst->dh = src->dh;
3377 }
3378
3379 if (src->sctl) {
3380 struct buffer *sctl;
3381
3382 sctl = calloc(1, sizeof(*sctl));
3383 if (!chunk_dup(sctl, src->sctl)) {
3384 free(sctl);
3385 sctl = NULL;
3386 goto error;
3387 }
3388 dst->sctl = sctl;
3389 }
3390
3391 if (src->ocsp_response) {
3392 struct buffer *ocsp_response;
3393
3394 ocsp_response = calloc(1, sizeof(*ocsp_response));
3395 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3396 free(ocsp_response);
3397 ocsp_response = NULL;
3398 goto error;
3399 }
3400 dst->ocsp_response = ocsp_response;
3401 }
3402
3403 if (src->ocsp_issuer) {
3404 X509_up_ref(src->ocsp_issuer);
3405 dst->ocsp_issuer = src->ocsp_issuer;
3406 }
3407
3408 return dst;
3409
3410error:
3411
3412 /* free everything */
3413 ssl_sock_free_cert_key_and_chain_contents(dst);
3414
3415 return NULL;
3416}
3417
3418
yanbzhu488a4d22015-12-01 15:16:07 -05003419/* checks if a key and cert exists in the ckch
3420 */
William Lallemand1633e392019-09-30 12:58:13 +02003421#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003422static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3423{
3424 return (ckch->cert != NULL && ckch->key != NULL);
3425}
William Lallemand1633e392019-09-30 12:58:13 +02003426#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003427
William Lallemandf9568fc2019-10-16 18:27:58 +02003428/*
3429 * return 0 on success or != 0 on failure
3430 */
3431static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3432{
3433 int ret = 1;
3434 BIO *in = NULL;
3435 X509 *issuer;
3436
3437 if (buf) {
3438 /* reading from a buffer */
3439 in = BIO_new_mem_buf(buf, -1);
3440 if (in == NULL) {
3441 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3442 goto end;
3443 }
3444
3445 } else {
3446 /* reading from a file */
3447 in = BIO_new(BIO_s_file());
3448 if (in == NULL)
3449 goto end;
3450
3451 if (BIO_read_filename(in, path) <= 0)
3452 goto end;
3453 }
3454
3455 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3456 if (!issuer) {
3457 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003458 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003459 goto end;
3460 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003461 /* no error, fill ckch with new context, old context must be free */
3462 if (ckch->ocsp_issuer)
3463 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003464 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003465 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003466
3467end:
3468
3469 ERR_clear_error();
3470 if (in)
3471 BIO_free(in);
3472
3473 return ret;
3474}
3475
William Lallemand96a9c972019-10-17 11:56:17 +02003476
3477/*
3478 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003479 * The PEM must contain at least a Certificate,
3480 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003481 *
William Lallemand96a9c972019-10-17 11:56:17 +02003482 * If it failed you should not attempt to use the ckch but free it.
3483 *
3484 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003485 */
William Lallemand96a9c972019-10-17 11:56:17 +02003486static 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 -05003487{
William Lallemandf11365b2019-09-19 14:25:58 +02003488 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003489 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003490 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003491 X509 *cert = NULL;
3492 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003493 DH *dh = NULL;
3494 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003495
3496 if (buf) {
3497 /* reading from a buffer */
3498 in = BIO_new_mem_buf(buf, -1);
3499 if (in == NULL) {
3500 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3501 goto end;
3502 }
yanbzhu488a4d22015-12-01 15:16:07 -05003503
William Lallemand96a9c972019-10-17 11:56:17 +02003504 } else {
3505 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003506 in = BIO_new(BIO_s_file());
William Lallemand7fd01b32020-04-07 14:16:32 +02003507 if (in == NULL) {
3508 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
William Lallemandf11365b2019-09-19 14:25:58 +02003509 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003510 }
yanbzhu488a4d22015-12-01 15:16:07 -05003511
William Lallemand7fd01b32020-04-07 14:16:32 +02003512 if (BIO_read_filename(in, path) <= 0) {
3513 memprintf(err, "%scannot open the file '%s'.\n",
3514 err && *err ? *err : "", path);
William Lallemandf11365b2019-09-19 14:25:58 +02003515 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003516 }
William Lallemandf11365b2019-09-19 14:25:58 +02003517 }
yanbzhu488a4d22015-12-01 15:16:07 -05003518
yanbzhu488a4d22015-12-01 15:16:07 -05003519 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003520 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003521 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003522
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003523#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003524 /* Seek back to beginning of file */
3525 if (BIO_reset(in) == -1) {
3526 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3527 err && *err ? *err : "", path);
3528 goto end;
3529 }
3530
William Lallemand96a9c972019-10-17 11:56:17 +02003531 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3532 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003533#endif
William Lallemandfa892222019-07-23 16:06:08 +02003534
Willy Tarreaubb137a82016-04-06 19:02:38 +02003535 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003536 if (BIO_reset(in) == -1) {
3537 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3538 err && *err ? *err : "", path);
3539 goto end;
3540 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003541
3542 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003543 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3544 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003545 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003546 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003547 goto end;
3548 }
3549
William Lallemand96a9c972019-10-17 11:56:17 +02003550 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003551 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3552 if (chain == NULL)
3553 chain = sk_X509_new_null();
3554 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003555 X509_free(ca);
3556 goto end;
3557 }
3558 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003559
yanbzhu488a4d22015-12-01 15:16:07 -05003560 ret = ERR_get_error();
3561 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3562 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003563 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003564 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003565 }
3566
William Lallemand75b15f72020-01-23 10:56:05 +01003567 /* once it loaded the PEM, it should remove everything else in the ckch */
3568 if (ckch->ocsp_response) {
3569 free(ckch->ocsp_response->area);
3570 ckch->ocsp_response->area = NULL;
3571 free(ckch->ocsp_response);
3572 ckch->ocsp_response = NULL;
3573 }
3574
3575 if (ckch->sctl) {
3576 free(ckch->sctl->area);
3577 ckch->sctl->area = NULL;
3578 free(ckch->sctl);
3579 ckch->sctl = NULL;
3580 }
3581
3582 if (ckch->ocsp_issuer) {
3583 X509_free(ckch->ocsp_issuer);
3584 ckch->ocsp_issuer = NULL;
3585 }
3586
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003587 /* no error, fill ckch with new context, old context will be free at end: */
3588 SWAP(ckch->key, key);
3589 SWAP(ckch->dh, dh);
3590 SWAP(ckch->cert, cert);
3591 SWAP(ckch->chain, chain);
3592
William Lallemand246c0242019-10-11 08:59:13 +02003593 ret = 0;
3594
William Lallemand96a9c972019-10-17 11:56:17 +02003595end:
William Lallemand246c0242019-10-11 08:59:13 +02003596
3597 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003598 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003599 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003600 if (key)
3601 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003602 if (dh)
3603 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003604 if (cert)
3605 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003606 if (chain)
3607 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003608
William Lallemand96a9c972019-10-17 11:56:17 +02003609 return ret;
3610}
3611
3612/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003613 * Try to load a private key file from a <path> or a buffer <buf>
3614 *
3615 * If it failed you should not attempt to use the ckch but free it.
3616 *
3617 * Return 0 on success or != 0 on failure
3618 */
3619static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3620{
3621 BIO *in = NULL;
3622 int ret = 1;
3623 EVP_PKEY *key = NULL;
3624
3625 if (buf) {
3626 /* reading from a buffer */
3627 in = BIO_new_mem_buf(buf, -1);
3628 if (in == NULL) {
3629 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3630 goto end;
3631 }
3632
3633 } else {
3634 /* reading from a file */
3635 in = BIO_new(BIO_s_file());
3636 if (in == NULL)
3637 goto end;
3638
3639 if (BIO_read_filename(in, path) <= 0)
3640 goto end;
3641 }
3642
3643 /* Read Private Key */
3644 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3645 if (key == NULL) {
3646 memprintf(err, "%sunable to load private key from file '%s'.\n",
3647 err && *err ? *err : "", path);
3648 goto end;
3649 }
3650
3651 ret = 0;
3652
3653 SWAP(ckch->key, key);
3654
3655end:
3656
3657 ERR_clear_error();
3658 if (in)
3659 BIO_free(in);
3660 if (key)
3661 EVP_PKEY_free(key);
3662
3663 return ret;
3664}
3665
3666/*
William Lallemand96a9c972019-10-17 11:56:17 +02003667 * Try to load in a ckch every files related to a ckch.
3668 * (PEM, sctl, ocsp, issuer etc.)
3669 *
3670 * This function is only used to load files during the configuration parsing,
3671 * it is not used with the CLI.
3672 *
3673 * This allows us to carry the contents of the file without having to read the
3674 * file multiple times. The caller must call
3675 * ssl_sock_free_cert_key_and_chain_contents.
3676 *
3677 * returns:
3678 * 0 on Success
3679 * 1 on SSL Failure
3680 */
3681static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3682{
3683 int ret = 1;
3684
3685 /* try to load the PEM */
3686 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3687 goto end;
3688 }
3689
William Lallemand4c5adbf2020-02-24 14:23:22 +01003690 /* try to load an external private key if it wasn't in the PEM */
3691 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3692 char fp[MAXPATHLEN+1];
3693 struct stat st;
3694
3695 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3696 if (stat(fp, &st) == 0) {
3697 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3698 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3699 err && *err ? *err : "", fp);
3700 goto end;
3701 }
3702 }
3703 }
3704
3705 if (ckch->key == NULL) {
3706 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3707 goto end;
3708 }
3709
3710 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3711 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3712 err && *err ? *err : "", path);
3713 goto end;
3714 }
3715
William Lallemanda17f4112019-10-10 15:16:44 +02003716#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3717 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003718 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003719 char fp[MAXPATHLEN+1];
3720 struct stat st;
3721
3722 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3723 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003724 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003725 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003726 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003727 ret = 1;
3728 goto end;
3729 }
3730 }
3731 }
3732#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003733
William Lallemand246c0242019-10-11 08:59:13 +02003734 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003735 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003736 char fp[MAXPATHLEN+1];
3737 struct stat st;
3738
3739 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3740 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003741 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003742 ret = 1;
3743 goto end;
3744 }
3745 }
3746 }
3747
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003748#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003749 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003750 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003751 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003752 struct stat st;
3753 char fp[MAXPATHLEN+1];
3754
3755 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3756 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003757 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003758 ret = 1;
3759 goto end;
3760 }
3761
3762 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003763 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003764 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003765 ret = 1;
3766 goto end;
3767 }
William Lallemand246c0242019-10-11 08:59:13 +02003768 }
3769 }
3770 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003771#endif
William Lallemand246c0242019-10-11 08:59:13 +02003772
yanbzhu488a4d22015-12-01 15:16:07 -05003773 ret = 0;
3774
3775end:
3776
3777 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003778
3779 /* Something went wrong in one of the reads */
3780 if (ret != 0)
3781 ssl_sock_free_cert_key_and_chain_contents(ckch);
3782
3783 return ret;
3784}
3785
3786/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003787 * Returns a bitfield containing the flags:
3788 * ERR_FATAL in any fatal error case
3789 * ERR_ALERT if the reason of the error is available in err
3790 * ERR_WARN if a warning is available into err
3791 * The value 0 means there is no error nor warning and
3792 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003793 */
3794static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3795{
Emeric Bruna96b5822019-10-17 13:25:14 +02003796 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003797 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003798
yanbzhu488a4d22015-12-01 15:16:07 -05003799 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3800 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3801 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003802 errcode |= ERR_ALERT | ERR_FATAL;
3803 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003804 }
3805
3806 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3807 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3808 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003809 errcode |= ERR_ALERT | ERR_FATAL;
3810 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003811 }
3812
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003813 if (ckch->chain) {
3814 find_chain = ckch->chain;
3815 } else {
3816 /* Find Certificate Chain in global */
3817 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003818 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003819 if (issuer)
3820 find_chain = issuer->chain;
3821 }
William Lallemand85888572020-02-27 14:48:35 +01003822
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003823 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003824 if (find_chain) {
3825 int i;
3826 X509 *ca;
3827 for (i = 0; i < sk_X509_num(find_chain); i++) {
3828 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003829 /* skip self issued (Root CA) */
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +02003830 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 +01003831 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003832 /*
3833 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3834 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3835 */
3836 X509_up_ref(ca);
3837 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3838 X509_free(ca);
3839 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3840 err && *err ? *err : "", path);
3841 errcode |= ERR_ALERT | ERR_FATAL;
3842 goto end;
3843 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003844 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003845 }
yanbzhu488a4d22015-12-01 15:16:07 -05003846
William Lallemandfa892222019-07-23 16:06:08 +02003847#ifndef OPENSSL_NO_DH
3848 /* store a NULL pointer to indicate we have not yet loaded
3849 a custom DH param file */
3850 if (ssl_dh_ptr_index >= 0) {
3851 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3852 }
3853
Emeric Brun7a883362019-10-17 13:27:40 +02003854 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3855 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003856 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3857 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003858 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003859 }
3860#endif
3861
William Lallemanda17f4112019-10-10 15:16:44 +02003862#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3863 if (sctl_ex_index >= 0 && ckch->sctl) {
3864 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3865 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003866 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003867 errcode |= ERR_ALERT | ERR_FATAL;
3868 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003869 }
3870 }
3871#endif
3872
William Lallemand4a660132019-10-14 14:51:41 +02003873#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003874 /* Load OCSP Info into context */
3875 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003876 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003877 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",
3878 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003879 errcode |= ERR_ALERT | ERR_FATAL;
3880 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003881 }
3882 }
William Lallemand246c0242019-10-11 08:59:13 +02003883#endif
3884
Emeric Bruna96b5822019-10-17 13:25:14 +02003885 end:
3886 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003887}
3888
William Lallemandc4ecddf2019-07-31 16:50:08 +02003889#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003890
William Lallemand28a8fce2019-10-04 17:36:55 +02003891static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003892{
3893 struct sni_keytype *s_kt = NULL;
3894 struct ebmb_node *node;
3895 int i;
3896
3897 for (i = 0; i < trash.size; i++) {
3898 if (!str[i])
3899 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003901 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003902 trash.area[i] = 0;
3903 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003904 if (!node) {
3905 /* CN not found in tree */
3906 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3907 /* Using memcpy here instead of strncpy.
3908 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3909 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3910 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003911 if (!s_kt)
3912 return -1;
3913
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003914 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003915 s_kt->keytypes = 0;
3916 ebst_insert(sni_keytypes, &s_kt->name);
3917 } else {
3918 /* CN found in tree */
3919 s_kt = container_of(node, struct sni_keytype, name);
3920 }
3921
3922 /* Mark that this CN has the keytype of key_index via keytypes mask */
3923 s_kt->keytypes |= 1<<key_index;
3924
William Lallemand28a8fce2019-10-04 17:36:55 +02003925 return 0;
3926
yanbzhu08ce6ab2015-12-02 13:01:29 -05003927}
3928
William Lallemandc4ecddf2019-07-31 16:50:08 +02003929#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003930/*
William Lallemandba1c33f2020-04-08 17:55:45 +02003931 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
William Lallemand8c1cdde2019-10-18 10:58:14 +02003932 */
William Lallemandba1c33f2020-04-08 17:55:45 +02003933static void ckch_store_free(struct ckch_store *store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003934{
William Lallemandba1c33f2020-04-08 17:55:45 +02003935 struct ckch_inst *inst, *inst_s;
3936
3937 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003938 return;
3939
William Lallemandba1c33f2020-04-08 17:55:45 +02003940#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L
3941 if (store->multi) {
William Lallemand8c1cdde2019-10-18 10:58:14 +02003942 int n;
3943
William Lallemandba1c33f2020-04-08 17:55:45 +02003944 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3945 ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003946 } else
3947#endif
3948 {
William Lallemandba1c33f2020-04-08 17:55:45 +02003949 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003950 }
3951
William Lallemandba1c33f2020-04-08 17:55:45 +02003952 free(store->ckch);
3953 store->ckch = NULL;
3954
3955 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
3956 ckch_inst_free(inst);
3957 }
3958 ebmb_delete(&store->node);
3959 free(store);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003960}
3961
William Lallemand8a874e42020-04-09 10:32:53 +02003962/*
3963 * create and initialize a ckch_store
3964 * <path> is the key name
3965 * <nmemb> is the number of store->ckch objects to allocate
3966 *
3967 * Return a ckch_store or NULL upon failure.
3968 */
3969static struct ckch_store *ckch_store_new(const char *filename, int nmemb)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003970{
William Lallemand8a874e42020-04-09 10:32:53 +02003971 struct ckch_store *store;
William Lallemand8c1cdde2019-10-18 10:58:14 +02003972 int pathlen;
3973
William Lallemand8a874e42020-04-09 10:32:53 +02003974 pathlen = strlen(filename);
3975 store = calloc(1, sizeof(*store) + pathlen + 1);
3976 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003977 return NULL;
William Lallemand8a874e42020-04-09 10:32:53 +02003978
3979 if (nmemb > 1)
3980 store->multi = 1;
3981 else
3982 store->multi = 0;
3983
3984 memcpy(store->path, filename, pathlen + 1);
3985
3986 LIST_INIT(&store->ckch_inst);
3987 LIST_INIT(&store->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003988
William Lallemand8a874e42020-04-09 10:32:53 +02003989 store->ckch = calloc(nmemb, sizeof(*store->ckch));
3990 if (!store->ckch)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003991 goto error;
3992
William Lallemand8a874e42020-04-09 10:32:53 +02003993 return store;
3994error:
3995 ckch_store_free(store);
3996 return NULL;
3997}
3998
3999/* allocate and duplicate a ckch_store
4000 * Return a new ckch_store or NULL */
4001static struct ckch_store *ckchs_dup(const struct ckch_store *src)
4002{
4003 struct ckch_store *dst;
4004
4005 dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1);
4006
William Lallemand8c1cdde2019-10-18 10:58:14 +02004007#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4008 if (src->multi) {
4009 int n;
4010
4011 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4012 if (&src->ckch[n]) {
4013 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
4014 goto error;
4015 }
4016 }
4017 } else
4018#endif
4019 {
4020 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
4021 goto error;
4022 }
4023
4024 return dst;
4025
4026error:
William Lallemandba1c33f2020-04-08 17:55:45 +02004027 ckch_store_free(dst);
William Lallemand8c1cdde2019-10-18 10:58:14 +02004028
4029 return NULL;
4030}
William Lallemandc4ecddf2019-07-31 16:50:08 +02004031
William Lallemand36b84632019-07-18 19:28:17 +02004032/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004033 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02004034 */
William Lallemande3af8fb2019-10-08 11:36:53 +02004035static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02004036{
4037 struct ebmb_node *eb;
4038
William Lallemande3af8fb2019-10-08 11:36:53 +02004039 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02004040 if (!eb)
4041 return NULL;
4042
William Lallemande3af8fb2019-10-08 11:36:53 +02004043 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02004044}
4045
4046/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004047 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02004048 */
William Lallemande3af8fb2019-10-08 11:36:53 +02004049static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02004050{
William Lallemande3af8fb2019-10-08 11:36:53 +02004051 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02004052
William Lallemand8a874e42020-04-09 10:32:53 +02004053 ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1);
William Lallemande3af8fb2019-10-08 11:36:53 +02004054 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02004055 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
4056 goto end;
4057 }
William Lallemand36b84632019-07-18 19:28:17 +02004058 if (!multi) {
4059
William Lallemand96a9c972019-10-17 11:56:17 +02004060 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004061 goto end;
4062
William Lallemande3af8fb2019-10-08 11:36:53 +02004063 /* insert into the ckchs tree */
4064 memcpy(ckchs->path, path, strlen(path) + 1);
4065 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004066 } else {
4067 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02004068#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4069 char fp[MAXPATHLEN+1] = {0};
4070 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02004071
4072 /* Load all possible certs and keys */
4073 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4074 struct stat buf;
4075 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
4076 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02004077 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004078 goto end;
4079 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02004080 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02004081 }
4082 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02004083#endif
William Lallemand36b84632019-07-18 19:28:17 +02004084
4085 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02004086 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02004087 goto end;
4088 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004089 /* insert into the ckchs tree */
4090 memcpy(ckchs->path, path, strlen(path) + 1);
4091 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004092 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004093 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004094
William Lallemand36b84632019-07-18 19:28:17 +02004095end:
William Lallemandba1c33f2020-04-08 17:55:45 +02004096 ckch_store_free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02004097
4098 return NULL;
4099}
4100
William Lallemandc4ecddf2019-07-31 16:50:08 +02004101#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4102
William Lallemand36b84632019-07-18 19:28:17 +02004103/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004104 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02004105 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05004106 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
4107 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08004108 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05004109 *
Emeric Brun054563d2019-10-17 13:16:58 +02004110 * Returns a bitfield containing the flags:
4111 * ERR_FATAL in any fatal error case
4112 * ERR_ALERT if the reason of the error is available in err
4113 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02004114 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05004115 */
Emeric Brun054563d2019-10-17 13:16:58 +02004116static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4117 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4118 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004119{
William Lallemand36b84632019-07-18 19:28:17 +02004120 int i = 0, n = 0;
4121 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02004122 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004123 struct ebmb_node *node;
4124 struct ebmb_node *next;
4125 /* Array of SSL_CTX pointers corresponding to each possible combo
4126 * of keytypes
4127 */
4128 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02004129 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004130 X509_NAME *xname = NULL;
4131 char *str = NULL;
4132#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4133 STACK_OF(GENERAL_NAME) *names = NULL;
4134#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02004135 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004136
Emeric Brun054563d2019-10-17 13:16:58 +02004137 *ckchi = NULL;
4138
William Lallemande3af8fb2019-10-08 11:36:53 +02004139 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02004140 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4141 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004142 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004143 }
4144
4145 ckch_inst = ckch_inst_new();
4146 if (!ckch_inst) {
4147 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4148 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004149 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004150 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004151 }
4152
William Lallemande3af8fb2019-10-08 11:36:53 +02004153 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004154
yanbzhu08ce6ab2015-12-02 13:01:29 -05004155 /* Process each ckch and update keytypes for each CN/SAN
4156 * for example, if CN/SAN www.a.com is associated with
4157 * certs with keytype 0 and 2, then at the end of the loop,
4158 * www.a.com will have:
4159 * keyindex = 0 | 1 | 4 = 5
4160 */
4161 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004162 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004163
4164 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4165 continue;
4166
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004167 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004168 for (i = 0; i < fcount; i++) {
4169 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4170 if (ret < 0) {
4171 memprintf(err, "%sunable to allocate SSL context.\n",
4172 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004173 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004174 goto end;
4175 }
4176 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004177 } else {
4178 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4179 * so the line that contains logic is marked via comments
4180 */
4181 xname = X509_get_subject_name(certs_and_keys[n].cert);
4182 i = -1;
4183 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4184 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004185 ASN1_STRING *value;
4186 value = X509_NAME_ENTRY_get_data(entry);
4187 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004188 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004189 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004190
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004191 OPENSSL_free(str);
4192 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004193 if (ret < 0) {
4194 memprintf(err, "%sunable to allocate SSL context.\n",
4195 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004196 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004197 goto end;
4198 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004199 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004200 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004201
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004202 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004203#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004204 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4205 if (names) {
4206 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4207 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004208
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004209 if (name->type == GEN_DNS) {
4210 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4211 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004212 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004213
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004214 OPENSSL_free(str);
4215 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004216 if (ret < 0) {
4217 memprintf(err, "%sunable to allocate SSL context.\n",
4218 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004219 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004220 goto end;
4221 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004222 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004223 }
4224 }
4225 }
4226 }
4227#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4228 }
4229
4230 /* If no files found, return error */
4231 if (eb_is_empty(&sni_keytypes_map)) {
4232 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4233 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004234 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004235 goto end;
4236 }
4237
4238 /* We now have a map of CN/SAN to keytypes that are loaded in
4239 * Iterate through the map to create the SSL_CTX's (if needed)
4240 * and add each CTX to the SNI tree
4241 *
4242 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004243 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004244 * combination is denoted by the key in the map. Each key
4245 * has a value between 1 and 2^n - 1. Conveniently, the array
4246 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4247 * entry in the array to correspond to the unique combo (key)
4248 * associated with i. This unique key combo (i) will be associated
4249 * with combos[i-1]
4250 */
4251
4252 node = ebmb_first(&sni_keytypes_map);
4253 while (node) {
4254 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004255 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004256 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004257
4258 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4259 i = container_of(node, struct sni_keytype, name)->keytypes;
4260 cur_ctx = key_combos[i-1].ctx;
4261
4262 if (cur_ctx == NULL) {
4263 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004264 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004265 if (cur_ctx == NULL) {
4266 memprintf(err, "%sunable to allocate SSL context.\n",
4267 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004268 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004269 goto end;
4270 }
4271
yanbzhube2774d2015-12-10 15:07:30 -05004272 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004273 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4274 if (i & (1<<n)) {
4275 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004276 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004277 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4278 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004279 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004280 }
4281 }
4282
yanbzhu08ce6ab2015-12-02 13:01:29 -05004283 /* Update key_combos */
4284 key_combos[i-1].ctx = cur_ctx;
4285 }
4286
4287 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004288
William Lallemand1d29c742019-10-04 00:53:29 +02004289 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 +02004290 kinfo, str, key_combos[i-1].order);
4291 if (key_combos[i-1].order < 0) {
4292 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004293 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004294 goto end;
4295 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004296 node = ebmb_next(node);
4297 }
4298
4299
4300 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4301 if (!bind_conf->default_ctx) {
4302 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4303 if (key_combos[i].ctx) {
4304 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004305 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004306 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004307 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004308 break;
4309 }
4310 }
4311 }
4312
William Lallemand614ca0d2019-10-07 13:52:11 +02004313 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004314 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004315 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004316
yanbzhu08ce6ab2015-12-02 13:01:29 -05004317end:
4318
4319 if (names)
4320 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4321
yanbzhu08ce6ab2015-12-02 13:01:29 -05004322 node = ebmb_first(&sni_keytypes_map);
4323 while (node) {
4324 next = ebmb_next(node);
4325 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004326 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004327 node = next;
4328 }
4329
William Lallemand02e19a52020-04-08 16:11:26 +02004330 /* we need to free the ctx since we incremented the refcount where it's used */
4331 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4332 if (key_combos[i].ctx)
4333 SSL_CTX_free(key_combos[i].ctx);
4334 }
4335
Emeric Brun054563d2019-10-17 13:16:58 +02004336 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004337 if (ckch_inst->is_default) {
4338 SSL_CTX_free(bind_conf->default_ctx);
4339 bind_conf->default_ctx = NULL;
4340 }
4341
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004342 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004343 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004344 }
4345
Emeric Brun054563d2019-10-17 13:16:58 +02004346 *ckchi = ckch_inst;
4347 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004348}
4349#else
4350/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004351static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4352 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4353 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004354{
4355 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4356 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004357 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004358}
4359
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004360#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004361
William Lallemand614ca0d2019-10-07 13:52:11 +02004362/*
4363 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004364 *
4365 * Returns a bitfield containing the flags:
4366 * ERR_FATAL in any fatal error case
4367 * ERR_ALERT if the reason of the error is available in err
4368 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004369 */
Emeric Brun054563d2019-10-17 13:16:58 +02004370static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4371 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004372{
William Lallemandc9402072019-05-15 15:33:54 +02004373 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004374 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004375 int order = 0;
4376 X509_NAME *xname;
4377 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004378 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004379 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004380#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4381 STACK_OF(GENERAL_NAME) *names;
4382#endif
William Lallemand36b84632019-07-18 19:28:17 +02004383 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004384 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004385 int errcode = 0;
4386
4387 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004388
William Lallemande3af8fb2019-10-08 11:36:53 +02004389 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004390 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004391
William Lallemande3af8fb2019-10-08 11:36:53 +02004392 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004393
William Lallemandc9402072019-05-15 15:33:54 +02004394 ctx = SSL_CTX_new(SSLv23_server_method());
4395 if (!ctx) {
4396 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4397 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004398 errcode |= ERR_ALERT | ERR_FATAL;
4399 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004400 }
4401
Emeric Bruna96b5822019-10-17 13:25:14 +02004402 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4403 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004404 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004405
4406 ckch_inst = ckch_inst_new();
4407 if (!ckch_inst) {
4408 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4409 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004410 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004411 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004412 }
4413
William Lallemand36b84632019-07-18 19:28:17 +02004414 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004415 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004416 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004417 switch(EVP_PKEY_base_id(pkey)) {
4418 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004419 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004420 break;
4421 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004422 kinfo.sig = TLSEXT_signature_ecdsa;
4423 break;
4424 case EVP_PKEY_DSA:
4425 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004426 break;
4427 }
4428 EVP_PKEY_free(pkey);
4429 }
4430
Emeric Brun50bcecc2013-04-22 13:05:23 +02004431 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004432 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004433 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 +02004434 if (order < 0) {
4435 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004436 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004437 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004438 }
4439 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004440 }
4441 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004442#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004443 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004444 if (names) {
4445 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4446 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4447 if (name->type == GEN_DNS) {
4448 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004449 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004450 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004451 if (order < 0) {
4452 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004453 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004454 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004455 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004456 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004457 }
4458 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004459 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004460 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004461#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004462 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004463 i = -1;
4464 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4465 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004466 ASN1_STRING *value;
4467
4468 value = X509_NAME_ENTRY_get_data(entry);
4469 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004470 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004471 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004472 if (order < 0) {
4473 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004474 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004475 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004476 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004477 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004478 }
4479 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004480 /* we must not free the SSL_CTX anymore below, since it's already in
4481 * the tree, so it will be discovered and cleaned in time.
4482 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004483
Emeric Brunfc0421f2012-09-07 17:30:07 +02004484#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004485 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004486 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4487 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004488 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004489 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004490 }
4491#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004492 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004493 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004494 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004495 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004496 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004497 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004498
William Lallemand9117de92019-10-04 00:29:42 +02004499 /* everything succeed, the ckch instance can be used */
4500 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004501 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004502 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004503
William Lallemand02e19a52020-04-08 16:11:26 +02004504 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4505
Emeric Brun054563d2019-10-17 13:16:58 +02004506 *ckchi = ckch_inst;
4507 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004508
4509error:
4510 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004511 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004512 if (ckch_inst->is_default)
4513 SSL_CTX_free(ctx);
4514
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004515 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004516 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004517 }
William Lallemandd9199372019-10-04 15:37:05 +02004518 SSL_CTX_free(ctx);
4519
Emeric Brun054563d2019-10-17 13:16:58 +02004520 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004521}
4522
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004523/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004524static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4525 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004526 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004527{
Emeric Brun054563d2019-10-17 13:16:58 +02004528 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004529
4530 /* we found the ckchs in the tree, we can use it directly */
4531 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004532 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 +02004533 else
William Lallemand24bde432020-03-09 16:48:43 +01004534 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 +02004535
Emeric Brun054563d2019-10-17 13:16:58 +02004536 if (errcode & ERR_CODE)
4537 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004538
William Lallemand24bde432020-03-09 16:48:43 +01004539 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004540
4541 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004542 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004543 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004544}
4545
William Lallemand6be66ec2020-03-06 22:26:32 +01004546
William Lallemand4c68bba2020-03-30 18:45:10 +02004547
4548
4549/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4550 * done once. Zero is returned if the operation fails. No error is returned
4551 * if the random is said as not implemented, because we expect that openssl
4552 * will use another method once needed.
4553 */
4554static int ssl_initialize_random()
4555{
4556 unsigned char random;
4557 static int random_initialized = 0;
4558
4559 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4560 random_initialized = 1;
4561
4562 return random_initialized;
4563}
4564
William Lallemandec2d4932020-04-09 13:44:21 +02004565
William Lallemand6be66ec2020-03-06 22:26:32 +01004566/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4567 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4568 *
4569 * This function tries to open and store certificate files.
4570 */
4571static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004572{
William Lallemand6be66ec2020-03-06 22:26:32 +01004573 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004574 struct dirent **de_list;
4575 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004576 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004577 char *end;
4578 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004579 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004580 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004581#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004582 int is_bundle;
4583 int j;
4584#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004585
William Lallemandec2d4932020-04-09 13:44:21 +02004586 dir = crtlist_new(path, 1);
William Lallemand6be66ec2020-03-06 22:26:32 +01004587 if (dir == NULL) {
4588 memprintf(err, "not enough memory");
4589 return ERR_ALERT | ERR_FATAL;
4590 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004591
William Lallemand2d232c22020-03-06 22:12:35 +01004592 n = scandir(path, &de_list, 0, alphasort);
4593 if (n < 0) {
4594 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4595 err && *err ? *err : "", path, strerror(errno));
4596 cfgerr |= ERR_ALERT | ERR_FATAL;
4597 }
4598 else {
4599 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004600 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004601 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004602
William Lallemand2d232c22020-03-06 22:12:35 +01004603 end = strrchr(de->d_name, '.');
4604 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4605 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004606
William Lallemanda64593c2020-03-17 20:11:41 +01004607 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4608 if (stat(fp, &buf) != 0) {
4609 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4610 err && *err ? *err : "", fp, strerror(errno));
4611 cfgerr |= ERR_ALERT | ERR_FATAL;
4612 goto ignore_entry;
4613 }
4614 if (!S_ISREG(buf.st_mode))
4615 goto ignore_entry;
4616
William Lallemande718dfb2020-04-10 11:09:25 +02004617 entry = crtlist_entry_new();
William Lallemand6be66ec2020-03-06 22:26:32 +01004618 if (entry == NULL) {
4619 memprintf(err, "not enough memory '%s'", fp);
4620 cfgerr |= ERR_ALERT | ERR_FATAL;
4621 goto ignore_entry;
4622 }
4623
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004624#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004625 is_bundle = 0;
4626 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004627
William Lallemand2d232c22020-03-06 22:12:35 +01004628 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4629 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4630 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4631 is_bundle = 1;
4632 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004633 }
William Lallemand2d232c22020-03-06 22:12:35 +01004634 }
yanbzhu63ea8462015-12-09 13:35:14 -05004635
William Lallemand2d232c22020-03-06 22:12:35 +01004636 if (is_bundle) {
4637 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004638
William Lallemand2d232c22020-03-06 22:12:35 +01004639 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004640
William Lallemand2d232c22020-03-06 22:12:35 +01004641 /* increment i and free de until we get to a non-bundle cert
4642 * Note here that we look at de_list[i + 1] before freeing de
4643 * this is important since ignore_entry will free de. This also
4644 * guarantees that de->d_name continues to hold the same prefix.
4645 */
4646 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4647 free(de);
4648 i++;
4649 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004650 }
William Lallemand2d232c22020-03-06 22:12:35 +01004651
4652 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004653 ckchs = ckchs_lookup(fp);
4654 if (ckchs == NULL)
4655 ckchs = ckchs_load_cert_file(fp, 1, err);
4656 if (ckchs == NULL) {
4657 free(de);
4658 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004659 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004660 goto end;
4661 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004662 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004663 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004664 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004665 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004666 ebpt_insert(&dir->entries, &entry->node);
4667
William Lallemand2d232c22020-03-06 22:12:35 +01004668 /* Successfully processed the bundle */
4669 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004670 }
William Lallemand2d232c22020-03-06 22:12:35 +01004671 }
yanbzhu63ea8462015-12-09 13:35:14 -05004672
4673#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004674 ckchs = ckchs_lookup(fp);
4675 if (ckchs == NULL)
4676 ckchs = ckchs_load_cert_file(fp, 0, err);
4677 if (ckchs == NULL) {
4678 free(de);
4679 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004680 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004681 goto end;
4682 }
4683 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004684 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004685 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004686 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004687 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004688
yanbzhu08ce6ab2015-12-02 13:01:29 -05004689ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004690 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004691 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004692end:
William Lallemand2d232c22020-03-06 22:12:35 +01004693 free(de_list);
4694 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004695
4696 if (cfgerr & ERR_CODE) {
4697 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004698 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004699 } else {
4700 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004701 }
William Lallemand2d232c22020-03-06 22:12:35 +01004702 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004703
William Lallemand2d232c22020-03-06 22:12:35 +01004704}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004705
William Lallemand97b08102020-04-01 10:14:00 +02004706/*
4707 * Read a single crt-list line. /!\ alter the <line> string.
4708 * Fill <crt_path> and <crtlist_entry>
4709 * <crtlist_entry> must be alloc and free by the caller
4710 * <crtlist_entry->ssl_conf> is alloc by the function
4711 * <crtlist_entry->filters> is alloc by the function
4712 * <crt_path> is a ptr in <line>
4713 * Return an error code
4714 */
4715static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4716{
4717 int cfgerr = 0;
4718 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4719 char *end;
4720 char *args[MAX_CRT_ARGS + 1];
4721 struct ssl_bind_conf *ssl_conf = NULL;
4722
4723 if (!line || !crt_path || !entry)
4724 return ERR_ALERT | ERR_FATAL;
4725
4726 end = line + strlen(line);
4727 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4728 /* Check if we reached the limit and the last char is not \n.
4729 * Watch out for the last line without the terminating '\n'!
4730 */
4731 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4732 linenum, file, CRT_LINESIZE-1);
4733 cfgerr |= ERR_ALERT | ERR_FATAL;
4734 goto error;
4735 }
4736 arg = 0;
4737 newarg = 1;
4738 while (*line) {
4739 if (isspace((unsigned char)*line)) {
4740 newarg = 1;
4741 *line = 0;
4742 } else if (*line == '[') {
4743 if (ssl_b) {
4744 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4745 cfgerr |= ERR_ALERT | ERR_FATAL;
4746 goto error;
4747 }
4748 if (!arg) {
4749 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4750 cfgerr |= ERR_ALERT | ERR_FATAL;
4751 goto error;
4752 }
4753 ssl_b = arg;
4754 newarg = 1;
4755 *line = 0;
4756 } else if (*line == ']') {
4757 if (ssl_e) {
4758 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4759 cfgerr |= ERR_ALERT | ERR_FATAL;
4760 goto error;
4761 }
4762 if (!ssl_b) {
4763 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4764 cfgerr |= ERR_ALERT | ERR_FATAL;
4765 goto error;
4766 }
4767 ssl_e = arg;
4768 newarg = 1;
4769 *line = 0;
4770 } else if (newarg) {
4771 if (arg == MAX_CRT_ARGS) {
4772 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4773 cfgerr |= ERR_ALERT | ERR_FATAL;
4774 goto error;
4775 }
4776 newarg = 0;
4777 args[arg++] = line;
4778 }
4779 line++;
4780 }
4781 args[arg++] = line;
4782
4783 /* empty line */
4784 if (!*args[0]) {
4785 cfgerr |= ERR_NONE;
4786 goto error;
4787 }
4788
4789 *crt_path = args[0];
4790
William Lallemand1b2988b2020-04-10 17:20:45 +02004791 if (ssl_b) {
4792 ssl_conf = calloc(1, sizeof *ssl_conf);
4793 if (!ssl_conf) {
4794 memprintf(err, "not enough memory!");
4795 cfgerr |= ERR_ALERT | ERR_FATAL;
4796 goto error;
4797 }
William Lallemand97b08102020-04-01 10:14:00 +02004798 }
4799 cur_arg = ssl_b ? ssl_b : 1;
4800 while (cur_arg < ssl_e) {
4801 newarg = 0;
4802 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4803 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4804 newarg = 1;
4805 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4806 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4807 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4808 args[cur_arg], linenum, file);
4809 cfgerr |= ERR_ALERT | ERR_FATAL;
4810 goto error;
4811 }
4812 cur_arg += 1 + ssl_bind_kws[i].skip;
4813 break;
4814 }
4815 }
4816 if (!cfgerr && !newarg) {
4817 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4818 args[cur_arg], linenum, file);
4819 cfgerr |= ERR_ALERT | ERR_FATAL;
4820 goto error;
4821 }
4822 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004823 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004824 entry->ssl_conf = ssl_conf;
4825 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4826 entry->fcount = arg - cur_arg - 1;
4827
4828 return cfgerr;
4829
4830error:
4831 crtlist_free_filters(entry->filters);
4832 entry->filters = NULL;
4833 ssl_sock_free_ssl_conf(entry->ssl_conf);
4834 free(entry->ssl_conf);
4835 entry->ssl_conf = NULL;
4836 return cfgerr;
4837}
4838
William Lallemand2954c472020-03-06 21:54:13 +01004839/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4840 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4841 *
4842 * This function tries to open and store certificate files.
4843 */
4844static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4845{
4846 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004847 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004848 char thisline[CRT_LINESIZE];
4849 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004850 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004851 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004852 int linenum = 0;
4853 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004854
Willy Tarreauad1731d2013-04-02 17:35:58 +02004855 if ((f = fopen(file, "r")) == NULL) {
4856 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004857 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004858 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004859
William Lallemandec2d4932020-04-09 13:44:21 +02004860 newlist = crtlist_new(file, 0);
William Lallemand2954c472020-03-06 21:54:13 +01004861 if (newlist == NULL) {
4862 memprintf(err, "Not enough memory!");
4863 cfgerr |= ERR_ALERT | ERR_FATAL;
4864 goto error;
4865 }
William Lallemand2954c472020-03-06 21:54:13 +01004866
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004867 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004868 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004869 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004870 char *crt_path;
William Lallemand2954c472020-03-06 21:54:13 +01004871 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004872
4873 linenum++;
4874 end = line + strlen(line);
4875 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4876 /* Check if we reached the limit and the last char is not \n.
4877 * Watch out for the last line without the terminating '\n'!
4878 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004879 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4880 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004881 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004882 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004883 }
William Lallemand97b08102020-04-01 10:14:00 +02004884
4885 if (*line == '#' || *line == '\n' || *line == '\r')
4886 continue;
4887
William Lallemande718dfb2020-04-10 11:09:25 +02004888 entry = crtlist_entry_new();
William Lallemand97b08102020-04-01 10:14:00 +02004889 if (entry == NULL) {
4890 memprintf(err, "Not enough memory!");
4891 cfgerr |= ERR_ALERT | ERR_FATAL;
4892 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004893 }
William Lallemand97b08102020-04-01 10:14:00 +02004894
4895 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4896 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004897 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004898 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004899
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004900 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004901 if (!crt_path || !*crt_path) {
William Lallemande718dfb2020-04-10 11:09:25 +02004902 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004903 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004904 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004905 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004906
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004907 if (*crt_path != '/' && global_ssl.crt_base) {
4908 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4909 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4910 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004911 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004912 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004913 }
4914 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4915 crt_path = path;
4916 }
4917
William Lallemand2954c472020-03-06 21:54:13 +01004918 /* Look for a ckch_store or create one */
4919 ckchs = ckchs_lookup(crt_path);
4920 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004921 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004922 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004923 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004924 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004925 }
William Lallemand909086e2020-03-17 16:53:27 +01004926 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004927 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004928
William Lallemand97b08102020-04-01 10:14:00 +02004929 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004930 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004931
William Lallemand2954c472020-03-06 21:54:13 +01004932 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004933 entry->crtlist = newlist;
William Lallemand2954c472020-03-06 21:54:13 +01004934 ebpt_insert(&newlist->entries, &entry->node);
4935 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004936 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004937
4938 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004939 }
William Lallemand2954c472020-03-06 21:54:13 +01004940 if (cfgerr & ERR_CODE)
4941 goto error;
4942
William Lallemandc69f02d2020-04-06 19:07:03 +02004943 newlist->linecount = linenum;
4944
William Lallemand2954c472020-03-06 21:54:13 +01004945 fclose(f);
4946 *crtlist = newlist;
4947
4948 return cfgerr;
4949error:
William Lallemande718dfb2020-04-10 11:09:25 +02004950 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004951
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004952 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004953 crtlist_free(newlist);
4954 return cfgerr;
4955}
4956
4957/* Load a crt-list file, this is done in 2 parts:
4958 * - store the content of the file in a crtlist structure with crtlist_entry structures
4959 * - generate the instances by iterating on entries in the crtlist struct
4960 *
4961 * Nothing is locked there, this function is used in the configuration parser.
4962 *
4963 * Returns a set of ERR_* flags possibly with an error in <err>.
4964 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004965int 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 +01004966{
4967 struct crtlist *crtlist = NULL;
4968 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004969 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004970 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004971 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004972 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004973
William Lallemand79d31ec2020-03-25 15:10:49 +01004974 bind_conf_node = malloc(sizeof(*bind_conf_node));
4975 if (!bind_conf_node) {
4976 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4977 cfgerr |= ERR_FATAL | ERR_ALERT;
4978 goto error;
4979 }
4980 bind_conf_node->next = NULL;
4981 bind_conf_node->bind_conf = bind_conf;
4982
William Lallemand41ca9302020-04-08 13:15:18 +02004983 /* strip trailing slashes, including first one */
4984 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4985 *end = 0;
4986
William Lallemand2954c472020-03-06 21:54:13 +01004987 /* look for an existing crtlist or create one */
4988 eb = ebst_lookup(&crtlists_tree, file);
4989 if (eb) {
4990 crtlist = ebmb_entry(eb, struct crtlist, node);
4991 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004992 /* load a crt-list OR a directory */
4993 if (dir)
4994 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4995 else
4996 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4997
William Lallemand2954c472020-03-06 21:54:13 +01004998 if (!(cfgerr & ERR_CODE))
4999 ebst_insert(&crtlists_tree, &crtlist->node);
5000 }
5001
5002 if (cfgerr & ERR_CODE) {
5003 cfgerr |= ERR_FATAL | ERR_ALERT;
5004 goto error;
5005 }
5006
5007 /* generates ckch instance from the crtlist_entry */
5008 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5009 struct ckch_store *store;
5010 struct ckch_inst *ckch_inst = NULL;
5011
5012 store = entry->node.key;
5013 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
5014 if (cfgerr & ERR_CODE) {
5015 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
5016 goto error;
5017 }
William Lallemand49398312020-03-30 17:01:33 +02005018 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02005019 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01005020 }
William Lallemand2954c472020-03-06 21:54:13 +01005021
William Lallemand79d31ec2020-03-25 15:10:49 +01005022 /* add the bind_conf to the list */
5023 bind_conf_node->next = crtlist->bind_conf;
5024 crtlist->bind_conf = bind_conf_node;
5025
William Lallemand2954c472020-03-06 21:54:13 +01005026 return cfgerr;
5027error:
5028 {
William Lallemand49398312020-03-30 17:01:33 +02005029 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01005030 struct ckch_inst *inst, *s_inst;
5031
William Lallemand49398312020-03-30 17:01:33 +02005032 lastentry = entry; /* which entry we tried to generate last */
5033 if (lastentry) {
5034 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5035 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
5036 break;
5037
5038 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01005039
William Lallemand49398312020-03-30 17:01:33 +02005040 /* this was not generated for this bind_conf, skip */
5041 if (inst->bind_conf != bind_conf)
5042 continue;
5043
William Lallemandd9d5d1b2020-04-09 16:31:05 +02005044 /* free the sni_ctx and instance */
5045 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02005046 }
William Lallemand2954c472020-03-06 21:54:13 +01005047 }
William Lallemand2954c472020-03-06 21:54:13 +01005048 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005049 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005050 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005051 return cfgerr;
5052}
5053
William Lallemand06b22a82020-03-16 14:45:55 +01005054/* Returns a set of ERR_* flags possibly with an error in <err>. */
5055int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5056{
5057 struct stat buf;
5058 char fp[MAXPATHLEN+1];
5059 int cfgerr = 0;
5060 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005061 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005062
5063 if ((ckchs = ckchs_lookup(path))) {
5064 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005065 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005066 }
5067 if (stat(path, &buf) == 0) {
5068 if (S_ISDIR(buf.st_mode) == 0) {
5069 ckchs = ckchs_load_cert_file(path, 0, err);
5070 if (!ckchs)
5071 return ERR_ALERT | ERR_FATAL;
5072
William Lallemand24bde432020-03-09 16:48:43 +01005073 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005074 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005075 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005076 }
5077 } else {
5078 /* stat failed, could be a bundle */
5079 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5080 /* try to load a bundle if it is permitted */
5081 ckchs = ckchs_load_cert_file(path, 1, err);
5082 if (!ckchs)
5083 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005084 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005085 } else {
5086 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5087 err && *err ? *err : "", fp, strerror(errno));
5088 cfgerr |= ERR_ALERT | ERR_FATAL;
5089 }
5090 }
5091
5092 return cfgerr;
5093}
5094
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005095/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005096static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005097ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005098{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005099 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005100 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005101 SSL_OP_ALL | /* all known workarounds for bugs */
5102 SSL_OP_NO_SSLv2 |
5103 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005104 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005105 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005106 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005107 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005108 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005109 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005110 SSL_MODE_ENABLE_PARTIAL_WRITE |
5111 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005112 SSL_MODE_RELEASE_BUFFERS |
5113 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005114 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005115 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005116 int flags = MC_SSL_O_ALL;
5117 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005119 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005120 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005121
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005122 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005123 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5124 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5125 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005126 else
5127 flags = conf_ssl_methods->flags;
5128
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005129 min = conf_ssl_methods->min;
5130 max = conf_ssl_methods->max;
5131 /* start with TLSv10 to remove SSLv3 per default */
5132 if (!min && (!max || max >= CONF_TLSV10))
5133 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005134 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005135 if (min)
5136 flags |= (methodVersions[min].flag - 1);
5137 if (max)
5138 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005139 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005140 min = max = CONF_TLSV_NONE;
5141 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005142 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005143 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005144 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005145 if (min) {
5146 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005147 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5148 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5149 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5150 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005151 hole = 0;
5152 }
5153 max = i;
5154 }
5155 else {
5156 min = max = i;
5157 }
5158 }
5159 else {
5160 if (min)
5161 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005162 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005163 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005164 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5165 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005166 cfgerr += 1;
5167 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005168 /* save real min/max in bind_conf */
5169 conf_ssl_methods->min = min;
5170 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005171
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005172#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005173 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005174 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005175 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005176 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005177 else
5178 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5179 if (flags & methodVersions[i].flag)
5180 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005181#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005182 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005183 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5184 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005185#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005186
5187 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5188 options |= SSL_OP_NO_TICKET;
5189 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5190 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005191
5192#ifdef SSL_OP_NO_RENEGOTIATION
5193 options |= SSL_OP_NO_RENEGOTIATION;
5194#endif
5195
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005196 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005197
Willy Tarreau5db847a2019-05-09 14:13:35 +02005198#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005199 if (global_ssl.async)
5200 mode |= SSL_MODE_ASYNC;
5201#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005202 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005203 if (global_ssl.life_time)
5204 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005205
5206#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5207#ifdef OPENSSL_IS_BORINGSSL
5208 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5209 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005210#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005211 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005212 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005213 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5214 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005215#else
5216 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005217#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005218 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005219#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005220 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005221}
5222
William Lallemand4f45bb92017-10-30 20:08:51 +01005223
5224static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5225{
5226 if (first == block) {
5227 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5228 if (first->len > 0)
5229 sh_ssl_sess_tree_delete(sh_ssl_sess);
5230 }
5231}
5232
5233/* return first block from sh_ssl_sess */
5234static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5235{
5236 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5237
5238}
5239
5240/* store a session into the cache
5241 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5242 * data: asn1 encoded session
5243 * data_len: asn1 encoded session length
5244 * Returns 1 id session was stored (else 0)
5245 */
5246static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5247{
5248 struct shared_block *first;
5249 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5250
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005251 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005252 if (!first) {
5253 /* Could not retrieve enough free blocks to store that session */
5254 return 0;
5255 }
5256
5257 /* STORE the key in the first elem */
5258 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5259 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5260 first->len = sizeof(struct sh_ssl_sess_hdr);
5261
5262 /* it returns the already existing node
5263 or current node if none, never returns null */
5264 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5265 if (oldsh_ssl_sess != sh_ssl_sess) {
5266 /* NOTE: Row couldn't be in use because we lock read & write function */
5267 /* release the reserved row */
5268 shctx_row_dec_hot(ssl_shctx, first);
5269 /* replace the previous session already in the tree */
5270 sh_ssl_sess = oldsh_ssl_sess;
5271 /* ignore the previous session data, only use the header */
5272 first = sh_ssl_sess_first_block(sh_ssl_sess);
5273 shctx_row_inc_hot(ssl_shctx, first);
5274 first->len = sizeof(struct sh_ssl_sess_hdr);
5275 }
5276
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005277 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005278 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005279 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005280 }
5281
5282 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005283
5284 return 1;
5285}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005286
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005287/* SSL callback used when a new session is created while connecting to a server */
5288static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5289{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005290 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005291 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005292
Willy Tarreau07d94e42018-09-20 10:57:52 +02005293 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005294
Olivier Houcharde6060c52017-11-16 17:42:52 +01005295 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5296 int len;
5297 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005298
Olivier Houcharde6060c52017-11-16 17:42:52 +01005299 len = i2d_SSL_SESSION(sess, NULL);
5300 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5301 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5302 } else {
5303 free(s->ssl_ctx.reused_sess[tid].ptr);
5304 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5305 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5306 }
5307 if (s->ssl_ctx.reused_sess[tid].ptr) {
5308 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5309 &ptr);
5310 }
5311 } else {
5312 free(s->ssl_ctx.reused_sess[tid].ptr);
5313 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5314 }
5315
5316 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005317}
5318
Olivier Houcharde6060c52017-11-16 17:42:52 +01005319
William Lallemanded0b5ad2017-10-30 19:36:36 +01005320/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005321int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005322{
5323 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5324 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5325 unsigned char *p;
5326 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005327 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005328 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005329
5330 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005331 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005332 * note: SSL_SESSION_set1_id is using
5333 * a memcpy so we need to use a different pointer
5334 * than sid_data or sid_ctx_data to avoid valgrind
5335 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005336 */
5337
5338 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005339
5340 /* copy value in an other buffer */
5341 memcpy(encid, sid_data, sid_length);
5342
5343 /* pad with 0 */
5344 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5345 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5346
5347 /* force length to zero to avoid ASN1 encoding */
5348 SSL_SESSION_set1_id(sess, encid, 0);
5349
5350 /* force length to zero to avoid ASN1 encoding */
5351 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005352
5353 /* check if buffer is large enough for the ASN1 encoded session */
5354 data_len = i2d_SSL_SESSION(sess, NULL);
5355 if (data_len > SHSESS_MAX_DATA_LEN)
5356 goto err;
5357
5358 p = encsess;
5359
5360 /* process ASN1 session encoding before the lock */
5361 i2d_SSL_SESSION(sess, &p);
5362
William Lallemanded0b5ad2017-10-30 19:36:36 +01005363
William Lallemanda3c77cf2017-10-30 23:44:40 +01005364 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005365 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005366 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005367 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005368err:
5369 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005370 SSL_SESSION_set1_id(sess, encid, sid_length);
5371 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005372
5373 return 0; /* do not increment session reference count */
5374}
5375
5376/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005377SSL_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 +01005378{
William Lallemand4f45bb92017-10-30 20:08:51 +01005379 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005380 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5381 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005382 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005383 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005384
5385 global.shctx_lookups++;
5386
5387 /* allow the session to be freed automatically by openssl */
5388 *do_copy = 0;
5389
5390 /* tree key is zeros padded sessionid */
5391 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5392 memcpy(tmpkey, key, key_len);
5393 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5394 key = tmpkey;
5395 }
5396
5397 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005398 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005399
5400 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005401 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5402 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005403 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005404 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005405 global.shctx_misses++;
5406 return NULL;
5407 }
5408
William Lallemand4f45bb92017-10-30 20:08:51 +01005409 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5410 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005411
William Lallemand4f45bb92017-10-30 20:08:51 +01005412 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 +01005413
William Lallemanda3c77cf2017-10-30 23:44:40 +01005414 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005415
5416 /* decode ASN1 session */
5417 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005418 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005419 /* Reset session id and session id contenxt */
5420 if (sess) {
5421 SSL_SESSION_set1_id(sess, key, key_len);
5422 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5423 }
5424
5425 return sess;
5426}
5427
William Lallemand4f45bb92017-10-30 20:08:51 +01005428
William Lallemanded0b5ad2017-10-30 19:36:36 +01005429/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005430void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005431{
William Lallemand4f45bb92017-10-30 20:08:51 +01005432 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005433 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5434 unsigned int sid_length;
5435 const unsigned char *sid_data;
5436 (void)ctx;
5437
5438 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5439 /* tree key is zeros padded sessionid */
5440 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5441 memcpy(tmpkey, sid_data, sid_length);
5442 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5443 sid_data = tmpkey;
5444 }
5445
William Lallemanda3c77cf2017-10-30 23:44:40 +01005446 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005447
5448 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005449 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5450 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005451 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005452 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005453 }
5454
5455 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005456 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005457}
5458
5459/* Set session cache mode to server and disable openssl internal cache.
5460 * Set shared cache callbacks on an ssl context.
5461 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005462void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005463{
5464 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5465
5466 if (!ssl_shctx) {
5467 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5468 return;
5469 }
5470
5471 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5472 SSL_SESS_CACHE_NO_INTERNAL |
5473 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5474
5475 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005476 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5477 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5478 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005479}
5480
William Lallemand8b453912019-11-21 15:48:10 +01005481/*
5482 * This function applies the SSL configuration on a SSL_CTX
5483 * It returns an error code and fills the <err> buffer
5484 */
5485int 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 +01005486{
5487 struct proxy *curproxy = bind_conf->frontend;
5488 int cfgerr = 0;
5489 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005490 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005491 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005492#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005493 const char *conf_ciphersuites;
5494#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005495 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005496
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005497 if (ssl_conf) {
5498 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5499 int i, min, max;
5500 int flags = MC_SSL_O_ALL;
5501
5502 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005503 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5504 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005505 if (min)
5506 flags |= (methodVersions[min].flag - 1);
5507 if (max)
5508 flags |= ~((methodVersions[max].flag << 1) - 1);
5509 min = max = CONF_TLSV_NONE;
5510 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5511 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5512 if (min)
5513 max = i;
5514 else
5515 min = max = i;
5516 }
5517 /* save real min/max */
5518 conf_ssl_methods->min = min;
5519 conf_ssl_methods->max = max;
5520 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005521 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5522 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005523 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005524 }
5525 }
5526
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005527 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005528 case SSL_SOCK_VERIFY_NONE:
5529 verify = SSL_VERIFY_NONE;
5530 break;
5531 case SSL_SOCK_VERIFY_OPTIONAL:
5532 verify = SSL_VERIFY_PEER;
5533 break;
5534 case SSL_SOCK_VERIFY_REQUIRED:
5535 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5536 break;
5537 }
5538 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5539 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005540 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 +01005541 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 +01005542 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 +01005543 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005544 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005545 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005546 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005547 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005548 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005549 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005550 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5551 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5552 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5553 cfgerr |= ERR_ALERT | ERR_FATAL;
5554 }
5555 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005556 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005557 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 +02005558 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005559 }
Emeric Brun850efd52014-01-29 12:24:34 +01005560 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005561 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5562 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005563 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005564 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005565#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005566 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005567 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5568
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005569 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005570 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5571 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005572 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005573 }
Emeric Brun561e5742012-10-02 15:20:55 +02005574 else {
5575 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5576 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005577 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005578#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005579 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005580 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005581#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005582 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005583 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005584 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5585 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005586 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005587 }
5588 }
5589#endif
5590
William Lallemand4f45bb92017-10-30 20:08:51 +01005591 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005592 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5593 if (conf_ciphers &&
5594 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005595 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5596 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005597 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005598 }
5599
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005600#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005601 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5602 if (conf_ciphersuites &&
5603 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005604 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5605 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005606 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005607 }
5608#endif
5609
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005610#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005611 /* If tune.ssl.default-dh-param has not been set,
5612 neither has ssl-default-dh-file and no static DH
5613 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005614 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005615 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005616 (ssl_dh_ptr_index == -1 ||
5617 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005618 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5619 const SSL_CIPHER * cipher = NULL;
5620 char cipher_description[128];
5621 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5622 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5623 which is not ephemeral DH. */
5624 const char dhe_description[] = " Kx=DH ";
5625 const char dhe_export_description[] = " Kx=DH(";
5626 int idx = 0;
5627 int dhe_found = 0;
5628 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005629
Remi Gacogne23d5d372014-10-10 17:04:26 +02005630 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005631
Remi Gacogne23d5d372014-10-10 17:04:26 +02005632 if (ssl) {
5633 ciphers = SSL_get_ciphers(ssl);
5634
5635 if (ciphers) {
5636 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5637 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5638 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5639 if (strstr(cipher_description, dhe_description) != NULL ||
5640 strstr(cipher_description, dhe_export_description) != NULL) {
5641 dhe_found = 1;
5642 break;
5643 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005644 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005645 }
5646 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005647 SSL_free(ssl);
5648 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005649 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005650
Lukas Tribus90132722014-08-18 00:56:33 +02005651 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005652 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",
5653 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005654 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005655 }
5656
Willy Tarreauef934602016-12-22 23:12:01 +01005657 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005658 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005659
Willy Tarreauef934602016-12-22 23:12:01 +01005660 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005661 if (local_dh_1024 == NULL) {
5662 local_dh_1024 = ssl_get_dh_1024();
5663 }
Willy Tarreauef934602016-12-22 23:12:01 +01005664 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005665 if (local_dh_2048 == NULL) {
5666 local_dh_2048 = ssl_get_dh_2048();
5667 }
Willy Tarreauef934602016-12-22 23:12:01 +01005668 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005669 if (local_dh_4096 == NULL) {
5670 local_dh_4096 = ssl_get_dh_4096();
5671 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005672 }
5673 }
5674 }
5675#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005676
Emeric Brunfc0421f2012-09-07 17:30:07 +02005677 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005678#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005679 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005680#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005681
Bernard Spil13c53f82018-02-15 13:34:58 +01005682#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005683 ssl_conf_cur = NULL;
5684 if (ssl_conf && ssl_conf->npn_str)
5685 ssl_conf_cur = ssl_conf;
5686 else if (bind_conf->ssl_conf.npn_str)
5687 ssl_conf_cur = &bind_conf->ssl_conf;
5688 if (ssl_conf_cur)
5689 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005690#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005691#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005692 ssl_conf_cur = NULL;
5693 if (ssl_conf && ssl_conf->alpn_str)
5694 ssl_conf_cur = ssl_conf;
5695 else if (bind_conf->ssl_conf.alpn_str)
5696 ssl_conf_cur = &bind_conf->ssl_conf;
5697 if (ssl_conf_cur)
5698 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005699#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005700#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005701 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5702 if (conf_curves) {
5703 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005704 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5705 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005706 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005707 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005708 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005709 }
5710#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005711#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005712 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005713 int i;
5714 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005715#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005716 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005717 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5718 NULL);
5719
5720 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005721 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005722 return cfgerr;
5723 }
5724#else
5725 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5726 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5727 ECDHE_DEFAULT_CURVE);
5728#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005729
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005730 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005731 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005732 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5733 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005734 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005735 }
5736 else {
5737 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5738 EC_KEY_free(ecdh);
5739 }
5740 }
5741#endif
5742
Emeric Brunfc0421f2012-09-07 17:30:07 +02005743 return cfgerr;
5744}
5745
Evan Broderbe554312013-06-27 00:05:25 -07005746static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5747{
5748 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5749 size_t prefixlen, suffixlen;
5750
5751 /* Trivial case */
5752 if (strcmp(pattern, hostname) == 0)
5753 return 1;
5754
Evan Broderbe554312013-06-27 00:05:25 -07005755 /* The rest of this logic is based on RFC 6125, section 6.4.3
5756 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5757
Emeric Bruna848dae2013-10-08 11:27:28 +02005758 pattern_wildcard = NULL;
5759 pattern_left_label_end = pattern;
5760 while (*pattern_left_label_end != '.') {
5761 switch (*pattern_left_label_end) {
5762 case 0:
5763 /* End of label not found */
5764 return 0;
5765 case '*':
5766 /* If there is more than one wildcards */
5767 if (pattern_wildcard)
5768 return 0;
5769 pattern_wildcard = pattern_left_label_end;
5770 break;
5771 }
5772 pattern_left_label_end++;
5773 }
5774
5775 /* If it's not trivial and there is no wildcard, it can't
5776 * match */
5777 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005778 return 0;
5779
5780 /* Make sure all labels match except the leftmost */
5781 hostname_left_label_end = strchr(hostname, '.');
5782 if (!hostname_left_label_end
5783 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5784 return 0;
5785
5786 /* Make sure the leftmost label of the hostname is long enough
5787 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005788 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005789 return 0;
5790
5791 /* Finally compare the string on either side of the
5792 * wildcard */
5793 prefixlen = pattern_wildcard - pattern;
5794 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005795 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5796 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005797 return 0;
5798
5799 return 1;
5800}
5801
5802static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5803{
5804 SSL *ssl;
5805 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005806 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005807 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005808 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005809
5810 int depth;
5811 X509 *cert;
5812 STACK_OF(GENERAL_NAME) *alt_names;
5813 int i;
5814 X509_NAME *cert_subject;
5815 char *str;
5816
5817 if (ok == 0)
5818 return ok;
5819
5820 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005821 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005822 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005823
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005824 /* We're checking if the provided hostnames match the desired one. The
5825 * desired hostname comes from the SNI we presented if any, or if not
5826 * provided then it may have been explicitly stated using a "verifyhost"
5827 * directive. If neither is set, we don't care about the name so the
5828 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005829 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005830 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005831 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005832 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005833 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005834 if (!servername)
5835 return ok;
5836 }
Evan Broderbe554312013-06-27 00:05:25 -07005837
5838 /* We only need to verify the CN on the actual server cert,
5839 * not the indirect CAs */
5840 depth = X509_STORE_CTX_get_error_depth(ctx);
5841 if (depth != 0)
5842 return ok;
5843
5844 /* At this point, the cert is *not* OK unless we can find a
5845 * hostname match */
5846 ok = 0;
5847
5848 cert = X509_STORE_CTX_get_current_cert(ctx);
5849 /* It seems like this might happen if verify peer isn't set */
5850 if (!cert)
5851 return ok;
5852
5853 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5854 if (alt_names) {
5855 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5856 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5857 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005858#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005859 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5860#else
Evan Broderbe554312013-06-27 00:05:25 -07005861 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005862#endif
Evan Broderbe554312013-06-27 00:05:25 -07005863 ok = ssl_sock_srv_hostcheck(str, servername);
5864 OPENSSL_free(str);
5865 }
5866 }
5867 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005868 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005869 }
5870
5871 cert_subject = X509_get_subject_name(cert);
5872 i = -1;
5873 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5874 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005875 ASN1_STRING *value;
5876 value = X509_NAME_ENTRY_get_data(entry);
5877 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005878 ok = ssl_sock_srv_hostcheck(str, servername);
5879 OPENSSL_free(str);
5880 }
5881 }
5882
Willy Tarreau71d058c2017-07-26 20:09:56 +02005883 /* report the mismatch and indicate if SNI was used or not */
5884 if (!ok && !conn->err_code)
5885 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005886 return ok;
5887}
5888
Emeric Brun94324a42012-10-11 14:00:19 +02005889/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005890int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005891{
Willy Tarreau03209342016-12-22 17:08:28 +01005892 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005893 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005894 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005895 SSL_OP_ALL | /* all known workarounds for bugs */
5896 SSL_OP_NO_SSLv2 |
5897 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005898 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005899 SSL_MODE_ENABLE_PARTIAL_WRITE |
5900 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005901 SSL_MODE_RELEASE_BUFFERS |
5902 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005903 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005904 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005905 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005906 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005907 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005908
Thierry Fournier383085f2013-01-24 14:15:43 +01005909 /* Make sure openssl opens /dev/urandom before the chroot */
5910 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005911 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005912 cfgerr++;
5913 }
5914
Willy Tarreaufce03112015-01-15 21:32:40 +01005915 /* Automatic memory computations need to know we use SSL there */
5916 global.ssl_used_backend = 1;
5917
5918 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005919 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005920 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005921 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5922 curproxy->id, srv->id,
5923 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005924 cfgerr++;
5925 return cfgerr;
5926 }
5927 }
Christopher Fauletf61f33a2020-03-27 18:55:49 +01005928 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02005929 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005930
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005931 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005932 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005933 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5934 proxy_type_str(curproxy), curproxy->id,
5935 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005936 cfgerr++;
5937 return cfgerr;
5938 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005939
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005940 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005941 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5942 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5943 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005944 else
5945 flags = conf_ssl_methods->flags;
5946
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005947 /* Real min and max should be determinate with configuration and openssl's capabilities */
5948 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005949 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005950 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005951 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005952
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005953 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005954 min = max = CONF_TLSV_NONE;
5955 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005956 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005957 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005958 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005959 if (min) {
5960 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005961 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5962 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5963 proxy_type_str(curproxy), curproxy->id, srv->id,
5964 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005965 hole = 0;
5966 }
5967 max = i;
5968 }
5969 else {
5970 min = max = i;
5971 }
5972 }
5973 else {
5974 if (min)
5975 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005976 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005977 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005978 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5979 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005980 cfgerr += 1;
5981 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005982
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005983#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005984 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005985 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005986 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005987 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005988 else
5989 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5990 if (flags & methodVersions[i].flag)
5991 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005992#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005993 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005994 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5995 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005996#endif
5997
5998 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5999 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01006000 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006001
Willy Tarreau5db847a2019-05-09 14:13:35 +02006002#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006003 if (global_ssl.async)
6004 mode |= SSL_MODE_ASYNC;
6005#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01006006 SSL_CTX_set_mode(ctx, mode);
6007 srv->ssl_ctx.ctx = ctx;
6008
Emeric Bruna7aa3092012-10-26 12:58:00 +02006009 if (srv->ssl_ctx.client_crt) {
6010 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 +01006011 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
6012 proxy_type_str(curproxy), curproxy->id,
6013 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006014 cfgerr++;
6015 }
6016 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 +01006017 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
6018 proxy_type_str(curproxy), curproxy->id,
6019 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006020 cfgerr++;
6021 }
6022 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006023 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
6024 proxy_type_str(curproxy), curproxy->id,
6025 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02006026 cfgerr++;
6027 }
6028 }
Emeric Brun94324a42012-10-11 14:00:19 +02006029
Emeric Brun850efd52014-01-29 12:24:34 +01006030 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
6031 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01006032 switch (srv->ssl_ctx.verify) {
6033 case SSL_SOCK_VERIFY_NONE:
6034 verify = SSL_VERIFY_NONE;
6035 break;
6036 case SSL_SOCK_VERIFY_REQUIRED:
6037 verify = SSL_VERIFY_PEER;
6038 break;
6039 }
Evan Broderbe554312013-06-27 00:05:25 -07006040 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01006041 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02006042 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01006043 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02006044 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02006045 /* set CAfile to verify */
6046 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
6047 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01006048 curproxy->id, srv->id,
6049 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006050 cfgerr++;
6051 }
6052 }
Emeric Brun850efd52014-01-29 12:24:34 +01006053 else {
6054 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01006055 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",
6056 curproxy->id, srv->id,
6057 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006058 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01006059 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
6060 curproxy->id, srv->id,
6061 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01006062 cfgerr++;
6063 }
Emeric Brunef42d922012-10-11 16:11:36 +02006064#ifdef X509_V_FLAG_CRL_CHECK
6065 if (srv->ssl_ctx.crl_file) {
6066 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6067
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006068 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006069 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6070 curproxy->id, srv->id,
6071 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006072 cfgerr++;
6073 }
6074 else {
6075 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6076 }
6077 }
6078#endif
6079 }
6080
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006081 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6082 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6083 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006084 if (srv->ssl_ctx.ciphers &&
6085 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006086 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6087 curproxy->id, srv->id,
6088 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006089 cfgerr++;
6090 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006091
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006092#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006093 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006094 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006095 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6096 curproxy->id, srv->id,
6097 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6098 cfgerr++;
6099 }
6100#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006101#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6102 if (srv->ssl_ctx.npn_str)
6103 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6104#endif
6105#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6106 if (srv->ssl_ctx.alpn_str)
6107 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6108#endif
6109
Emeric Brun94324a42012-10-11 14:00:19 +02006110
6111 return cfgerr;
6112}
6113
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006114/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006115 * be NULL, in which case nothing is done. Returns the number of errors
6116 * encountered.
6117 */
Willy Tarreau03209342016-12-22 17:08:28 +01006118int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006119{
6120 struct ebmb_node *node;
6121 struct sni_ctx *sni;
6122 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006123 int errcode = 0;
6124 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006125
Willy Tarreaufce03112015-01-15 21:32:40 +01006126 /* Automatic memory computations need to know we use SSL there */
6127 global.ssl_used_frontend = 1;
6128
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006129 /* Make sure openssl opens /dev/urandom before the chroot */
6130 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006131 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006132 err++;
6133 }
6134 /* Create initial_ctx used to start the ssl connection before do switchctx */
6135 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006136 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006137 /* It should not be necessary to call this function, but it's
6138 necessary first to check and move all initialisation related
6139 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006140 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006141 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006142 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006143 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006144
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006145 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006146 while (node) {
6147 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006148 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6149 /* only initialize the CTX on its first occurrence and
6150 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006151 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006152 node = ebmb_next(node);
6153 }
6154
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006155 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006156 while (node) {
6157 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006158 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006159 /* only initialize the CTX on its first occurrence and
6160 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006161 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6162 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006163 node = ebmb_next(node);
6164 }
William Lallemand8b453912019-11-21 15:48:10 +01006165
6166 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006167 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006168 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006169 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006170 err++;
6171 }
6172
6173 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006174 return err;
6175}
6176
Willy Tarreau55d37912016-12-21 23:38:39 +01006177/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6178 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6179 * alerts are directly emitted since the rest of the stack does it below.
6180 */
6181int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6182{
6183 struct proxy *px = bind_conf->frontend;
6184 int alloc_ctx;
6185 int err;
6186
6187 if (!bind_conf->is_ssl) {
6188 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006189 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6190 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006191 }
6192 return 0;
6193 }
6194 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006195 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006196 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6197 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006198 }
6199 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006200 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6201 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006202 return -1;
6203 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006204 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006205 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006206 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006207 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006208 sizeof(*sh_ssl_sess_tree),
6209 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006210 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006211 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6212 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");
6213 else
6214 ha_alert("Unable to allocate SSL session cache.\n");
6215 return -1;
6216 }
6217 /* free block callback */
6218 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6219 /* init the root tree within the extra space */
6220 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6221 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006222 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006223 err = 0;
6224 /* initialize all certificate contexts */
6225 err += ssl_sock_prepare_all_ctx(bind_conf);
6226
6227 /* initialize CA variables if the certificates generation is enabled */
6228 err += ssl_sock_load_ca(bind_conf);
6229
6230 return -err;
6231}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006232
6233/* release ssl context allocated for servers. */
6234void ssl_sock_free_srv_ctx(struct server *srv)
6235{
Olivier Houchardc7566002018-11-20 23:33:50 +01006236#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6237 if (srv->ssl_ctx.alpn_str)
6238 free(srv->ssl_ctx.alpn_str);
6239#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006240#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006241 if (srv->ssl_ctx.npn_str)
6242 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006243#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006244 if (srv->ssl_ctx.ctx)
6245 SSL_CTX_free(srv->ssl_ctx.ctx);
6246}
6247
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006248/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006249 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6250 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006251void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006252{
6253 struct ebmb_node *node, *back;
6254 struct sni_ctx *sni;
6255
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006256 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006257 while (node) {
6258 sni = ebmb_entry(node, struct sni_ctx, name);
6259 back = ebmb_next(node);
6260 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006261 SSL_CTX_free(sni->ctx);
6262 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006263 ssl_sock_free_ssl_conf(sni->conf);
6264 free(sni->conf);
6265 sni->conf = NULL;
6266 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006267 free(sni);
6268 node = back;
6269 }
6270
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006271 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006272 while (node) {
6273 sni = ebmb_entry(node, struct sni_ctx, name);
6274 back = ebmb_next(node);
6275 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006276 SSL_CTX_free(sni->ctx);
6277 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006278 ssl_sock_free_ssl_conf(sni->conf);
6279 free(sni->conf);
6280 sni->conf = NULL;
6281 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006282 free(sni);
6283 node = back;
6284 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006285 SSL_CTX_free(bind_conf->initial_ctx);
6286 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006287 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006288 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006289 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006290}
6291
Willy Tarreau795cdab2016-12-22 17:30:54 +01006292/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6293void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6294{
6295 ssl_sock_free_ca(bind_conf);
6296 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006297 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006298 free(bind_conf->ca_sign_file);
6299 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006300 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006301 free(bind_conf->keys_ref->filename);
6302 free(bind_conf->keys_ref->tlskeys);
6303 LIST_DEL(&bind_conf->keys_ref->list);
6304 free(bind_conf->keys_ref);
6305 }
6306 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006307 bind_conf->ca_sign_pass = NULL;
6308 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006309}
6310
Christopher Faulet31af49d2015-06-09 17:29:50 +02006311/* Load CA cert file and private key used to generate certificates */
6312int
Willy Tarreau03209342016-12-22 17:08:28 +01006313ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006314{
Willy Tarreau03209342016-12-22 17:08:28 +01006315 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006316 FILE *fp;
6317 X509 *cacert = NULL;
6318 EVP_PKEY *capkey = NULL;
6319 int err = 0;
6320
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006321 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006322 return err;
6323
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006324#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006325 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006326 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006327 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006328 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006329 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006330#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006331
Christopher Faulet31af49d2015-06-09 17:29:50 +02006332 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006333 ha_alert("Proxy '%s': cannot enable certificate generation, "
6334 "no CA certificate File configured at [%s:%d].\n",
6335 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006336 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006337 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006338
6339 /* read in the CA certificate */
6340 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006341 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6342 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006343 goto load_error;
6344 }
6345 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006346 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6347 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006348 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006349 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006350 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006351 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006352 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6353 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006354 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006355 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006356
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006357 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006358 bind_conf->ca_sign_cert = cacert;
6359 bind_conf->ca_sign_pkey = capkey;
6360 return err;
6361
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006362 read_error:
6363 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006364 if (capkey) EVP_PKEY_free(capkey);
6365 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006366 load_error:
6367 bind_conf->generate_certs = 0;
6368 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006369 return err;
6370}
6371
6372/* Release CA cert and private key used to generate certificated */
6373void
6374ssl_sock_free_ca(struct bind_conf *bind_conf)
6375{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006376 if (bind_conf->ca_sign_pkey)
6377 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6378 if (bind_conf->ca_sign_cert)
6379 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006380 bind_conf->ca_sign_pkey = NULL;
6381 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006382}
6383
Emeric Brun46591952012-05-18 15:47:34 +02006384/*
6385 * This function is called if SSL * context is not yet allocated. The function
6386 * is designed to be called before any other data-layer operation and sets the
6387 * handshake flag on the connection. It is safe to call it multiple times.
6388 * It returns 0 on success and -1 in error case.
6389 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006390static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006391{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006392 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006393 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006394 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006395 return 0;
6396
Willy Tarreau3c728722014-01-23 13:50:42 +01006397 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006398 return 0;
6399
Olivier Houchard66ab4982019-02-26 18:37:15 +01006400 ctx = pool_alloc(ssl_sock_ctx_pool);
6401 if (!ctx) {
6402 conn->err_code = CO_ER_SSL_NO_MEM;
6403 return -1;
6404 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006405 ctx->wait_event.tasklet = tasklet_new();
6406 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006407 conn->err_code = CO_ER_SSL_NO_MEM;
6408 pool_free(ssl_sock_ctx_pool, ctx);
6409 return -1;
6410 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006411 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6412 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006413 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006414 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006415 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006416 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006417 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006418 ctx->xprt_st = 0;
6419 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006420
6421 /* Only work with sockets for now, this should be adapted when we'll
6422 * add QUIC support.
6423 */
6424 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006425 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006426 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6427 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006428 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006429
Willy Tarreau20879a02012-12-03 16:32:10 +01006430 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6431 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006432 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006433 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006434
Emeric Brun46591952012-05-18 15:47:34 +02006435 /* If it is in client mode initiate SSL session
6436 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006437 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006438 int may_retry = 1;
6439
6440 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006441 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006442 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6443 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006444 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006445 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006446 goto retry_connect;
6447 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006448 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006449 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006450 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006451 ctx->bio = BIO_new(ha_meth);
6452 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006453 SSL_free(ctx->ssl);
6454 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006455 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006456 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006457 goto retry_connect;
6458 }
Emeric Brun55476152014-11-12 17:35:37 +01006459 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006460 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006461 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006462 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006463 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006464
Evan Broderbe554312013-06-27 00:05:25 -07006465 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006466 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6467 SSL_free(ctx->ssl);
6468 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006469 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006470 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006471 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006472 goto retry_connect;
6473 }
Emeric Brun55476152014-11-12 17:35:37 +01006474 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006475 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006476 }
6477
Olivier Houchard66ab4982019-02-26 18:37:15 +01006478 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006479 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6480 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6481 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 +01006482 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006483 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006484 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6485 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006486 } else if (sess) {
6487 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006488 }
6489 }
Evan Broderbe554312013-06-27 00:05:25 -07006490
Emeric Brun46591952012-05-18 15:47:34 +02006491 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006492 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006493
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006494 _HA_ATOMIC_ADD(&sslconns, 1);
6495 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006496 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006497 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006498 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006499 return 0;
6500 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006501 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006502 int may_retry = 1;
6503
6504 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006505 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006506 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6507 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006508 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006509 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006510 goto retry_accept;
6511 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006512 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006513 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006514 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006515 ctx->bio = BIO_new(ha_meth);
6516 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006517 SSL_free(ctx->ssl);
6518 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006519 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006520 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006521 goto retry_accept;
6522 }
Emeric Brun55476152014-11-12 17:35:37 +01006523 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006524 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006525 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006526 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006527 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006528
Emeric Brune1f38db2012-09-03 20:36:47 +02006529 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006530 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6531 SSL_free(ctx->ssl);
6532 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006533 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006534 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006535 goto retry_accept;
6536 }
Emeric Brun55476152014-11-12 17:35:37 +01006537 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006538 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006539 }
6540
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006541#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6542 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6543 b_alloc(&ctx->early_buf);
6544 SSL_set_max_early_data(ctx->ssl,
6545 /* Only allow early data if we managed to allocate
6546 * a buffer.
6547 */
6548 (!b_is_null(&ctx->early_buf)) ?
6549 global.tune.bufsize - global.tune.maxrewrite : 0);
6550 }
6551#endif
6552
Olivier Houchard66ab4982019-02-26 18:37:15 +01006553 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006554
Emeric Brun46591952012-05-18 15:47:34 +02006555 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006556 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006557#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006558 conn->flags |= CO_FL_EARLY_SSL_HS;
6559#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006560
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006561 _HA_ATOMIC_ADD(&sslconns, 1);
6562 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006563 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006564 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006565 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006566 return 0;
6567 }
6568 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006569 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006570err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006571 if (ctx && ctx->wait_event.tasklet)
6572 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006573 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006574 return -1;
6575}
6576
6577
6578/* This is the callback which is used when an SSL handshake is pending. It
6579 * updates the FD status if it wants some polling before being called again.
6580 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6581 * otherwise it returns non-zero and removes itself from the connection's
6582 * flags (the bit is provided in <flag> by the caller).
6583 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006584static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006585{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006586 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006587 int ret;
6588
Willy Tarreau3c728722014-01-23 13:50:42 +01006589 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006590 return 0;
6591
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006592 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006593 goto out_error;
6594
Willy Tarreau5db847a2019-05-09 14:13:35 +02006595#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006596 /*
6597 * Check if we have early data. If we do, we have to read them
6598 * before SSL_do_handshake() is called, And there's no way to
6599 * detect early data, except to try to read them
6600 */
6601 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006602 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006603
Olivier Houchard54907bb2019-12-19 15:02:39 +01006604 while (1) {
6605 ret = SSL_read_early_data(ctx->ssl,
6606 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6607 &read_data);
6608 if (ret == SSL_READ_EARLY_DATA_ERROR)
6609 goto check_error;
6610 if (read_data > 0) {
6611 conn->flags |= CO_FL_EARLY_DATA;
6612 b_add(&ctx->early_buf, read_data);
6613 }
6614 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6615 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6616 if (!b_data(&ctx->early_buf))
6617 b_free(&ctx->early_buf);
6618 break;
6619 }
6620 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006621 }
6622#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006623 /* If we use SSL_do_handshake to process a reneg initiated by
6624 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6625 * Usually SSL_write and SSL_read are used and process implicitly
6626 * the reneg handshake.
6627 * Here we use SSL_peek as a workaround for reneg.
6628 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006629 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006630 char c;
6631
Olivier Houchard66ab4982019-02-26 18:37:15 +01006632 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006633 if (ret <= 0) {
6634 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006635 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006636
Emeric Brun674b7432012-11-08 19:21:55 +01006637 if (ret == SSL_ERROR_WANT_WRITE) {
6638 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006639 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006640 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006641 return 0;
6642 }
6643 else if (ret == SSL_ERROR_WANT_READ) {
6644 /* handshake may have been completed but we have
6645 * no more data to read.
6646 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006647 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006648 ret = 1;
6649 goto reneg_ok;
6650 }
6651 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006652 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006653 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006654 return 0;
6655 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006656#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006657 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006658 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006659 return 0;
6660 }
6661#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006662 else if (ret == SSL_ERROR_SYSCALL) {
6663 /* if errno is null, then connection was successfully established */
6664 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6665 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006666 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006667#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6668 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006669 conn->err_code = CO_ER_SSL_HANDSHAKE;
6670#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006671 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006672#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006673 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006674 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006675 empty_handshake = state == TLS_ST_BEFORE;
6676#else
Lukas Tribus49799162019-07-08 14:29:15 +02006677 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6678 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006679#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006680 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006681 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006682 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006683 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6684 else
6685 conn->err_code = CO_ER_SSL_EMPTY;
6686 }
6687 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006688 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006689 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6690 else
6691 conn->err_code = CO_ER_SSL_ABORT;
6692 }
6693 }
6694 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006695 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006696 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006697 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006698 conn->err_code = CO_ER_SSL_HANDSHAKE;
6699 }
Lukas Tribus49799162019-07-08 14:29:15 +02006700#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006701 }
Emeric Brun674b7432012-11-08 19:21:55 +01006702 goto out_error;
6703 }
6704 else {
6705 /* Fail on all other handshake errors */
6706 /* Note: OpenSSL may leave unread bytes in the socket's
6707 * buffer, causing an RST to be emitted upon close() on
6708 * TCP sockets. We first try to drain possibly pending
6709 * data to avoid this as much as possible.
6710 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006711 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006712 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006713 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006714 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006715 goto out_error;
6716 }
6717 }
6718 /* read some data: consider handshake completed */
6719 goto reneg_ok;
6720 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006721 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006722check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006723 if (ret != 1) {
6724 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006725 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006726
6727 if (ret == SSL_ERROR_WANT_WRITE) {
6728 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006729 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006730 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006731 return 0;
6732 }
6733 else if (ret == SSL_ERROR_WANT_READ) {
6734 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006735 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006736 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6737 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006738 return 0;
6739 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006740#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006741 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006742 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006743 return 0;
6744 }
6745#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006746 else if (ret == SSL_ERROR_SYSCALL) {
6747 /* if errno is null, then connection was successfully established */
6748 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6749 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006750 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006751#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6752 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006753 conn->err_code = CO_ER_SSL_HANDSHAKE;
6754#else
6755 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006756#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006757 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006758 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006759 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006760#else
Lukas Tribus49799162019-07-08 14:29:15 +02006761 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6762 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006763#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006764 if (empty_handshake) {
6765 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006766 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006767 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6768 else
6769 conn->err_code = CO_ER_SSL_EMPTY;
6770 }
6771 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006772 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006773 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6774 else
6775 conn->err_code = CO_ER_SSL_ABORT;
6776 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006777 }
6778 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006779 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006780 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6781 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006782 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006783 }
Lukas Tribus49799162019-07-08 14:29:15 +02006784#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006785 }
Willy Tarreau89230192012-09-28 20:22:13 +02006786 goto out_error;
6787 }
Emeric Brun46591952012-05-18 15:47:34 +02006788 else {
6789 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006790 /* Note: OpenSSL may leave unread bytes in the socket's
6791 * buffer, causing an RST to be emitted upon close() on
6792 * TCP sockets. We first try to drain possibly pending
6793 * data to avoid this as much as possible.
6794 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006795 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006796 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006797 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006798 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006799 goto out_error;
6800 }
6801 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006802#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006803 else {
6804 /*
6805 * If the server refused the early data, we have to send a
6806 * 425 to the client, as we no longer have the data to sent
6807 * them again.
6808 */
6809 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006810 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006811 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6812 goto out_error;
6813 }
6814 }
6815 }
6816#endif
6817
Emeric Brun46591952012-05-18 15:47:34 +02006818
Emeric Brun674b7432012-11-08 19:21:55 +01006819reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006820
Willy Tarreau5db847a2019-05-09 14:13:35 +02006821#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006822 /* ASYNC engine API doesn't support moving read/write
6823 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006824 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006825 */
6826 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006827 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006828#endif
Emeric Brun46591952012-05-18 15:47:34 +02006829 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006830 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006831 if (objt_server(conn->target)) {
6832 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6833 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6834 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006835 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006836 else {
6837 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6838 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6839 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6840 }
Emeric Brun46591952012-05-18 15:47:34 +02006841 }
6842
6843 /* The connection is now established at both layers, it's time to leave */
6844 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6845 return 1;
6846
6847 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006848 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006849 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006850 ERR_clear_error();
6851
Emeric Brun9fa89732012-10-04 17:09:56 +02006852 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006853 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6854 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6855 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006856 }
6857
Emeric Brun46591952012-05-18 15:47:34 +02006858 /* Fail on all other handshake errors */
6859 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006860 if (!conn->err_code)
6861 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006862 return 0;
6863}
6864
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006865/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6866 * event subscriber <es> is not allowed to change from a previous call as long
6867 * as at least one event is still subscribed. The <event_type> must only be a
6868 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6869 * unless the transport layer was already released.
6870 */
6871static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006872{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006873 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006874
Olivier Houchard0ff28652019-06-24 18:57:39 +02006875 if (!ctx)
6876 return -1;
6877
Willy Tarreau113d52b2020-01-10 09:20:26 +01006878 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006879 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006880
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006881 ctx->subs = es;
6882 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006883
6884 /* we may have to subscribe to lower layers for new events */
6885 event_type &= ~ctx->wait_event.events;
6886 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6887 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006888 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006889}
6890
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006891/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6892 * The <es> pointer is not allowed to differ from the one passed to the
6893 * subscribe() call. It always returns zero.
6894 */
6895static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006896{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006897 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006898
Willy Tarreau113d52b2020-01-10 09:20:26 +01006899 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006900 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006901
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006902 es->events &= ~event_type;
6903 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006904 ctx->subs = NULL;
6905
6906 /* If we subscribed, and we're not doing the handshake,
6907 * then we subscribed because the upper layer asked for it,
6908 * as the upper layer is no longer interested, we can
6909 * unsubscribe too.
6910 */
6911 event_type &= ctx->wait_event.events;
6912 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6913 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006914
6915 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006916}
6917
Olivier Houchard2e055482019-05-27 19:50:12 +02006918/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6919 * Returns 0 on success, and non-zero on failure.
6920 */
6921static 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)
6922{
6923 struct ssl_sock_ctx *ctx = xprt_ctx;
6924
6925 if (oldxprt_ops != NULL)
6926 *oldxprt_ops = ctx->xprt;
6927 if (oldxprt_ctx != NULL)
6928 *oldxprt_ctx = ctx->xprt_ctx;
6929 ctx->xprt = toadd_ops;
6930 ctx->xprt_ctx = toadd_ctx;
6931 return 0;
6932}
6933
Olivier Houchard5149b592019-05-23 17:47:36 +02006934/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6935 * return 0, otherwise just call the remove_xprt method from the underlying
6936 * XPRT.
6937 */
6938static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6939{
6940 struct ssl_sock_ctx *ctx = xprt_ctx;
6941
6942 if (ctx->xprt_ctx == toremove_ctx) {
6943 ctx->xprt_ctx = newctx;
6944 ctx->xprt = newops;
6945 return 0;
6946 }
6947 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6948}
6949
Olivier Houchardea8dd942019-05-20 14:02:16 +02006950static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6951{
6952 struct ssl_sock_ctx *ctx = context;
6953
6954 /* First if we're doing an handshake, try that */
6955 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6956 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6957 /* If we had an error, or the handshake is done and I/O is available,
6958 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006959 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006960 * we can't be sure conn_fd_handler() will be called again.
6961 */
6962 if ((ctx->conn->flags & CO_FL_ERROR) ||
6963 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6964 int ret = 0;
6965 int woke = 0;
6966
6967 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006968 if (ctx->subs) {
6969 tasklet_wakeup(ctx->subs->tasklet);
6970 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006971 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006972 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006973 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006974
Olivier Houchardea8dd942019-05-20 14:02:16 +02006975 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006976 * upper layers know. If we have no mux, create it,
6977 * and once we have a mux, call its wake method if we didn't
6978 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006979 */
6980 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006981 if (!ctx->conn->mux)
6982 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006983 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6984 ctx->conn->mux->wake(ctx->conn);
6985 return NULL;
6986 }
6987 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006988#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6989 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006990 else if (b_data(&ctx->early_buf) && ctx->subs &&
6991 ctx->subs->events & SUB_RETRY_RECV) {
6992 tasklet_wakeup(ctx->subs->tasklet);
6993 ctx->subs->events &= ~SUB_RETRY_RECV;
6994 if (!ctx->subs->events)
6995 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006996 }
6997#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006998 return NULL;
6999}
7000
Emeric Brun46591952012-05-18 15:47:34 +02007001/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01007002 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02007003 * buffer wraps, in which case a second call may be performed. The connection's
7004 * flags are updated with whatever special event is detected (error, read0,
7005 * empty). The caller is responsible for taking care of those events and
7006 * avoiding the call if inappropriate. The function does not call the
7007 * connection's polling update function, so the caller is responsible for this.
7008 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007009static 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 +02007010{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007011 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02007012 ssize_t ret;
7013 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02007014
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007015 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007016 goto out_error;
7017
Olivier Houchard54907bb2019-12-19 15:02:39 +01007018#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
7019 if (b_data(&ctx->early_buf)) {
7020 try = b_contig_space(buf);
7021 if (try > b_data(&ctx->early_buf))
7022 try = b_data(&ctx->early_buf);
7023 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
7024 b_add(buf, try);
7025 b_del(&ctx->early_buf, try);
7026 if (b_data(&ctx->early_buf) == 0)
7027 b_free(&ctx->early_buf);
7028 return try;
7029 }
7030#endif
7031
Willy Tarreau911db9b2020-01-23 16:27:54 +01007032 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007033 /* a handshake was requested */
7034 return 0;
7035
Emeric Brun46591952012-05-18 15:47:34 +02007036 /* read the largest possible block. For this, we perform only one call
7037 * to recv() unless the buffer wraps and we exactly fill the first hunk,
7038 * in which case we accept to do it once again. A new attempt is made on
7039 * EINTR too.
7040 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01007041 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007042
Willy Tarreau591d4452018-06-15 17:21:00 +02007043 try = b_contig_space(buf);
7044 if (!try)
7045 break;
7046
Willy Tarreauabf08d92014-01-14 11:31:27 +01007047 if (try > count)
7048 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02007049
Olivier Houchard66ab4982019-02-26 18:37:15 +01007050 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02007051
Emeric Brune1f38db2012-09-03 20:36:47 +02007052 if (conn->flags & CO_FL_ERROR) {
7053 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007054 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007055 }
Emeric Brun46591952012-05-18 15:47:34 +02007056 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02007057 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007058 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007059 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007060 }
Emeric Brun46591952012-05-18 15:47:34 +02007061 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007062 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02007063 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007064 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007065 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007066 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007067#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007068 /* Async mode can be re-enabled, because we're leaving data state.*/
7069 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007070 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007071#endif
Emeric Brun46591952012-05-18 15:47:34 +02007072 break;
7073 }
7074 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007075 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007076 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7077 SUB_RETRY_RECV,
7078 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007079 /* handshake is running, and it may need to re-enable read */
7080 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007081#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007082 /* Async mode can be re-enabled, because we're leaving data state.*/
7083 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007084 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007085#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007086 break;
7087 }
Emeric Brun46591952012-05-18 15:47:34 +02007088 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007089 } else if (ret == SSL_ERROR_ZERO_RETURN)
7090 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007091 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7092 * stack before shutting down the connection for
7093 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007094 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7095 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007096 /* otherwise it's a real error */
7097 goto out_error;
7098 }
7099 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007100 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007101 return done;
7102
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007103 clear_ssl_error:
7104 /* Clear openssl global errors stack */
7105 ssl_sock_dump_errors(conn);
7106 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007107 read0:
7108 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007109 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007110
Emeric Brun46591952012-05-18 15:47:34 +02007111 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007112 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007113 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007114 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007115 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007116 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007117}
7118
7119
Willy Tarreau787db9a2018-06-14 18:31:46 +02007120/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7121 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7122 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007123 * Only one call to send() is performed, unless the buffer wraps, in which case
7124 * a second call may be performed. The connection's flags are updated with
7125 * whatever special event is detected (error, empty). The caller is responsible
7126 * for taking care of those events and avoiding the call if inappropriate. The
7127 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007128 * is responsible for this. The buffer's output is not adjusted, it's up to the
7129 * caller to take care of this. It's up to the caller to update the buffer's
7130 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007131 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007132static 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 +02007133{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007134 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007135 ssize_t ret;
7136 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007137
7138 done = 0;
7139
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007140 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007141 goto out_error;
7142
Willy Tarreau911db9b2020-01-23 16:27:54 +01007143 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007144 /* a handshake was requested */
7145 return 0;
7146
7147 /* send the largest possible block. For this we perform only one call
7148 * to send() unless the buffer wraps and we exactly fill the first hunk,
7149 * in which case we accept to do it once again.
7150 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007151 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007152#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007153 size_t written_data;
7154#endif
7155
Willy Tarreau787db9a2018-06-14 18:31:46 +02007156 try = b_contig_data(buf, done);
7157 if (try > count)
7158 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007159
Willy Tarreau7bed9452014-02-02 02:00:24 +01007160 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007161 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007162 global_ssl.max_record && try > global_ssl.max_record) {
7163 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007164 }
7165 else {
7166 /* we need to keep the information about the fact that
7167 * we're not limiting the upcoming send(), because if it
7168 * fails, we'll have to retry with at least as many data.
7169 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007170 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007171 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007172
Willy Tarreau5db847a2019-05-09 14:13:35 +02007173#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007174 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007175 unsigned int max_early;
7176
Olivier Houchard522eea72017-11-03 16:27:47 +01007177 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007178 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007179 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007180 if (SSL_get0_session(ctx->ssl))
7181 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007182 else
7183 max_early = 0;
7184 }
7185
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007186 if (try + ctx->sent_early_data > max_early) {
7187 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007188 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007189 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007190 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007191 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007192 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007193 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007194 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007195 if (ret == 1) {
7196 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007197 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007198 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007199 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007200 /* Initiate the handshake, now */
7201 tasklet_wakeup(ctx->wait_event.tasklet);
7202 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007203
Olivier Houchardc2aae742017-09-22 18:26:28 +02007204 }
7205
7206 } else
7207#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007208 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007209
Emeric Brune1f38db2012-09-03 20:36:47 +02007210 if (conn->flags & CO_FL_ERROR) {
7211 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007212 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007213 }
Emeric Brun46591952012-05-18 15:47:34 +02007214 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007215 /* A send succeeded, so we can consider ourself connected */
7216 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007217 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007218 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007219 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007220 }
7221 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007222 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007223
Emeric Brun46591952012-05-18 15:47:34 +02007224 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007225 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007226 /* handshake is running, and it may need to re-enable write */
7227 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007228 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007229#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007230 /* Async mode can be re-enabled, because we're leaving data state.*/
7231 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007232 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007233#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007234 break;
7235 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007236
Emeric Brun46591952012-05-18 15:47:34 +02007237 break;
7238 }
7239 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007240 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007241 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007242 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7243 SUB_RETRY_RECV,
7244 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007245#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007246 /* Async mode can be re-enabled, because we're leaving data state.*/
7247 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007248 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007249#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007250 break;
7251 }
Emeric Brun46591952012-05-18 15:47:34 +02007252 goto out_error;
7253 }
7254 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007255 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007256 return done;
7257
7258 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007259 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007260 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007261 ERR_clear_error();
7262
Emeric Brun46591952012-05-18 15:47:34 +02007263 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007264 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007265}
7266
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007267static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007268
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007269 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007270
Olivier Houchardea8dd942019-05-20 14:02:16 +02007271
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007272 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007273 if (ctx->wait_event.events != 0)
7274 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7275 ctx->wait_event.events,
7276 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007277 if (ctx->subs) {
7278 ctx->subs->events = 0;
7279 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007280 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007281
Olivier Houchard692c1d02019-05-23 18:41:47 +02007282 if (ctx->xprt->close)
7283 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007284#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007285 if (global_ssl.async) {
7286 OSSL_ASYNC_FD all_fd[32], afd;
7287 size_t num_all_fds = 0;
7288 int i;
7289
Olivier Houchard66ab4982019-02-26 18:37:15 +01007290 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007291 if (num_all_fds > 32) {
7292 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7293 return;
7294 }
7295
Olivier Houchard66ab4982019-02-26 18:37:15 +01007296 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007297
7298 /* If an async job is pending, we must try to
7299 to catch the end using polling before calling
7300 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007301 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007302 for (i=0 ; i < num_all_fds ; i++) {
7303 /* switch on an handler designed to
7304 * handle the SSL_free
7305 */
7306 afd = all_fd[i];
7307 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007308 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007309 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007310 /* To ensure that the fd cache won't be used
7311 * and we'll catch a real RD event.
7312 */
7313 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007314 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007315 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007316 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007317 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007318 return;
7319 }
Emeric Brun3854e012017-05-17 20:42:48 +02007320 /* Else we can remove the fds from the fdtab
7321 * and call SSL_free.
7322 * note: we do a fd_remove and not a delete
7323 * because the fd is owned by the engine.
7324 * the engine is responsible to close
7325 */
7326 for (i=0 ; i < num_all_fds ; i++)
7327 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007328 }
7329#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007330 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007331 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007332 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007333 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007334 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007335 }
Emeric Brun46591952012-05-18 15:47:34 +02007336}
7337
7338/* This function tries to perform a clean shutdown on an SSL connection, and in
7339 * any case, flags the connection as reusable if no handshake was in progress.
7340 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007341static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007342{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007343 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007344
Willy Tarreau911db9b2020-01-23 16:27:54 +01007345 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007346 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007347 if (!clean)
7348 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007349 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007350 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007351 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007352 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007353 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007354 ERR_clear_error();
7355 }
Emeric Brun46591952012-05-18 15:47:34 +02007356}
7357
William Lallemandd4f946c2019-12-05 10:26:40 +01007358/* fill a buffer with the algorithm and size of a public key */
7359static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007360{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007361 int bits = 0;
7362 int sig = TLSEXT_signature_anonymous;
7363 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007364 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007365
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007366 pkey = X509_get_pubkey(crt);
7367 if (pkey) {
7368 bits = EVP_PKEY_bits(pkey);
7369 switch(EVP_PKEY_base_id(pkey)) {
7370 case EVP_PKEY_RSA:
7371 sig = TLSEXT_signature_rsa;
7372 break;
7373 case EVP_PKEY_EC:
7374 sig = TLSEXT_signature_ecdsa;
7375 break;
7376 case EVP_PKEY_DSA:
7377 sig = TLSEXT_signature_dsa;
7378 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007379 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007380 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007381 }
7382
7383 switch(sig) {
7384 case TLSEXT_signature_rsa:
7385 len = chunk_printf(out, "RSA%d", bits);
7386 break;
7387 case TLSEXT_signature_ecdsa:
7388 len = chunk_printf(out, "EC%d", bits);
7389 break;
7390 case TLSEXT_signature_dsa:
7391 len = chunk_printf(out, "DSA%d", bits);
7392 break;
7393 default:
7394 return 0;
7395 }
7396 if (len < 0)
7397 return 0;
7398 return 1;
7399}
7400
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007401/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007402int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7403{
7404 struct ssl_sock_ctx *ctx;
7405 X509 *crt;
7406
7407 if (!ssl_sock_is_ssl(conn))
7408 return 0;
7409
7410 ctx = conn->xprt_ctx;
7411
7412 crt = SSL_get_certificate(ctx->ssl);
7413 if (!crt)
7414 return 0;
7415
7416 return cert_get_pkey_algo(crt, out);
7417}
7418
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007419/* used for ppv2 cert signature (can be used for logging) */
7420const char *ssl_sock_get_cert_sig(struct connection *conn)
7421{
Christopher Faulet82004142019-09-10 10:12:03 +02007422 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007423
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007424 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7425 X509 *crt;
7426
7427 if (!ssl_sock_is_ssl(conn))
7428 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007429 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007430 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007431 if (!crt)
7432 return NULL;
7433 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7434 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7435}
7436
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007437/* used for ppv2 authority */
7438const char *ssl_sock_get_sni(struct connection *conn)
7439{
7440#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007441 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007442
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007443 if (!ssl_sock_is_ssl(conn))
7444 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007445 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007446 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007447#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007448 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007449#endif
7450}
7451
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007452/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007453const char *ssl_sock_get_cipher_name(struct connection *conn)
7454{
Christopher Faulet82004142019-09-10 10:12:03 +02007455 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007456
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007457 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007458 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007459 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007460 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007461}
7462
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007463/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007464const char *ssl_sock_get_proto_version(struct connection *conn)
7465{
Christopher Faulet82004142019-09-10 10:12:03 +02007466 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007467
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007468 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007469 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007470 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007471 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007472}
7473
Willy Tarreau8d598402012-10-22 17:58:39 +02007474/* Extract a serial from a cert, and copy it to a chunk.
7475 * Returns 1 if serial is found and copied, 0 if no serial found and
7476 * -1 if output is not large enough.
7477 */
7478static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007479ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007480{
7481 ASN1_INTEGER *serial;
7482
7483 serial = X509_get_serialNumber(crt);
7484 if (!serial)
7485 return 0;
7486
7487 if (out->size < serial->length)
7488 return -1;
7489
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007490 memcpy(out->area, serial->data, serial->length);
7491 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007492 return 1;
7493}
7494
Emeric Brun43e79582014-10-29 19:03:26 +01007495/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007496 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7497 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007498 */
7499static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007500ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007501{
7502 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007503 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007504
7505 len =i2d_X509(crt, NULL);
7506 if (len <= 0)
7507 return 1;
7508
7509 if (out->size < len)
7510 return -1;
7511
7512 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007513 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007514 return 1;
7515}
7516
Emeric Brunce5ad802012-10-22 14:11:22 +02007517
Willy Tarreau83061a82018-07-13 11:56:34 +02007518/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007519 * Returns 1 if serial is found and copied, 0 if no valid time found
7520 * and -1 if output is not large enough.
7521 */
7522static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007523ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007524{
7525 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7526 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7527
7528 if (gentm->length < 12)
7529 return 0;
7530 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7531 return 0;
7532 if (out->size < gentm->length-2)
7533 return -1;
7534
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007535 memcpy(out->area, gentm->data+2, gentm->length-2);
7536 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007537 return 1;
7538 }
7539 else if (tm->type == V_ASN1_UTCTIME) {
7540 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7541
7542 if (utctm->length < 10)
7543 return 0;
7544 if (utctm->data[0] >= 0x35)
7545 return 0;
7546 if (out->size < utctm->length)
7547 return -1;
7548
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007549 memcpy(out->area, utctm->data, utctm->length);
7550 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007551 return 1;
7552 }
7553
7554 return 0;
7555}
7556
Emeric Brun87855892012-10-17 17:39:35 +02007557/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7558 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7559 */
7560static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007561ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7562 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007563{
7564 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007565 ASN1_OBJECT *obj;
7566 ASN1_STRING *data;
7567 const unsigned char *data_ptr;
7568 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007569 int i, j, n;
7570 int cur = 0;
7571 const char *s;
7572 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007573 int name_count;
7574
7575 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007576
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007577 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007578 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007579 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007580 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007581 else
7582 j = i;
7583
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007584 ne = X509_NAME_get_entry(a, j);
7585 obj = X509_NAME_ENTRY_get_object(ne);
7586 data = X509_NAME_ENTRY_get_data(ne);
7587 data_ptr = ASN1_STRING_get0_data(data);
7588 data_len = ASN1_STRING_length(data);
7589 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007590 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007591 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007592 s = tmp;
7593 }
7594
7595 if (chunk_strcasecmp(entry, s) != 0)
7596 continue;
7597
7598 if (pos < 0)
7599 cur--;
7600 else
7601 cur++;
7602
7603 if (cur != pos)
7604 continue;
7605
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007606 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007607 return -1;
7608
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007609 memcpy(out->area, data_ptr, data_len);
7610 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007611 return 1;
7612 }
7613
7614 return 0;
7615
William Lallemandd4f946c2019-12-05 10:26:40 +01007616}
7617
7618/*
7619 * Extract and format the DNS SAN extensions and copy result into a chuink
7620 * Return 0;
7621 */
7622#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7623static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7624{
7625 int i;
7626 char *str;
7627 STACK_OF(GENERAL_NAME) *names = NULL;
7628
7629 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7630 if (names) {
7631 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7632 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7633 if (i > 0)
7634 chunk_appendf(out, ", ");
7635 if (name->type == GEN_DNS) {
7636 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7637 chunk_appendf(out, "DNS:%s", str);
7638 OPENSSL_free(str);
7639 }
7640 }
7641 }
7642 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7643 }
7644 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007645}
William Lallemandd4f946c2019-12-05 10:26:40 +01007646#endif
Emeric Brun87855892012-10-17 17:39:35 +02007647
Elliot Otchet71f82972020-01-15 08:12:14 -05007648/*
7649 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7650 * Currently supports rfc2253 for returning LDAP V3 DNs.
7651 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7652 */
7653static int
7654ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7655{
7656 BIO *bio = NULL;
7657 int ret = 0;
7658 int data_len = 0;
7659
7660 if (chunk_strcmp(format, "rfc2253") == 0) {
7661 bio = BIO_new(BIO_s_mem());
7662 if (bio == NULL)
7663 goto out;
7664
7665 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7666 goto out;
7667
7668 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7669 goto out;
7670
7671 out->data = data_len;
7672
7673 ret = 1;
7674 }
7675out:
7676 if (bio)
7677 BIO_free(bio);
7678 return ret;
7679}
7680
Emeric Brun87855892012-10-17 17:39:35 +02007681/* Extract and format full DN from a X509_NAME and copy result into a chunk
7682 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7683 */
7684static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007685ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007686{
7687 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007688 ASN1_OBJECT *obj;
7689 ASN1_STRING *data;
7690 const unsigned char *data_ptr;
7691 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007692 int i, n, ln;
7693 int l = 0;
7694 const char *s;
7695 char *p;
7696 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007697 int name_count;
7698
7699
7700 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007701
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007702 out->data = 0;
7703 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007704 for (i = 0; i < name_count; i++) {
7705 ne = X509_NAME_get_entry(a, i);
7706 obj = X509_NAME_ENTRY_get_object(ne);
7707 data = X509_NAME_ENTRY_get_data(ne);
7708 data_ptr = ASN1_STRING_get0_data(data);
7709 data_len = ASN1_STRING_length(data);
7710 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007711 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007712 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007713 s = tmp;
7714 }
7715 ln = strlen(s);
7716
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007717 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007718 if (l > out->size)
7719 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007720 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007721
7722 *(p++)='/';
7723 memcpy(p, s, ln);
7724 p += ln;
7725 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007726 memcpy(p, data_ptr, data_len);
7727 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007728 }
7729
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007730 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007731 return 0;
7732
7733 return 1;
7734}
7735
Olivier Houchardab28a322018-12-21 19:45:40 +01007736void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7737{
7738#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007739 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007740
Olivier Houcharde488ea82019-06-28 14:10:33 +02007741 if (!ssl_sock_is_ssl(conn))
7742 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007743 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007744 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007745#endif
7746}
7747
Willy Tarreau119a4082016-12-22 21:58:38 +01007748/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7749 * to disable SNI.
7750 */
Willy Tarreau63076412015-07-10 11:33:32 +02007751void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7752{
7753#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007754 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007755
Willy Tarreau119a4082016-12-22 21:58:38 +01007756 char *prev_name;
7757
Willy Tarreau63076412015-07-10 11:33:32 +02007758 if (!ssl_sock_is_ssl(conn))
7759 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007760 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007761
Willy Tarreau119a4082016-12-22 21:58:38 +01007762 /* if the SNI changes, we must destroy the reusable context so that a
7763 * new connection will present a new SNI. As an optimization we could
7764 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7765 * server.
7766 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007767 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007768 if ((!prev_name && hostname) ||
7769 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007770 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007771
Olivier Houchard66ab4982019-02-26 18:37:15 +01007772 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007773#endif
7774}
7775
Emeric Brun0abf8362014-06-24 18:26:41 +02007776/* Extract peer certificate's common name into the chunk dest
7777 * Returns
7778 * the len of the extracted common name
7779 * or 0 if no CN found in DN
7780 * or -1 on error case (i.e. no peer certificate)
7781 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007782int ssl_sock_get_remote_common_name(struct connection *conn,
7783 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007784{
Christopher Faulet82004142019-09-10 10:12:03 +02007785 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007786 X509 *crt = NULL;
7787 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007788 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007789 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007790 .area = (char *)&find_cn,
7791 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007792 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007793 int result = -1;
David Safb76832014-05-08 23:42:08 -04007794
7795 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007796 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007797 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007798
7799 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007800 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007801 if (!crt)
7802 goto out;
7803
7804 name = X509_get_subject_name(crt);
7805 if (!name)
7806 goto out;
David Safb76832014-05-08 23:42:08 -04007807
Emeric Brun0abf8362014-06-24 18:26:41 +02007808 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7809out:
David Safb76832014-05-08 23:42:08 -04007810 if (crt)
7811 X509_free(crt);
7812
7813 return result;
7814}
7815
Dave McCowan328fb582014-07-30 10:39:13 -04007816/* returns 1 if client passed a certificate for this session, 0 if not */
7817int ssl_sock_get_cert_used_sess(struct connection *conn)
7818{
Christopher Faulet82004142019-09-10 10:12:03 +02007819 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007820 X509 *crt = NULL;
7821
7822 if (!ssl_sock_is_ssl(conn))
7823 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007824 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007825
7826 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007827 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007828 if (!crt)
7829 return 0;
7830
7831 X509_free(crt);
7832 return 1;
7833}
7834
7835/* returns 1 if client passed a certificate for this connection, 0 if not */
7836int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007837{
Christopher Faulet82004142019-09-10 10:12:03 +02007838 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007839
David Safb76832014-05-08 23:42:08 -04007840 if (!ssl_sock_is_ssl(conn))
7841 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007842 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007843 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007844}
7845
7846/* returns result from SSL verify */
7847unsigned int ssl_sock_get_verify_result(struct connection *conn)
7848{
Christopher Faulet82004142019-09-10 10:12:03 +02007849 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007850
David Safb76832014-05-08 23:42:08 -04007851 if (!ssl_sock_is_ssl(conn))
7852 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007853 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007854 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007855}
7856
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007857/* Returns the application layer protocol name in <str> and <len> when known.
7858 * Zero is returned if the protocol name was not found, otherwise non-zero is
7859 * returned. The string is allocated in the SSL context and doesn't have to be
7860 * freed by the caller. NPN is also checked if available since older versions
7861 * of openssl (1.0.1) which are more common in field only support this one.
7862 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007863static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007864{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007865#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7866 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007867 struct ssl_sock_ctx *ctx = xprt_ctx;
7868 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007869 return 0;
7870
7871 *str = NULL;
7872
7873#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007874 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007875 if (*str)
7876 return 1;
7877#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007878#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007879 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007880 if (*str)
7881 return 1;
7882#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007883#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007884 return 0;
7885}
7886
Willy Tarreau7875d092012-09-10 08:20:03 +02007887/***** Below are some sample fetching functions for ACL/patterns *****/
7888
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007889static int
7890smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7891{
7892 struct connection *conn;
7893
7894 conn = objt_conn(smp->sess->origin);
7895 if (!conn || conn->xprt != &ssl_sock)
7896 return 0;
7897
7898 smp->flags = 0;
7899 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007900#ifdef OPENSSL_IS_BORINGSSL
7901 {
7902 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7903 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7904 SSL_early_data_accepted(ctx->ssl));
7905 }
7906#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007907 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007908 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007909#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007910 return 1;
7911}
7912
Emeric Brune64aef12012-09-21 13:15:06 +02007913/* boolean, returns true if client cert was present */
7914static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007915smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007916{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007917 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007918 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007919
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007920 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007921 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007922 return 0;
7923
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007924 ctx = conn->xprt_ctx;
7925
Willy Tarreau911db9b2020-01-23 16:27:54 +01007926 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007927 smp->flags |= SMP_F_MAY_CHANGE;
7928 return 0;
7929 }
7930
7931 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007932 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007933 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007934
7935 return 1;
7936}
7937
Emeric Brun43e79582014-10-29 19:03:26 +01007938/* binary, returns a certificate in a binary chunk (der/raw).
7939 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7940 * should be use.
7941 */
7942static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007943smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007944{
7945 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7946 X509 *crt = NULL;
7947 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007948 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007949 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007950 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007951
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007952 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007953 if (!conn || conn->xprt != &ssl_sock)
7954 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007955 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007956
Willy Tarreau911db9b2020-01-23 16:27:54 +01007957 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007958 smp->flags |= SMP_F_MAY_CHANGE;
7959 return 0;
7960 }
7961
7962 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007963 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007964 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007965 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007966
7967 if (!crt)
7968 goto out;
7969
7970 smp_trash = get_trash_chunk();
7971 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7972 goto out;
7973
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007974 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007975 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007976 ret = 1;
7977out:
7978 /* SSL_get_peer_certificate, it increase X509 * ref count */
7979 if (cert_peer && crt)
7980 X509_free(crt);
7981 return ret;
7982}
7983
Emeric Brunba841a12014-04-30 17:05:08 +02007984/* binary, returns serial of certificate in a binary chunk.
7985 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7986 * should be use.
7987 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007989smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007990{
Emeric Brunba841a12014-04-30 17:05:08 +02007991 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007992 X509 *crt = NULL;
7993 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007994 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007995 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007996 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007997
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007998 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007999 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02008000 return 0;
8001
Olivier Houchard66ab4982019-02-26 18:37:15 +01008002 ctx = conn->xprt_ctx;
8003
Willy Tarreau911db9b2020-01-23 16:27:54 +01008004 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02008005 smp->flags |= SMP_F_MAY_CHANGE;
8006 return 0;
8007 }
8008
Emeric Brunba841a12014-04-30 17:05:08 +02008009 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008010 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008011 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008012 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008013
Willy Tarreau8d598402012-10-22 17:58:39 +02008014 if (!crt)
8015 goto out;
8016
Willy Tarreau47ca5452012-12-23 20:22:19 +01008017 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02008018 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
8019 goto out;
8020
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008021 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008022 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02008023 ret = 1;
8024out:
Emeric Brunba841a12014-04-30 17:05:08 +02008025 /* SSL_get_peer_certificate, it increase X509 * ref count */
8026 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02008027 X509_free(crt);
8028 return ret;
8029}
Emeric Brune64aef12012-09-21 13:15:06 +02008030
Emeric Brunba841a12014-04-30 17:05:08 +02008031/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
8032 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8033 * should be use.
8034 */
James Votha051b4a2013-05-14 20:37:59 +02008035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008036smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02008037{
Emeric Brunba841a12014-04-30 17:05:08 +02008038 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02008039 X509 *crt = NULL;
8040 const EVP_MD *digest;
8041 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01008042 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008043 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008044 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008045 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02008046
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008047 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008048 if (!conn || conn->xprt != &ssl_sock)
8049 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008050 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008051
Willy Tarreau911db9b2020-01-23 16:27:54 +01008052 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02008053 smp->flags |= SMP_F_MAY_CHANGE;
8054 return 0;
8055 }
8056
Emeric Brunba841a12014-04-30 17:05:08 +02008057 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008058 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008059 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008060 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02008061 if (!crt)
8062 goto out;
8063
8064 smp_trash = get_trash_chunk();
8065 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008066 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8067 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008068 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008069 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008070 ret = 1;
8071out:
Emeric Brunba841a12014-04-30 17:05:08 +02008072 /* SSL_get_peer_certificate, it increase X509 * ref count */
8073 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008074 X509_free(crt);
8075 return ret;
8076}
8077
Emeric Brunba841a12014-04-30 17:05:08 +02008078/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8079 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8080 * should be use.
8081 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008083smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008084{
Emeric Brunba841a12014-04-30 17:05:08 +02008085 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008086 X509 *crt = NULL;
8087 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008088 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008089 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008090 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008091
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008092 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008093 if (!conn || conn->xprt != &ssl_sock)
8094 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008095 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008096
Willy Tarreau911db9b2020-01-23 16:27:54 +01008097 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008098 smp->flags |= SMP_F_MAY_CHANGE;
8099 return 0;
8100 }
8101
Emeric Brunba841a12014-04-30 17:05:08 +02008102 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008103 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008104 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008105 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008106 if (!crt)
8107 goto out;
8108
Willy Tarreau47ca5452012-12-23 20:22:19 +01008109 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008110 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008111 goto out;
8112
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008113 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008114 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008115 ret = 1;
8116out:
Emeric Brunba841a12014-04-30 17:05:08 +02008117 /* SSL_get_peer_certificate, it increase X509 * ref count */
8118 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008119 X509_free(crt);
8120 return ret;
8121}
8122
Emeric Brunba841a12014-04-30 17:05:08 +02008123/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8124 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8125 * should be use.
8126 */
Emeric Brun87855892012-10-17 17:39:35 +02008127static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008128smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008129{
Emeric Brunba841a12014-04-30 17:05:08 +02008130 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008131 X509 *crt = NULL;
8132 X509_NAME *name;
8133 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008134 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008135 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008136 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008137
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008138 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008139 if (!conn || conn->xprt != &ssl_sock)
8140 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008141 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008142
Willy Tarreau911db9b2020-01-23 16:27:54 +01008143 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008144 smp->flags |= SMP_F_MAY_CHANGE;
8145 return 0;
8146 }
8147
Emeric Brunba841a12014-04-30 17:05:08 +02008148 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008149 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008150 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008151 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008152 if (!crt)
8153 goto out;
8154
8155 name = X509_get_issuer_name(crt);
8156 if (!name)
8157 goto out;
8158
Willy Tarreau47ca5452012-12-23 20:22:19 +01008159 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008160 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008161 int pos = 1;
8162
8163 if (args[1].type == ARGT_SINT)
8164 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008165
8166 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8167 goto out;
8168 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008169 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8170 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8171 goto out;
8172 }
Emeric Brun87855892012-10-17 17:39:35 +02008173 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8174 goto out;
8175
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008176 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008177 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008178 ret = 1;
8179out:
Emeric Brunba841a12014-04-30 17:05:08 +02008180 /* SSL_get_peer_certificate, it increase X509 * ref count */
8181 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008182 X509_free(crt);
8183 return ret;
8184}
8185
Emeric Brunba841a12014-04-30 17:05:08 +02008186/* string, returns notbefore date in ASN1_UTCTIME format.
8187 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8188 * should be use.
8189 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008190static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008191smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008192{
Emeric Brunba841a12014-04-30 17:05:08 +02008193 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008194 X509 *crt = NULL;
8195 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008196 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008197 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008198 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008199
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008200 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008201 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02008202 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008203 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02008204
Willy Tarreau911db9b2020-01-23 16:27:54 +01008205 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008206 smp->flags |= SMP_F_MAY_CHANGE;
8207 return 0;
8208 }
8209
Emeric Brunba841a12014-04-30 17:05:08 +02008210 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008211 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008212 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008213 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008214 if (!crt)
8215 goto out;
8216
Willy Tarreau47ca5452012-12-23 20:22:19 +01008217 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008218 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008219 goto out;
8220
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008221 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008222 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008223 ret = 1;
8224out:
Emeric Brunba841a12014-04-30 17:05:08 +02008225 /* SSL_get_peer_certificate, it increase X509 * ref count */
8226 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008227 X509_free(crt);
8228 return ret;
8229}
8230
Emeric Brunba841a12014-04-30 17:05:08 +02008231/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8232 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8233 * should be use.
8234 */
Emeric Brun87855892012-10-17 17:39:35 +02008235static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008236smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008237{
Emeric Brunba841a12014-04-30 17:05:08 +02008238 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008239 X509 *crt = NULL;
8240 X509_NAME *name;
8241 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008242 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008243 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008244 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02008245
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008246 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008247 if (!conn || conn->xprt != &ssl_sock)
8248 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008249 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008250
Willy Tarreau911db9b2020-01-23 16:27:54 +01008251 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008252 smp->flags |= SMP_F_MAY_CHANGE;
8253 return 0;
8254 }
8255
Emeric Brunba841a12014-04-30 17:05:08 +02008256 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008257 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008258 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008259 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008260 if (!crt)
8261 goto out;
8262
8263 name = X509_get_subject_name(crt);
8264 if (!name)
8265 goto out;
8266
Willy Tarreau47ca5452012-12-23 20:22:19 +01008267 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008268 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008269 int pos = 1;
8270
8271 if (args[1].type == ARGT_SINT)
8272 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008273
8274 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8275 goto out;
8276 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008277 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8278 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8279 goto out;
8280 }
Emeric Brun87855892012-10-17 17:39:35 +02008281 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8282 goto out;
8283
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008284 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008285 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008286 ret = 1;
8287out:
Emeric Brunba841a12014-04-30 17:05:08 +02008288 /* SSL_get_peer_certificate, it increase X509 * ref count */
8289 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008290 X509_free(crt);
8291 return ret;
8292}
Emeric Brun9143d372012-12-20 15:44:16 +01008293
8294/* integer, returns true if current session use a client certificate */
8295static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008296smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008297{
8298 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008299 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008300 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01008301
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008302 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008303 if (!conn || conn->xprt != &ssl_sock)
8304 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008305 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008306
Willy Tarreau911db9b2020-01-23 16:27:54 +01008307 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008308 smp->flags |= SMP_F_MAY_CHANGE;
8309 return 0;
8310 }
8311
8312 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01008313 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008314 if (crt) {
8315 X509_free(crt);
8316 }
8317
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008318 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008319 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008320 return 1;
8321}
8322
Emeric Brunba841a12014-04-30 17:05:08 +02008323/* integer, returns the certificate version
8324 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8325 * should be use.
8326 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008327static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008328smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008329{
Emeric Brunba841a12014-04-30 17:05:08 +02008330 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008331 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008332 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008333 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008334
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008335 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008336 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008337 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008338 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008339
Willy Tarreau911db9b2020-01-23 16:27:54 +01008340 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008341 smp->flags |= SMP_F_MAY_CHANGE;
8342 return 0;
8343 }
8344
Emeric Brunba841a12014-04-30 17:05:08 +02008345 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008346 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008347 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008348 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008349 if (!crt)
8350 return 0;
8351
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008352 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008353 /* SSL_get_peer_certificate increase X509 * ref count */
8354 if (cert_peer)
8355 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008356 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008357
8358 return 1;
8359}
8360
Emeric Brunba841a12014-04-30 17:05:08 +02008361/* string, returns the certificate's signature algorithm.
8362 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8363 * should be use.
8364 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008365static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008366smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008367{
Emeric Brunba841a12014-04-30 17:05:08 +02008368 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008369 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008370 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008371 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008372 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008373 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02008374
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008375 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008376 if (!conn || conn->xprt != &ssl_sock)
8377 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008378 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008379
Willy Tarreau911db9b2020-01-23 16:27:54 +01008380 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008381 smp->flags |= SMP_F_MAY_CHANGE;
8382 return 0;
8383 }
8384
Emeric Brunba841a12014-04-30 17:05:08 +02008385 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008386 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008387 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008388 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008389 if (!crt)
8390 return 0;
8391
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008392 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8393 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008394
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008395 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8396 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008397 /* SSL_get_peer_certificate increase X509 * ref count */
8398 if (cert_peer)
8399 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008400 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008401 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008402
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008403 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008404 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008405 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008406 /* SSL_get_peer_certificate increase X509 * ref count */
8407 if (cert_peer)
8408 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008409
8410 return 1;
8411}
8412
Emeric Brunba841a12014-04-30 17:05:08 +02008413/* string, returns the certificate's key algorithm.
8414 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8415 * should be use.
8416 */
Emeric Brun521a0112012-10-22 12:22:55 +02008417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008418smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008419{
Emeric Brunba841a12014-04-30 17:05:08 +02008420 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008421 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008422 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008423 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008424 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008425 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008426
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008427 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008428 if (!conn || conn->xprt != &ssl_sock)
8429 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008430 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008431
Willy Tarreau911db9b2020-01-23 16:27:54 +01008432 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008433 smp->flags |= SMP_F_MAY_CHANGE;
8434 return 0;
8435 }
8436
Emeric Brunba841a12014-04-30 17:05:08 +02008437 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008438 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008439 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008440 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008441 if (!crt)
8442 return 0;
8443
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008444 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8445 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008446
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008447 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8448 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008449 /* SSL_get_peer_certificate increase X509 * ref count */
8450 if (cert_peer)
8451 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008452 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008453 }
Emeric Brun521a0112012-10-22 12:22:55 +02008454
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008455 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008456 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008457 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008458 if (cert_peer)
8459 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008460
8461 return 1;
8462}
8463
Emeric Brun645ae792014-04-30 14:21:06 +02008464/* boolean, returns true if front conn. transport layer is SSL.
8465 * This function is also usable on backend conn if the fetch keyword 5th
8466 * char is 'b'.
8467 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008468static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008469smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008470{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008471 struct connection *conn;
8472
8473 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8474 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8475 else
8476 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8477 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008478
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008479 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008480 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008481 return 1;
8482}
8483
Emeric Brun2525b6b2012-10-18 15:59:43 +02008484/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008485static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008486smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008487{
8488#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008489 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008490 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008491
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008492 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008493 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008494 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008495 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008496 return 1;
8497#else
8498 return 0;
8499#endif
8500}
8501
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008502/* boolean, returns true if client session has been resumed.
8503 * This function is also usable on backend conn if the fetch keyword 5th
8504 * char is 'b'.
8505 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008506static int
8507smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8508{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008509 struct connection *conn;
8510 struct ssl_sock_ctx *ctx;
8511
8512 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8513 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8514 else
8515 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8516 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008517
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008518 ctx = conn ? conn->xprt_ctx : NULL;
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008519
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008520 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008521 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008522 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008523 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008524 return 1;
8525}
8526
Emeric Brun645ae792014-04-30 14:21:06 +02008527/* string, returns the used cipher if front conn. transport layer is SSL.
8528 * This function is also usable on backend conn if the fetch keyword 5th
8529 * char is 'b'.
8530 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008531static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008532smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008533{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008534 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008535 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008536
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008537 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8538 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8539 else
8540 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8541 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8542
Willy Tarreaube508f12016-03-10 11:47:01 +01008543 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008544 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008545 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008546 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008547
Olivier Houchard66ab4982019-02-26 18:37:15 +01008548 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008549 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008550 return 0;
8551
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008552 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008553 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008554 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008555
8556 return 1;
8557}
8558
Emeric Brun645ae792014-04-30 14:21:06 +02008559/* integer, returns the algoritm's keysize if front conn. transport layer
8560 * is SSL.
8561 * This function is also usable on backend conn if the fetch keyword 5th
8562 * char is 'b'.
8563 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008564static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008565smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008566{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008567 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008568 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008569 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008570
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008571 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8572 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8573 else
8574 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8575 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8576
Emeric Brun589fcad2012-10-16 14:13:26 +02008577 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008578 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008579 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008580 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008581
Olivier Houchard66ab4982019-02-26 18:37:15 +01008582 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008583 return 0;
8584
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008585 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008586 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008587
8588 return 1;
8589}
8590
Emeric Brun645ae792014-04-30 14:21:06 +02008591/* integer, returns the used keysize if front conn. transport layer is SSL.
8592 * This function is also usable on backend conn if the fetch keyword 5th
8593 * char is 'b'.
8594 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008595static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008596smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008597{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008598 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008599 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008600
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008601 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8602 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8603 else
8604 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8605 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8606
Emeric Brun589fcad2012-10-16 14:13:26 +02008607 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008608 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8609 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008610 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008611
Olivier Houchard66ab4982019-02-26 18:37:15 +01008612 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008613 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008614 return 0;
8615
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008616 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008617
8618 return 1;
8619}
8620
Bernard Spil13c53f82018-02-15 13:34:58 +01008621#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008622static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008623smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008624{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008625 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008626 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008627 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008628
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008629 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008630 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008631
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008632 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8633 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8634 else
8635 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)
8639 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008640 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +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_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008644 (const unsigned char **)&smp->data.u.str.area,
8645 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008647 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008648 return 0;
8649
Willy Tarreau105599c2020-02-25 08:59:23 +01008650 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008651 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008652}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008653#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008654
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008655#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008656static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008657smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008658{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008659 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008660 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008661 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008662
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008663 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008664 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008665
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008666 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8667 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8668 else
8669 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8670 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard6b77f492018-11-22 18:18:29 +01008671
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008672 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008673 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008674 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008675
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008676 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008677 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008678 (const unsigned char **)&smp->data.u.str.area,
8679 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008680
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008681 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008682 return 0;
8683
Willy Tarreau105599c2020-02-25 08:59:23 +01008684 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008685 return 1;
8686}
8687#endif
8688
Emeric Brun645ae792014-04-30 14:21:06 +02008689/* string, returns the used protocol if front conn. transport layer is SSL.
8690 * This function is also usable on backend conn if the fetch keyword 5th
8691 * char is 'b'.
8692 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008693static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008694smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008695{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008696 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008697 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008698
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008699 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8700 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8701 else
8702 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8703 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8704
Emeric Brun589fcad2012-10-16 14:13:26 +02008705 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008706 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8707 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008708 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008709
Olivier Houchard66ab4982019-02-26 18:37:15 +01008710 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008711 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008712 return 0;
8713
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008714 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008715 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008716 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008717
8718 return 1;
8719}
8720
Willy Tarreau87b09662015-04-03 00:22:06 +02008721/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008722 * This function is also usable on backend conn if the fetch keyword 5th
8723 * char is 'b'.
8724 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008725#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008726static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008727smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008728{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008729 struct connection *conn;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008730 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008731 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008732 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008733
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008734 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008735 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008736
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008737 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8738 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8739 else
8740 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8741 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8742
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008743 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8744 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008745 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008746
Olivier Houchard66ab4982019-02-26 18:37:15 +01008747 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008748 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008749 return 0;
8750
Willy Tarreau105599c2020-02-25 08:59:23 +01008751 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Dragan Dosenf35d69e2020-05-04 09:07:28 +02008752 if (!smp->data.u.str.area || !len)
Emeric Brunfe68f682012-10-16 14:59:28 +02008753 return 0;
8754
Willy Tarreau105599c2020-02-25 08:59:23 +01008755 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008756 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008757}
Patrick Hemmer41966772018-04-28 19:15:48 -04008758#endif
8759
Emeric Brunfe68f682012-10-16 14:59:28 +02008760
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008761#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008762static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008763smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8764{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008765 struct connection *conn;
Patrick Hemmer65674662019-06-04 08:13:03 -04008766 struct buffer *data;
8767 struct ssl_sock_ctx *ctx;
8768
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008769 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8770 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8771 else
8772 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8773 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8774
Patrick Hemmer65674662019-06-04 08:13:03 -04008775 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8776 return 0;
8777 ctx = conn->xprt_ctx;
8778
8779 data = get_trash_chunk();
8780 if (kw[7] == 'c')
8781 data->data = SSL_get_client_random(ctx->ssl,
8782 (unsigned char *) data->area,
8783 data->size);
8784 else
8785 data->data = SSL_get_server_random(ctx->ssl,
8786 (unsigned char *) data->area,
8787 data->size);
8788 if (!data->data)
8789 return 0;
8790
8791 smp->flags = 0;
8792 smp->data.type = SMP_T_BIN;
8793 smp->data.u.str = *data;
8794
8795 return 1;
8796}
8797
8798static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008799smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8800{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008801 struct connection *conn;
Patrick Hemmere0275472018-04-28 19:15:51 -04008802 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008803 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008804 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008805
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008806 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8807 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8808 else
8809 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8810 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8811
Patrick Hemmere0275472018-04-28 19:15:51 -04008812 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8813 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008814 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008815
Olivier Houchard66ab4982019-02-26 18:37:15 +01008816 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008817 if (!ssl_sess)
8818 return 0;
8819
8820 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008821 data->data = SSL_SESSION_get_master_key(ssl_sess,
8822 (unsigned char *) data->area,
8823 data->size);
8824 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008825 return 0;
8826
8827 smp->flags = 0;
8828 smp->data.type = SMP_T_BIN;
8829 smp->data.u.str = *data;
8830
8831 return 1;
8832}
8833#endif
8834
Patrick Hemmer41966772018-04-28 19:15:48 -04008835#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008836static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008837smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008838{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008839 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008840 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008841
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008842 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008843 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008844
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008845 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008846 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8847 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008848 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008849
Olivier Houchard66ab4982019-02-26 18:37:15 +01008850 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008851 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008852 return 0;
8853
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008854 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008855 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008856}
Patrick Hemmer41966772018-04-28 19:15:48 -04008857#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008858
David Sc1ad52e2014-04-08 18:48:47 -04008859static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008860smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8861{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008862 struct connection *conn;
8863 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008864 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008865
8866 conn = objt_conn(smp->sess->origin);
8867 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8868 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008869 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008870
Olivier Houchard66ab4982019-02-26 18:37:15 +01008871 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008872 if (!capture)
8873 return 0;
8874
8875 smp->flags = SMP_F_CONST;
8876 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008877 smp->data.u.str.area = capture->ciphersuite;
8878 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008879 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008880}
8881
8882static int
8883smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8884{
Willy Tarreau83061a82018-07-13 11:56:34 +02008885 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008886
8887 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8888 return 0;
8889
8890 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008891 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008892 smp->data.type = SMP_T_BIN;
8893 smp->data.u.str = *data;
8894 return 1;
8895}
8896
8897static int
8898smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8899{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008900 struct connection *conn;
8901 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008902 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008903
8904 conn = objt_conn(smp->sess->origin);
8905 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8906 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008907 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008908
Olivier Houchard66ab4982019-02-26 18:37:15 +01008909 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008910 if (!capture)
8911 return 0;
8912
8913 smp->data.type = SMP_T_SINT;
8914 smp->data.u.sint = capture->xxh64;
8915 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008916}
8917
8918static int
8919smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8920{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008921#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008922 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008923 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008924
8925 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8926 return 0;
8927
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008928 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008929 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008930 const char *str;
8931 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008932 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008933 uint16_t id = (bin[0] << 8) | bin[1];
8934#if defined(OPENSSL_IS_BORINGSSL)
8935 cipher = SSL_get_cipher_by_value(id);
8936#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008937 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008938 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8939 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008940#endif
8941 str = SSL_CIPHER_get_name(cipher);
8942 if (!str || strcmp(str, "(NONE)") == 0)
8943 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008944 else
8945 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8946 }
8947 smp->data.type = SMP_T_STR;
8948 smp->data.u.str = *data;
8949 return 1;
8950#else
8951 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8952#endif
8953}
8954
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008955#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008956static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008957smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008958{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008959 struct connection *conn;
David Sc1ad52e2014-04-08 18:48:47 -04008960 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008961 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008962 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008963
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008964 if (smp->sess && obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
8965 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8966 else
8967 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8968 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8969
David Sc1ad52e2014-04-08 18:48:47 -04008970 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008971 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8972 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008973 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008974
Willy Tarreau911db9b2020-01-23 16:27:54 +01008975 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008976 smp->flags |= SMP_F_MAY_CHANGE;
8977 return 0;
8978 }
8979
8980 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008981 if (!SSL_session_reused(ctx->ssl))
8982 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008983 finished_trash->area,
8984 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008985 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008986 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008987 finished_trash->area,
8988 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008989
8990 if (!finished_len)
8991 return 0;
8992
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008993 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008994 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008995 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008996
8997 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008998}
Patrick Hemmer41966772018-04-28 19:15:48 -04008999#endif
David Sc1ad52e2014-04-08 18:48:47 -04009000
Emeric Brun2525b6b2012-10-18 15:59:43 +02009001/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02009002static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009003smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02009004{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009005 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009006 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009007
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009008 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009009 if (!conn || conn->xprt != &ssl_sock)
9010 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009011 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009012
Willy Tarreau911db9b2020-01-23 16:27:54 +01009013 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02009014 smp->flags = SMP_F_MAY_CHANGE;
9015 return 0;
9016 }
9017
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009018 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009019 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009020 smp->flags = 0;
9021
9022 return 1;
9023}
9024
Emeric Brun2525b6b2012-10-18 15:59:43 +02009025/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02009026static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009027smp_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 +02009028{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009029 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009030 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009031
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009032 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009033 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02009034 return 0;
9035
Willy Tarreau911db9b2020-01-23 16:27:54 +01009036 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02009037 smp->flags = SMP_F_MAY_CHANGE;
9038 return 0;
9039 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009040 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02009041
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009042 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009043 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009044 smp->flags = 0;
9045
9046 return 1;
9047}
9048
Emeric Brun2525b6b2012-10-18 15:59:43 +02009049/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02009050static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009051smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02009052{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009053 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009054 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009055
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009056 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009057 if (!conn || conn->xprt != &ssl_sock)
9058 return 0;
9059
Willy Tarreau911db9b2020-01-23 16:27:54 +01009060 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02009061 smp->flags = SMP_F_MAY_CHANGE;
9062 return 0;
9063 }
9064
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009065 ctx = conn->xprt_ctx;
9066
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009067 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009068 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009069 smp->flags = 0;
9070
9071 return 1;
9072}
9073
Emeric Brun2525b6b2012-10-18 15:59:43 +02009074/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009075static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009076smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009077{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009078 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009079 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009080
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009081 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009082 if (!conn || conn->xprt != &ssl_sock)
9083 return 0;
9084
Willy Tarreau911db9b2020-01-23 16:27:54 +01009085 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009086 smp->flags = SMP_F_MAY_CHANGE;
9087 return 0;
9088 }
9089
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009090 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009091 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009092 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009093
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009094 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01009095 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009096 smp->flags = 0;
9097
9098 return 1;
9099}
9100
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009101/* for ca-file and ca-verify-file */
9102static 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 +02009103{
9104 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009105 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009106 return ERR_ALERT | ERR_FATAL;
9107 }
9108
Willy Tarreauef934602016-12-22 23:12:01 +01009109 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009110 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009111 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009112 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009113
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009114 if (!ssl_store_load_locations_file(*ca_file_p)) {
9115 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009116 return ERR_ALERT | ERR_FATAL;
9117 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009118 return 0;
9119}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009120
9121/* parse the "ca-file" bind keyword */
9122static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9123{
9124 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9125}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009126static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9127{
9128 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9129}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009130
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009131/* parse the "ca-verify-file" bind keyword */
9132static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9133{
9134 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9135}
9136static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9137{
9138 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9139}
9140
Christopher Faulet31af49d2015-06-09 17:29:50 +02009141/* parse the "ca-sign-file" bind keyword */
9142static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9143{
9144 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009145 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009146 return ERR_ALERT | ERR_FATAL;
9147 }
9148
Willy Tarreauef934602016-12-22 23:12:01 +01009149 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9150 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009151 else
9152 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9153
9154 return 0;
9155}
9156
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009157/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009158static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9159{
9160 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009161 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009162 return ERR_ALERT | ERR_FATAL;
9163 }
9164 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9165 return 0;
9166}
9167
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009168/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009169static 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 +02009170{
9171 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009172 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009173 return ERR_ALERT | ERR_FATAL;
9174 }
9175
Emeric Brun76d88952012-10-05 15:47:31 +02009176 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009177 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009178 return 0;
9179}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009180static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9181{
9182 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9183}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009184
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009185#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009186/* parse the "ciphersuites" bind keyword */
9187static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9188{
9189 if (!*args[cur_arg + 1]) {
9190 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9191 return ERR_ALERT | ERR_FATAL;
9192 }
9193
9194 free(conf->ciphersuites);
9195 conf->ciphersuites = strdup(args[cur_arg + 1]);
9196 return 0;
9197}
9198static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9199{
9200 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9201}
9202#endif
9203
Willy Tarreaubbc91962019-10-16 16:42:19 +02009204/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009205static 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 +02009206{
Willy Tarreau38011032013-08-13 16:59:39 +02009207 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009208
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009209 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009210 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009211 return ERR_ALERT | ERR_FATAL;
9212 }
9213
Willy Tarreauef934602016-12-22 23:12:01 +01009214 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9215 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009216 memprintf(err, "'%s' : path too long", args[cur_arg]);
9217 return ERR_ALERT | ERR_FATAL;
9218 }
Willy Tarreauef934602016-12-22 23:12:01 +01009219 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009220 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009221 }
9222
Willy Tarreaubbc91962019-10-16 16:42:19 +02009223 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009224}
9225
Willy Tarreaubbc91962019-10-16 16:42:19 +02009226/* 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 +01009227static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9228{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009229 int err_code;
9230
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009231 if (!*args[cur_arg + 1]) {
9232 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9233 return ERR_ALERT | ERR_FATAL;
9234 }
9235
William Lallemand6be66ec2020-03-06 22:26:32 +01009236 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009237 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009238 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009239
Willy Tarreaubbc91962019-10-16 16:42:19 +02009240 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009241}
9242
Emeric Brunfb510ea2012-10-05 12:00:26 +02009243/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009244static 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 +02009245{
Emeric Brun051cdab2012-10-02 19:25:50 +02009246#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009247 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009248 return ERR_ALERT | ERR_FATAL;
9249#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009250 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009251 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009252 return ERR_ALERT | ERR_FATAL;
9253 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009254
Willy Tarreauef934602016-12-22 23:12:01 +01009255 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9256 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009257 else
9258 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009259
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009260 if (!ssl_store_load_locations_file(conf->crl_file)) {
9261 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9262 return ERR_ALERT | ERR_FATAL;
9263 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009264 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009265#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009266}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009267static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9268{
9269 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9270}
Emeric Brun2b58d042012-09-20 17:10:03 +02009271
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009272/* parse the "curves" bind keyword keyword */
9273static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9274{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009275#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009276 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009277 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009278 return ERR_ALERT | ERR_FATAL;
9279 }
9280 conf->curves = strdup(args[cur_arg + 1]);
9281 return 0;
9282#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009283 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009284 return ERR_ALERT | ERR_FATAL;
9285#endif
9286}
9287static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9288{
9289 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9290}
9291
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009292/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009293static 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 +02009294{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009295#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009296 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009297 return ERR_ALERT | ERR_FATAL;
9298#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009299 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 +02009300 return ERR_ALERT | ERR_FATAL;
9301#else
9302 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009303 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009304 return ERR_ALERT | ERR_FATAL;
9305 }
9306
9307 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009308
9309 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009310#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009311}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009312static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9313{
9314 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9315}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009316
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009317/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009318static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9319{
9320 int code;
9321 char *p = args[cur_arg + 1];
9322 unsigned long long *ignerr = &conf->crt_ignerr;
9323
9324 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009325 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009326 return ERR_ALERT | ERR_FATAL;
9327 }
9328
9329 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9330 ignerr = &conf->ca_ignerr;
9331
9332 if (strcmp(p, "all") == 0) {
9333 *ignerr = ~0ULL;
9334 return 0;
9335 }
9336
9337 while (p) {
9338 code = atoi(p);
9339 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009340 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9341 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009342 return ERR_ALERT | ERR_FATAL;
9343 }
9344 *ignerr |= 1ULL << code;
9345 p = strchr(p, ',');
9346 if (p)
9347 p++;
9348 }
9349
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009350 return 0;
9351}
9352
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009353/* parse tls_method_options "no-xxx" and "force-xxx" */
9354static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009355{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009356 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009357 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009358 p = strchr(arg, '-');
9359 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009360 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009361 p++;
9362 if (!strcmp(p, "sslv3"))
9363 v = CONF_SSLV3;
9364 else if (!strcmp(p, "tlsv10"))
9365 v = CONF_TLSV10;
9366 else if (!strcmp(p, "tlsv11"))
9367 v = CONF_TLSV11;
9368 else if (!strcmp(p, "tlsv12"))
9369 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009370 else if (!strcmp(p, "tlsv13"))
9371 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009372 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009373 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009374 if (!strncmp(arg, "no-", 3))
9375 methods->flags |= methodVersions[v].flag;
9376 else if (!strncmp(arg, "force-", 6))
9377 methods->min = methods->max = v;
9378 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009379 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009380 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009381 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009382 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009383 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009384}
9385
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009386static 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 +02009387{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009388 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009389}
9390
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009391static 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 +02009392{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009393 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9394}
9395
9396/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9397static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9398{
9399 uint16_t i, v = 0;
9400 char *argv = args[cur_arg + 1];
9401 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009402 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009403 return ERR_ALERT | ERR_FATAL;
9404 }
9405 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9406 if (!strcmp(argv, methodVersions[i].name))
9407 v = i;
9408 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009409 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009410 return ERR_ALERT | ERR_FATAL;
9411 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009412 if (!strcmp("ssl-min-ver", args[cur_arg]))
9413 methods->min = v;
9414 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9415 methods->max = v;
9416 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009417 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009418 return ERR_ALERT | ERR_FATAL;
9419 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009420 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009421}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009422
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009423static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9424{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009425#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009426 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 +02009427#endif
9428 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9429}
9430
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009431static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9432{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009433 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009434}
9435
9436static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9437{
9438 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9439}
9440
Emeric Brun2d0c4822012-10-02 13:45:20 +02009441/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009442static 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 +02009443{
Emeric Brun89675492012-10-05 13:48:26 +02009444 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009445 return 0;
9446}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009447
Olivier Houchardc2aae742017-09-22 18:26:28 +02009448/* parse the "allow-0rtt" bind keyword */
9449static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9450{
9451 conf->early_data = 1;
9452 return 0;
9453}
9454
9455static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9456{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009457 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009458 return 0;
9459}
9460
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009461/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009462static 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 +02009463{
Bernard Spil13c53f82018-02-15 13:34:58 +01009464#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009465 char *p1, *p2;
9466
9467 if (!*args[cur_arg + 1]) {
9468 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9469 return ERR_ALERT | ERR_FATAL;
9470 }
9471
9472 free(conf->npn_str);
9473
Willy Tarreau3724da12016-02-12 17:11:12 +01009474 /* the NPN string is built as a suite of (<len> <name>)*,
9475 * so we reuse each comma to store the next <len> and need
9476 * one more for the end of the string.
9477 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009478 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009479 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009480 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9481
9482 /* replace commas with the name length */
9483 p1 = conf->npn_str;
9484 p2 = p1 + 1;
9485 while (1) {
9486 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9487 if (!p2)
9488 p2 = p1 + 1 + strlen(p1 + 1);
9489
9490 if (p2 - (p1 + 1) > 255) {
9491 *p2 = '\0';
9492 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9493 return ERR_ALERT | ERR_FATAL;
9494 }
9495
9496 *p1 = p2 - (p1 + 1);
9497 p1 = p2;
9498
9499 if (!*p2)
9500 break;
9501
9502 *(p2++) = '\0';
9503 }
9504 return 0;
9505#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009506 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009507 return ERR_ALERT | ERR_FATAL;
9508#endif
9509}
9510
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009511static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9512{
9513 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9514}
9515
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009516
9517/* Parses a alpn string and converts it to the right format for the SSL api */
9518int ssl_sock_parse_alpn(char *arg, char **alpn_str, int *alpn_len, char **err)
Willy Tarreauab861d32013-04-02 02:30:41 +02009519{
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009520 char *p1, *p2, *alpn = NULL;
9521 int len, ret = 0;
Willy Tarreauab861d32013-04-02 02:30:41 +02009522
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009523 *alpn_str = NULL;
9524 *alpn_len = 0;
Willy Tarreauab861d32013-04-02 02:30:41 +02009525
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009526 if (!*arg) {
9527 memprintf(err, "missing the comma-delimited ALPN protocol suite");
9528 goto error;
9529 }
Willy Tarreauab861d32013-04-02 02:30:41 +02009530
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009531 /* the ALPN string is built as a suite of (<len> <name>)*,
9532 * so we reuse each comma to store the next <len> and need
9533 * one more for the end of the string.
9534 */
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009535 len = strlen(arg) + 1;
9536 alpn = calloc(1, len+1);
9537 if (!alpn) {
9538 memprintf(err, "'%s' : out of memory", arg);
9539 goto error;
9540 }
9541 memcpy(alpn+1, arg, len);
Willy Tarreauab861d32013-04-02 02:30:41 +02009542
9543 /* replace commas with the name length */
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009544 p1 = alpn;
Willy Tarreauab861d32013-04-02 02:30:41 +02009545 p2 = p1 + 1;
9546 while (1) {
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009547 p2 = memchr(p1 + 1, ',', alpn + len - (p1 + 1));
Willy Tarreauab861d32013-04-02 02:30:41 +02009548 if (!p2)
9549 p2 = p1 + 1 + strlen(p1 + 1);
9550
9551 if (p2 - (p1 + 1) > 255) {
9552 *p2 = '\0';
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009553 memprintf(err, "ALPN protocol name too long : '%s'", p1 + 1);
9554 goto error;
Willy Tarreauab861d32013-04-02 02:30:41 +02009555 }
9556
9557 *p1 = p2 - (p1 + 1);
9558 p1 = p2;
9559
9560 if (!*p2)
9561 break;
9562
9563 *(p2++) = '\0';
9564 }
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009565
9566 *alpn_str = alpn;
9567 *alpn_len = len;
9568
9569 out:
9570 return ret;
9571
9572 error:
9573 free(alpn);
9574 ret = ERR_ALERT | ERR_FATAL;
9575 goto out;
9576}
9577
9578/* parse the "alpn" bind keyword */
9579static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9580{
9581#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9582 int ret;
9583
9584 free(conf->alpn_str);
9585
9586 ret = ssl_sock_parse_alpn(args[cur_arg + 1], &conf->alpn_str, &conf->alpn_len, err);
9587 if (ret)
9588 memprintf(err, "'%s' : %s", args[cur_arg], *err);
9589 return ret;
Willy Tarreauab861d32013-04-02 02:30:41 +02009590#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009591 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009592 return ERR_ALERT | ERR_FATAL;
9593#endif
9594}
9595
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009596static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9597{
9598 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9599}
9600
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009601/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009602static 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 +02009603{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009604 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009605 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009606
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009607 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9608 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Jerome Magninb203ff62020-04-03 15:28:22 +02009609#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
9610 if (global_ssl.listen_default_curves && !conf->ssl_conf.curves)
9611 conf->ssl_conf.curves = strdup(global_ssl.listen_default_curves);
9612#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009613#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009614 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9615 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9616#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009617 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009618 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9619 if (!conf->ssl_conf.ssl_methods.min)
9620 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9621 if (!conf->ssl_conf.ssl_methods.max)
9622 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009623
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009624 return 0;
9625}
9626
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009627/* parse the "prefer-client-ciphers" bind keyword */
9628static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9629{
9630 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9631 return 0;
9632}
9633
Christopher Faulet31af49d2015-06-09 17:29:50 +02009634/* parse the "generate-certificates" bind keyword */
9635static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9636{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009637#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009638 conf->generate_certs = 1;
9639#else
9640 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9641 err && *err ? *err : "");
9642#endif
9643 return 0;
9644}
9645
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009646/* parse the "strict-sni" bind keyword */
9647static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9648{
9649 conf->strict_sni = 1;
9650 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009651}
9652
9653/* parse the "tls-ticket-keys" bind keyword */
9654static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9655{
9656#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009657 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009658 int i = 0;
9659 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009660 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009661
9662 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009663 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009664 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009665 }
9666
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009667 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009668 if (keys_ref) {
9669 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009670 conf->keys_ref = keys_ref;
9671 return 0;
9672 }
9673
Christopher Faulete566f3d2019-10-21 09:55:49 +02009674 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009675 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009676 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009677 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009678 }
9679
Emeric Brun9e754772019-01-10 17:51:55 +01009680 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009681 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009682 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009683 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009684 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009685
9686 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009687 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009688 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009689 }
9690
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009691 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009692 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009693 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009694 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009695 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009696
Emeric Brun9e754772019-01-10 17:51:55 +01009697 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009698 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9699 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009700 int dec_size;
9701
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009702 /* Strip newline characters from the end */
9703 if(thisline[len - 1] == '\n')
9704 thisline[--len] = 0;
9705
9706 if(thisline[len - 1] == '\r')
9707 thisline[--len] = 0;
9708
Emeric Brun9e754772019-01-10 17:51:55 +01009709 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9710 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009711 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009712 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009713 }
Emeric Brun9e754772019-01-10 17:51:55 +01009714 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9715 keys_ref->key_size_bits = 128;
9716 }
9717 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9718 keys_ref->key_size_bits = 256;
9719 }
9720 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9721 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9722 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009723 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009724 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009725 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009726 i++;
9727 }
9728
9729 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009730 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 +02009731 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009732 }
9733
9734 fclose(f);
9735
9736 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009737 i -= 2;
9738 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009739 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009740 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009741 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009742 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009743
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009744 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9745
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009746 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009747
9748 fail:
9749 if (f)
9750 fclose(f);
9751 if (keys_ref) {
9752 free(keys_ref->filename);
9753 free(keys_ref->tlskeys);
9754 free(keys_ref);
9755 }
9756 return ERR_ALERT | ERR_FATAL;
9757
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009758#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009759 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009760 return ERR_ALERT | ERR_FATAL;
9761#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009762}
9763
Emeric Brund94b3fe2012-09-20 18:23:56 +02009764/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009765static 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 +02009766{
9767 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009768 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009769 return ERR_ALERT | ERR_FATAL;
9770 }
9771
9772 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009773 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009774 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009775 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009776 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009777 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009778 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009779 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9780 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009781 return ERR_ALERT | ERR_FATAL;
9782 }
9783
9784 return 0;
9785}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009786static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9787{
9788 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9789}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009790
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009791/* parse the "no-ca-names" bind keyword */
9792static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9793{
9794 conf->no_ca_names = 1;
9795 return 0;
9796}
9797static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9798{
9799 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9800}
9801
Willy Tarreau92faadf2012-10-10 23:04:25 +02009802/************** "server" keywords ****************/
9803
Olivier Houchardc7566002018-11-20 23:33:50 +01009804/* parse the "npn" bind keyword */
9805static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9806{
9807#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9808 char *p1, *p2;
9809
9810 if (!*args[*cur_arg + 1]) {
9811 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9812 return ERR_ALERT | ERR_FATAL;
9813 }
9814
9815 free(newsrv->ssl_ctx.npn_str);
9816
9817 /* the NPN string is built as a suite of (<len> <name>)*,
9818 * so we reuse each comma to store the next <len> and need
9819 * one more for the end of the string.
9820 */
9821 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9822 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9823 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9824 newsrv->ssl_ctx.npn_len);
9825
9826 /* replace commas with the name length */
9827 p1 = newsrv->ssl_ctx.npn_str;
9828 p2 = p1 + 1;
9829 while (1) {
9830 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9831 newsrv->ssl_ctx.npn_len - (p1 + 1));
9832 if (!p2)
9833 p2 = p1 + 1 + strlen(p1 + 1);
9834
9835 if (p2 - (p1 + 1) > 255) {
9836 *p2 = '\0';
9837 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9838 return ERR_ALERT | ERR_FATAL;
9839 }
9840
9841 *p1 = p2 - (p1 + 1);
9842 p1 = p2;
9843
9844 if (!*p2)
9845 break;
9846
9847 *(p2++) = '\0';
9848 }
9849 return 0;
9850#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009851 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009852 return ERR_ALERT | ERR_FATAL;
9853#endif
9854}
9855
Olivier Houchard92150142018-12-21 19:47:01 +01009856/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009857static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9858{
9859#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard92150142018-12-21 19:47:01 +01009860 char **alpn_str;
9861 int *alpn_len;
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009862 int ret;
Olivier Houchardc7566002018-11-20 23:33:50 +01009863
Olivier Houchard92150142018-12-21 19:47:01 +01009864 if (*args[*cur_arg] == 'c') {
9865 alpn_str = &newsrv->check.alpn_str;
9866 alpn_len = &newsrv->check.alpn_len;
9867 } else {
9868 alpn_str = &newsrv->ssl_ctx.alpn_str;
9869 alpn_len = &newsrv->ssl_ctx.alpn_len;
9870
9871 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009872
Olivier Houchard92150142018-12-21 19:47:01 +01009873 free(*alpn_str);
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009874 ret = ssl_sock_parse_alpn(args[*cur_arg + 1], alpn_str, alpn_len, err);
9875 if (ret)
9876 memprintf(err, "'%s' : %s", args[*cur_arg], *err);
9877 return ret;
Olivier Houchardc7566002018-11-20 23:33:50 +01009878#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009879 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009880 return ERR_ALERT | ERR_FATAL;
9881#endif
9882}
9883
Emeric Brunef42d922012-10-11 16:11:36 +02009884/* parse the "ca-file" server keyword */
9885static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9886{
9887 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009888 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009889 return ERR_ALERT | ERR_FATAL;
9890 }
9891
Willy Tarreauef934602016-12-22 23:12:01 +01009892 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9893 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009894 else
9895 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9896
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009897 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9898 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9899 return ERR_ALERT | ERR_FATAL;
9900 }
Emeric Brunef42d922012-10-11 16:11:36 +02009901 return 0;
9902}
9903
Olivier Houchard9130a962017-10-17 17:33:43 +02009904/* parse the "check-sni" server keyword */
9905static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9906{
9907 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009908 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009909 return ERR_ALERT | ERR_FATAL;
9910 }
9911
9912 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9913 if (!newsrv->check.sni) {
9914 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9915 return ERR_ALERT | ERR_FATAL;
9916 }
9917 return 0;
9918
9919}
9920
Willy Tarreau92faadf2012-10-10 23:04:25 +02009921/* parse the "check-ssl" server keyword */
9922static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9923{
9924 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009925 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9926 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009927#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009928 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9929 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9930#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009931 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009932 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9933 if (!newsrv->ssl_ctx.methods.min)
9934 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9935 if (!newsrv->ssl_ctx.methods.max)
9936 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9937
Willy Tarreau92faadf2012-10-10 23:04:25 +02009938 return 0;
9939}
9940
9941/* parse the "ciphers" server keyword */
9942static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9943{
9944 if (!*args[*cur_arg + 1]) {
9945 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9946 return ERR_ALERT | ERR_FATAL;
9947 }
9948
9949 free(newsrv->ssl_ctx.ciphers);
9950 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9951 return 0;
9952}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009953
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009954#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009955/* parse the "ciphersuites" server keyword */
9956static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9957{
9958 if (!*args[*cur_arg + 1]) {
9959 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9960 return ERR_ALERT | ERR_FATAL;
9961 }
9962
9963 free(newsrv->ssl_ctx.ciphersuites);
9964 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9965 return 0;
9966}
9967#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009968
Emeric Brunef42d922012-10-11 16:11:36 +02009969/* parse the "crl-file" server keyword */
9970static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9971{
9972#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009973 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009974 return ERR_ALERT | ERR_FATAL;
9975#else
9976 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009977 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009978 return ERR_ALERT | ERR_FATAL;
9979 }
9980
Willy Tarreauef934602016-12-22 23:12:01 +01009981 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9982 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009983 else
9984 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9985
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009986 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9987 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9988 return ERR_ALERT | ERR_FATAL;
9989 }
Emeric Brunef42d922012-10-11 16:11:36 +02009990 return 0;
9991#endif
9992}
9993
Emeric Bruna7aa3092012-10-26 12:58:00 +02009994/* parse the "crt" server keyword */
9995static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9996{
9997 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009998 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009999 return ERR_ALERT | ERR_FATAL;
10000 }
10001
Willy Tarreauef934602016-12-22 23:12:01 +010010002 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +010010003 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +020010004 else
10005 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
10006
10007 return 0;
10008}
Emeric Brunef42d922012-10-11 16:11:36 +020010009
Frédéric Lécaille340ae602017-03-13 10:38:04 +010010010/* parse the "no-check-ssl" server keyword */
10011static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10012{
Christopher Fauletf61f33a2020-03-27 18:55:49 +010010013 newsrv->check.use_ssl = -1;
Frédéric Lécaille340ae602017-03-13 10:38:04 +010010014 free(newsrv->ssl_ctx.ciphers);
10015 newsrv->ssl_ctx.ciphers = NULL;
10016 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
10017 return 0;
10018}
10019
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +010010020/* parse the "no-send-proxy-v2-ssl" server keyword */
10021static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10022{
10023 newsrv->pp_opts &= ~SRV_PP_V2;
10024 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
10025 return 0;
10026}
10027
10028/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
10029static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10030{
10031 newsrv->pp_opts &= ~SRV_PP_V2;
10032 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
10033 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
10034 return 0;
10035}
10036
Frédéric Lécaillee381d762017-03-13 11:54:17 +010010037/* parse the "no-ssl" server keyword */
10038static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10039{
Christopher Fauletf61f33a2020-03-27 18:55:49 +010010040 newsrv->use_ssl = -1;
Frédéric Lécaillee381d762017-03-13 11:54:17 +010010041 free(newsrv->ssl_ctx.ciphers);
10042 newsrv->ssl_ctx.ciphers = NULL;
10043 return 0;
10044}
10045
Olivier Houchard522eea72017-11-03 16:27:47 +010010046/* parse the "allow-0rtt" server keyword */
10047static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10048{
10049 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
10050 return 0;
10051}
10052
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +010010053/* parse the "no-ssl-reuse" server keyword */
10054static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10055{
10056 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
10057 return 0;
10058}
10059
Emeric Brunf9c5c472012-10-11 15:28:34 +020010060/* parse the "no-tls-tickets" server keyword */
10061static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10062{
10063 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
10064 return 0;
10065}
David Safb76832014-05-08 23:42:08 -040010066/* parse the "send-proxy-v2-ssl" server keyword */
10067static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10068{
10069 newsrv->pp_opts |= SRV_PP_V2;
10070 newsrv->pp_opts |= SRV_PP_V2_SSL;
10071 return 0;
10072}
10073
10074/* parse the "send-proxy-v2-ssl-cn" server keyword */
10075static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10076{
10077 newsrv->pp_opts |= SRV_PP_V2;
10078 newsrv->pp_opts |= SRV_PP_V2_SSL;
10079 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
10080 return 0;
10081}
Emeric Brunf9c5c472012-10-11 15:28:34 +020010082
Willy Tarreau732eac42015-07-09 11:40:25 +020010083/* parse the "sni" server keyword */
10084static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10085{
10086#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
10087 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
10088 return ERR_ALERT | ERR_FATAL;
10089#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010090 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +020010091
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010092 arg = args[*cur_arg + 1];
10093 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +020010094 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
10095 return ERR_ALERT | ERR_FATAL;
10096 }
10097
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010098 free(newsrv->sni_expr);
10099 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010100
Willy Tarreau732eac42015-07-09 11:40:25 +020010101 return 0;
10102#endif
10103}
10104
Willy Tarreau92faadf2012-10-10 23:04:25 +020010105/* parse the "ssl" server keyword */
10106static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10107{
10108 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010109 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10110 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010111#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010112 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10113 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10114#endif
Jerome Magnin2e8d52f2020-04-22 11:40:18 +020010115 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
10116 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
10117
10118 if (!newsrv->ssl_ctx.methods.min)
10119 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
10120
10121 if (!newsrv->ssl_ctx.methods.max)
10122 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
10123
10124
Willy Tarreau92faadf2012-10-10 23:04:25 +020010125 return 0;
10126}
10127
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010128/* parse the "ssl-reuse" server keyword */
10129static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10130{
10131 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10132 return 0;
10133}
10134
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010135/* parse the "tls-tickets" server keyword */
10136static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10137{
10138 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10139 return 0;
10140}
10141
Emeric Brunef42d922012-10-11 16:11:36 +020010142/* parse the "verify" server keyword */
10143static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10144{
10145 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010146 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010147 return ERR_ALERT | ERR_FATAL;
10148 }
10149
10150 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010151 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010152 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010153 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010154 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010155 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10156 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010157 return ERR_ALERT | ERR_FATAL;
10158 }
10159
Evan Broderbe554312013-06-27 00:05:25 -070010160 return 0;
10161}
10162
10163/* parse the "verifyhost" server keyword */
10164static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10165{
10166 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010167 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010168 return ERR_ALERT | ERR_FATAL;
10169 }
10170
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010171 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010172 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10173
Emeric Brunef42d922012-10-11 16:11:36 +020010174 return 0;
10175}
10176
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010177/* parse the "ssl-default-bind-options" keyword in global section */
10178static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10179 struct proxy *defpx, const char *file, int line,
10180 char **err) {
10181 int i = 1;
10182
10183 if (*(args[i]) == 0) {
10184 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10185 return -1;
10186 }
10187 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010188 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010189 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010190 else if (!strcmp(args[i], "prefer-client-ciphers"))
10191 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010192 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10193 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10194 i++;
10195 else {
10196 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10197 return -1;
10198 }
10199 }
10200 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010201 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10202 return -1;
10203 }
10204 i++;
10205 }
10206 return 0;
10207}
10208
10209/* parse the "ssl-default-server-options" keyword in global section */
10210static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10211 struct proxy *defpx, const char *file, int line,
10212 char **err) {
10213 int i = 1;
10214
10215 if (*(args[i]) == 0) {
10216 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10217 return -1;
10218 }
10219 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010220 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010221 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010222 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10223 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10224 i++;
10225 else {
10226 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10227 return -1;
10228 }
10229 }
10230 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010231 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10232 return -1;
10233 }
10234 i++;
10235 }
10236 return 0;
10237}
10238
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010239/* parse the "ca-base" / "crt-base" keywords in global section.
10240 * Returns <0 on alert, >0 on warning, 0 on success.
10241 */
10242static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10243 struct proxy *defpx, const char *file, int line,
10244 char **err)
10245{
10246 char **target;
10247
Willy Tarreauef934602016-12-22 23:12:01 +010010248 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010249
10250 if (too_many_args(1, args, err, NULL))
10251 return -1;
10252
10253 if (*target) {
10254 memprintf(err, "'%s' already specified.", args[0]);
10255 return -1;
10256 }
10257
10258 if (*(args[1]) == 0) {
10259 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10260 return -1;
10261 }
10262 *target = strdup(args[1]);
10263 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010264}
10265
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020010266/* parse the "ssl-skip-self-issued-ca" keyword in global section. */
10267static int ssl_parse_skip_self_issued_ca(char **args, int section_type, struct proxy *curpx,
10268 struct proxy *defpx, const char *file, int line,
10269 char **err)
10270{
10271 global_ssl.skip_self_issued_ca = 1;
10272 return 0;
10273}
10274
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010275/* "issuers-chain-path" load chain certificate in global */
10276static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10277{
10278 X509 *ca;
10279 X509_NAME *name = NULL;
10280 ASN1_OCTET_STRING *skid = NULL;
10281 STACK_OF(X509) *chain = NULL;
10282 struct issuer_chain *issuer;
10283 struct eb64_node *node;
10284 char *path;
10285 u64 key;
10286 int ret = 0;
10287
10288 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10289 if (chain == NULL) {
10290 chain = sk_X509_new_null();
10291 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10292 name = X509_get_subject_name(ca);
10293 }
10294 if (!sk_X509_push(chain, ca)) {
10295 X509_free(ca);
10296 goto end;
10297 }
10298 }
10299 if (!chain) {
10300 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10301 goto end;
10302 }
10303 if (!skid) {
10304 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10305 goto end;
10306 }
10307 if (!name) {
10308 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10309 goto end;
10310 }
10311 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010312 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010313 issuer = container_of(node, typeof(*issuer), node);
10314 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10315 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10316 goto end;
10317 }
10318 }
10319 issuer = calloc(1, sizeof *issuer);
10320 path = strdup(fp);
10321 if (!issuer || !path) {
10322 free(issuer);
10323 free(path);
10324 goto end;
10325 }
10326 issuer->node.key = key;
10327 issuer->path = path;
10328 issuer->chain = chain;
10329 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010330 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010331 ret = 1;
10332 end:
10333 if (skid)
10334 ASN1_OCTET_STRING_free(skid);
10335 if (chain)
10336 sk_X509_pop_free(chain, X509_free);
10337 return ret;
10338}
10339
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010340static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010341{
10342 AUTHORITY_KEYID *akid;
10343 struct issuer_chain *issuer = NULL;
10344
10345 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10346 if (akid) {
10347 struct eb64_node *node;
10348 u64 hk;
10349 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10350 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10351 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10352 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10353 issuer = ti;
10354 break;
10355 }
10356 }
10357 AUTHORITY_KEYID_free(akid);
10358 }
10359 return issuer;
10360}
10361
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010362static void ssl_free_global_issuers(void)
10363{
10364 struct eb64_node *node, *back;
10365 struct issuer_chain *issuer;
10366
William Lallemande0f3fd52020-02-25 14:53:06 +010010367 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010368 while (node) {
10369 issuer = container_of(node, typeof(*issuer), node);
10370 back = eb64_next(node);
10371 eb64_delete(node);
10372 free(issuer->path);
10373 sk_X509_pop_free(issuer->chain, X509_free);
10374 free(issuer);
10375 node = back;
10376 }
10377}
10378
10379static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10380 struct proxy *defpx, const char *file, int line,
10381 char **err)
10382{
10383 char *path;
10384 struct dirent **de_list;
10385 int i, n;
10386 struct stat buf;
10387 char *end;
10388 char fp[MAXPATHLEN+1];
10389
10390 if (too_many_args(1, args, err, NULL))
10391 return -1;
10392
10393 path = args[1];
10394 if (*path == 0 || stat(path, &buf)) {
10395 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10396 err && *err ? *err : "", args[0]);
10397 return -1;
10398 }
10399 if (S_ISDIR(buf.st_mode) == 0) {
10400 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10401 err && *err ? *err : "", args[0], path);
10402 return -1;
10403 }
10404
10405 /* strip trailing slashes, including first one */
10406 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10407 *end = 0;
10408 /* path already parsed? */
10409 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10410 return 0;
10411 /* overwrite old issuers_chain_path */
10412 free(global_ssl.issuers_chain_path);
10413 global_ssl.issuers_chain_path = strdup(path);
10414 ssl_free_global_issuers();
10415
10416 n = scandir(path, &de_list, 0, alphasort);
10417 if (n < 0) {
10418 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10419 err && *err ? *err : "", args[0], path, strerror(errno));
10420 return -1;
10421 }
10422 for (i = 0; i < n; i++) {
10423 struct dirent *de = de_list[i];
10424 BIO *in = NULL;
10425 char *warn = NULL;
10426
10427 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10428 free(de);
10429 if (stat(fp, &buf) != 0) {
10430 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10431 goto next;
10432 }
10433 if (!S_ISREG(buf.st_mode))
10434 goto next;
10435
10436 in = BIO_new(BIO_s_file());
10437 if (in == NULL)
10438 goto next;
10439 if (BIO_read_filename(in, fp) <= 0)
10440 goto next;
10441 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10442 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010443 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010444 free(warn);
10445 warn = NULL;
10446 }
10447 next:
10448 if (in)
10449 BIO_free(in);
10450 }
10451 free(de_list);
10452
10453 return 0;
10454}
10455
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010456/* parse the "ssl-mode-async" keyword in global section.
10457 * Returns <0 on alert, >0 on warning, 0 on success.
10458 */
10459static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10460 struct proxy *defpx, const char *file, int line,
10461 char **err)
10462{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010463#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010464 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010465 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010466 return 0;
10467#else
10468 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10469 return -1;
10470#endif
10471}
10472
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010473#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010474static int ssl_check_async_engine_count(void) {
10475 int err_code = 0;
10476
Emeric Brun3854e012017-05-17 20:42:48 +020010477 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010478 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010479 err_code = ERR_ABORT;
10480 }
10481 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010482}
10483
Grant Zhang872f9c22017-01-21 01:10:18 +000010484/* parse the "ssl-engine" keyword in global section.
10485 * Returns <0 on alert, >0 on warning, 0 on success.
10486 */
10487static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10488 struct proxy *defpx, const char *file, int line,
10489 char **err)
10490{
10491 char *algo;
10492 int ret = -1;
10493
10494 if (*(args[1]) == 0) {
10495 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10496 return ret;
10497 }
10498
10499 if (*(args[2]) == 0) {
10500 /* if no list of algorithms is given, it defaults to ALL */
10501 algo = strdup("ALL");
10502 goto add_engine;
10503 }
10504
10505 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10506 if (strcmp(args[2], "algo") != 0) {
10507 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10508 return ret;
10509 }
10510
10511 if (*(args[3]) == 0) {
10512 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10513 return ret;
10514 }
10515 algo = strdup(args[3]);
10516
10517add_engine:
10518 if (ssl_init_single_engine(args[1], algo)==0) {
10519 openssl_engines_initialized++;
10520 ret = 0;
10521 }
10522 free(algo);
10523 return ret;
10524}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010525#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010526
Willy Tarreauf22e9682016-12-21 23:23:19 +010010527/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10528 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10529 */
10530static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10531 struct proxy *defpx, const char *file, int line,
10532 char **err)
10533{
10534 char **target;
10535
Willy Tarreauef934602016-12-22 23:12:01 +010010536 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010537
10538 if (too_many_args(1, args, err, NULL))
10539 return -1;
10540
10541 if (*(args[1]) == 0) {
10542 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10543 return -1;
10544 }
10545
10546 free(*target);
10547 *target = strdup(args[1]);
10548 return 0;
10549}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010550
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010551#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010552/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10553 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10554 */
10555static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10556 struct proxy *defpx, const char *file, int line,
10557 char **err)
10558{
10559 char **target;
10560
10561 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10562
10563 if (too_many_args(1, args, err, NULL))
10564 return -1;
10565
10566 if (*(args[1]) == 0) {
10567 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10568 return -1;
10569 }
10570
10571 free(*target);
10572 *target = strdup(args[1]);
10573 return 0;
10574}
10575#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010576
Jerome Magninb203ff62020-04-03 15:28:22 +020010577#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
10578/*
10579 * parse the "ssl-default-bind-curves" keyword in a global section.
10580 * Returns <0 on alert, >0 on warning, 0 on success.
10581 */
10582static int ssl_parse_global_curves(char **args, int section_type, struct proxy *curpx,
10583 struct proxy *defpx, const char *file, int line,
10584 char **err)
10585{
10586 char **target;
10587 target = &global_ssl.listen_default_curves;
10588
10589 if (too_many_args(1, args, err, NULL))
10590 return -1;
10591
10592 if (*(args[1]) == 0) {
10593 memprintf(err, "global statement '%s' expects a curves suite as an arguments.", args[0]);
10594 return -1;
10595 }
10596
10597 free(*target);
10598 *target = strdup(args[1]);
10599 return 0;
10600}
10601#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010010602/* parse various global tune.ssl settings consisting in positive integers.
10603 * Returns <0 on alert, >0 on warning, 0 on success.
10604 */
10605static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10606 struct proxy *defpx, const char *file, int line,
10607 char **err)
10608{
10609 int *target;
10610
10611 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10612 target = &global.tune.sslcachesize;
10613 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010614 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010615 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010616 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010617 else if (strcmp(args[0], "maxsslconn") == 0)
10618 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010619 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10620 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010621 else {
10622 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10623 return -1;
10624 }
10625
10626 if (too_many_args(1, args, err, NULL))
10627 return -1;
10628
10629 if (*(args[1]) == 0) {
10630 memprintf(err, "'%s' expects an integer argument.", args[0]);
10631 return -1;
10632 }
10633
10634 *target = atoi(args[1]);
10635 if (*target < 0) {
10636 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10637 return -1;
10638 }
10639 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010640}
10641
10642static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10643 struct proxy *defpx, const char *file, int line,
10644 char **err)
10645{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010646 int ret;
10647
10648 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10649 if (ret != 0)
10650 return ret;
10651
Willy Tarreaubafbe012017-11-24 17:34:44 +010010652 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010653 memprintf(err, "'%s' is already configured.", args[0]);
10654 return -1;
10655 }
10656
Willy Tarreaubafbe012017-11-24 17:34:44 +010010657 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10658 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010659 memprintf(err, "Out of memory error.");
10660 return -1;
10661 }
10662 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010663}
10664
10665/* parse "ssl.force-private-cache".
10666 * Returns <0 on alert, >0 on warning, 0 on success.
10667 */
10668static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10669 struct proxy *defpx, const char *file, int line,
10670 char **err)
10671{
10672 if (too_many_args(0, args, err, NULL))
10673 return -1;
10674
Willy Tarreauef934602016-12-22 23:12:01 +010010675 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010676 return 0;
10677}
10678
10679/* parse "ssl.lifetime".
10680 * Returns <0 on alert, >0 on warning, 0 on success.
10681 */
10682static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10683 struct proxy *defpx, const char *file, int line,
10684 char **err)
10685{
10686 const char *res;
10687
10688 if (too_many_args(1, args, err, NULL))
10689 return -1;
10690
10691 if (*(args[1]) == 0) {
10692 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10693 return -1;
10694 }
10695
Willy Tarreauef934602016-12-22 23:12:01 +010010696 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010697 if (res == PARSE_TIME_OVER) {
10698 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10699 args[1], args[0]);
10700 return -1;
10701 }
10702 else if (res == PARSE_TIME_UNDER) {
10703 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10704 args[1], args[0]);
10705 return -1;
10706 }
10707 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010708 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10709 return -1;
10710 }
10711 return 0;
10712}
10713
10714#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010715/* parse "ssl-dh-param-file".
10716 * Returns <0 on alert, >0 on warning, 0 on success.
10717 */
10718static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10719 struct proxy *defpx, const char *file, int line,
10720 char **err)
10721{
10722 if (too_many_args(1, args, err, NULL))
10723 return -1;
10724
10725 if (*(args[1]) == 0) {
10726 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10727 return -1;
10728 }
10729
10730 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10731 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10732 return -1;
10733 }
10734 return 0;
10735}
10736
Willy Tarreau9ceda382016-12-21 23:13:03 +010010737/* parse "ssl.default-dh-param".
10738 * Returns <0 on alert, >0 on warning, 0 on success.
10739 */
10740static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10741 struct proxy *defpx, const char *file, int line,
10742 char **err)
10743{
10744 if (too_many_args(1, args, err, NULL))
10745 return -1;
10746
10747 if (*(args[1]) == 0) {
10748 memprintf(err, "'%s' expects an integer argument.", args[0]);
10749 return -1;
10750 }
10751
Willy Tarreauef934602016-12-22 23:12:01 +010010752 global_ssl.default_dh_param = atoi(args[1]);
10753 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010754 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10755 return -1;
10756 }
10757 return 0;
10758}
10759#endif
10760
William Lallemand3af48e72020-02-03 17:15:52 +010010761
10762/*
10763 * parse "ssl-load-extra-files".
10764 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10765 */
10766static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10767 struct proxy *defpx, const char *file, int line,
10768 char **err)
10769{
10770 int i;
10771 int gf = SSL_GF_NONE;
10772
10773 if (*(args[1]) == 0)
10774 goto err_arg;
10775
10776 for (i = 1; *args[i]; i++) {
10777
10778 if (!strcmp("bundle", args[i])) {
10779 gf |= SSL_GF_BUNDLE;
10780
10781 } else if (!strcmp("sctl", args[i])) {
10782 gf |= SSL_GF_SCTL;
10783
10784 } else if (!strcmp("ocsp", args[i])){
10785 gf |= SSL_GF_OCSP;
10786
10787 } else if (!strcmp("issuer", args[i])){
10788 gf |= SSL_GF_OCSP_ISSUER;
10789
William Lallemand4c5adbf2020-02-24 14:23:22 +010010790 } else if (!strcmp("key", args[i])) {
10791 gf |= SSL_GF_KEY;
10792
William Lallemand3af48e72020-02-03 17:15:52 +010010793 } else if (!strcmp("none", args[i])) {
10794 if (gf != SSL_GF_NONE)
10795 goto err_alone;
10796 gf = SSL_GF_NONE;
10797 i++;
10798 break;
10799
10800 } else if (!strcmp("all", args[i])) {
10801 if (gf != SSL_GF_NONE)
10802 goto err_alone;
10803 gf = SSL_GF_ALL;
10804 i++;
10805 break;
10806 } else {
10807 goto err_arg;
10808 }
10809 }
10810 /* break from loop but there are still arguments */
10811 if (*args[i])
10812 goto err_alone;
10813
10814 global_ssl.extra_files = gf;
10815
10816 return 0;
10817
10818err_alone:
10819 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10820 return -1;
10821
10822err_arg:
10823 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10824 return -1;
10825}
10826
Willy Tarreau9ceda382016-12-21 23:13:03 +010010827
William Lallemand32af2032016-10-29 18:09:35 +020010828/* This function is used with TLS ticket keys management. It permits to browse
10829 * each reference. The variable <getnext> must contain the current node,
10830 * <end> point to the root node.
10831 */
10832#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10833static inline
10834struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10835{
10836 struct tls_keys_ref *ref = getnext;
10837
10838 while (1) {
10839
10840 /* Get next list entry. */
10841 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10842
10843 /* If the entry is the last of the list, return NULL. */
10844 if (&ref->list == end)
10845 return NULL;
10846
10847 return ref;
10848 }
10849}
10850
10851static inline
10852struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10853{
10854 int id;
10855 char *error;
10856
10857 /* If the reference starts by a '#', this is numeric id. */
10858 if (reference[0] == '#') {
10859 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10860 id = strtol(reference + 1, &error, 10);
10861 if (*error != '\0')
10862 return NULL;
10863
10864 /* Perform the unique id lookup. */
10865 return tlskeys_ref_lookupid(id);
10866 }
10867
10868 /* Perform the string lookup. */
10869 return tlskeys_ref_lookup(reference);
10870}
10871#endif
10872
10873
10874#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10875
10876static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10877
10878static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10879 return cli_io_handler_tlskeys_files(appctx);
10880}
10881
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010882/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10883 * (next index to be dumped), and cli.p0 (next key reference).
10884 */
William Lallemand32af2032016-10-29 18:09:35 +020010885static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10886
10887 struct stream_interface *si = appctx->owner;
10888
10889 switch (appctx->st2) {
10890 case STAT_ST_INIT:
10891 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010892 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010893 * later and restart at the state "STAT_ST_INIT".
10894 */
10895 chunk_reset(&trash);
10896
10897 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10898 chunk_appendf(&trash, "# id secret\n");
10899 else
10900 chunk_appendf(&trash, "# id (file)\n");
10901
Willy Tarreau06d80a92017-10-19 14:32:15 +020010902 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010903 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010904 return 0;
10905 }
10906
William Lallemand32af2032016-10-29 18:09:35 +020010907 /* Now, we start the browsing of the references lists.
10908 * Note that the following call to LIST_ELEM return bad pointer. The only
10909 * available field of this pointer is <list>. It is used with the function
10910 * tlskeys_list_get_next() for retruning the first available entry
10911 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010912 if (appctx->ctx.cli.p0 == NULL) {
10913 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10914 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010915 }
10916
10917 appctx->st2 = STAT_ST_LIST;
10918 /* fall through */
10919
10920 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010921 while (appctx->ctx.cli.p0) {
10922 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010923
10924 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010925 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010926 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010927
10928 if (appctx->ctx.cli.i1 == 0)
10929 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10930
William Lallemand32af2032016-10-29 18:09:35 +020010931 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010932 int head;
10933
10934 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10935 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010936 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010937 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010938
10939 chunk_reset(t2);
10940 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010941 if (ref->key_size_bits == 128) {
10942 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10943 sizeof(struct tls_sess_key_128),
10944 t2->area, t2->size);
10945 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10946 t2->area);
10947 }
10948 else if (ref->key_size_bits == 256) {
10949 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10950 sizeof(struct tls_sess_key_256),
10951 t2->area, t2->size);
10952 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10953 t2->area);
10954 }
10955 else {
10956 /* This case should never happen */
10957 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10958 }
William Lallemand32af2032016-10-29 18:09:35 +020010959
Willy Tarreau06d80a92017-10-19 14:32:15 +020010960 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010961 /* let's try again later from this stream. We add ourselves into
10962 * this stream's users so that it can remove us upon termination.
10963 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010964 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010965 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010966 return 0;
10967 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010968 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010969 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010970 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010971 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010972 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010973 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010974 /* let's try again later from this stream. We add ourselves into
10975 * this stream's users so that it can remove us upon termination.
10976 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010977 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010978 return 0;
10979 }
10980
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010981 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010982 break;
10983
10984 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010985 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010986 }
10987
10988 appctx->st2 = STAT_ST_FIN;
10989 /* fall through */
10990
10991 default:
10992 appctx->st2 = STAT_ST_FIN;
10993 return 1;
10994 }
10995 return 0;
10996}
10997
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010998/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010999static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020011000{
William Lallemand32af2032016-10-29 18:09:35 +020011001 /* no parameter, shows only file list */
11002 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010011003 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020011004 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010011005 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011006 }
11007
11008 if (args[2][0] == '*') {
11009 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010011010 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020011011 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010011012 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020011013 if (!appctx->ctx.cli.p0)
11014 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020011015 }
William Lallemand32af2032016-10-29 18:09:35 +020011016 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010011017 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011018}
11019
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020011020static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020011021{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010011022 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020011023 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010011024
William Lallemand32af2032016-10-29 18:09:35 +020011025 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020011026 if (!*args[3] || !*args[4])
11027 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 +020011028
Willy Tarreauf5f26e82016-12-16 18:47:27 +010011029 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020011030 if (!ref)
11031 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020011032
Willy Tarreau1c913e42018-08-22 05:26:57 +020011033 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020011034 if (ret < 0)
11035 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010011036
Willy Tarreau1c913e42018-08-22 05:26:57 +020011037 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020011038 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
11039 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011040
Willy Tarreau9d008692019-08-09 11:21:01 +020011041 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020011042}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011043#endif
William Lallemand32af2032016-10-29 18:09:35 +020011044
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011045/*
11046 * Take an ssl_bind_conf structure and append the configuration line used to
11047 * create it in the buffer
11048 */
11049static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
11050{
11051 int space = 0;
11052
11053 if (conf == NULL)
11054 return;
11055
11056 chunk_appendf(buf, " [");
11057#ifdef OPENSSL_NPN_NEGOTIATED
11058 if (conf->npn_str) {
11059 int len = conf->npn_len;
11060 char *ptr = conf->npn_str;
11061 int comma = 0;
11062
11063 if (space) chunk_appendf(buf, " ");
11064 chunk_appendf(buf, "npn ");
11065 while (len) {
11066 unsigned short size;
11067
11068 size = *ptr;
11069 ptr++;
11070 if (comma)
11071 chunk_memcat(buf, ",", 1);
11072 chunk_memcat(buf, ptr, size);
11073 ptr += size;
11074 len -= size + 1;
11075 comma = 1;
11076 }
11077 chunk_memcat(buf, "", 1); /* finish with a \0 */
11078 space++;
11079 }
11080#endif
11081#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
11082 if (conf->alpn_str) {
11083 int len = conf->alpn_len;
11084 char *ptr = conf->alpn_str;
11085 int comma = 0;
11086
11087 if (space) chunk_appendf(buf, " ");
11088 chunk_appendf(buf, "alpn ");
11089 while (len) {
11090 unsigned short size;
11091
11092 size = *ptr;
11093 ptr++;
11094 if (comma)
11095 chunk_memcat(buf, ",", 1);
11096 chunk_memcat(buf, ptr, size);
11097 ptr += size;
11098 len -= size + 1;
11099 comma = 1;
11100 }
11101 chunk_memcat(buf, "", 1); /* finish with a \0 */
11102 space++;
11103 }
11104#endif
11105 /* verify */
11106 {
11107 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
11108 if (space) chunk_appendf(buf, " ");
11109 chunk_appendf(buf, "verify none");
11110 space++;
11111 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
11112 if (space) chunk_appendf(buf, " ");
11113 chunk_appendf(buf, "verify optional");
11114 space++;
11115 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
11116 if (space) chunk_appendf(buf, " ");
11117 chunk_appendf(buf, "verify required");
11118 space++;
11119 }
11120 }
11121
11122 if (conf->no_ca_names) {
11123 if (space) chunk_appendf(buf, " ");
11124 chunk_appendf(buf, "no-ca-names");
11125 space++;
11126 }
11127
11128 if (conf->early_data) {
11129 if (space) chunk_appendf(buf, " ");
11130 chunk_appendf(buf, "allow-0rtt");
11131 space++;
11132 }
11133 if (conf->ca_file) {
11134 if (space) chunk_appendf(buf, " ");
11135 chunk_appendf(buf, "ca-file %s", conf->ca_file);
11136 space++;
11137 }
11138 if (conf->crl_file) {
11139 if (space) chunk_appendf(buf, " ");
11140 chunk_appendf(buf, "crl-file %s", conf->crl_file);
11141 space++;
11142 }
11143 if (conf->ciphers) {
11144 if (space) chunk_appendf(buf, " ");
11145 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11146 space++;
11147 }
11148#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11149 if (conf->ciphersuites) {
11150 if (space) chunk_appendf(buf, " ");
11151 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11152 space++;
11153 }
11154#endif
11155 if (conf->curves) {
11156 if (space) chunk_appendf(buf, " ");
11157 chunk_appendf(buf, "curves %s", conf->curves);
11158 space++;
11159 }
11160 if (conf->ecdhe) {
11161 if (space) chunk_appendf(buf, " ");
11162 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11163 space++;
11164 }
11165
11166 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11167 /* XXX: this part need to be revamp so we don't dump the default settings */
11168 if (conf->ssl_methods.min) {
11169 if (space) chunk_appendf(buf, " ");
11170 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11171 space++;
11172 }
11173
11174 if (conf->ssl_methods.max) {
11175 if (space) chunk_appendf(buf, " ");
11176 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11177 space++;
11178 }
11179
William Lallemand58a52222020-04-02 18:11:47 +020011180 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011181
11182 return;
11183}
11184
11185/* dump a list of filters */
11186static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11187{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011188 int i;
11189
11190 if (!entry->fcount)
11191 return;
11192
11193 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011194 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011195 }
11196 return;
11197}
11198
11199/* CLI IO handler for '(show|dump) ssl crt-list' */
11200static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11201{
11202 struct buffer *trash = alloc_trash_chunk();
11203 struct stream_interface *si = appctx->owner;
11204 struct ebmb_node *lnode;
11205
11206 if (trash == NULL)
11207 return 1;
11208
11209 /* dump the list of crt-lists */
11210 lnode = appctx->ctx.cli.p1;
11211 if (lnode == NULL)
11212 lnode = ebmb_first(&crtlists_tree);
11213 while (lnode) {
11214 chunk_appendf(trash, "%s\n", lnode->key);
11215 if (ci_putchk(si_ic(si), trash) == -1) {
11216 si_rx_room_blk(si);
11217 goto yield;
11218 }
11219 lnode = ebmb_next(lnode);
11220 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011221 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011222 return 1;
11223yield:
11224 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011225 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011226 return 0;
11227}
11228
11229/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11230static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11231{
11232 struct buffer *trash = alloc_trash_chunk();
11233 struct crtlist *crtlist;
11234 struct stream_interface *si = appctx->owner;
11235 struct crtlist_entry *entry;
11236
11237 if (trash == NULL)
11238 return 1;
11239
11240 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11241
11242 entry = appctx->ctx.cli.p1;
11243 if (entry == NULL) {
11244 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11245 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11246 if (ci_putchk(si_ic(si), trash) == -1) {
11247 si_rx_room_blk(si);
11248 goto yield;
11249 }
11250 }
11251
11252 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11253 struct ckch_store *store;
11254 const char *filename;
11255
11256 store = entry->node.key;
11257 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011258 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011259 if (appctx->ctx.cli.i0 == 's') /* show */
11260 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011261 dump_crtlist_sslconf(trash, entry->ssl_conf);
11262 dump_crtlist_filters(trash, entry);
11263 chunk_appendf(trash, "\n");
11264
11265 if (ci_putchk(si_ic(si), trash) == -1) {
11266 si_rx_room_blk(si);
11267 goto yield;
11268 }
11269 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011270 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011271 return 1;
11272yield:
11273 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011274 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011275 return 0;
11276}
11277
11278/* CLI argument parser for '(show|dump) ssl crt-list' */
11279static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11280{
11281 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011282 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011283 int mode;
11284
11285 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11286 return 1;
11287
11288 appctx->ctx.cli.p0 = NULL;
11289 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011290
William Lallemandc69f02d2020-04-06 19:07:03 +020011291 if (*args[3] && !strcmp(args[3], "-n")) {
11292 mode = 's';
11293 filename = args[4];
11294 } else {
11295 mode = 'd';
11296 filename = args[3];
11297 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011298
William Lallemandc69f02d2020-04-06 19:07:03 +020011299 if (mode == 's' && !*args[4])
11300 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11301
11302 if (filename && *filename) {
11303 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011304 if (lnode == NULL)
11305 return cli_err(appctx, "didn't find the specified filename\n");
11306
11307 appctx->ctx.cli.p0 = lnode;
11308 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11309 }
11310 appctx->ctx.cli.i0 = mode;
11311
11312 return 0;
11313}
William Lallemand44b35322019-10-17 16:28:40 +020011314
William Lallemande67c80b2020-03-25 14:42:37 +010011315/* release function of the "add ssl crt-list' command, free things and unlock
11316 the spinlock */
11317static void cli_release_add_crtlist(struct appctx *appctx)
11318{
11319 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11320
11321 if (appctx->st2 != SETCERT_ST_FIN) {
11322 struct ckch_inst *inst, *inst_s;
11323 /* upon error free the ckch_inst and everything inside */
11324 ebpt_delete(&entry->node);
11325 LIST_DEL(&entry->by_crtlist);
11326 LIST_DEL(&entry->by_ckch_store);
11327
11328 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +020011329 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011330 }
11331 crtlist_free_filters(entry->filters);
11332 ssl_sock_free_ssl_conf(entry->ssl_conf);
11333 free(entry->ssl_conf);
11334 free(entry);
11335 }
11336
11337 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11338}
11339
11340
11341/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11342 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11343 *
11344 * The logic is the same as the "commit ssl cert" command but without the
11345 * freeing of the old structures, because there are none.
11346 */
11347static int cli_io_handler_add_crtlist(struct appctx *appctx)
11348{
11349 struct bind_conf_list *bind_conf_node;
11350 struct stream_interface *si = appctx->owner;
11351 struct crtlist *crtlist = appctx->ctx.cli.p0;
11352 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11353 struct ckch_store *store = entry->node.key;
11354 struct buffer *trash = alloc_trash_chunk();
11355 struct ckch_inst *new_inst;
11356 char *err = NULL;
11357 int i = 0;
11358 int errcode = 0;
11359
11360 if (trash == NULL)
11361 goto error;
11362
11363 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11364 * created.
11365 */
11366 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11367 goto error;
11368
11369 while (1) {
11370 switch (appctx->st2) {
11371 case SETCERT_ST_INIT:
11372 /* This state just print the update message */
11373 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11374 if (ci_putchk(si_ic(si), trash) == -1) {
11375 si_rx_room_blk(si);
11376 goto yield;
11377 }
11378 appctx->st2 = SETCERT_ST_GEN;
11379 /* fallthrough */
11380 case SETCERT_ST_GEN:
11381 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11382 if (bind_conf_node == NULL)
11383 bind_conf_node = crtlist->bind_conf;
11384 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11385 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11386 struct sni_ctx *sni;
11387
11388 /* yield every 10 generations */
11389 if (i > 10) {
11390 appctx->ctx.cli.p2 = bind_conf_node;
11391 goto yield;
11392 }
11393
11394 /* we don't support multi-cert bundles, only simple ones */
11395 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11396 if (errcode & ERR_CODE)
11397 goto error;
11398
11399 /* we need to initialize the SSL_CTX generated */
11400 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11401 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11402 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11403 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11404 if (errcode & ERR_CODE)
11405 goto error;
11406 }
11407 }
11408 /* display one dot for each new instance */
11409 chunk_appendf(trash, ".");
11410 i++;
11411 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11412 }
11413 appctx->st2 = SETCERT_ST_INSERT;
11414 /* fallthrough */
11415 case SETCERT_ST_INSERT:
11416 /* insert SNIs in bind_conf */
11417 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11418 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11419 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11420 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11421 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011422 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011423 appctx->st2 = SETCERT_ST_FIN;
11424 goto end;
11425 }
11426 }
11427
11428end:
11429 chunk_appendf(trash, "\n");
11430 if (errcode & ERR_WARN)
11431 chunk_appendf(trash, "%s", err);
11432 chunk_appendf(trash, "Success!\n");
11433 if (ci_putchk(si_ic(si), trash) == -1)
11434 si_rx_room_blk(si);
11435 free_trash_chunk(trash);
11436 /* success: call the release function and don't come back */
11437 return 1;
11438yield:
11439 /* store the state */
11440 if (ci_putchk(si_ic(si), trash) == -1)
11441 si_rx_room_blk(si);
11442 free_trash_chunk(trash);
11443 si_rx_endp_more(si); /* let's come back later */
11444 return 0; /* should come back */
11445
11446error:
11447 /* spin unlock and free are done in the release function */
11448 if (trash) {
11449 chunk_appendf(trash, "\n%sFailed!\n", err);
11450 if (ci_putchk(si_ic(si), trash) == -1)
11451 si_rx_room_blk(si);
11452 free_trash_chunk(trash);
11453 }
11454 /* error: call the release function and don't come back */
11455 return 1;
11456}
11457
11458
11459/*
11460 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011461 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011462 */
11463static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11464{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011465 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011466 struct ckch_store *store;
11467 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011468 char path[MAXPATHLEN+1];
11469 char *crtlist_path;
11470 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011471 struct ebmb_node *eb;
11472 struct ebpt_node *inserted;
11473 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011474 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011475
11476 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11477 return 1;
11478
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011479 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011480 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11481
11482 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011483
11484 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11485 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11486
11487 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11488 if (!eb) {
11489 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11490 goto error;
11491 }
11492 crtlist = ebmb_entry(eb, struct crtlist, node);
11493
William Lallemande718dfb2020-04-10 11:09:25 +020011494 entry = crtlist_entry_new();
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011495 if (entry == NULL) {
11496 memprintf(&err, "Not enough memory!");
11497 goto error;
11498 }
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011499
11500 if (payload) {
11501 char *lf;
11502
11503 lf = strrchr(payload, '\n');
11504 if (lf) {
11505 memprintf(&err, "only one line of payload is supported!");
11506 goto error;
11507 }
11508 /* cert_path is filled here */
11509 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11510 if (cfgerr & ERR_CODE)
11511 goto error;
11512 } else {
11513 cert_path = args[4];
11514 }
11515
11516 if (!cert_path) {
11517 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11518 cfgerr |= ERR_ALERT | ERR_FATAL;
11519 goto error;
11520 }
11521
William Lallemand916d0b52020-04-21 18:29:12 +020011522 if (eb_gettag(crtlist->entries.b[EB_RGHT])) {
11523 char *slash;
11524
11525 slash = strrchr(cert_path, '/');
11526 if (!slash) {
11527 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
11528 goto error;
11529 }
11530 /* temporary replace / by 0 to do an strcmp */
11531 *slash = '\0';
11532 if (strcmp(cert_path, (char*)crtlist->node.key) != 0) {
11533 *slash = '/';
11534 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
11535 goto error;
11536 }
11537 *slash = '/';
11538 }
11539
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011540 if (*cert_path != '/' && global_ssl.crt_base) {
11541 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11542 memprintf(&err, "'%s' : path too long", cert_path);
11543 cfgerr |= ERR_ALERT | ERR_FATAL;
11544 goto error;
11545 }
11546 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11547 cert_path = path;
11548 }
11549
William Lallemande67c80b2020-03-25 14:42:37 +010011550 store = ckchs_lookup(cert_path);
11551 if (store == NULL) {
11552 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11553 goto error;
11554 }
William Lallemand36ccc392020-04-08 10:57:24 +020011555 if (store->multi) {
11556 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);
11557 goto error;
11558 }
William Lallemande67c80b2020-03-25 14:42:37 +010011559 if (store->ckch == NULL || store->ckch->cert == NULL) {
11560 memprintf(&err, "certificate '%s' is empty!", cert_path);
11561 goto error;
11562 }
11563
William Lallemande67c80b2020-03-25 14:42:37 +010011564 /* check if it's possible to insert this new crtlist_entry */
11565 entry->node.key = store;
11566 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11567 if (inserted != &entry->node) {
11568 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011569 goto error;
11570 }
11571
William Lallemandb74d5642020-04-21 16:54:19 +020011572 /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */
11573 if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) {
11574 memprintf(&err, "this is a directory, SSL configuration and filters are not allowed");
11575 goto error;
11576 }
11577
William Lallemande67c80b2020-03-25 14:42:37 +010011578 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
William Lallemande67c80b2020-03-25 14:42:37 +010011579 entry->crtlist = crtlist;
11580 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11581
11582 appctx->st2 = SETCERT_ST_INIT;
11583 appctx->ctx.cli.p0 = crtlist;
11584 appctx->ctx.cli.p1 = entry;
11585
11586 /* unlock is done in the release handler */
11587 return 0;
11588
11589error:
William Lallemande718dfb2020-04-10 11:09:25 +020011590 crtlist_entry_free(entry);
William Lallemande67c80b2020-03-25 14:42:37 +010011591 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11592 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11593 return cli_dynerr(appctx, err);
11594}
11595
William Lallemand0a9b9412020-04-06 17:43:05 +020011596/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11597static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11598{
11599 struct ckch_store *store;
11600 char *err = NULL;
11601 char *crtlist_path, *cert_path;
11602 struct ebmb_node *ebmb;
11603 struct ebpt_node *ebpt;
11604 struct crtlist *crtlist;
11605 struct crtlist_entry *entry = NULL;
11606 struct ckch_inst *inst, *inst_s;
11607 int linenum = 0;
11608 char *colons;
11609
11610 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11611 return 1;
11612
11613 if (!*args[3] || !*args[4])
11614 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11615
William Lallemand463b5242020-04-08 10:30:44 +020011616 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11617 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11618
William Lallemand0a9b9412020-04-06 17:43:05 +020011619 crtlist_path = args[3];
11620 cert_path = args[4];
11621
11622 colons = strchr(cert_path, ':');
11623 if (colons) {
11624 char *endptr;
11625
11626 linenum = strtol(colons + 1, &endptr, 10);
11627 if (colons + 1 == endptr || *endptr != '\0') {
11628 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11629 goto error;
11630 }
11631 *colons = '\0';
11632 }
11633 /* look for crtlist */
11634 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11635 if (!ebmb) {
11636 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11637 goto error;
11638 }
11639 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11640
11641 /* look for store */
11642 store = ckchs_lookup(cert_path);
11643 if (store == NULL) {
11644 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11645 goto error;
11646 }
William Lallemand36ccc392020-04-08 10:57:24 +020011647 if (store->multi) {
11648 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);
11649 goto error;
11650 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011651 if (store->ckch == NULL || store->ckch->cert == NULL) {
11652 memprintf(&err, "certificate '%s' is empty!", cert_path);
11653 goto error;
11654 }
11655
11656 ebpt = ebpt_lookup(&crtlist->entries, store);
11657 if (!ebpt) {
11658 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11659 goto error;
11660 }
11661
11662 /* list the line number of entries for errors in err, and select the right ebpt */
11663 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11664 struct crtlist_entry *tmp;
11665
11666 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11667 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11668
11669 /* select the entry we wanted */
11670 if (linenum == 0 || tmp->linenum == linenum) {
11671 if (!entry)
11672 entry = tmp;
11673 }
11674 }
11675
11676 /* we didn't found the specified entry */
11677 if (!entry) {
11678 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);
11679 goto error;
11680 }
11681
11682 /* we didn't specified a line number but there were several entries */
11683 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11684 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11685 goto error;
11686 }
11687
11688 /* upon error free the ckch_inst and everything inside */
11689
11690 ebpt_delete(&entry->node);
11691 LIST_DEL(&entry->by_crtlist);
11692 LIST_DEL(&entry->by_ckch_store);
11693
11694 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11695 struct sni_ctx *sni, *sni_s;
11696
11697 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11698 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11699 ebmb_delete(&sni->name);
11700 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011701 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011702 free(sni);
11703 }
11704 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11705 LIST_DEL(&inst->by_ckchs);
11706 free(inst);
11707 }
11708
11709 crtlist_free_filters(entry->filters);
11710 ssl_sock_free_ssl_conf(entry->ssl_conf);
11711 free(entry->ssl_conf);
11712 free(entry);
11713
11714 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11715 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11716 return cli_dynmsg(appctx, LOG_NOTICE, err);
11717
11718error:
11719 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11720 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11721 return cli_dynerr(appctx, err);
11722}
William Lallemande67c80b2020-03-25 14:42:37 +010011723
William Lallemand44b35322019-10-17 16:28:40 +020011724/* Type of SSL payloads that can be updated over the CLI */
11725
11726enum {
11727 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011728 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011729#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011730 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011731#endif
William Lallemand44b35322019-10-17 16:28:40 +020011732 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011733#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011734 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011735#endif
William Lallemand44b35322019-10-17 16:28:40 +020011736 CERT_TYPE_MAX,
11737};
11738
11739struct {
11740 const char *ext;
11741 int type;
11742 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11743 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011744} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011745 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011746 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011747#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011748 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011749#endif
11750#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011751 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011752#endif
William Lallemand44b35322019-10-17 16:28:40 +020011753 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011754 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011755};
11756
William Lallemand8f840d72019-10-23 10:53:05 +020011757
William Lallemandd4f946c2019-12-05 10:26:40 +010011758/* release function of the `show ssl cert' command */
11759static void cli_release_show_cert(struct appctx *appctx)
11760{
11761 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11762}
11763
11764/* IO handler of "show ssl cert <filename>" */
11765static int cli_io_handler_show_cert(struct appctx *appctx)
11766{
11767 struct buffer *trash = alloc_trash_chunk();
11768 struct ebmb_node *node;
11769 struct stream_interface *si = appctx->owner;
11770 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011771
11772 if (trash == NULL)
11773 return 1;
11774
11775 if (!appctx->ctx.ssl.old_ckchs) {
11776 if (ckchs_transaction.old_ckchs) {
11777 ckchs = ckchs_transaction.old_ckchs;
11778 chunk_appendf(trash, "# transaction\n");
11779 if (!ckchs->multi) {
11780 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011781#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011782 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011783 int n;
11784
William Lallemandd4f946c2019-12-05 10:26:40 +010011785 chunk_appendf(trash, "*%s:", ckchs->path);
11786 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11787 if (ckchs->ckch[n].cert)
11788 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11789 }
11790 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011791#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011792 }
11793 }
11794 }
11795
11796 if (!appctx->ctx.cli.p0) {
11797 chunk_appendf(trash, "# filename\n");
11798 node = ebmb_first(&ckchs_tree);
11799 } else {
11800 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11801 }
11802 while (node) {
11803 ckchs = ebmb_entry(node, struct ckch_store, node);
11804 if (!ckchs->multi) {
11805 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011806#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011807 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011808 int n;
11809
William Lallemandd4f946c2019-12-05 10:26:40 +010011810 chunk_appendf(trash, "%s:", ckchs->path);
11811 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11812 if (ckchs->ckch[n].cert)
11813 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11814 }
11815 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011816#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011817 }
11818
11819 node = ebmb_next(node);
11820 if (ci_putchk(si_ic(si), trash) == -1) {
11821 si_rx_room_blk(si);
11822 goto yield;
11823 }
11824 }
11825
11826 appctx->ctx.cli.p0 = NULL;
11827 free_trash_chunk(trash);
11828 return 1;
11829yield:
11830
11831 free_trash_chunk(trash);
11832 appctx->ctx.cli.p0 = ckchs;
11833 return 0; /* should come back */
11834}
11835
11836/* IO handler of the details "show ssl cert <filename>" */
11837static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11838{
11839 struct stream_interface *si = appctx->owner;
11840 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11841 struct buffer *out = alloc_trash_chunk();
11842 struct buffer *tmp = alloc_trash_chunk();
11843 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011844 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011845 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011846 int write = -1;
11847 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011848 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011849
11850 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011851 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011852
11853 if (!ckchs->multi) {
11854 chunk_appendf(out, "Filename: ");
11855 if (ckchs == ckchs_transaction.new_ckchs)
11856 chunk_appendf(out, "*");
11857 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011858
William Lallemand59c16fc2020-03-19 20:26:02 +010011859 chunk_appendf(out, "Status: ");
11860 if (ckchs->ckch->cert == NULL)
11861 chunk_appendf(out, "Empty\n");
11862 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11863 chunk_appendf(out, "Unused\n");
11864 else
11865 chunk_appendf(out, "Used\n");
11866
William Lallemandea987ed2020-03-19 16:48:33 +010011867 if (ckchs->ckch->cert == NULL)
11868 goto end;
11869
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011870 chain = ckchs->ckch->chain;
11871 if (chain == NULL) {
11872 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011873 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011874 if (issuer) {
11875 chain = issuer->chain;
11876 chunk_appendf(out, "Chain Filename: ");
11877 chunk_appendf(out, "%s\n", issuer->path);
11878 }
11879 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011880 chunk_appendf(out, "Serial: ");
11881 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11882 goto end;
11883 dump_binary(out, tmp->area, tmp->data);
11884 chunk_appendf(out, "\n");
11885
11886 chunk_appendf(out, "notBefore: ");
11887 chunk_reset(tmp);
11888 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11889 goto end;
11890 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11891 goto end;
11892 write = BIO_read(bio, tmp->area, tmp->size-1);
11893 tmp->area[write] = '\0';
11894 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011895 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011896 chunk_appendf(out, "%s\n", tmp->area);
11897
11898 chunk_appendf(out, "notAfter: ");
11899 chunk_reset(tmp);
11900 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11901 goto end;
11902 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11903 goto end;
11904 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11905 goto end;
11906 tmp->area[write] = '\0';
11907 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011908 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011909 chunk_appendf(out, "%s\n", tmp->area);
11910
William Lallemandd4f946c2019-12-05 10:26:40 +010011911#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11912 chunk_appendf(out, "Subject Alternative Name: ");
11913 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11914 goto end;
11915 *(out->area + out->data) = '\0';
11916 chunk_appendf(out, "\n");
11917#endif
11918 chunk_reset(tmp);
11919 chunk_appendf(out, "Algorithm: ");
11920 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11921 goto end;
11922 chunk_appendf(out, "%s\n", tmp->area);
11923
11924 chunk_reset(tmp);
11925 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011926 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011927 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011928 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011929 dump_binary(out, tmp->area, tmp->data);
11930 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011931
William Lallemanda90e5932020-02-25 14:07:58 +010011932 chunk_appendf(out, "Subject: ");
11933 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11934 goto end;
11935 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11936 goto end;
11937 *(tmp->area + tmp->data) = '\0';
11938 chunk_appendf(out, "%s\n", tmp->area);
11939
11940 chunk_appendf(out, "Issuer: ");
11941 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11942 goto end;
11943 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11944 goto end;
11945 *(tmp->area + tmp->data) = '\0';
11946 chunk_appendf(out, "%s\n", tmp->area);
11947
William Lallemand35f4a9d2020-02-25 11:56:32 +010011948 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011949 for (i = 0; i < sk_X509_num(chain); i++) {
11950 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011951
William Lallemandbb7288a2020-02-25 14:04:33 +010011952 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011953 if ((name = X509_get_subject_name(ca)) == NULL)
11954 goto end;
11955 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11956 goto end;
11957 *(tmp->area + tmp->data) = '\0';
11958 chunk_appendf(out, "%s\n", tmp->area);
11959
William Lallemandbb7288a2020-02-25 14:04:33 +010011960 chunk_appendf(out, "Chain Issuer: ");
11961 if ((name = X509_get_issuer_name(ca)) == NULL)
11962 goto end;
11963 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11964 goto end;
11965 *(tmp->area + tmp->data) = '\0';
11966 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011967 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011968 }
11969
William Lallemandea987ed2020-03-19 16:48:33 +010011970end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011971 if (ci_putchk(si_ic(si), out) == -1) {
11972 si_rx_room_blk(si);
11973 goto yield;
11974 }
11975
William Lallemand18eeb8e2020-03-20 14:42:36 +010011976end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011977 if (bio)
11978 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011979 free_trash_chunk(tmp);
11980 free_trash_chunk(out);
11981 return 1;
11982yield:
11983 free_trash_chunk(tmp);
11984 free_trash_chunk(out);
11985 return 0; /* should come back */
11986}
11987
11988/* parsing function for 'show ssl cert [certfile]' */
11989static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11990{
11991 struct ckch_store *ckchs;
11992
11993 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11994 return cli_err(appctx, "Can't allocate memory!\n");
11995
11996 /* The operations on the CKCH architecture are locked so we can
11997 * manipulate ckch_store and ckch_inst */
11998 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11999 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
12000
12001 /* check if there is a certificate to lookup */
12002 if (*args[3]) {
12003 if (*args[3] == '*') {
12004 if (!ckchs_transaction.new_ckchs)
12005 goto error;
12006
12007 ckchs = ckchs_transaction.new_ckchs;
12008
12009 if (strcmp(args[3] + 1, ckchs->path))
12010 goto error;
12011
12012 } else {
12013 if ((ckchs = ckchs_lookup(args[3])) == NULL)
12014 goto error;
12015
12016 }
12017
12018 if (ckchs->multi)
12019 goto error;
12020
12021 appctx->ctx.cli.p0 = ckchs;
12022 /* use the IO handler that shows details */
12023 appctx->io_handler = cli_io_handler_show_cert_detail;
12024 }
12025
12026 return 0;
12027
12028error:
12029 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12030 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
12031}
12032
William Lallemand430413e2019-10-28 14:30:47 +010012033/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012034static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020012035{
12036 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012037
William Lallemand430413e2019-10-28 14:30:47 +010012038 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020012039
William Lallemand430413e2019-10-28 14:30:47 +010012040 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020012041 /* 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 +010012042 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012043
William Lallemandbeea2a42019-10-30 17:45:33 +010012044 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020012045 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020012046 }
12047}
12048
12049
12050/*
12051 * This function tries to create the new ckch_inst and their SNIs
12052 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012053static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020012054{
12055 struct stream_interface *si = appctx->owner;
12056 int y = 0;
12057 char *err = NULL;
12058 int errcode = 0;
12059 struct ckch_store *old_ckchs, *new_ckchs = NULL;
12060 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020012061 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012062 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020012063 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020012064
William Lallemand33cc76f2019-10-31 11:43:45 +010012065 if (trash == NULL)
12066 goto error;
12067
William Lallemand8f840d72019-10-23 10:53:05 +020012068 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
12069 goto error;
12070
William Lallemand430413e2019-10-28 14:30:47 +010012071 while (1) {
12072 switch (appctx->st2) {
12073 case SETCERT_ST_INIT:
12074 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012075 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010012076 if (ci_putchk(si_ic(si), trash) == -1) {
12077 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020012078 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010012079 }
12080 appctx->st2 = SETCERT_ST_GEN;
12081 /* fallthrough */
12082 case SETCERT_ST_GEN:
12083 /*
12084 * This state generates the ckch instances with their
12085 * sni_ctxs and SSL_CTX.
12086 *
William Lallemand430413e2019-10-28 14:30:47 +010012087 * Since the SSL_CTX generation can be CPU consumer, we
12088 * yield every 10 instances.
12089 */
William Lallemand8f840d72019-10-23 10:53:05 +020012090
William Lallemandbeea2a42019-10-30 17:45:33 +010012091 old_ckchs = appctx->ctx.ssl.old_ckchs;
12092 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012093
William Lallemandbeea2a42019-10-30 17:45:33 +010012094 if (!new_ckchs)
12095 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020012096
William Lallemandbeea2a42019-10-30 17:45:33 +010012097 /* get the next ckchi to regenerate */
12098 ckchi = appctx->ctx.ssl.next_ckchi;
12099 /* we didn't start yet, set it to the first elem */
12100 if (ckchi == NULL)
12101 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020012102
William Lallemandbeea2a42019-10-30 17:45:33 +010012103 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
12104 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
12105 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010012106 char **sni_filter = NULL;
12107 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020012108
William Lallemandbeea2a42019-10-30 17:45:33 +010012109 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
12110 if (y >= 10) {
12111 /* save the next ckchi to compute */
12112 appctx->ctx.ssl.next_ckchi = ckchi;
12113 goto yield;
12114 }
William Lallemandcaa16192020-04-08 16:29:15 +020012115
12116 if (ckchi->crtlist_entry) {
12117 sni_filter = ckchi->crtlist_entry->filters;
12118 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010012119 }
William Lallemand38df1c82019-12-04 15:39:35 +010012120
William Lallemandbeea2a42019-10-30 17:45:33 +010012121 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010012122 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 +010012123 else
William Lallemand38df1c82019-12-04 15:39:35 +010012124 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
12125
William Lallemandbeea2a42019-10-30 17:45:33 +010012126 if (errcode & ERR_CODE)
12127 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020012128
William Lallemand21724f02019-11-04 17:56:13 +010012129 /* if the previous ckchi was used as the default */
12130 if (ckchi->is_default)
12131 new_inst->is_default = 1;
12132
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012133 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010012134 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
12135 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012136 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 +010012137 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
12138 if (errcode & ERR_CODE)
12139 goto error;
12140 }
12141 }
12142
12143
William Lallemandbeea2a42019-10-30 17:45:33 +010012144 /* display one dot per new instance */
12145 chunk_appendf(trash, ".");
12146 /* link the new ckch_inst to the duplicate */
12147 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
12148 y++;
12149 }
William Lallemand430413e2019-10-28 14:30:47 +010012150 appctx->st2 = SETCERT_ST_INSERT;
12151 /* fallthrough */
12152 case SETCERT_ST_INSERT:
12153 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012154
William Lallemandbeea2a42019-10-30 17:45:33 +010012155 old_ckchs = appctx->ctx.ssl.old_ckchs;
12156 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012157
William Lallemandbeea2a42019-10-30 17:45:33 +010012158 if (!new_ckchs)
12159 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012160
William Lallemand90afe902020-03-30 19:29:45 +020012161 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12162 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12163 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12164 ebpt_delete(&entry->node);
12165 /* change the ptr and reinsert the node */
12166 entry->node.key = new_ckchs;
12167 ebpt_insert(&entry->crtlist->entries, &entry->node);
12168 }
12169
William Lallemand21724f02019-11-04 17:56:13 +010012170 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012171 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12172 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12173 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12174 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12175 }
William Lallemand8f840d72019-10-23 10:53:05 +020012176
William Lallemandbeea2a42019-10-30 17:45:33 +010012177 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12178 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
Willy Tarreaua6cd0782020-05-01 11:38:39 +020012179 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
William Lallemandd5e93772020-04-09 17:12:16 +020012180
12181 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020012182 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020012183 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010012184 }
William Lallemand8f840d72019-10-23 10:53:05 +020012185
William Lallemandbeea2a42019-10-30 17:45:33 +010012186 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020012187 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010012188 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012189 appctx->st2 = SETCERT_ST_FIN;
12190 /* fallthrough */
12191 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012192 /* we achieved the transaction, we can set everything to NULL */
12193 free(ckchs_transaction.path);
12194 ckchs_transaction.path = NULL;
12195 ckchs_transaction.new_ckchs = NULL;
12196 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012197 goto end;
12198 }
William Lallemand8f840d72019-10-23 10:53:05 +020012199 }
William Lallemand430413e2019-10-28 14:30:47 +010012200end:
William Lallemand8f840d72019-10-23 10:53:05 +020012201
William Lallemanded442432019-11-21 16:41:07 +010012202 chunk_appendf(trash, "\n");
12203 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012204 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012205 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012206 if (ci_putchk(si_ic(si), trash) == -1)
12207 si_rx_room_blk(si);
12208 free_trash_chunk(trash);
12209 /* success: call the release function and don't come back */
12210 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012211yield:
12212 /* store the state */
12213 if (ci_putchk(si_ic(si), trash) == -1)
12214 si_rx_room_blk(si);
12215 free_trash_chunk(trash);
12216 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012217 return 0; /* should come back */
12218
12219error:
12220 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012221 if (trash) {
12222 chunk_appendf(trash, "\n%sFailed!\n", err);
12223 if (ci_putchk(si_ic(si), trash) == -1)
12224 si_rx_room_blk(si);
12225 free_trash_chunk(trash);
12226 }
William Lallemand430413e2019-10-28 14:30:47 +010012227 /* error: call the release function and don't come back */
12228 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012229}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012230
12231/*
12232 * Parsing function of 'commit ssl cert'
12233 */
12234static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12235{
12236 char *err = NULL;
12237
William Lallemand230662a2019-12-03 13:32:54 +010012238 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12239 return 1;
12240
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012241 if (!*args[3])
12242 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12243
12244 /* The operations on the CKCH architecture are locked so we can
12245 * manipulate ckch_store and ckch_inst */
12246 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12247 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12248
12249 if (!ckchs_transaction.path) {
12250 memprintf(&err, "No ongoing transaction! !\n");
12251 goto error;
12252 }
12253
12254 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12255 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12256 goto error;
12257 }
12258
William Lallemand4c5adbf2020-02-24 14:23:22 +010012259#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12260 if (ckchs_transaction.new_ckchs->multi) {
12261 int n;
12262
12263 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12264 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)) {
12265 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12266 goto error;
12267 }
12268 }
12269 } else
12270#endif
12271 {
12272 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12273 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12274 goto error;
12275 }
12276 }
12277
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012278 /* init the appctx structure */
12279 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012280 appctx->ctx.ssl.next_ckchi = NULL;
12281 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12282 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12283
12284 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12285 return 0;
12286
12287error:
12288
12289 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12290 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12291
12292 return cli_dynerr(appctx, err);
12293}
12294
William Lallemand8f840d72019-10-23 10:53:05 +020012295/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012296 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012297 */
William Lallemand150bfa82019-09-19 17:12:49 +020012298static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12299{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012300 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012301 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012302 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012303 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012304 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012305 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012306 char *end;
12307 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012308 struct cert_key_and_chain *ckch;
12309 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012310
William Lallemand230662a2019-12-03 13:32:54 +010012311 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12312 return 1;
12313
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012314 if ((buf = alloc_trash_chunk()) == NULL)
12315 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012316
12317 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012318 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012319
12320 /* The operations on the CKCH architecture are locked so we can
12321 * manipulate ckch_store and ckch_inst */
12322 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12323 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12324
William Lallemand8f840d72019-10-23 10:53:05 +020012325 if (!chunk_strcpy(buf, args[3])) {
12326 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12327 errcode |= ERR_ALERT | ERR_FATAL;
12328 goto end;
12329 }
12330
William Lallemand44b35322019-10-17 16:28:40 +020012331 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012332 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012333 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012334 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12335 *end = '\0';
12336 type = cert_exts[i].type;
12337 break;
12338 }
12339 }
12340
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012341 appctx->ctx.ssl.old_ckchs = NULL;
12342 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012343
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012344 /* if there is an ongoing transaction */
12345 if (ckchs_transaction.path) {
12346 /* 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 +020012347#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012348 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012349 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012350 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012351
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012352 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012353 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012354 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012355 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012356 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12357 bundle = j; /* keep the type of certificate so we insert it at the right place */
12358 *end = '\0'; /* it's a bundle let's end the string*/
12359 break;
12360 }
William Lallemand150bfa82019-09-19 17:12:49 +020012361 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012362 if (bundle < 0) {
12363 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);
12364 errcode |= ERR_ALERT | ERR_FATAL;
12365 goto end;
12366 }
12367 }
12368#endif
12369
12370 /* if there is an ongoing transaction, check if this is the same file */
12371 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12372 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12373 errcode |= ERR_ALERT | ERR_FATAL;
12374 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012375 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012376
12377 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12378
12379 } else {
12380 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12381
12382 /* lookup for the certificate in the tree:
12383 * check if this is used as a bundle AND as a unique certificate */
12384 for (i = 0; i < 2; i++) {
12385
12386 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12387 /* only the bundle name is in the tree and you should
12388 * never update a bundle name, only a filename */
12389 if (bundle < 0 && find_ckchs[i]->multi) {
12390 /* we tried to look for a non-bundle and we found a bundle */
12391 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12392 err ? err : "", args[3], args[3]);
12393 errcode |= ERR_ALERT | ERR_FATAL;
12394 goto end;
12395 }
William Lallemand3246d942019-11-04 14:02:11 +010012396 /* If we want a bundle but this is not a bundle
12397 * example: When you try to update <file>.rsa, but
12398 * <file> is a regular file */
12399 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12400 find_ckchs[i] = NULL;
12401 break;
12402 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012403 }
12404#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12405 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012406 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012407 int j;
12408
12409 /* check if it was used in a bundle by removing the
12410 * .dsa/.rsa/.ecdsa at the end of the filename */
12411 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012412 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012413 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12414 bundle = j; /* keep the type of certificate so we insert it at the right place */
12415 *end = '\0'; /* it's a bundle let's end the string*/
12416 break;
12417 }
12418 }
William Lallemand37031b82019-11-04 13:38:53 +010012419 if (bundle < 0) /* we didn't find a bundle extension */
12420 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012421 }
William Lallemand963b2e72019-10-14 11:38:36 +020012422#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012423 /* bundles are not supported here, so we don't need to lookup again */
12424 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012425#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012426 }
12427
12428 if (find_ckchs[0] && find_ckchs[1]) {
12429 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",
12430 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12431 errcode |= ERR_ALERT | ERR_FATAL;
12432 goto end;
12433 }
12434
12435 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012436 }
12437
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012438 if (!appctx->ctx.ssl.old_ckchs) {
12439 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012440 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012441 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012442 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012443 }
12444
William Lallemand8a7fdf02019-11-04 10:59:32 +010012445 if (!appctx->ctx.ssl.path) {
12446 /* this is a new transaction, set the path of the transaction */
12447 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12448 if (!appctx->ctx.ssl.path) {
12449 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12450 errcode |= ERR_ALERT | ERR_FATAL;
12451 goto end;
12452 }
12453 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012454
12455 old_ckchs = appctx->ctx.ssl.old_ckchs;
12456
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012457 /* duplicate the ckch store */
12458 new_ckchs = ckchs_dup(old_ckchs);
12459 if (!new_ckchs) {
12460 memprintf(&err, "%sCannot allocate memory!\n",
12461 err ? err : "");
12462 errcode |= ERR_ALERT | ERR_FATAL;
12463 goto end;
12464 }
12465
12466 if (!new_ckchs->multi)
12467 ckch = new_ckchs->ckch;
12468 else
12469 ckch = &new_ckchs->ckch[bundle];
12470
12471 /* appply the change on the duplicate */
12472 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12473 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12474 errcode |= ERR_ALERT | ERR_FATAL;
12475 goto end;
12476 }
12477
12478 appctx->ctx.ssl.new_ckchs = new_ckchs;
12479
12480 /* we succeed, we can save the ckchs in the transaction */
12481
12482 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012483 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012484 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12485 ckchs_transaction.path = appctx->ctx.ssl.path;
12486 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12487 } else {
12488 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12489
12490 }
12491
12492 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020012493 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012494
12495 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12496
12497
William Lallemand8f840d72019-10-23 10:53:05 +020012498 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012499
William Lallemand8f840d72019-10-23 10:53:05 +020012500end:
12501 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012502
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012503 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012504
William Lallemandba1c33f2020-04-08 17:55:45 +020012505 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012506 appctx->ctx.ssl.new_ckchs = NULL;
12507
12508 appctx->ctx.ssl.old_ckchs = NULL;
12509
12510 free(appctx->ctx.ssl.path);
12511 appctx->ctx.ssl.path = NULL;
12512
12513 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012514 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012515 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012516
12517 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12518 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012519 }
William Lallemand8f840d72019-10-23 10:53:05 +020012520 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012521}
12522
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012523/* parsing function of 'abort ssl cert' */
12524static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12525{
12526 char *err = NULL;
12527
William Lallemand230662a2019-12-03 13:32:54 +010012528 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12529 return 1;
12530
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012531 if (!*args[3])
12532 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12533
12534 /* The operations on the CKCH architecture are locked so we can
12535 * manipulate ckch_store and ckch_inst */
12536 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12537 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12538
12539 if (!ckchs_transaction.path) {
12540 memprintf(&err, "No ongoing transaction!\n");
12541 goto error;
12542 }
12543
12544 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12545 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12546 goto error;
12547 }
12548
12549 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020012550 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012551 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020012552 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012553 ckchs_transaction.old_ckchs = NULL;
12554 free(ckchs_transaction.path);
12555 ckchs_transaction.path = NULL;
12556
12557 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12558
12559 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12560 return cli_dynmsg(appctx, LOG_NOTICE, err);
12561
12562error:
12563 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12564
12565 return cli_dynerr(appctx, err);
12566}
12567
William Lallemandea987ed2020-03-19 16:48:33 +010012568/* parsing function of 'new ssl cert' */
12569static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12570{
12571 struct ckch_store *store;
12572 char *err = NULL;
12573 char *path;
12574
12575 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12576 return 1;
12577
12578 if (!*args[3])
12579 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12580
12581 path = args[3];
12582
12583 /* The operations on the CKCH architecture are locked so we can
12584 * manipulate ckch_store and ckch_inst */
12585 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12586 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12587
12588 store = ckchs_lookup(path);
12589 if (store != NULL) {
12590 memprintf(&err, "Certificate '%s' already exists!\n", path);
12591 store = NULL; /* we don't want to free it */
12592 goto error;
12593 }
William Lallemand8a874e42020-04-09 10:32:53 +020012594 /* we won't support multi-certificate bundle here */
12595 store = ckch_store_new(path, 1);
William Lallemandea987ed2020-03-19 16:48:33 +010012596 if (!store) {
12597 memprintf(&err, "unable to allocate memory.\n");
12598 goto error;
12599 }
William Lallemandea987ed2020-03-19 16:48:33 +010012600
12601 /* insert into the ckchs tree */
William Lallemandea987ed2020-03-19 16:48:33 +010012602 ebst_insert(&ckchs_tree, &store->node);
12603 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12604
12605 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12606 return cli_dynmsg(appctx, LOG_NOTICE, err);
12607error:
12608 free(store);
12609 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12610 return cli_dynerr(appctx, err);
12611}
12612
William Lallemand419e6342020-04-08 12:05:39 +020012613/* parsing function of 'del ssl cert' */
12614static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12615{
12616 struct ckch_store *store;
12617 char *err = NULL;
12618 char *filename;
12619
12620 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12621 return 1;
12622
12623 if (!*args[3])
12624 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12625
12626 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12627 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12628
12629 filename = args[3];
12630
12631 store = ckchs_lookup(filename);
12632 if (store == NULL) {
12633 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12634 goto error;
12635 }
12636 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12637 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12638 goto error;
12639 }
12640
12641 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020012642 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020012643
12644 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12645
12646 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12647 return cli_dynmsg(appctx, LOG_NOTICE, err);
12648
12649error:
12650 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12651 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12652 return cli_dynerr(appctx, err);
12653}
12654
12655
12656
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012657static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012658{
12659#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12660 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012661 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012662
12663 if (!payload)
12664 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012665
12666 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012667 if (!*payload)
12668 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012669
12670 /* remove \r and \n from the payload */
12671 for (i = 0, j = 0; payload[i]; i++) {
12672 if (payload[i] == '\r' || payload[i] == '\n')
12673 continue;
12674 payload[j++] = payload[i];
12675 }
12676 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012677
Willy Tarreau1c913e42018-08-22 05:26:57 +020012678 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012679 if (ret < 0)
12680 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012681
Willy Tarreau1c913e42018-08-22 05:26:57 +020012682 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012683 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012684 if (err)
12685 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12686 else
12687 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012688 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012689
12690 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012691#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012692 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 +020012693#endif
12694
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012695}
12696
Willy Tarreau86a394e2019-05-09 14:15:32 +020012697#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012698static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
12699{
12700 switch (arg->type) {
12701 case ARGT_STR:
12702 smp->data.type = SMP_T_STR;
12703 smp->data.u.str = arg->data.str;
12704 return 1;
12705 case ARGT_VAR:
12706 if (!vars_get_by_desc(&arg->data.var, smp))
12707 return 0;
12708 if (!sample_casts[smp->data.type][SMP_T_STR])
12709 return 0;
12710 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
12711 return 0;
12712 return 1;
12713 default:
12714 return 0;
12715 }
12716}
12717
12718static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
12719 const char *file, int line, char **err)
12720{
12721 switch(args[0].data.sint) {
12722 case 128:
12723 case 192:
12724 case 256:
12725 break;
12726 default:
12727 memprintf(err, "key size must be 128, 192 or 256 (bits).");
12728 return 0;
12729 }
12730 /* Try to decode a variable. */
12731 vars_check_arg(&args[1], NULL);
12732 vars_check_arg(&args[2], NULL);
12733 vars_check_arg(&args[3], NULL);
12734 return 1;
12735}
12736
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012737/* Arguments: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012738static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
12739{
12740 struct sample nonce, key, aead_tag;
12741 struct buffer *smp_trash, *smp_trash_alloc;
12742 EVP_CIPHER_CTX *ctx;
12743 int dec_size, ret;
12744
12745 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
12746 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
12747 return 0;
12748
12749 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
12750 if (!sample_conv_var2smp_str(&arg_p[2], &key))
12751 return 0;
12752
12753 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
12754 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
12755 return 0;
12756
12757 smp_trash = get_trash_chunk();
12758 smp_trash_alloc = alloc_trash_chunk();
12759 if (!smp_trash_alloc)
12760 return 0;
12761
12762 ctx = EVP_CIPHER_CTX_new();
12763
12764 if (!ctx)
12765 goto err;
12766
12767 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
12768 if (dec_size < 0)
12769 goto err;
12770 smp_trash->data = dec_size;
12771
12772 /* Set cipher type and mode */
12773 switch(arg_p[0].data.sint) {
12774 case 128:
12775 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
12776 break;
12777 case 192:
12778 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
12779 break;
12780 case 256:
12781 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
12782 break;
12783 }
12784
12785 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
12786
12787 /* Initialise IV */
12788 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
12789 goto err;
12790
12791 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
12792 if (dec_size < 0)
12793 goto err;
12794 smp_trash->data = dec_size;
12795
12796 /* Initialise key */
12797 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
12798 goto err;
12799
12800 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
12801 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
12802 goto err;
12803
12804 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
12805 if (dec_size < 0)
12806 goto err;
12807 smp_trash_alloc->data = dec_size;
12808 dec_size = smp_trash->data;
12809
12810 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
12811 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
12812
12813 if (ret <= 0)
12814 goto err;
12815
12816 smp->data.u.str.data = dec_size + smp_trash->data;
12817 smp->data.u.str.area = smp_trash->area;
12818 smp->data.type = SMP_T_BIN;
12819 smp->flags &= ~SMP_F_CONST;
12820 free_trash_chunk(smp_trash_alloc);
12821 return 1;
12822
12823err:
12824 free_trash_chunk(smp_trash_alloc);
12825 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020012826}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012827# endif
William Lallemand32af2032016-10-29 18:09:35 +020012828
Elliot Otchet71f82972020-01-15 08:12:14 -050012829/* Argument validation functions */
12830
12831/* This function is used to validate the arguments passed to any "x_dn" ssl
12832 * keywords. These keywords support specifying a third parameter that must be
12833 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12834 */
12835int val_dnfmt(struct arg *arg, char **err_msg)
12836{
12837 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12838 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12839 return 0;
12840 }
12841 return 1;
12842}
12843
William Lallemand32af2032016-10-29 18:09:35 +020012844/* register cli keywords */
12845static struct cli_kw_list cli_kws = {{ },{
12846#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12847 { { "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 +020012848 { { "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 +020012849#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012850 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012851 { { "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 +010012852 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12853 { { "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 +010012854 { { "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 +020012855 { { "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 +010012856 { { "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 +010012857 { { "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 +020012858 { { "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 +020012859 { { "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 +020012860 { { NULL }, NULL, NULL, NULL }
12861}};
12862
Willy Tarreau0108d902018-11-25 19:14:37 +010012863INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012864
Willy Tarreau7875d092012-09-10 08:20:03 +020012865/* Note: must not be declared <const> as its list will be overwritten.
12866 * Please take care of keeping this list alphabetically sorted.
12867 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012868static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012869 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012870 { "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 +010012871#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012872 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012873#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012874 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012875#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12876 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12877#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012878 { "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 +020012879 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012880 { "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 +020012881 { "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 +020012882#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012883 { "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 -040012884#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012885#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012886 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12887 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012888 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12889#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012890 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12891 { "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 +010012892 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012893 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012894 { "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 +020012895 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12896 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12897 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12898 { "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 -050012899 { "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 +020012900 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12901 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012902 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012903 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12904 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012905 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012906 { "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 +020012907 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12908 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12909 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12910 { "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 -050012911 { "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 +020012912 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012913 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012914 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012915 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012916 { "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 +010012917 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012918 { "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 +020012919 { "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 +010012920 { "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 +020012921 { "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 +010012922#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012923 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012924#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012925#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012926 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012927#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012928 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012929#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012930 { "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 -040012931#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012932 { "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 +020012933#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012934 { "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 -040012935#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012936#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012937 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12938 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012939 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12940#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012941#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012942 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012943#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012944 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12945 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12946 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12947 { "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 +020012948 { NULL, NULL, 0, 0, 0 },
12949}};
12950
Willy Tarreau0108d902018-11-25 19:14:37 +010012951INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12952
Willy Tarreau7875d092012-09-10 08:20:03 +020012953/* Note: must not be declared <const> as its list will be overwritten.
12954 * Please take care of keeping this list alphabetically sorted.
12955 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012956static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012957 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12958 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012959 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012960}};
12961
Willy Tarreau0108d902018-11-25 19:14:37 +010012962INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12963
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012964/* Note: must not be declared <const> as its list will be overwritten.
12965 * Please take care of keeping this list alphabetically sorted, doing so helps
12966 * all code contributors.
12967 * Optional keywords are also declared with a NULL ->parse() function so that
12968 * the config parser can report an appropriate error when a known keyword was
12969 * not enabled.
12970 */
William Lallemand557823f2020-04-01 17:42:47 +020012971
12972/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12973 * with their proxy argument NULL and must only fill the ssl_bind_conf */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012974static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012975 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012976 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012977 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12978 { "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 +010012979 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012980#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012981 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12982#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012983 { "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 +010012984 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012985 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012986 { "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 +010012987 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012988 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12989 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012990 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12991 { NULL, NULL, 0 },
12992};
12993
Willy Tarreau0108d902018-11-25 19:14:37 +010012994/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12995
Willy Tarreau51fb7652012-09-18 18:24:39 +020012996static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012997 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012998 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012999 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
13000 { "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 +020013001 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
13002 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
13003 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
13004 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013005#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013006 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
13007#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050013008 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020013009 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050013010 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020013011 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
13012 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
13013 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
13014 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
13015 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
13016 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
13017 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020013018 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020013019 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020013020 { "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 +020013021 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
13022 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
13023 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
13024 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020013025 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020013026 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
13027 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020013028 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
13029 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020013030 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
13031 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
13032 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
13033 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
13034 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020013035 { NULL, NULL, 0 },
13036}};
Emeric Brun46591952012-05-18 15:47:34 +020013037
Willy Tarreau0108d902018-11-25 19:14:37 +010013038INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
13039
Willy Tarreau92faadf2012-10-10 23:04:25 +020013040/* Note: must not be declared <const> as its list will be overwritten.
13041 * Please take care of keeping this list alphabetically sorted, doing so helps
13042 * all code contributors.
13043 * Optional keywords are also declared with a NULL ->parse() function so that
13044 * the config parser can report an appropriate error when a known keyword was
13045 * not enabled.
13046 */
13047static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010013048 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010013049 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020013050 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010013051 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020013052 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020013053 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
13054 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013055#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013056 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
13057#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020013058 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
13059 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
13060 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
13061 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
13062 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
13063 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
13064 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
13065 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
13066 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
13067 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
13068 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
13069 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
13070 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
13071 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
13072 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
13073 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
13074 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
13075 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010013076 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020013077 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
13078 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
13079 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
13080 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
13081 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
13082 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
13083 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
13084 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
13085 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
13086 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020013087 { NULL, NULL, 0, 0 },
13088}};
13089
Willy Tarreau0108d902018-11-25 19:14:37 +010013090INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
13091
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013092static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010013093 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
13094 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013095 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010013096 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013097 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
13098 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010013099#ifndef OPENSSL_NO_DH
13100 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
13101#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013102 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013103#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013104 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013105#endif
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020013106 { CFG_GLOBAL, "ssl-skip-self-issued-ca", ssl_parse_skip_self_issued_ca },
Willy Tarreau9ceda382016-12-21 23:13:03 +010013107 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
13108#ifndef OPENSSL_NO_DH
13109 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
13110#endif
13111 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
13112 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
13113 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
13114 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010013115 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010013116 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
13117 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Jerome Magninb203ff62020-04-03 15:28:22 +020013118#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
13119 { CFG_GLOBAL, "ssl-default-bind-curves", ssl_parse_global_curves },
13120#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013121#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013122 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
13123 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
13124#endif
William Lallemand3af48e72020-02-03 17:15:52 +010013125 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010013126 { 0, NULL, NULL },
13127}};
13128
Willy Tarreau0108d902018-11-25 19:14:37 +010013129INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
13130
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010013131/* Note: must not be declared <const> as its list will be overwritten */
13132static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020013133#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010013134 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
13135#endif
13136 { NULL, NULL, 0, 0, 0 },
13137}};
13138
13139INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
13140
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020013141/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010013142static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020013143 .snd_buf = ssl_sock_from_buf,
13144 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010013145 .subscribe = ssl_subscribe,
13146 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020013147 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020013148 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020013149 .rcv_pipe = NULL,
13150 .snd_pipe = NULL,
13151 .shutr = NULL,
13152 .shutw = ssl_sock_shutw,
13153 .close = ssl_sock_close,
13154 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010013155 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010013156 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010013157 .prepare_srv = ssl_sock_prepare_srv_ctx,
13158 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010013159 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010013160 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020013161};
13162
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013163enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
13164 struct session *sess, struct stream *s, int flags)
13165{
13166 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013167 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013168
13169 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013170 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013171
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013172 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013173 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010013174 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020013175 s->req.flags |= CF_READ_NULL;
13176 return ACT_RET_YIELD;
13177 }
13178 }
13179 return (ACT_RET_CONT);
13180}
13181
13182static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
13183{
13184 rule->action_ptr = ssl_action_wait_for_hs;
13185
13186 return ACT_RET_PRS_OK;
13187}
13188
13189static struct action_kw_list http_req_actions = {ILH, {
13190 { "wait-for-handshake", ssl_parse_wait_for_hs },
13191 { /* END */ }
13192}};
13193
Willy Tarreau0108d902018-11-25 19:14:37 +010013194INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
13195
Willy Tarreau5db847a2019-05-09 14:13:35 +020013196#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013197
13198static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13199{
13200 if (ptr) {
13201 chunk_destroy(ptr);
13202 free(ptr);
13203 }
13204}
13205
13206#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013207static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
13208{
Willy Tarreaubafbe012017-11-24 17:34:44 +010013209 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010013210}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013211
Emeric Brun46591952012-05-18 15:47:34 +020013212__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013213static void __ssl_sock_init(void)
13214{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013215#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013216 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013217 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013218#endif
Emeric Brun46591952012-05-18 15:47:34 +020013219
Willy Tarreauef934602016-12-22 23:12:01 +010013220 if (global_ssl.listen_default_ciphers)
13221 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13222 if (global_ssl.connect_default_ciphers)
13223 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013224#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013225 if (global_ssl.listen_default_ciphersuites)
13226 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13227 if (global_ssl.connect_default_ciphersuites)
13228 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13229#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013230
Willy Tarreau13e14102016-12-22 20:25:26 +010013231 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013232#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013233 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013234#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013235#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013236 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013237 n = sk_SSL_COMP_num(cm);
13238 while (n--) {
13239 (void) sk_SSL_COMP_pop(cm);
13240 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013241#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013242
Willy Tarreau5db847a2019-05-09 14:13:35 +020013243#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013244 ssl_locking_init();
13245#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013246#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013247 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13248#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013249 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013250 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 +020013251#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013252 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013253 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013254#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013255#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13256 hap_register_post_check(tlskeys_finalize_config);
13257#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013258
13259 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13260 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13261
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013262 hap_register_post_deinit(ssl_free_global_issuers);
13263
Willy Tarreau80713382018-11-26 10:19:54 +010013264#ifndef OPENSSL_NO_DH
13265 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13266 hap_register_post_deinit(ssl_free_dh);
13267#endif
13268#ifndef OPENSSL_NO_ENGINE
13269 hap_register_post_deinit(ssl_free_engines);
13270#endif
13271 /* Load SSL string for the verbose & debug mode. */
13272 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013273 ha_meth = BIO_meth_new(0x666, "ha methods");
13274 BIO_meth_set_write(ha_meth, ha_ssl_write);
13275 BIO_meth_set_read(ha_meth, ha_ssl_read);
13276 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13277 BIO_meth_set_create(ha_meth, ha_ssl_new);
13278 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13279 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13280 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013281
13282 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010013283}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013284
Willy Tarreau80713382018-11-26 10:19:54 +010013285/* Compute and register the version string */
13286static void ssl_register_build_options()
13287{
13288 char *ptr = NULL;
13289 int i;
13290
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013291 memprintf(&ptr, "Built with OpenSSL version : "
13292#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013293 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013294#else /* OPENSSL_IS_BORINGSSL */
13295 OPENSSL_VERSION_TEXT
13296 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013297 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013298 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013299#endif
13300 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013301#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013302 "no (library version too old)"
13303#elif defined(OPENSSL_NO_TLSEXT)
13304 "no (disabled via OPENSSL_NO_TLSEXT)"
13305#else
13306 "yes"
13307#endif
13308 "", ptr);
13309
13310 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13311#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13312 "yes"
13313#else
13314#ifdef OPENSSL_NO_TLSEXT
13315 "no (because of OPENSSL_NO_TLSEXT)"
13316#else
13317 "no (version might be too old, 0.9.8f min needed)"
13318#endif
13319#endif
13320 "", ptr);
13321
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013322 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13323 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13324 if (methodVersions[i].option)
13325 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013326
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013327 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013328}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013329
Willy Tarreau80713382018-11-26 10:19:54 +010013330INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013331
Emeric Brun46591952012-05-18 15:47:34 +020013332
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013333#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013334void ssl_free_engines(void) {
13335 struct ssl_engine_list *wl, *wlb;
13336 /* free up engine list */
13337 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13338 ENGINE_finish(wl->e);
13339 ENGINE_free(wl->e);
13340 LIST_DEL(&wl->list);
13341 free(wl);
13342 }
13343}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013344#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013345
Remi Gacogned3a23c32015-05-28 16:39:47 +020013346#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013347void ssl_free_dh(void) {
13348 if (local_dh_1024) {
13349 DH_free(local_dh_1024);
13350 local_dh_1024 = NULL;
13351 }
13352 if (local_dh_2048) {
13353 DH_free(local_dh_2048);
13354 local_dh_2048 = NULL;
13355 }
13356 if (local_dh_4096) {
13357 DH_free(local_dh_4096);
13358 local_dh_4096 = NULL;
13359 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013360 if (global_dh) {
13361 DH_free(global_dh);
13362 global_dh = NULL;
13363 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013364}
13365#endif
13366
13367__attribute__((destructor))
13368static void __ssl_sock_deinit(void)
13369{
13370#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013371 if (ssl_ctx_lru_tree) {
13372 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013373 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013374 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013375#endif
13376
Willy Tarreau5db847a2019-05-09 14:13:35 +020013377#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013378 ERR_remove_state(0);
13379 ERR_free_strings();
13380
13381 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013382#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013383
Willy Tarreau5db847a2019-05-09 14:13:35 +020013384#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013385 CRYPTO_cleanup_all_ex_data();
13386#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013387 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013388}
13389
13390
Emeric Brun46591952012-05-18 15:47:34 +020013391/*
13392 * Local variables:
13393 * c-indent-level: 8
13394 * c-basic-offset: 8
13395 * End:
13396 */