blob: e00dafaa0eda223fd9f0a84a4ee24fe543710158 [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
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
William Lallemand32af2032016-10-29 18:09:35 +020062#include <types/applet.h>
63#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020064#include <types/global.h>
65#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020066#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020067
Willy Tarreau7875d092012-09-10 08:20:03 +020068#include <proto/acl.h>
69#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020070#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020072#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/fd.h>
74#include <proto/freq_ctr.h>
75#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020076#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020077#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010078#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020079#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020080#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020081#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020082#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020083#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020084#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020085#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020086#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020087#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010089#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020090
Willy Tarreau9356dac2019-05-10 09:22:53 +020091/* ***** READ THIS before adding code here! *****
92 *
93 * Due to API incompatibilities between multiple OpenSSL versions and their
94 * derivatives, it's often tempting to add macros to (re-)define certain
95 * symbols. Please do not do this here, and do it in common/openssl-compat.h
96 * exclusively so that the whole code consistently uses the same macros.
97 *
98 * Whenever possible if a macro is missing in certain versions, it's better
99 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
100 */
101
Willy Tarreau518cedd2014-02-17 15:43:01 +0100102/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200103#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100104#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100105#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200106#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
107
Emeric Brunf282a812012-09-21 15:27:54 +0200108/* bits 0xFFFF0000 are reserved to store verify errors */
109
110/* Verify errors macros */
111#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
112#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
113#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
114
115#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
116#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
117#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119/* ssl_methods flags for ssl options */
120#define MC_SSL_O_ALL 0x0000
121#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
122#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
123#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
124#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200125#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200126
William Lallemand3af48e72020-02-03 17:15:52 +0100127/* file to guess during file loading */
128#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
129#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
130#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
131#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
132#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
133
134#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER)
135
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200136/* ssl_methods versions */
137enum {
138 CONF_TLSV_NONE = 0,
139 CONF_TLSV_MIN = 1,
140 CONF_SSLV3 = 1,
141 CONF_TLSV10 = 2,
142 CONF_TLSV11 = 3,
143 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200144 CONF_TLSV13 = 5,
145 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200146};
147
Emeric Brun850efd52014-01-29 12:24:34 +0100148/* server and bind verify method, it uses a global value as default */
149enum {
150 SSL_SOCK_VERIFY_DEFAULT = 0,
151 SSL_SOCK_VERIFY_REQUIRED = 1,
152 SSL_SOCK_VERIFY_OPTIONAL = 2,
153 SSL_SOCK_VERIFY_NONE = 3,
154};
155
William Lallemand3f85c9a2017-10-09 16:30:50 +0200156
Willy Tarreau71b734c2014-01-28 15:19:44 +0100157int sslconns = 0;
158int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100159static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100160int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200161
Willy Tarreauef934602016-12-22 23:12:01 +0100162static struct {
163 char *crt_base; /* base directory path for certificates */
164 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000165 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100166
167 char *listen_default_ciphers;
168 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200169#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200170 char *listen_default_ciphersuites;
171 char *connect_default_ciphersuites;
172#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100173 int listen_default_ssloptions;
174 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200175 struct tls_version_filter listen_default_sslmethods;
176 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100177
178 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
179 unsigned int life_time; /* SSL session lifetime in seconds */
180 unsigned int max_record; /* SSL max record size */
181 unsigned int default_dh_param; /* SSL maximum DH parameter size */
182 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100183 int capture_cipherlist; /* Size of the cipherlist buffer. */
William Lallemand3af48e72020-02-03 17:15:52 +0100184 int extra_files; /* which files not defined in the configuration file are we looking for */
Willy Tarreauef934602016-12-22 23:12:01 +0100185} global_ssl = {
186#ifdef LISTEN_DEFAULT_CIPHERS
187 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
188#endif
189#ifdef CONNECT_DEFAULT_CIPHERS
190 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
191#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200192#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200193#ifdef LISTEN_DEFAULT_CIPHERSUITES
194 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
195#endif
196#ifdef CONNECT_DEFAULT_CIPHERSUITES
197 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
198#endif
199#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100200 .listen_default_ssloptions = BC_SSL_O_NONE,
201 .connect_default_ssloptions = SRV_SSL_O_NONE,
202
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200203 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
204 .listen_default_sslmethods.min = CONF_TLSV_NONE,
205 .listen_default_sslmethods.max = CONF_TLSV_NONE,
206 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
207 .connect_default_sslmethods.min = CONF_TLSV_NONE,
208 .connect_default_sslmethods.max = CONF_TLSV_NONE,
209
Willy Tarreauef934602016-12-22 23:12:01 +0100210#ifdef DEFAULT_SSL_MAX_RECORD
211 .max_record = DEFAULT_SSL_MAX_RECORD,
212#endif
213 .default_dh_param = SSL_DEFAULT_DH_PARAM,
214 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100215 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100216 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100217};
218
Olivier Houcharda8955d52019-04-07 22:00:38 +0200219static BIO_METHOD *ha_meth;
220
Olivier Houchard66ab4982019-02-26 18:37:15 +0100221struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200222 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100223 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200224 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200225 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100226 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200227 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100228 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100229 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100230 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100231 int sent_early_data; /* Amount of early data we sent so far */
232
Olivier Houchard66ab4982019-02-26 18:37:15 +0100233};
234
235DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
236
Olivier Houchardea8dd942019-05-20 14:02:16 +0200237static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200238static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200239
Olivier Houcharda8955d52019-04-07 22:00:38 +0200240/* Methods to implement OpenSSL BIO */
241static int ha_ssl_write(BIO *h, const char *buf, int num)
242{
243 struct buffer tmpbuf;
244 struct ssl_sock_ctx *ctx;
245 int ret;
246
247 ctx = BIO_get_data(h);
248 tmpbuf.size = num;
249 tmpbuf.area = (void *)(uintptr_t)buf;
250 tmpbuf.data = num;
251 tmpbuf.head = 0;
252 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200253 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200254 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200255 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200256 } else if (ret == 0)
257 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200258 return ret;
259}
260
261static int ha_ssl_gets(BIO *h, char *buf, int size)
262{
263
264 return 0;
265}
266
267static int ha_ssl_puts(BIO *h, const char *str)
268{
269
270 return ha_ssl_write(h, str, strlen(str));
271}
272
273static int ha_ssl_read(BIO *h, char *buf, int size)
274{
275 struct buffer tmpbuf;
276 struct ssl_sock_ctx *ctx;
277 int ret;
278
279 ctx = BIO_get_data(h);
280 tmpbuf.size = size;
281 tmpbuf.area = buf;
282 tmpbuf.data = 0;
283 tmpbuf.head = 0;
284 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200285 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200286 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200287 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200288 } else if (ret == 0)
289 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200290
291 return ret;
292}
293
294static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
295{
296 int ret = 0;
297 switch (cmd) {
298 case BIO_CTRL_DUP:
299 case BIO_CTRL_FLUSH:
300 ret = 1;
301 break;
302 }
303 return ret;
304}
305
306static int ha_ssl_new(BIO *h)
307{
308 BIO_set_init(h, 1);
309 BIO_set_data(h, NULL);
310 BIO_clear_flags(h, ~0);
311 return 1;
312}
313
314static int ha_ssl_free(BIO *data)
315{
316
317 return 1;
318}
319
320
Willy Tarreau5db847a2019-05-09 14:13:35 +0200321#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100322
Emeric Brun821bb9b2017-06-15 16:37:39 +0200323static HA_RWLOCK_T *ssl_rwlocks;
324
325
326unsigned long ssl_id_function(void)
327{
328 return (unsigned long)tid;
329}
330
331void ssl_locking_function(int mode, int n, const char * file, int line)
332{
333 if (mode & CRYPTO_LOCK) {
334 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100335 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200336 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100337 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200338 }
339 else {
340 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100341 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200342 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100343 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200344 }
345}
346
347static int ssl_locking_init(void)
348{
349 int i;
350
351 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
352 if (!ssl_rwlocks)
353 return -1;
354
355 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100356 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200357
358 CRYPTO_set_id_callback(ssl_id_function);
359 CRYPTO_set_locking_callback(ssl_locking_function);
360
361 return 0;
362}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100363
Emeric Brun821bb9b2017-06-15 16:37:39 +0200364#endif
365
William Lallemand150bfa82019-09-19 17:12:49 +0200366__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200367
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100368/* Uncommitted CKCH transaction */
369
370static struct {
371 struct ckch_store *new_ckchs;
372 struct ckch_store *old_ckchs;
373 char *path;
374} ckchs_transaction;
375
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200376/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100377 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200378 */
379struct cafile_entry {
380 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200381 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200382 struct ebmb_node node;
383 char path[0];
384};
385
386static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
387
388static X509_STORE* ssl_store_get0_locations_file(char *path)
389{
390 struct ebmb_node *eb;
391
392 eb = ebst_lookup(&cafile_tree, path);
393 if (eb) {
394 struct cafile_entry *ca_e;
395 ca_e = ebmb_entry(eb, struct cafile_entry, node);
396 return ca_e->ca_store;
397 }
398 return NULL;
399}
400
401static int ssl_store_load_locations_file(char *path)
402{
403 if (ssl_store_get0_locations_file(path) == NULL) {
404 struct cafile_entry *ca_e;
405 X509_STORE *store = X509_STORE_new();
406 if (X509_STORE_load_locations(store, path, NULL)) {
407 int pathlen;
408 pathlen = strlen(path);
409 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
410 if (ca_e) {
411 memcpy(ca_e->path, path, pathlen + 1);
412 ca_e->ca_store = store;
413 ebst_insert(&cafile_tree, &ca_e->node);
414 return 1;
415 }
416 }
417 X509_STORE_free(store);
418 return 0;
419 }
420 return 1;
421}
422
423/* mimic what X509_STORE_load_locations do with store_ctx */
424static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
425{
426 X509_STORE *store;
427 store = ssl_store_get0_locations_file(path);
428 if (store_ctx && store) {
429 int i;
430 X509_OBJECT *obj;
431 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
432 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
433 obj = sk_X509_OBJECT_value(objs, i);
434 switch (X509_OBJECT_get_type(obj)) {
435 case X509_LU_X509:
436 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
437 break;
438 case X509_LU_CRL:
439 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
440 break;
441 default:
442 break;
443 }
444 }
445 return 1;
446 }
447 return 0;
448}
449
450/* SSL_CTX_load_verify_locations substitute, internaly call X509_STORE_load_locations */
451static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
452{
453 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
454 return ssl_set_cert_crl_file(store_ctx, path);
455}
456
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200457/*
458 Extract CA_list from CA_file already in tree.
459 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
460 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
461*/
462static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
463{
464 struct ebmb_node *eb;
465 struct cafile_entry *ca_e;
466
467 eb = ebst_lookup(&cafile_tree, path);
468 if (!eb)
469 return NULL;
470 ca_e = ebmb_entry(eb, struct cafile_entry, node);
471
472 if (ca_e->ca_list == NULL) {
473 int i;
474 unsigned long key;
475 struct eb_root ca_name_tree = EB_ROOT;
476 struct eb64_node *node, *back;
477 struct {
478 struct eb64_node node;
479 X509_NAME *xname;
480 } *ca_name;
481 STACK_OF(X509_OBJECT) *objs;
482 STACK_OF(X509_NAME) *skn;
483 X509 *x;
484 X509_NAME *xn;
485
486 skn = sk_X509_NAME_new_null();
487 /* take x509 from cafile_tree */
488 objs = X509_STORE_get0_objects(ca_e->ca_store);
489 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
490 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
491 if (!x)
492 continue;
493 xn = X509_get_subject_name(x);
494 if (!xn)
495 continue;
496 /* Check for duplicates. */
497 key = X509_NAME_hash(xn);
498 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
499 node && ca_name == NULL;
500 node = eb64_next(node)) {
501 ca_name = container_of(node, typeof(*ca_name), node);
502 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
503 ca_name = NULL;
504 }
505 /* find a duplicate */
506 if (ca_name)
507 continue;
508 ca_name = calloc(1, sizeof *ca_name);
509 xn = X509_NAME_dup(xn);
510 if (!ca_name ||
511 !xn ||
512 !sk_X509_NAME_push(skn, xn)) {
513 free(ca_name);
514 X509_NAME_free(xn);
515 sk_X509_NAME_pop_free(skn, X509_NAME_free);
516 sk_X509_NAME_free(skn);
517 skn = NULL;
518 break;
519 }
520 ca_name->node.key = key;
521 ca_name->xname = xn;
522 eb64_insert(&ca_name_tree, &ca_name->node);
523 }
524 ca_e->ca_list = skn;
525 /* remove temporary ca_name tree */
526 node = eb64_first(&ca_name_tree);
527 while (node) {
528 ca_name = container_of(node, typeof(*ca_name), node);
529 back = eb64_next(node);
530 eb64_delete(node);
531 free(ca_name);
532 node = back;
533 }
534 }
535 return ca_e->ca_list;
536}
537
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100538/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100539struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100540 unsigned long long int xxh64;
541 unsigned char ciphersuite_len;
542 char ciphersuite[0];
543};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100544struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100545static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200546static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100547
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200548#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
549struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
550#endif
551
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200552#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000553static unsigned int openssl_engines_initialized;
554struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
555struct ssl_engine_list {
556 struct list list;
557 ENGINE *e;
558};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200559#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000560
Remi Gacogne8de54152014-07-15 11:36:40 +0200561#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200562static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200563static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200564static DH *local_dh_1024 = NULL;
565static DH *local_dh_2048 = NULL;
566static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100567static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200568#endif /* OPENSSL_NO_DH */
569
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100570#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200571/* X509V3 Extensions that will be added on generated certificates */
572#define X509V3_EXT_SIZE 5
573static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
574 "basicConstraints",
575 "nsComment",
576 "subjectKeyIdentifier",
577 "authorityKeyIdentifier",
578 "keyUsage",
579};
580static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
581 "CA:FALSE",
582 "\"OpenSSL Generated Certificate\"",
583 "hash",
584 "keyid,issuer:always",
585 "nonRepudiation,digitalSignature,keyEncipherment"
586};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200587/* LRU cache to store generated certificate */
588static struct lru64_head *ssl_ctx_lru_tree = NULL;
589static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200590static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100591__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200592
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200593#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
594
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100595static struct ssl_bind_kw ssl_bind_kws[];
596
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200597#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500598/* The order here matters for picking a default context,
599 * keep the most common keytype at the bottom of the list
600 */
601const char *SSL_SOCK_KEYTYPE_NAMES[] = {
602 "dsa",
603 "ecdsa",
604 "rsa"
605};
606#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100607#else
608#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500609#endif
610
William Lallemandc3cd35f2017-11-28 11:04:43 +0100611static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100612static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
613
614#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
615
616#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
617 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
618
619#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
620 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200621
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100622/*
623 * This function gives the detail of the SSL error. It is used only
624 * if the debug mode and the verbose mode are activated. It dump all
625 * the SSL error until the stack was empty.
626 */
627static forceinline void ssl_sock_dump_errors(struct connection *conn)
628{
629 unsigned long ret;
630
631 if (unlikely(global.mode & MODE_DEBUG)) {
632 while(1) {
633 ret = ERR_get_error();
634 if (ret == 0)
635 return;
636 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200637 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100638 ERR_func_error_string(ret), ERR_reason_error_string(ret));
639 }
640 }
641}
642
yanbzhube2774d2015-12-10 15:07:30 -0500643
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200644#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000645static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
646{
647 int err_code = ERR_ABORT;
648 ENGINE *engine;
649 struct ssl_engine_list *el;
650
651 /* grab the structural reference to the engine */
652 engine = ENGINE_by_id(engine_id);
653 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100654 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000655 goto fail_get;
656 }
657
658 if (!ENGINE_init(engine)) {
659 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100660 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000661 goto fail_init;
662 }
663
664 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100665 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000666 goto fail_set_method;
667 }
668
669 el = calloc(1, sizeof(*el));
670 el->e = engine;
671 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100672 nb_engines++;
673 if (global_ssl.async)
674 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000675 return 0;
676
677fail_set_method:
678 /* release the functional reference from ENGINE_init() */
679 ENGINE_finish(engine);
680
681fail_init:
682 /* release the structural reference from ENGINE_by_id() */
683 ENGINE_free(engine);
684
685fail_get:
686 return err_code;
687}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200688#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000689
Willy Tarreau5db847a2019-05-09 14:13:35 +0200690#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200691/*
692 * openssl async fd handler
693 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200694void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000695{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200696 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000697
Emeric Brun3854e012017-05-17 20:42:48 +0200698 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000699 * to poll this fd until it is requested
700 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000701 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000702 fd_cant_recv(fd);
703
704 /* crypto engine is available, let's notify the associated
705 * connection that it can pursue its processing.
706 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200707 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000708}
709
Emeric Brun3854e012017-05-17 20:42:48 +0200710/*
711 * openssl async delayed SSL_free handler
712 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200713void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000714{
715 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200716 OSSL_ASYNC_FD all_fd[32];
717 size_t num_all_fds = 0;
718 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000719
Emeric Brun3854e012017-05-17 20:42:48 +0200720 /* We suppose that the async job for a same SSL *
721 * are serialized. So if we are awake it is
722 * because the running job has just finished
723 * and we can remove all async fds safely
724 */
725 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
726 if (num_all_fds > 32) {
727 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
728 return;
729 }
730
731 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
732 for (i=0 ; i < num_all_fds ; i++)
733 fd_remove(all_fd[i]);
734
735 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000736 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100737 _HA_ATOMIC_SUB(&sslconns, 1);
738 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000739}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000740/*
Emeric Brun3854e012017-05-17 20:42:48 +0200741 * function used to manage a returned SSL_ERROR_WANT_ASYNC
742 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000743 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200744static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000745{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100746 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200747 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200748 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000749 size_t num_add_fds = 0;
750 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200751 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000752
753 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
754 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200755 if (num_add_fds > 32 || num_del_fds > 32) {
756 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 +0000757 return;
758 }
759
Emeric Brun3854e012017-05-17 20:42:48 +0200760 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000761
Emeric Brun3854e012017-05-17 20:42:48 +0200762 /* We remove unused fds from the fdtab */
763 for (i=0 ; i < num_del_fds ; i++)
764 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000765
Emeric Brun3854e012017-05-17 20:42:48 +0200766 /* We add new fds to the fdtab */
767 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200768 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000769 }
770
Emeric Brun3854e012017-05-17 20:42:48 +0200771 num_add_fds = 0;
772 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
773 if (num_add_fds > 32) {
774 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
775 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000776 }
Emeric Brun3854e012017-05-17 20:42:48 +0200777
778 /* We activate the polling for all known async fds */
779 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000780 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200781 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000782 /* To ensure that the fd cache won't be used
783 * We'll prefer to catch a real RD event
784 * because handling an EAGAIN on this fd will
785 * result in a context switch and also
786 * some engines uses a fd in blocking mode.
787 */
788 fd_cant_recv(add_fd[i]);
789 }
Emeric Brun3854e012017-05-17 20:42:48 +0200790
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000791}
792#endif
793
William Lallemand104a7a62019-10-14 14:14:59 +0200794#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200795/*
796 * This function returns the number of seconds elapsed
797 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
798 * date presented un ASN1_GENERALIZEDTIME.
799 *
800 * In parsing error case, it returns -1.
801 */
802static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
803{
804 long epoch;
805 char *p, *end;
806 const unsigned short month_offset[12] = {
807 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
808 };
809 int year, month;
810
811 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
812
813 p = (char *)d->data;
814 end = p + d->length;
815
816 if (end - p < 4) return -1;
817 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
818 p += 4;
819 if (end - p < 2) return -1;
820 month = 10 * (p[0] - '0') + p[1] - '0';
821 if (month < 1 || month > 12) return -1;
822 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
823 We consider leap years and the current month (<marsh or not) */
824 epoch = ( ((year - 1970) * 365)
825 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
826 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
827 + month_offset[month-1]
828 ) * 24 * 60 * 60;
829 p += 2;
830 if (end - p < 2) return -1;
831 /* Add the number of seconds of completed days of current month */
832 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
833 p += 2;
834 if (end - p < 2) return -1;
835 /* Add the completed hours of the current day */
836 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
837 p += 2;
838 if (end - p < 2) return -1;
839 /* Add the completed minutes of the current hour */
840 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
841 p += 2;
842 if (p == end) return -1;
843 /* Test if there is available seconds */
844 if (p[0] < '0' || p[0] > '9')
845 goto nosec;
846 if (end - p < 2) return -1;
847 /* Add the seconds of the current minute */
848 epoch += 10 * (p[0] - '0') + p[1] - '0';
849 p += 2;
850 if (p == end) return -1;
851 /* Ignore seconds float part if present */
852 if (p[0] == '.') {
853 do {
854 if (++p == end) return -1;
855 } while (p[0] >= '0' && p[0] <= '9');
856 }
857
858nosec:
859 if (p[0] == 'Z') {
860 if (end - p != 1) return -1;
861 return epoch;
862 }
863 else if (p[0] == '+') {
864 if (end - p != 5) return -1;
865 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700866 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 +0200867 }
868 else if (p[0] == '-') {
869 if (end - p != 5) return -1;
870 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700871 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 +0200872 }
873
874 return -1;
875}
876
William Lallemand104a7a62019-10-14 14:14:59 +0200877/*
878 * struct alignment works here such that the key.key is the same as key_data
879 * Do not change the placement of key_data
880 */
881struct certificate_ocsp {
882 struct ebmb_node key;
883 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
884 struct buffer response;
885 long expire;
886};
887
888struct ocsp_cbk_arg {
889 int is_single;
890 int single_kt;
891 union {
892 struct certificate_ocsp *s_ocsp;
893 /*
894 * m_ocsp will have multiple entries dependent on key type
895 * Entry 0 - DSA
896 * Entry 1 - ECDSA
897 * Entry 2 - RSA
898 */
899 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
900 };
901};
902
Emeric Brun1d3865b2014-06-20 15:37:32 +0200903static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200904
905/* This function starts to check if the OCSP response (in DER format) contained
906 * in chunk 'ocsp_response' is valid (else exits on error).
907 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
908 * contained in the OCSP Response and exits on error if no match.
909 * If it's a valid OCSP Response:
910 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
911 * pointed by 'ocsp'.
912 * If 'ocsp' is NULL, the function looks up into the OCSP response's
913 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
914 * from the response) and exits on error if not found. Finally, If an OCSP response is
915 * already present in the container, it will be overwritten.
916 *
917 * Note: OCSP response containing more than one OCSP Single response is not
918 * considered valid.
919 *
920 * Returns 0 on success, 1 in error case.
921 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200922static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
923 struct certificate_ocsp *ocsp,
924 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200925{
926 OCSP_RESPONSE *resp;
927 OCSP_BASICRESP *bs = NULL;
928 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200929 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200930 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200931 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200932 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200933 int reason;
934 int ret = 1;
935
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200936 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
937 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200938 if (!resp) {
939 memprintf(err, "Unable to parse OCSP response");
940 goto out;
941 }
942
943 rc = OCSP_response_status(resp);
944 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
945 memprintf(err, "OCSP response status not successful");
946 goto out;
947 }
948
949 bs = OCSP_response_get1_basic(resp);
950 if (!bs) {
951 memprintf(err, "Failed to get basic response from OCSP Response");
952 goto out;
953 }
954
955 count_sr = OCSP_resp_count(bs);
956 if (count_sr > 1) {
957 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
958 goto out;
959 }
960
961 sr = OCSP_resp_get0(bs, 0);
962 if (!sr) {
963 memprintf(err, "Failed to get OCSP single response");
964 goto out;
965 }
966
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200967 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
968
Emeric Brun4147b2e2014-06-16 18:36:30 +0200969 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200970 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200971 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200972 goto out;
973 }
974
Emeric Brun13a6b482014-06-20 15:44:34 +0200975 if (!nextupd) {
976 memprintf(err, "OCSP single response: missing nextupdate");
977 goto out;
978 }
979
Emeric Brunc8b27b62014-06-19 14:16:17 +0200980 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200981 if (!rc) {
982 memprintf(err, "OCSP single response: no longer valid.");
983 goto out;
984 }
985
986 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200987 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200988 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
989 goto out;
990 }
991 }
992
993 if (!ocsp) {
994 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
995 unsigned char *p;
996
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200997 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200998 if (!rc) {
999 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
1000 goto out;
1001 }
1002
1003 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
1004 memprintf(err, "OCSP single response: Certificate ID too long");
1005 goto out;
1006 }
1007
1008 p = key;
1009 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001010 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001011 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1012 if (!ocsp) {
1013 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1014 goto out;
1015 }
1016 }
1017
1018 /* According to comments on "chunk_dup", the
1019 previous chunk buffer will be freed */
1020 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1021 memprintf(err, "OCSP response: Memory allocation error");
1022 goto out;
1023 }
1024
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001025 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1026
Emeric Brun4147b2e2014-06-16 18:36:30 +02001027 ret = 0;
1028out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001029 ERR_clear_error();
1030
Emeric Brun4147b2e2014-06-16 18:36:30 +02001031 if (bs)
1032 OCSP_BASICRESP_free(bs);
1033
1034 if (resp)
1035 OCSP_RESPONSE_free(resp);
1036
1037 return ret;
1038}
1039/*
1040 * External function use to update the OCSP response in the OCSP response's
1041 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1042 * to update in DER format.
1043 *
1044 * Returns 0 on success, 1 in error case.
1045 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001046int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001047{
1048 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1049}
1050
William Lallemand4a660132019-10-14 14:51:41 +02001051#endif
1052
1053#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001054/*
1055 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001056 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001057 *
1058 * Returns 0 on success, 1 in error case.
1059 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001060static 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 +02001061{
1062 int fd = -1;
1063 int r = 0;
1064 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001065 struct buffer *ocsp_response;
1066 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001067
William Lallemand3b5f3602019-10-16 18:05:05 +02001068 if (buf) {
1069 int i, j;
1070 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001071
William Lallemand3b5f3602019-10-16 18:05:05 +02001072 /* remove \r and \n from the payload */
1073 for (i = 0, j = 0; buf[i]; i++) {
1074 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001075 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001076 buf[j++] = buf[i];
1077 }
1078 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001079
William Lallemand3b5f3602019-10-16 18:05:05 +02001080 ret = base64dec(buf, j, trash.area, trash.size);
1081 if (ret < 0) {
1082 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001083 goto end;
1084 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001085 trash.data = ret;
1086 src = &trash;
1087 } else {
1088 fd = open(ocsp_path, O_RDONLY);
1089 if (fd == -1) {
1090 memprintf(err, "Error opening OCSP response file");
1091 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001092 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001093
1094 trash.data = 0;
1095 while (trash.data < trash.size) {
1096 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1097 if (r < 0) {
1098 if (errno == EINTR)
1099 continue;
1100
1101 memprintf(err, "Error reading OCSP response from file");
1102 goto end;
1103 }
1104 else if (r == 0) {
1105 break;
1106 }
1107 trash.data += r;
1108 }
1109 close(fd);
1110 fd = -1;
1111 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001112 }
1113
William Lallemand3b5f3602019-10-16 18:05:05 +02001114 ocsp_response = calloc(1, sizeof(*ocsp_response));
1115 if (!chunk_dup(ocsp_response, src)) {
1116 free(ocsp_response);
1117 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001118 goto end;
1119 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001120 /* no error, fill ckch with new context, old context must be free */
1121 if (ckch->ocsp_response) {
1122 free(ckch->ocsp_response->area);
1123 ckch->ocsp_response->area = NULL;
1124 free(ckch->ocsp_response);
1125 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001126 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001127 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001128end:
1129 if (fd != -1)
1130 close(fd);
1131
1132 return ret;
1133}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001134#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001135
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001136#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1137static 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)
1138{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001139 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001140 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001141 struct connection *conn;
1142 int head;
1143 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001144 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001145
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001146 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001147 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001148 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1149
1150 keys = ref->tlskeys;
1151 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001152
1153 if (enc) {
1154 memcpy(key_name, keys[head].name, 16);
1155
1156 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001157 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001158
Emeric Brun9e754772019-01-10 17:51:55 +01001159 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001160
Emeric Brun9e754772019-01-10 17:51:55 +01001161 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1162 goto end;
1163
Willy Tarreau9356dac2019-05-10 09:22:53 +02001164 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001165 ret = 1;
1166 }
1167 else if (ref->key_size_bits == 256 ) {
1168
1169 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1170 goto end;
1171
Willy Tarreau9356dac2019-05-10 09:22:53 +02001172 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001173 ret = 1;
1174 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001175 } else {
1176 for (i = 0; i < TLS_TICKETS_NO; i++) {
1177 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1178 goto found;
1179 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001180 ret = 0;
1181 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001182
Christopher Faulet16f45c82018-02-16 11:23:49 +01001183 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001184 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001185 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 +01001186 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1187 goto end;
1188 /* 2 for key renewal, 1 if current key is still valid */
1189 ret = i ? 2 : 1;
1190 }
1191 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001192 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 +01001193 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1194 goto end;
1195 /* 2 for key renewal, 1 if current key is still valid */
1196 ret = i ? 2 : 1;
1197 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001198 }
Emeric Brun9e754772019-01-10 17:51:55 +01001199
Christopher Faulet16f45c82018-02-16 11:23:49 +01001200 end:
1201 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1202 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001203}
1204
1205struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1206{
1207 struct tls_keys_ref *ref;
1208
1209 list_for_each_entry(ref, &tlskeys_reference, list)
1210 if (ref->filename && strcmp(filename, ref->filename) == 0)
1211 return ref;
1212 return NULL;
1213}
1214
1215struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1216{
1217 struct tls_keys_ref *ref;
1218
1219 list_for_each_entry(ref, &tlskeys_reference, list)
1220 if (ref->unique_id == unique_id)
1221 return ref;
1222 return NULL;
1223}
1224
Emeric Brun9e754772019-01-10 17:51:55 +01001225/* Update the key into ref: if keysize doesnt
1226 * match existing ones, this function returns -1
1227 * else it returns 0 on success.
1228 */
1229int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001230 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001231{
Emeric Brun9e754772019-01-10 17:51:55 +01001232 if (ref->key_size_bits == 128) {
1233 if (tlskey->data != sizeof(struct tls_sess_key_128))
1234 return -1;
1235 }
1236 else if (ref->key_size_bits == 256) {
1237 if (tlskey->data != sizeof(struct tls_sess_key_256))
1238 return -1;
1239 }
1240 else
1241 return -1;
1242
Christopher Faulet16f45c82018-02-16 11:23:49 +01001243 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001244 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1245 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001246 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1247 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001248
1249 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001250}
1251
Willy Tarreau83061a82018-07-13 11:56:34 +02001252int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001253{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001254 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1255
1256 if(!ref) {
1257 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1258 return 1;
1259 }
Emeric Brun9e754772019-01-10 17:51:55 +01001260 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1261 memprintf(err, "Invalid key size");
1262 return 1;
1263 }
1264
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001265 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001266}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001267
1268/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001269 * automatic ids. It's called just after the basic checks. It returns
1270 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001271 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001272static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001273{
1274 int i = 0;
1275 struct tls_keys_ref *ref, *ref2, *ref3;
1276 struct list tkr = LIST_HEAD_INIT(tkr);
1277
1278 list_for_each_entry(ref, &tlskeys_reference, list) {
1279 if (ref->unique_id == -1) {
1280 /* Look for the first free id. */
1281 while (1) {
1282 list_for_each_entry(ref2, &tlskeys_reference, list) {
1283 if (ref2->unique_id == i) {
1284 i++;
1285 break;
1286 }
1287 }
1288 if (&ref2->list == &tlskeys_reference)
1289 break;
1290 }
1291
1292 /* Uses the unique id and increment it for the next entry. */
1293 ref->unique_id = i;
1294 i++;
1295 }
1296 }
1297
1298 /* This sort the reference list by id. */
1299 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1300 LIST_DEL(&ref->list);
1301 list_for_each_entry(ref3, &tkr, list) {
1302 if (ref->unique_id < ref3->unique_id) {
1303 LIST_ADDQ(&ref3->list, &ref->list);
1304 break;
1305 }
1306 }
1307 if (&ref3->list == &tkr)
1308 LIST_ADDQ(&tkr, &ref->list);
1309 }
1310
1311 /* swap root */
1312 LIST_ADD(&tkr, &tlskeys_reference);
1313 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001314 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001315}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001316#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1317
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001318#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001319int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1320{
1321 switch (evp_keytype) {
1322 case EVP_PKEY_RSA:
1323 return 2;
1324 case EVP_PKEY_DSA:
1325 return 0;
1326 case EVP_PKEY_EC:
1327 return 1;
1328 }
1329
1330 return -1;
1331}
1332
Emeric Brun4147b2e2014-06-16 18:36:30 +02001333/*
1334 * Callback used to set OCSP status extension content in server hello.
1335 */
1336int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1337{
yanbzhube2774d2015-12-10 15:07:30 -05001338 struct certificate_ocsp *ocsp;
1339 struct ocsp_cbk_arg *ocsp_arg;
1340 char *ssl_buf;
1341 EVP_PKEY *ssl_pkey;
1342 int key_type;
1343 int index;
1344
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001345 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001346
1347 ssl_pkey = SSL_get_privatekey(ssl);
1348 if (!ssl_pkey)
1349 return SSL_TLSEXT_ERR_NOACK;
1350
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001351 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001352
1353 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1354 ocsp = ocsp_arg->s_ocsp;
1355 else {
1356 /* For multiple certs per context, we have to find the correct OCSP response based on
1357 * the certificate type
1358 */
1359 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1360
1361 if (index < 0)
1362 return SSL_TLSEXT_ERR_NOACK;
1363
1364 ocsp = ocsp_arg->m_ocsp[index];
1365
1366 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001367
1368 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001369 !ocsp->response.area ||
1370 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001371 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001372 return SSL_TLSEXT_ERR_NOACK;
1373
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001374 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001375 if (!ssl_buf)
1376 return SSL_TLSEXT_ERR_NOACK;
1377
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001378 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1379 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001380
1381 return SSL_TLSEXT_ERR_OK;
1382}
1383
William Lallemand4a660132019-10-14 14:51:41 +02001384#endif
1385
1386#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001387/*
1388 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001389 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1390 * status extension, the issuer's certificate is mandatory. It should be
1391 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392 *
William Lallemand246c0242019-10-11 08:59:13 +02001393 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1394 * OCSP response. If file is empty or content is not a valid OCSP response,
1395 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1396 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001397 *
1398 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001399 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001400 */
William Lallemand4a660132019-10-14 14:51:41 +02001401#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001402static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001403{
William Lallemand246c0242019-10-11 08:59:13 +02001404 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001405 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001406 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001407 struct certificate_ocsp *ocsp = NULL, *iocsp;
1408 char *warn = NULL;
1409 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001410 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001411
Emeric Brun4147b2e2014-06-16 18:36:30 +02001412
William Lallemand246c0242019-10-11 08:59:13 +02001413 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001414 if (!x)
1415 goto out;
1416
William Lallemand246c0242019-10-11 08:59:13 +02001417 issuer = ckch->ocsp_issuer;
1418 if (!issuer)
1419 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001420
1421 cid = OCSP_cert_to_id(0, x, issuer);
1422 if (!cid)
1423 goto out;
1424
1425 i = i2d_OCSP_CERTID(cid, NULL);
1426 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1427 goto out;
1428
Vincent Bernat02779b62016-04-03 13:48:43 +02001429 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001430 if (!ocsp)
1431 goto out;
1432
1433 p = ocsp->key_data;
1434 i2d_OCSP_CERTID(cid, &p);
1435
1436 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1437 if (iocsp == ocsp)
1438 ocsp = NULL;
1439
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001440#ifndef SSL_CTX_get_tlsext_status_cb
1441# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1442 *cb = (void (*) (void))ctx->tlsext_status_cb;
1443#endif
1444 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1445
1446 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001447 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001448 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001449
1450 cb_arg->is_single = 1;
1451 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001452
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001453 pkey = X509_get_pubkey(x);
1454 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1455 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001456
1457 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1458 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1459 } else {
1460 /*
1461 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1462 * Update that cb_arg with the new cert's staple
1463 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001464 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001465 struct certificate_ocsp *tmp_ocsp;
1466 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001467 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001468 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001469
1470#ifdef SSL_CTX_get_tlsext_status_arg
1471 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1472#else
1473 cb_arg = ctx->tlsext_status_arg;
1474#endif
yanbzhube2774d2015-12-10 15:07:30 -05001475
1476 /*
1477 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1478 * the order of operations below matter, take care when changing it
1479 */
1480 tmp_ocsp = cb_arg->s_ocsp;
1481 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1482 cb_arg->s_ocsp = NULL;
1483 cb_arg->m_ocsp[index] = tmp_ocsp;
1484 cb_arg->is_single = 0;
1485 cb_arg->single_kt = 0;
1486
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001487 pkey = X509_get_pubkey(x);
1488 key_type = EVP_PKEY_base_id(pkey);
1489 EVP_PKEY_free(pkey);
1490
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001491 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001492 if (index >= 0 && !cb_arg->m_ocsp[index])
1493 cb_arg->m_ocsp[index] = iocsp;
1494
1495 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001496
1497 ret = 0;
1498
1499 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001500 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001501 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001502 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001503 }
1504
1505out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001506 if (cid)
1507 OCSP_CERTID_free(cid);
1508
1509 if (ocsp)
1510 free(ocsp);
1511
1512 if (warn)
1513 free(warn);
1514
Emeric Brun4147b2e2014-06-16 18:36:30 +02001515 return ret;
1516}
William Lallemand4a660132019-10-14 14:51:41 +02001517#else /* OPENSSL_IS_BORINGSSL */
1518static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001519{
William Lallemand4a660132019-10-14 14:51:41 +02001520 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 +02001521}
1522#endif
1523
William Lallemand4a660132019-10-14 14:51:41 +02001524#endif
1525
1526
Willy Tarreau5db847a2019-05-09 14:13:35 +02001527#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001528
1529#define CT_EXTENSION_TYPE 18
1530
1531static int sctl_ex_index = -1;
1532
1533/*
1534 * Try to parse Signed Certificate Timestamp List structure. This function
1535 * makes only basic test if the data seems like SCTL. No signature validation
1536 * is performed.
1537 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001538static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001539{
1540 int ret = 1;
1541 int len, pos, sct_len;
1542 unsigned char *data;
1543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001544 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001545 goto out;
1546
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001547 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001548 len = (data[0] << 8) | data[1];
1549
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001550 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001551 goto out;
1552
1553 data = data + 2;
1554 pos = 0;
1555 while (pos < len) {
1556 if (len - pos < 2)
1557 goto out;
1558
1559 sct_len = (data[pos] << 8) | data[pos + 1];
1560 if (pos + sct_len + 2 > len)
1561 goto out;
1562
1563 pos += sct_len + 2;
1564 }
1565
1566 ret = 0;
1567
1568out:
1569 return ret;
1570}
1571
William Lallemand0dfae6c2019-10-16 18:06:58 +02001572/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1573 * It fills the ckch->sctl buffer
1574 * return 0 on success or != 0 on failure */
1575static 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 +01001576{
1577 int fd = -1;
1578 int r = 0;
1579 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001580 struct buffer tmp;
1581 struct buffer *src;
1582 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001583
William Lallemand0dfae6c2019-10-16 18:06:58 +02001584 if (buf) {
1585 tmp.area = buf;
1586 tmp.data = strlen(buf);
1587 tmp.size = tmp.data + 1;
1588 src = &tmp;
1589 } else {
1590 fd = open(sctl_path, O_RDONLY);
1591 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001592 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001593
1594 trash.data = 0;
1595 while (trash.data < trash.size) {
1596 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1597 if (r < 0) {
1598 if (errno == EINTR)
1599 continue;
1600 goto end;
1601 }
1602 else if (r == 0) {
1603 break;
1604 }
1605 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001606 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001607 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001608 }
1609
William Lallemand0dfae6c2019-10-16 18:06:58 +02001610 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001611 if (ret)
1612 goto end;
1613
William Lallemand0dfae6c2019-10-16 18:06:58 +02001614 sctl = calloc(1, sizeof(*sctl));
1615 if (!chunk_dup(sctl, src)) {
1616 free(sctl);
1617 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001618 goto end;
1619 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001620 /* no error, fill ckch with new context, old context must be free */
1621 if (ckch->sctl) {
1622 free(ckch->sctl->area);
1623 ckch->sctl->area = NULL;
1624 free(ckch->sctl);
1625 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001626 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001627 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001628end:
1629 if (fd != -1)
1630 close(fd);
1631
1632 return ret;
1633}
1634
1635int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1636{
Willy Tarreau83061a82018-07-13 11:56:34 +02001637 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001638
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001639 *out = (unsigned char *) sctl->area;
1640 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001641
1642 return 1;
1643}
1644
1645int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1646{
1647 return 1;
1648}
1649
William Lallemanda17f4112019-10-10 15:16:44 +02001650static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001651{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001652 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001653
William Lallemanda17f4112019-10-10 15:16:44 +02001654 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 +01001655 goto out;
1656
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001657 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1658
1659 ret = 0;
1660
1661out:
1662 return ret;
1663}
1664
1665#endif
1666
Emeric Brune1f38db2012-09-03 20:36:47 +02001667void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1668{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001669 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001670 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001671 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001672 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001673
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001674#ifndef SSL_OP_NO_RENEGOTIATION
1675 /* Please note that BoringSSL defines this macro to zero so don't
1676 * change this to #if and do not assign a default value to this macro!
1677 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001678 if (where & SSL_CB_HANDSHAKE_START) {
1679 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001680 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 +02001681 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001682 conn->err_code = CO_ER_SSL_RENEG;
1683 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001684 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001685#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001686
1687 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001688 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001689 /* Long certificate chains optimz
1690 If write and read bios are differents, we
1691 consider that the buffering was activated,
1692 so we rise the output buffer size from 4k
1693 to 16k */
1694 write_bio = SSL_get_wbio(ssl);
1695 if (write_bio != SSL_get_rbio(ssl)) {
1696 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001697 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001698 }
1699 }
1700 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001701}
1702
Emeric Brune64aef12012-09-21 13:15:06 +02001703/* Callback is called for each certificate of the chain during a verify
1704 ok is set to 1 if preverify detect no error on current certificate.
1705 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001706int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001707{
1708 SSL *ssl;
1709 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001710 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001711 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001712
1713 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001714 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001715
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001716 ctx = conn->xprt_ctx;
1717
1718 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001719
Emeric Brun81c00f02012-09-21 14:31:21 +02001720 if (ok) /* no errors */
1721 return ok;
1722
1723 depth = X509_STORE_CTX_get_error_depth(x_store);
1724 err = X509_STORE_CTX_get_error(x_store);
1725
1726 /* check if CA error needs to be ignored */
1727 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001728 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1729 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1730 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001731 }
1732
Willy Tarreau731248f2020-02-04 14:02:02 +01001733 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001734 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001735 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001736 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001737 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001738
Willy Tarreau20879a02012-12-03 16:32:10 +01001739 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001740 return 0;
1741 }
1742
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001743 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1744 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001745
Emeric Brun81c00f02012-09-21 14:31:21 +02001746 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001747 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001748 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001749 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001750 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001751 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001752
Willy Tarreau20879a02012-12-03 16:32:10 +01001753 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001754 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001755}
1756
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001757static inline
1758void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001759 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001760{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001761 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001762 unsigned char *msg;
1763 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001764 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001765
1766 /* This function is called for "from client" and "to server"
1767 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001768 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001769 */
1770
1771 /* "write_p" is set to 0 is the bytes are received messages,
1772 * otherwise it is set to 1.
1773 */
1774 if (write_p != 0)
1775 return;
1776
1777 /* content_type contains the type of message received or sent
1778 * according with the SSL/TLS protocol spec. This message is
1779 * encoded with one byte. The value 256 (two bytes) is used
1780 * for designing the SSL/TLS record layer. According with the
1781 * rfc6101, the expected message (other than 256) are:
1782 * - change_cipher_spec(20)
1783 * - alert(21)
1784 * - handshake(22)
1785 * - application_data(23)
1786 * - (255)
1787 * We are interessed by the handshake and specially the client
1788 * hello.
1789 */
1790 if (content_type != 22)
1791 return;
1792
1793 /* The message length is at least 4 bytes, containing the
1794 * message type and the message length.
1795 */
1796 if (len < 4)
1797 return;
1798
1799 /* First byte of the handshake message id the type of
1800 * message. The konwn types are:
1801 * - hello_request(0)
1802 * - client_hello(1)
1803 * - server_hello(2)
1804 * - certificate(11)
1805 * - server_key_exchange (12)
1806 * - certificate_request(13)
1807 * - server_hello_done(14)
1808 * We are interested by the client hello.
1809 */
1810 msg = (unsigned char *)buf;
1811 if (msg[0] != 1)
1812 return;
1813
1814 /* Next three bytes are the length of the message. The total length
1815 * must be this decoded length + 4. If the length given as argument
1816 * is not the same, we abort the protocol dissector.
1817 */
1818 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1819 if (len < rec_len + 4)
1820 return;
1821 msg += 4;
1822 end = msg + rec_len;
1823 if (end < msg)
1824 return;
1825
1826 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1827 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001828 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1829 */
1830 msg += 1 + 1 + 4 + 28;
1831 if (msg > end)
1832 return;
1833
1834 /* Next, is session id:
1835 * if present, we have to jump by length + 1 for the size information
1836 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001837 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001838 if (msg[0] > 0)
1839 msg += msg[0];
1840 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001841 if (msg > end)
1842 return;
1843
1844 /* Next two bytes are the ciphersuite length. */
1845 if (msg + 2 > end)
1846 return;
1847 rec_len = (msg[0] << 8) + msg[1];
1848 msg += 2;
1849 if (msg + rec_len > end || msg + rec_len < msg)
1850 return;
1851
Willy Tarreaubafbe012017-11-24 17:34:44 +01001852 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001853 if (!capture)
1854 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001855 /* Compute the xxh64 of the ciphersuite. */
1856 capture->xxh64 = XXH64(msg, rec_len, 0);
1857
1858 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001859 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1860 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001861 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001862
1863 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001864}
1865
Emeric Brun29f037d2014-04-25 19:05:36 +02001866/* Callback is called for ssl protocol analyse */
1867void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1868{
Emeric Brun29f037d2014-04-25 19:05:36 +02001869#ifdef TLS1_RT_HEARTBEAT
1870 /* test heartbeat received (write_p is set to 0
1871 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001872 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001873 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001874 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001875 const unsigned char *p = buf;
1876 unsigned int payload;
1877
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001878 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001879
1880 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1881 if (*p != TLS1_HB_REQUEST)
1882 return;
1883
Willy Tarreauaeed6722014-04-25 23:59:58 +02001884 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001885 goto kill_it;
1886
1887 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001888 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001889 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001890 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001891 /* We have a clear heartbleed attack (CVE-2014-0160), the
1892 * advertised payload is larger than the advertised packet
1893 * length, so we have garbage in the buffer between the
1894 * payload and the end of the buffer (p+len). We can't know
1895 * if the SSL stack is patched, and we don't know if we can
1896 * safely wipe out the area between p+3+len and payload.
1897 * So instead, we prevent the response from being sent by
1898 * setting the max_send_fragment to 0 and we report an SSL
1899 * error, which will kill this connection. It will be reported
1900 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001901 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1902 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001903 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001904 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1905 return;
1906 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001907#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001908 if (global_ssl.capture_cipherlist > 0)
1909 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001910}
1911
Bernard Spil13c53f82018-02-15 13:34:58 +01001912#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001913static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1914 const unsigned char *in, unsigned int inlen,
1915 void *arg)
1916{
1917 struct server *srv = arg;
1918
1919 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1920 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1921 return SSL_TLSEXT_ERR_OK;
1922 return SSL_TLSEXT_ERR_NOACK;
1923}
1924#endif
1925
1926#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001927/* This callback is used so that the server advertises the list of
1928 * negociable protocols for NPN.
1929 */
1930static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1931 unsigned int *len, void *arg)
1932{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001933 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001934
1935 *data = (const unsigned char *)conf->npn_str;
1936 *len = conf->npn_len;
1937 return SSL_TLSEXT_ERR_OK;
1938}
1939#endif
1940
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001941#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001942/* This callback is used so that the server advertises the list of
1943 * negociable protocols for ALPN.
1944 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001945static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1946 unsigned char *outlen,
1947 const unsigned char *server,
1948 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001949{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001950 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001951
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001952 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1953 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1954 return SSL_TLSEXT_ERR_NOACK;
1955 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001956 return SSL_TLSEXT_ERR_OK;
1957}
1958#endif
1959
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001960#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001961#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001962
Christopher Faulet30548802015-06-11 13:39:32 +02001963/* Create a X509 certificate with the specified servername and serial. This
1964 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001965static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001966ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001967{
Christopher Faulet7969a332015-10-09 11:15:03 +02001968 X509 *cacert = bind_conf->ca_sign_cert;
1969 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001970 SSL_CTX *ssl_ctx = NULL;
1971 X509 *newcrt = NULL;
1972 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001973 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001974 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001975 X509_NAME *name;
1976 const EVP_MD *digest;
1977 X509V3_CTX ctx;
1978 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001979 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001980
Christopher Faulet48a83322017-07-28 16:56:09 +02001981 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001982#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001983 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1984#else
1985 tmp_ssl = SSL_new(bind_conf->default_ctx);
1986 if (tmp_ssl)
1987 pkey = SSL_get_privatekey(tmp_ssl);
1988#endif
1989 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001990 goto mkcert_error;
1991
1992 /* Create the certificate */
1993 if (!(newcrt = X509_new()))
1994 goto mkcert_error;
1995
1996 /* Set version number for the certificate (X509v3) and the serial
1997 * number */
1998 if (X509_set_version(newcrt, 2L) != 1)
1999 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002000 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002001
2002 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002003 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2004 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002005 goto mkcert_error;
2006
2007 /* set public key in the certificate */
2008 if (X509_set_pubkey(newcrt, pkey) != 1)
2009 goto mkcert_error;
2010
2011 /* Set issuer name from the CA */
2012 if (!(name = X509_get_subject_name(cacert)))
2013 goto mkcert_error;
2014 if (X509_set_issuer_name(newcrt, name) != 1)
2015 goto mkcert_error;
2016
2017 /* Set the subject name using the same, but the CN */
2018 name = X509_NAME_dup(name);
2019 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2020 (const unsigned char *)servername,
2021 -1, -1, 0) != 1) {
2022 X509_NAME_free(name);
2023 goto mkcert_error;
2024 }
2025 if (X509_set_subject_name(newcrt, name) != 1) {
2026 X509_NAME_free(name);
2027 goto mkcert_error;
2028 }
2029 X509_NAME_free(name);
2030
2031 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002032 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002033 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2034 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2035 X509_EXTENSION *ext;
2036
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002037 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002038 goto mkcert_error;
2039 if (!X509_add_ext(newcrt, ext, -1)) {
2040 X509_EXTENSION_free(ext);
2041 goto mkcert_error;
2042 }
2043 X509_EXTENSION_free(ext);
2044 }
2045
2046 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002047
2048 key_type = EVP_PKEY_base_id(capkey);
2049
2050 if (key_type == EVP_PKEY_DSA)
2051 digest = EVP_sha1();
2052 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002053 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002054 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002055 digest = EVP_sha256();
2056 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002057#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002058 int nid;
2059
2060 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2061 goto mkcert_error;
2062 if (!(digest = EVP_get_digestbynid(nid)))
2063 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002064#else
2065 goto mkcert_error;
2066#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002067 }
2068
Christopher Faulet31af49d2015-06-09 17:29:50 +02002069 if (!(X509_sign(newcrt, capkey, digest)))
2070 goto mkcert_error;
2071
2072 /* Create and set the new SSL_CTX */
2073 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2074 goto mkcert_error;
2075 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2076 goto mkcert_error;
2077 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2078 goto mkcert_error;
2079 if (!SSL_CTX_check_private_key(ssl_ctx))
2080 goto mkcert_error;
2081
2082 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002083
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002084#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002085 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002086#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002087#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2088 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002089 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002090 EC_KEY *ecc;
2091 int nid;
2092
2093 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2094 goto end;
2095 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2096 goto end;
2097 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2098 EC_KEY_free(ecc);
2099 }
2100#endif
2101 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002102 return ssl_ctx;
2103
2104 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002105 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002106 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002107 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2108 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002109 return NULL;
2110}
2111
Christopher Faulet7969a332015-10-09 11:15:03 +02002112SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002113ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002114{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002115 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002116 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002117
Olivier Houchard66ab4982019-02-26 18:37:15 +01002118 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002119}
2120
Christopher Faulet30548802015-06-11 13:39:32 +02002121/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002122 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002123SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002124ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002125{
2126 struct lru64 *lru = NULL;
2127
2128 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002129 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002130 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002131 if (lru && lru->domain) {
2132 if (ssl)
2133 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002134 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002135 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002136 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002137 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002138 }
2139 return NULL;
2140}
2141
Emeric Brun821bb9b2017-06-15 16:37:39 +02002142/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2143 * function is not thread-safe, it should only be used to check if a certificate
2144 * exists in the lru cache (with no warranty it will not be removed by another
2145 * thread). It is kept for backward compatibility. */
2146SSL_CTX *
2147ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2148{
2149 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2150}
2151
Christopher Fauletd2cab922015-07-28 16:03:47 +02002152/* Set a certificate int the LRU cache used to store generated
2153 * certificate. Return 0 on success, otherwise -1 */
2154int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002155ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002156{
2157 struct lru64 *lru = NULL;
2158
2159 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002160 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002161 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002162 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002164 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002165 }
Christopher Faulet30548802015-06-11 13:39:32 +02002166 if (lru->domain && lru->data)
2167 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002168 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002169 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002170 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002171 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002172 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002173}
2174
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002175/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002176unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002177ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002178{
2179 return XXH32(data, len, ssl_ctx_lru_seed);
2180}
2181
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002182/* Generate a cert and immediately assign it to the SSL session so that the cert's
2183 * refcount is maintained regardless of the cert's presence in the LRU cache.
2184 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002185static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002186ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002187{
2188 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002189 SSL_CTX *ssl_ctx = NULL;
2190 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002191 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002192
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002193 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002194 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002195 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002196 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002197 if (lru && lru->domain)
2198 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002199 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002200 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002201 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002202 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002203 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002204 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002205 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002206 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002207 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002208 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002209 SSL_set_SSL_CTX(ssl, ssl_ctx);
2210 /* No LRU cache, this CTX will be released as soon as the session dies */
2211 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002212 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002213 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002214 return 0;
2215}
2216static int
2217ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2218{
2219 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002220 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002221
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002222 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002223 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002224 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002225 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002226 }
2227 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002228}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002229#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002230
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002231#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002232typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2233
2234static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002235{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002236#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002237 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002238 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2239#endif
2240}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002241static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2242 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002243 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2244}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002245static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002246#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002247 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002248 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2249#endif
2250}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002251static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002252#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002253 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002254 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2255#endif
2256}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002257/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002258static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2259/* Unusable in this context. */
2260static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2261static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2262static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2263static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2264static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002265#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002266typedef enum { SET_MIN, SET_MAX } set_context_func;
2267
2268static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2269 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002270 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2271}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002272static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2273 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2274 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2275}
2276static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2277 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002278 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2279}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002280static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2281 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2282 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2283}
2284static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2285 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002286 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2287}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002288static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2289 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2290 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2291}
2292static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2293 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002294 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2295}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002296static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2297 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2298 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2299}
2300static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002301#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002302 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002303 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2304#endif
2305}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002306static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2307#if SSL_OP_NO_TLSv1_3
2308 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2309 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002310#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002311}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002312#endif
2313static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2314static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002315
2316static struct {
2317 int option;
2318 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002319 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2320 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002321 const char *name;
2322} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002323 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2324 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2325 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2326 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2327 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2328 {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 +02002329};
2330
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002331static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2332{
2333 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2334 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2335 SSL_set_SSL_CTX(ssl, ctx);
2336}
2337
Willy Tarreau5db847a2019-05-09 14:13:35 +02002338#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002339
2340static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2341{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002342 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002343 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002344
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002345 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2346 return SSL_TLSEXT_ERR_OK;
2347 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002348}
2349
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002350#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002351static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2352{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002353 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002354#else
2355static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2356{
2357#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358 struct connection *conn;
2359 struct bind_conf *s;
2360 const uint8_t *extension_data;
2361 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002362 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002363
2364 char *wildp = NULL;
2365 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002366 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002367 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002368 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369 int i;
2370
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002371 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002372 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002373
Olivier Houchard9679ac92017-10-27 14:58:08 +02002374 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002375 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002376#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002377 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2378 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002379#else
2380 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2381#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002382 /*
2383 * The server_name extension was given too much extensibility when it
2384 * was written, so parsing the normal case is a bit complex.
2385 */
2386 size_t len;
2387 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002388 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002389 /* Extract the length of the supplied list of names. */
2390 len = (*extension_data++) << 8;
2391 len |= *extension_data++;
2392 if (len + 2 != extension_len)
2393 goto abort;
2394 /*
2395 * The list in practice only has a single element, so we only consider
2396 * the first one.
2397 */
2398 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2399 goto abort;
2400 extension_len = len - 1;
2401 /* Now we can finally pull out the byte array with the actual hostname. */
2402 if (extension_len <= 2)
2403 goto abort;
2404 len = (*extension_data++) << 8;
2405 len |= *extension_data++;
2406 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2407 || memchr(extension_data, 0, len) != NULL)
2408 goto abort;
2409 servername = extension_data;
2410 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002411 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002412#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2413 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002414 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002415 }
2416#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002417 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002418 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002419 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002420 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002421 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002422 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002423 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002424 goto abort;
2425 }
2426
2427 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002428#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002430#else
2431 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2432#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002433 uint8_t sign;
2434 size_t len;
2435 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002436 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002437 len = (*extension_data++) << 8;
2438 len |= *extension_data++;
2439 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002440 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002441 if (len % 2 != 0)
2442 goto abort;
2443 for (; len > 0; len -= 2) {
2444 extension_data++; /* hash */
2445 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002446 switch (sign) {
2447 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002448 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002449 break;
2450 case TLSEXT_signature_ecdsa:
2451 has_ecdsa_sig = 1;
2452 break;
2453 default:
2454 continue;
2455 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002456 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002457 break;
2458 }
2459 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002460 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002461 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002462 }
2463 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002464 const SSL_CIPHER *cipher;
2465 size_t len;
2466 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002467 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002468#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002469 len = ctx->cipher_suites_len;
2470 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002471#else
2472 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2473#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002474 if (len % 2 != 0)
2475 goto abort;
2476 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002477#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002478 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002479 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002480#else
2481 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2482#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002483 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002484 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002485 break;
2486 }
2487 }
2488 }
2489
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002490 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002491 trash.area[i] = tolower(servername[i]);
2492 if (!wildp && (trash.area[i] == '.'))
2493 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002494 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002495 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002496
William Lallemand150bfa82019-09-19 17:12:49 +02002497 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002498
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002499 for (i = 0; i < 2; i++) {
2500 if (i == 0) /* lookup in full qualified names */
2501 node = ebst_lookup(&s->sni_ctx, trash.area);
2502 else if (i == 1 && wildp) /* lookup in wildcards names */
2503 node = ebst_lookup(&s->sni_w_ctx, wildp);
2504 else
2505 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002506 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002507 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002508 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002509 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002510 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002511 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002512 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002513 break;
2514 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002515 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002516 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002517 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002518 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002519 if (!node_anonymous)
2520 node_anonymous = n;
2521 break;
2522 }
2523 }
2524 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002525 /* select by key_signature priority order */
2526 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2527 : ((has_rsa_sig && node_rsa) ? node_rsa
2528 : (node_anonymous ? node_anonymous
2529 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2530 : node_rsa /* no rsa signature case (far far away) */
2531 )));
2532 if (node) {
2533 /* switch ctx */
2534 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2535 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002536 if (conf) {
2537 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2538 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2539 if (conf->early_data)
2540 allow_early = 1;
2541 }
William Lallemand02010472019-10-18 11:02:19 +02002542 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002543 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002544 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002545 }
William Lallemand150bfa82019-09-19 17:12:49 +02002546
William Lallemand02010472019-10-18 11:02:19 +02002547 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002548#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002549 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002550 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002551 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002552 }
2553#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002554 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002555 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002556 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002557 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002558 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002559 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002560allow_early:
2561#ifdef OPENSSL_IS_BORINGSSL
2562 if (allow_early)
2563 SSL_set_early_data_enabled(ssl, 1);
2564#else
2565 if (!allow_early)
2566 SSL_set_max_early_data(ssl, 0);
2567#endif
2568 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002569 abort:
2570 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2571 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002572#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002573 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002574#else
2575 *al = SSL_AD_UNRECOGNIZED_NAME;
2576 return 0;
2577#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002578}
2579
2580#else /* OPENSSL_IS_BORINGSSL */
2581
Emeric Brunfc0421f2012-09-07 17:30:07 +02002582/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2583 * warning when no match is found, which implies the default (first) cert
2584 * will keep being used.
2585 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002586static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002587{
2588 const char *servername;
2589 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002590 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002591 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002592 int i;
2593 (void)al; /* shut gcc stupid warning */
2594
2595 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002596 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002597#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002598 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2599 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002600#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002601 if (s->strict_sni)
2602 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002603 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002604 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002605 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002606 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002607 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002608
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002609 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002610 if (!servername[i])
2611 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002612 trash.area[i] = tolower(servername[i]);
2613 if (!wildp && (trash.area[i] == '.'))
2614 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002615 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002616 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002617
William Lallemand150bfa82019-09-19 17:12:49 +02002618 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002619 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002620 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002621 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2622 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002623 if (!container_of(n, struct sni_ctx, name)->neg) {
2624 node = n;
2625 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002626 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002627 }
2628 if (!node && wildp) {
2629 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002630 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2631 /* lookup a not neg filter */
2632 if (!container_of(n, struct sni_ctx, name)->neg) {
2633 node = n;
2634 break;
2635 }
2636 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002637 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002638 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002639#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002640 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2641 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002642 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002643 return SSL_TLSEXT_ERR_OK;
2644 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002645#endif
William Lallemand21724f02019-11-04 17:56:13 +01002646 if (s->strict_sni) {
2647 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002648 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002649 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002650 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002651 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002652 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002653 }
2654
2655 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002656 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002657 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002658 return SSL_TLSEXT_ERR_OK;
2659}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002660#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002661#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2662
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002663#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002664
2665static DH * ssl_get_dh_1024(void)
2666{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002667 static unsigned char dh1024_p[]={
2668 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2669 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2670 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2671 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2672 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2673 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2674 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2675 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2676 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2677 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2678 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2679 };
2680 static unsigned char dh1024_g[]={
2681 0x02,
2682 };
2683
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002684 BIGNUM *p;
2685 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002686 DH *dh = DH_new();
2687 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002688 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2689 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002690
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002691 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002692 DH_free(dh);
2693 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002694 } else {
2695 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002696 }
2697 }
2698 return dh;
2699}
2700
2701static DH *ssl_get_dh_2048(void)
2702{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002703 static unsigned char dh2048_p[]={
2704 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2705 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2706 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2707 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2708 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2709 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2710 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2711 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2712 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2713 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2714 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2715 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2716 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2717 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2718 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2719 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2720 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2721 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2722 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2723 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2724 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2725 0xB7,0x1F,0x77,0xF3,
2726 };
2727 static unsigned char dh2048_g[]={
2728 0x02,
2729 };
2730
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002731 BIGNUM *p;
2732 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002733 DH *dh = DH_new();
2734 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002735 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2736 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002737
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002738 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002739 DH_free(dh);
2740 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002741 } else {
2742 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002743 }
2744 }
2745 return dh;
2746}
2747
2748static DH *ssl_get_dh_4096(void)
2749{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002750 static unsigned char dh4096_p[]={
2751 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2752 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2753 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2754 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2755 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2756 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2757 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2758 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2759 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2760 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2761 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2762 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2763 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2764 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2765 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2766 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2767 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2768 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2769 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2770 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2771 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2772 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2773 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2774 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2775 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2776 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2777 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2778 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2779 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2780 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2781 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2782 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2783 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2784 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2785 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2786 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2787 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2788 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2789 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2790 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2791 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2792 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2793 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002794 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002795 static unsigned char dh4096_g[]={
2796 0x02,
2797 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002798
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002799 BIGNUM *p;
2800 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002801 DH *dh = DH_new();
2802 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002803 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2804 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002805
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002806 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002807 DH_free(dh);
2808 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002809 } else {
2810 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002811 }
2812 }
2813 return dh;
2814}
2815
2816/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002817 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002818static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2819{
2820 DH *dh = NULL;
2821 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002822 int type;
2823
2824 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002825
2826 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2827 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2828 */
2829 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2830 keylen = EVP_PKEY_bits(pkey);
2831 }
2832
Willy Tarreauef934602016-12-22 23:12:01 +01002833 if (keylen > global_ssl.default_dh_param) {
2834 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002835 }
2836
Remi Gacogned3a341a2015-05-29 16:26:17 +02002837 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002838 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002839 }
2840 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002841 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002842 }
2843 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002844 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002845 }
2846
2847 return dh;
2848}
2849
Remi Gacogne47783ef2015-05-29 15:53:22 +02002850static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002851{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002852 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002853 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002854
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002855 if (in == NULL)
2856 goto end;
2857
Remi Gacogne47783ef2015-05-29 15:53:22 +02002858 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002859 goto end;
2860
Remi Gacogne47783ef2015-05-29 15:53:22 +02002861 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2862
2863end:
2864 if (in)
2865 BIO_free(in);
2866
Emeric Brune1b4ed42018-08-16 15:14:12 +02002867 ERR_clear_error();
2868
Remi Gacogne47783ef2015-05-29 15:53:22 +02002869 return dh;
2870}
2871
2872int ssl_sock_load_global_dh_param_from_file(const char *filename)
2873{
2874 global_dh = ssl_sock_get_dh_from_file(filename);
2875
2876 if (global_dh) {
2877 return 0;
2878 }
2879
2880 return -1;
2881}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002882#endif
2883
William Lallemand9117de92019-10-04 00:29:42 +02002884/* Alloc and init a ckch_inst */
2885static struct ckch_inst *ckch_inst_new()
2886{
2887 struct ckch_inst *ckch_inst;
2888
2889 ckch_inst = calloc(1, sizeof *ckch_inst);
2890 if (ckch_inst)
2891 LIST_INIT(&ckch_inst->sni_ctx);
2892
2893 return ckch_inst;
2894}
2895
2896
2897/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002898static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002899 struct bind_conf *s, struct ssl_bind_conf *conf,
2900 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002901{
2902 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002903 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002904
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002905 if (*name == '!') {
2906 neg = 1;
2907 name++;
2908 }
2909 if (*name == '*') {
2910 wild = 1;
2911 name++;
2912 }
2913 /* !* filter is a nop */
2914 if (neg && wild)
2915 return order;
2916 if (*name) {
2917 int j, len;
2918 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002919 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002920 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002921 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002922 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002923 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002924
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002925 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002926 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002927 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002928 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002929 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002930 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002931 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002932 sc->order = order++;
2933 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002934 sc->wild = wild;
2935 sc->name.node.leaf_p = NULL;
William Lallemand1d29c742019-10-04 00:53:29 +02002936 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002937 }
2938 return order;
2939}
2940
William Lallemand6af03992019-07-23 15:00:54 +02002941/*
William Lallemand1d29c742019-10-04 00:53:29 +02002942 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2943 * This function can't return an error.
2944 *
2945 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2946 */
2947static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2948{
2949
2950 struct sni_ctx *sc0, *sc0b, *sc1;
2951 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002952 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002953
2954 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2955
2956 /* ignore if sc0 was already inserted in a tree */
2957 if (sc0->name.node.leaf_p)
2958 continue;
2959
2960 /* Check for duplicates. */
2961 if (sc0->wild)
2962 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2963 else
2964 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2965
2966 for (; node; node = ebmb_next_dup(node)) {
2967 sc1 = ebmb_entry(node, struct sni_ctx, name);
2968 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2969 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2970 /* it's a duplicate, we should remove and free it */
2971 LIST_DEL(&sc0->by_ckch_inst);
2972 free(sc0);
2973 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002974 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002975 }
2976 }
2977
2978 /* if duplicate, ignore the insertion */
2979 if (!sc0)
2980 continue;
2981
2982 if (sc0->wild)
2983 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2984 else
2985 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002986
2987 /* replace the default_ctx if required with the first ctx */
2988 if (ckch_inst->is_default && !def) {
2989 /* we don't need to free the default_ctx because the refcount was not incremented */
2990 bind_conf->default_ctx = sc0->ctx;
2991 def = 1;
2992 }
William Lallemand1d29c742019-10-04 00:53:29 +02002993 }
2994}
2995
2996/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002997 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002998 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002999struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003000
William Lallemandfa892222019-07-23 16:06:08 +02003001
Emeric Brun7a883362019-10-17 13:27:40 +02003002/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
3003 * If there is no DH paramater availaible in the ckchs, the global
3004 * DH parameter is loaded into the SSL_CTX and if there is no
3005 * DH parameter available in ckchs nor in global, the default
3006 * DH parameters are applied on the SSL_CTX.
3007 * Returns a bitfield containing the flags:
3008 * ERR_FATAL in any fatal error case
3009 * ERR_ALERT if a reason of the error is availabine in err
3010 * ERR_WARN if a warning is available into err
3011 * The value 0 means there is no error nor warning and
3012 * the operation succeed.
3013 */
William Lallemandfa892222019-07-23 16:06:08 +02003014#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003015static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3016 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003017{
Emeric Brun7a883362019-10-17 13:27:40 +02003018 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003019 DH *dh = NULL;
3020
William Lallemanda8c73742019-07-31 18:31:34 +02003021 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003022 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003023 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3024 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3025 err && *err ? *err : "", path);
3026#if defined(SSL_CTX_set_dh_auto)
3027 SSL_CTX_set_dh_auto(ctx, 1);
3028 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3029 err && *err ? *err : "");
3030#else
3031 memprintf(err, "%s, DH ciphers won't be available.\n",
3032 err && *err ? *err : "");
3033#endif
3034 ret |= ERR_WARN;
3035 goto end;
3036 }
William Lallemandfa892222019-07-23 16:06:08 +02003037
3038 if (ssl_dh_ptr_index >= 0) {
3039 /* store a pointer to the DH params to avoid complaining about
3040 ssl-default-dh-param not being set for this SSL_CTX */
3041 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3042 }
3043 }
3044 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003045 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3046 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3047 err && *err ? *err : "", path);
3048#if defined(SSL_CTX_set_dh_auto)
3049 SSL_CTX_set_dh_auto(ctx, 1);
3050 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3051 err && *err ? *err : "");
3052#else
3053 memprintf(err, "%s, DH ciphers won't be available.\n",
3054 err && *err ? *err : "");
3055#endif
3056 ret |= ERR_WARN;
3057 goto end;
3058 }
William Lallemandfa892222019-07-23 16:06:08 +02003059 }
3060 else {
3061 /* Clear openssl global errors stack */
3062 ERR_clear_error();
3063
3064 if (global_ssl.default_dh_param <= 1024) {
3065 /* we are limited to DH parameter of 1024 bits anyway */
3066 if (local_dh_1024 == NULL)
3067 local_dh_1024 = ssl_get_dh_1024();
3068
Emeric Brun7a883362019-10-17 13:27:40 +02003069 if (local_dh_1024 == NULL) {
3070 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3071 err && *err ? *err : "", path);
3072 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003073 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003074 }
William Lallemandfa892222019-07-23 16:06:08 +02003075
Emeric Bruna9363eb2019-10-17 14:53:03 +02003076 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3077 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3078 err && *err ? *err : "", path);
3079#if defined(SSL_CTX_set_dh_auto)
3080 SSL_CTX_set_dh_auto(ctx, 1);
3081 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3082 err && *err ? *err : "");
3083#else
3084 memprintf(err, "%s, DH ciphers won't be available.\n",
3085 err && *err ? *err : "");
3086#endif
3087 ret |= ERR_WARN;
3088 goto end;
3089 }
William Lallemandfa892222019-07-23 16:06:08 +02003090 }
3091 else {
3092 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3093 }
William Lallemandfa892222019-07-23 16:06:08 +02003094 }
3095
3096end:
William Lallemandfa892222019-07-23 16:06:08 +02003097 return ret;
3098}
3099#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003100
yanbzhu488a4d22015-12-01 15:16:07 -05003101/* Frees the contents of a cert_key_and_chain
3102 */
3103static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3104{
yanbzhu488a4d22015-12-01 15:16:07 -05003105 if (!ckch)
3106 return;
3107
3108 /* Free the certificate and set pointer to NULL */
3109 if (ckch->cert)
3110 X509_free(ckch->cert);
3111 ckch->cert = NULL;
3112
3113 /* Free the key and set pointer to NULL */
3114 if (ckch->key)
3115 EVP_PKEY_free(ckch->key);
3116 ckch->key = NULL;
3117
3118 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003119 if (ckch->chain)
3120 sk_X509_pop_free(ckch->chain, X509_free);
3121 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003122
William Lallemand455af502019-10-17 18:04:45 +02003123 if (ckch->dh)
3124 DH_free(ckch->dh);
3125 ckch->dh = NULL;
3126
3127 if (ckch->sctl) {
3128 free(ckch->sctl->area);
3129 ckch->sctl->area = NULL;
3130 free(ckch->sctl);
3131 ckch->sctl = NULL;
3132 }
3133
3134 if (ckch->ocsp_response) {
3135 free(ckch->ocsp_response->area);
3136 ckch->ocsp_response->area = NULL;
3137 free(ckch->ocsp_response);
3138 ckch->ocsp_response = NULL;
3139 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003140
3141 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003142 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003143 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003144}
3145
William Lallemand8d0f8932019-10-17 18:03:58 +02003146/*
3147 *
3148 * This function copy a cert_key_and_chain in memory
3149 *
3150 * It's used to try to apply changes on a ckch before committing them, because
3151 * most of the time it's not possible to revert those changes
3152 *
3153 * Return a the dst or NULL
3154 */
3155static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3156 struct cert_key_and_chain *dst)
3157{
3158 if (src->cert) {
3159 dst->cert = src->cert;
3160 X509_up_ref(src->cert);
3161 }
3162
3163 if (src->key) {
3164 dst->key = src->key;
3165 EVP_PKEY_up_ref(src->key);
3166 }
3167
3168 if (src->chain) {
3169 dst->chain = X509_chain_up_ref(src->chain);
3170 }
3171
3172 if (src->dh) {
3173 DH_up_ref(src->dh);
3174 dst->dh = src->dh;
3175 }
3176
3177 if (src->sctl) {
3178 struct buffer *sctl;
3179
3180 sctl = calloc(1, sizeof(*sctl));
3181 if (!chunk_dup(sctl, src->sctl)) {
3182 free(sctl);
3183 sctl = NULL;
3184 goto error;
3185 }
3186 dst->sctl = sctl;
3187 }
3188
3189 if (src->ocsp_response) {
3190 struct buffer *ocsp_response;
3191
3192 ocsp_response = calloc(1, sizeof(*ocsp_response));
3193 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3194 free(ocsp_response);
3195 ocsp_response = NULL;
3196 goto error;
3197 }
3198 dst->ocsp_response = ocsp_response;
3199 }
3200
3201 if (src->ocsp_issuer) {
3202 X509_up_ref(src->ocsp_issuer);
3203 dst->ocsp_issuer = src->ocsp_issuer;
3204 }
3205
3206 return dst;
3207
3208error:
3209
3210 /* free everything */
3211 ssl_sock_free_cert_key_and_chain_contents(dst);
3212
3213 return NULL;
3214}
3215
3216
yanbzhu488a4d22015-12-01 15:16:07 -05003217/* checks if a key and cert exists in the ckch
3218 */
William Lallemand1633e392019-09-30 12:58:13 +02003219#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003220static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3221{
3222 return (ckch->cert != NULL && ckch->key != NULL);
3223}
William Lallemand1633e392019-09-30 12:58:13 +02003224#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003225
William Lallemandf9568fc2019-10-16 18:27:58 +02003226/*
3227 * return 0 on success or != 0 on failure
3228 */
3229static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3230{
3231 int ret = 1;
3232 BIO *in = NULL;
3233 X509 *issuer;
3234
3235 if (buf) {
3236 /* reading from a buffer */
3237 in = BIO_new_mem_buf(buf, -1);
3238 if (in == NULL) {
3239 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3240 goto end;
3241 }
3242
3243 } else {
3244 /* reading from a file */
3245 in = BIO_new(BIO_s_file());
3246 if (in == NULL)
3247 goto end;
3248
3249 if (BIO_read_filename(in, path) <= 0)
3250 goto end;
3251 }
3252
3253 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3254 if (!issuer) {
3255 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003256 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003257 goto end;
3258 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003259 /* no error, fill ckch with new context, old context must be free */
3260 if (ckch->ocsp_issuer)
3261 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003262 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003263 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003264
3265end:
3266
3267 ERR_clear_error();
3268 if (in)
3269 BIO_free(in);
3270
3271 return ret;
3272}
3273
William Lallemand96a9c972019-10-17 11:56:17 +02003274
3275/*
3276 * Try to load a PEM file from a <path> or a buffer <buf>
3277 * The PEM must contain at least a Private Key and a Certificate,
3278 * It could contain a DH and a certificate chain.
yanbzhu488a4d22015-12-01 15:16:07 -05003279 *
William Lallemand96a9c972019-10-17 11:56:17 +02003280 * If it failed you should not attempt to use the ckch but free it.
3281 *
3282 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003283 */
William Lallemand96a9c972019-10-17 11:56:17 +02003284static 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 -05003285{
William Lallemandf11365b2019-09-19 14:25:58 +02003286 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003287 int ret = 1;
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003288 int i;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003289 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003290 X509 *cert = NULL;
3291 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003292 DH *dh = NULL;
3293 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003294
3295 if (buf) {
3296 /* reading from a buffer */
3297 in = BIO_new_mem_buf(buf, -1);
3298 if (in == NULL) {
3299 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3300 goto end;
3301 }
yanbzhu488a4d22015-12-01 15:16:07 -05003302
William Lallemand96a9c972019-10-17 11:56:17 +02003303 } else {
3304 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003305 in = BIO_new(BIO_s_file());
3306 if (in == NULL)
3307 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003308
William Lallemandf11365b2019-09-19 14:25:58 +02003309 if (BIO_read_filename(in, path) <= 0)
3310 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003311 }
yanbzhu488a4d22015-12-01 15:16:07 -05003312
yanbzhu488a4d22015-12-01 15:16:07 -05003313 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003314 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3315 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003316 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003317 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003318 goto end;
3319 }
3320
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003321#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003322 /* Seek back to beginning of file */
3323 if (BIO_reset(in) == -1) {
3324 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3325 err && *err ? *err : "", path);
3326 goto end;
3327 }
3328
William Lallemand96a9c972019-10-17 11:56:17 +02003329 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3330 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003331#endif
William Lallemandfa892222019-07-23 16:06:08 +02003332
Willy Tarreaubb137a82016-04-06 19:02:38 +02003333 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003334 if (BIO_reset(in) == -1) {
3335 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3336 err && *err ? *err : "", path);
3337 goto end;
3338 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003339
3340 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003341 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3342 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003343 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003344 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003345 goto end;
3346 }
3347
William Lallemand96a9c972019-10-17 11:56:17 +02003348 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003349 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003350 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003351 goto end;
3352 }
3353
William Lallemand96a9c972019-10-17 11:56:17 +02003354 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003355 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3356 if (chain == NULL)
3357 chain = sk_X509_new_null();
3358 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003359 X509_free(ca);
3360 goto end;
3361 }
3362 }
yanbzhu488a4d22015-12-01 15:16:07 -05003363
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003364 /* no chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003365 if (chain == NULL) {
3366 chain = sk_X509_new_null();
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003367 }
3368
yanbzhu488a4d22015-12-01 15:16:07 -05003369 ret = ERR_get_error();
3370 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3371 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003372 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003373 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003374 }
3375
William Lallemand75b15f72020-01-23 10:56:05 +01003376 /* once it loaded the PEM, it should remove everything else in the ckch */
3377 if (ckch->ocsp_response) {
3378 free(ckch->ocsp_response->area);
3379 ckch->ocsp_response->area = NULL;
3380 free(ckch->ocsp_response);
3381 ckch->ocsp_response = NULL;
3382 }
3383
3384 if (ckch->sctl) {
3385 free(ckch->sctl->area);
3386 ckch->sctl->area = NULL;
3387 free(ckch->sctl);
3388 ckch->sctl = NULL;
3389 }
3390
3391 if (ckch->ocsp_issuer) {
3392 X509_free(ckch->ocsp_issuer);
3393 ckch->ocsp_issuer = NULL;
3394 }
3395
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003396 /* no error, fill ckch with new context, old context will be free at end: */
3397 SWAP(ckch->key, key);
3398 SWAP(ckch->dh, dh);
3399 SWAP(ckch->cert, cert);
3400 SWAP(ckch->chain, chain);
3401
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003402 /* check if one of the certificate of the chain is the issuer */
3403 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3404 X509 *issuer = sk_X509_value(ckch->chain, i);
3405 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3406 ckch->ocsp_issuer = issuer;
3407 X509_up_ref(issuer);
3408 break;
3409 }
3410 }
William Lallemand246c0242019-10-11 08:59:13 +02003411 ret = 0;
3412
William Lallemand96a9c972019-10-17 11:56:17 +02003413end:
William Lallemand246c0242019-10-11 08:59:13 +02003414
3415 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003416 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003417 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003418 if (key)
3419 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003420 if (dh)
3421 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003422 if (cert)
3423 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003424 if (chain)
3425 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003426
William Lallemand96a9c972019-10-17 11:56:17 +02003427 return ret;
3428}
3429
3430/*
3431 * Try to load in a ckch every files related to a ckch.
3432 * (PEM, sctl, ocsp, issuer etc.)
3433 *
3434 * This function is only used to load files during the configuration parsing,
3435 * it is not used with the CLI.
3436 *
3437 * This allows us to carry the contents of the file without having to read the
3438 * file multiple times. The caller must call
3439 * ssl_sock_free_cert_key_and_chain_contents.
3440 *
3441 * returns:
3442 * 0 on Success
3443 * 1 on SSL Failure
3444 */
3445static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3446{
3447 int ret = 1;
3448
3449 /* try to load the PEM */
3450 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3451 goto end;
3452 }
3453
William Lallemanda17f4112019-10-10 15:16:44 +02003454#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3455 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003456 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003457 char fp[MAXPATHLEN+1];
3458 struct stat st;
3459
3460 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3461 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003462 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003463 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003464 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003465 ret = 1;
3466 goto end;
3467 }
3468 }
3469 }
3470#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003471
William Lallemand246c0242019-10-11 08:59:13 +02003472 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003473 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003474 char fp[MAXPATHLEN+1];
3475 struct stat st;
3476
3477 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3478 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003479 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003480 ret = 1;
3481 goto end;
3482 }
3483 }
3484 }
3485
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003486#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003487 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003488 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003489 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003490 struct stat st;
3491 char fp[MAXPATHLEN+1];
3492
3493 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3494 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003495 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003496 ret = 1;
3497 goto end;
3498 }
3499
3500 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003501 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003502 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003503 ret = 1;
3504 goto end;
3505 }
3506 } else {
3507 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003508 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003509 ret = 1;
3510 goto end;
3511 }
3512 }
3513 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003514#endif
William Lallemand246c0242019-10-11 08:59:13 +02003515
yanbzhu488a4d22015-12-01 15:16:07 -05003516 ret = 0;
3517
3518end:
3519
3520 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003521
3522 /* Something went wrong in one of the reads */
3523 if (ret != 0)
3524 ssl_sock_free_cert_key_and_chain_contents(ckch);
3525
3526 return ret;
3527}
3528
3529/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003530 * Returns a bitfield containing the flags:
3531 * ERR_FATAL in any fatal error case
3532 * ERR_ALERT if the reason of the error is available in err
3533 * ERR_WARN if a warning is available into err
3534 * The value 0 means there is no error nor warning and
3535 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003536 */
3537static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3538{
Emeric Bruna96b5822019-10-17 13:25:14 +02003539 int errcode = 0;
3540
yanbzhu488a4d22015-12-01 15:16:07 -05003541 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3542 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3543 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003544 errcode |= ERR_ALERT | ERR_FATAL;
3545 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003546 }
3547
3548 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3549 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3550 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003551 errcode |= ERR_ALERT | ERR_FATAL;
3552 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003553 }
3554
yanbzhu488a4d22015-12-01 15:16:07 -05003555 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003556#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003557 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3558 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3559 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003560 errcode |= ERR_ALERT | ERR_FATAL;
3561 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003562 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003563#else
3564 { /* legacy compat (< openssl 1.0.2) */
3565 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003566 STACK_OF(X509) *chain;
3567 chain = X509_chain_up_ref(ckch->chain);
3568 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003569 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3570 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3571 err && *err ? *err : "", path);
3572 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003573 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003574 errcode |= ERR_ALERT | ERR_FATAL;
3575 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003576 }
3577 }
3578#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003579
William Lallemandfa892222019-07-23 16:06:08 +02003580#ifndef OPENSSL_NO_DH
3581 /* store a NULL pointer to indicate we have not yet loaded
3582 a custom DH param file */
3583 if (ssl_dh_ptr_index >= 0) {
3584 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3585 }
3586
Emeric Brun7a883362019-10-17 13:27:40 +02003587 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3588 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003589 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3590 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003591 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003592 }
3593#endif
3594
William Lallemanda17f4112019-10-10 15:16:44 +02003595#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3596 if (sctl_ex_index >= 0 && ckch->sctl) {
3597 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3598 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003599 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003600 errcode |= ERR_ALERT | ERR_FATAL;
3601 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003602 }
3603 }
3604#endif
3605
William Lallemand4a660132019-10-14 14:51:41 +02003606#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003607 /* Load OCSP Info into context */
3608 if (ckch->ocsp_response) {
3609 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003610 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",
3611 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003612 errcode |= ERR_ALERT | ERR_FATAL;
3613 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003614 }
3615 }
William Lallemand246c0242019-10-11 08:59:13 +02003616#endif
3617
Emeric Bruna96b5822019-10-17 13:25:14 +02003618 end:
3619 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003620}
3621
William Lallemandc4ecddf2019-07-31 16:50:08 +02003622#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003623
William Lallemand28a8fce2019-10-04 17:36:55 +02003624static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003625{
3626 struct sni_keytype *s_kt = NULL;
3627 struct ebmb_node *node;
3628 int i;
3629
3630 for (i = 0; i < trash.size; i++) {
3631 if (!str[i])
3632 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003633 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003634 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003635 trash.area[i] = 0;
3636 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003637 if (!node) {
3638 /* CN not found in tree */
3639 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3640 /* Using memcpy here instead of strncpy.
3641 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3642 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3643 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003644 if (!s_kt)
3645 return -1;
3646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003647 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003648 s_kt->keytypes = 0;
3649 ebst_insert(sni_keytypes, &s_kt->name);
3650 } else {
3651 /* CN found in tree */
3652 s_kt = container_of(node, struct sni_keytype, name);
3653 }
3654
3655 /* Mark that this CN has the keytype of key_index via keytypes mask */
3656 s_kt->keytypes |= 1<<key_index;
3657
William Lallemand28a8fce2019-10-04 17:36:55 +02003658 return 0;
3659
yanbzhu08ce6ab2015-12-02 13:01:29 -05003660}
3661
William Lallemandc4ecddf2019-07-31 16:50:08 +02003662#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003663/*
3664 * Free a ckch_store and its ckch(s)
3665 * The linked ckch_inst are not free'd
3666 */
3667void ckchs_free(struct ckch_store *ckchs)
3668{
3669 if (!ckchs)
3670 return;
3671
3672#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3673 if (ckchs->multi) {
3674 int n;
3675
3676 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3677 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3678 } else
3679#endif
3680 {
3681 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3682 ckchs->ckch = NULL;
3683 }
3684
3685 free(ckchs);
3686}
3687
3688/* allocate and duplicate a ckch_store
3689 * Return a new ckch_store or NULL */
3690static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3691{
3692 struct ckch_store *dst;
3693 int pathlen;
3694
3695 pathlen = strlen(src->path);
3696 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3697 if (!dst)
3698 return NULL;
3699 /* copy previous key */
3700 memcpy(dst->path, src->path, pathlen + 1);
3701 dst->multi = src->multi;
3702 LIST_INIT(&dst->ckch_inst);
3703
3704 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3705 if (!dst->ckch)
3706 goto error;
3707
3708#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3709 if (src->multi) {
3710 int n;
3711
3712 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3713 if (&src->ckch[n]) {
3714 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3715 goto error;
3716 }
3717 }
3718 } else
3719#endif
3720 {
3721 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3722 goto error;
3723 }
3724
3725 return dst;
3726
3727error:
3728 ckchs_free(dst);
3729
3730 return NULL;
3731}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003732
William Lallemand36b84632019-07-18 19:28:17 +02003733/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003734 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003735 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003736static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003737{
3738 struct ebmb_node *eb;
3739
William Lallemande3af8fb2019-10-08 11:36:53 +02003740 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003741 if (!eb)
3742 return NULL;
3743
William Lallemande3af8fb2019-10-08 11:36:53 +02003744 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003745}
3746
3747/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003748 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003749 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003750static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003751{
William Lallemande3af8fb2019-10-08 11:36:53 +02003752 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003753
William Lallemande3af8fb2019-10-08 11:36:53 +02003754 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3755 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003756 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3757 goto end;
3758 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003759 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003760
William Lallemande3af8fb2019-10-08 11:36:53 +02003761 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003762 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3763 goto end;
3764 }
3765
William Lallemand9117de92019-10-04 00:29:42 +02003766 LIST_INIT(&ckchs->ckch_inst);
3767
William Lallemand36b84632019-07-18 19:28:17 +02003768 if (!multi) {
3769
William Lallemand96a9c972019-10-17 11:56:17 +02003770 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003771 goto end;
3772
William Lallemande3af8fb2019-10-08 11:36:53 +02003773 /* insert into the ckchs tree */
3774 memcpy(ckchs->path, path, strlen(path) + 1);
3775 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003776 } else {
3777 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003778#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3779 char fp[MAXPATHLEN+1] = {0};
3780 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003781
3782 /* Load all possible certs and keys */
3783 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3784 struct stat buf;
3785 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3786 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003787 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003788 goto end;
3789 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003790 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003791 }
3792 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003793#endif
William Lallemand36b84632019-07-18 19:28:17 +02003794
3795 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003796 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003797 goto end;
3798 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003799 /* insert into the ckchs tree */
3800 memcpy(ckchs->path, path, strlen(path) + 1);
3801 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003802 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003803 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003804
William Lallemand36b84632019-07-18 19:28:17 +02003805end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003806 if (ckchs) {
3807 free(ckchs->ckch);
3808 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003809 }
3810
William Lallemande3af8fb2019-10-08 11:36:53 +02003811 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003812
3813 return NULL;
3814}
3815
William Lallemandc4ecddf2019-07-31 16:50:08 +02003816#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3817
William Lallemand36b84632019-07-18 19:28:17 +02003818/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003819 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003820 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003821 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3822 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003823 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003824 *
Emeric Brun054563d2019-10-17 13:16:58 +02003825 * Returns a bitfield containing the flags:
3826 * ERR_FATAL in any fatal error case
3827 * ERR_ALERT if the reason of the error is available in err
3828 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003829 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003830 */
Emeric Brun054563d2019-10-17 13:16:58 +02003831static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3832 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3833 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003834{
William Lallemand36b84632019-07-18 19:28:17 +02003835 int i = 0, n = 0;
3836 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003837 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003838 struct ebmb_node *node;
3839 struct ebmb_node *next;
3840 /* Array of SSL_CTX pointers corresponding to each possible combo
3841 * of keytypes
3842 */
3843 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003844 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003845 X509_NAME *xname = NULL;
3846 char *str = NULL;
3847#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3848 STACK_OF(GENERAL_NAME) *names = NULL;
3849#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003850 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003851
Emeric Brun054563d2019-10-17 13:16:58 +02003852 *ckchi = NULL;
3853
William Lallemande3af8fb2019-10-08 11:36:53 +02003854 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003855 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3856 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003857 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003858 }
3859
3860 ckch_inst = ckch_inst_new();
3861 if (!ckch_inst) {
3862 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3863 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003864 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003865 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003866 }
3867
William Lallemande3af8fb2019-10-08 11:36:53 +02003868 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003869
William Lallemand150bfa82019-09-19 17:12:49 +02003870 /* at least one of the instances is using filters during the config
3871 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003872 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003873
yanbzhu08ce6ab2015-12-02 13:01:29 -05003874 /* Process each ckch and update keytypes for each CN/SAN
3875 * for example, if CN/SAN www.a.com is associated with
3876 * certs with keytype 0 and 2, then at the end of the loop,
3877 * www.a.com will have:
3878 * keyindex = 0 | 1 | 4 = 5
3879 */
3880 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003881 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003882
3883 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3884 continue;
3885
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003886 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003887 for (i = 0; i < fcount; i++) {
3888 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3889 if (ret < 0) {
3890 memprintf(err, "%sunable to allocate SSL context.\n",
3891 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003892 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003893 goto end;
3894 }
3895 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003896 } else {
3897 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3898 * so the line that contains logic is marked via comments
3899 */
3900 xname = X509_get_subject_name(certs_and_keys[n].cert);
3901 i = -1;
3902 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3903 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003904 ASN1_STRING *value;
3905 value = X509_NAME_ENTRY_get_data(entry);
3906 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003907 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003908 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003909
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003910 OPENSSL_free(str);
3911 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003912 if (ret < 0) {
3913 memprintf(err, "%sunable to allocate SSL context.\n",
3914 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003915 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003916 goto end;
3917 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003918 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003919 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003920
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003921 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003922#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003923 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3924 if (names) {
3925 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3926 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003927
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003928 if (name->type == GEN_DNS) {
3929 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3930 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003931 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003932
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003933 OPENSSL_free(str);
3934 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003935 if (ret < 0) {
3936 memprintf(err, "%sunable to allocate SSL context.\n",
3937 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003938 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003939 goto end;
3940 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003941 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003942 }
3943 }
3944 }
3945 }
3946#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3947 }
3948
3949 /* If no files found, return error */
3950 if (eb_is_empty(&sni_keytypes_map)) {
3951 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3952 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003953 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003954 goto end;
3955 }
3956
3957 /* We now have a map of CN/SAN to keytypes that are loaded in
3958 * Iterate through the map to create the SSL_CTX's (if needed)
3959 * and add each CTX to the SNI tree
3960 *
3961 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003962 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003963 * combination is denoted by the key in the map. Each key
3964 * has a value between 1 and 2^n - 1. Conveniently, the array
3965 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3966 * entry in the array to correspond to the unique combo (key)
3967 * associated with i. This unique key combo (i) will be associated
3968 * with combos[i-1]
3969 */
3970
3971 node = ebmb_first(&sni_keytypes_map);
3972 while (node) {
3973 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003974 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003975 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003976
3977 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3978 i = container_of(node, struct sni_keytype, name)->keytypes;
3979 cur_ctx = key_combos[i-1].ctx;
3980
3981 if (cur_ctx == NULL) {
3982 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003983 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003984 if (cur_ctx == NULL) {
3985 memprintf(err, "%sunable to allocate SSL context.\n",
3986 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003987 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003988 goto end;
3989 }
3990
yanbzhube2774d2015-12-10 15:07:30 -05003991 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003992 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3993 if (i & (1<<n)) {
3994 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003995 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003996 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3997 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003998 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003999 }
4000 }
4001
yanbzhu08ce6ab2015-12-02 13:01:29 -05004002 /* Update key_combos */
4003 key_combos[i-1].ctx = cur_ctx;
4004 }
4005
4006 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004007
William Lallemand1d29c742019-10-04 00:53:29 +02004008 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 +02004009 kinfo, str, key_combos[i-1].order);
4010 if (key_combos[i-1].order < 0) {
4011 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004012 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004013 goto end;
4014 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004015 node = ebmb_next(node);
4016 }
4017
4018
4019 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4020 if (!bind_conf->default_ctx) {
4021 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4022 if (key_combos[i].ctx) {
4023 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004024 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004025 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004026 break;
4027 }
4028 }
4029 }
4030
William Lallemand614ca0d2019-10-07 13:52:11 +02004031 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004032 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004033end:
4034
4035 if (names)
4036 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4037
yanbzhu08ce6ab2015-12-02 13:01:29 -05004038 node = ebmb_first(&sni_keytypes_map);
4039 while (node) {
4040 next = ebmb_next(node);
4041 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004042 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004043 node = next;
4044 }
4045
Emeric Brun054563d2019-10-17 13:16:58 +02004046 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004047 struct sni_ctx *sc0, *sc0b;
4048
4049 /* free the SSL_CTX in case of error */
4050 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4051 if (key_combos[i].ctx)
4052 SSL_CTX_free(key_combos[i].ctx);
4053 }
4054
4055 /* free the sni_ctx in case of error */
4056 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4057
4058 ebmb_delete(&sc0->name);
4059 LIST_DEL(&sc0->by_ckch_inst);
4060 free(sc0);
4061 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004062 free(ckch_inst);
4063 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004064 }
4065
Emeric Brun054563d2019-10-17 13:16:58 +02004066 *ckchi = ckch_inst;
4067 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004068}
4069#else
4070/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004071static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4072 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4073 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004074{
4075 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4076 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004077 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004078}
4079
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004080#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004081
William Lallemand614ca0d2019-10-07 13:52:11 +02004082/*
4083 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004084 *
4085 * Returns a bitfield containing the flags:
4086 * ERR_FATAL in any fatal error case
4087 * ERR_ALERT if the reason of the error is available in err
4088 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004089 */
Emeric Brun054563d2019-10-17 13:16:58 +02004090static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4091 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004092{
William Lallemandc9402072019-05-15 15:33:54 +02004093 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004094 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004095 int order = 0;
4096 X509_NAME *xname;
4097 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004098 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004099 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004100#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4101 STACK_OF(GENERAL_NAME) *names;
4102#endif
William Lallemand36b84632019-07-18 19:28:17 +02004103 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004104 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004105 int errcode = 0;
4106
4107 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004108
William Lallemande3af8fb2019-10-08 11:36:53 +02004109 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004110 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004111
William Lallemande3af8fb2019-10-08 11:36:53 +02004112 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004113
William Lallemand150bfa82019-09-19 17:12:49 +02004114 /* at least one of the instances is using filters during the config
4115 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004116 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004117
William Lallemandc9402072019-05-15 15:33:54 +02004118 ctx = SSL_CTX_new(SSLv23_server_method());
4119 if (!ctx) {
4120 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4121 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004122 errcode |= ERR_ALERT | ERR_FATAL;
4123 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004124 }
4125
Emeric Bruna96b5822019-10-17 13:25:14 +02004126 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4127 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004128 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004129
4130 ckch_inst = ckch_inst_new();
4131 if (!ckch_inst) {
4132 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4133 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004134 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004135 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004136 }
4137
William Lallemand36b84632019-07-18 19:28:17 +02004138 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004139 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004140 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004141 switch(EVP_PKEY_base_id(pkey)) {
4142 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004143 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004144 break;
4145 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004146 kinfo.sig = TLSEXT_signature_ecdsa;
4147 break;
4148 case EVP_PKEY_DSA:
4149 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004150 break;
4151 }
4152 EVP_PKEY_free(pkey);
4153 }
4154
Emeric Brun50bcecc2013-04-22 13:05:23 +02004155 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004156 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004157 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 +02004158 if (order < 0) {
4159 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004160 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004161 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004162 }
4163 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004164 }
4165 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004166#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004167 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004168 if (names) {
4169 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4170 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4171 if (name->type == GEN_DNS) {
4172 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004173 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004174 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004175 if (order < 0) {
4176 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004177 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004178 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004179 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004180 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004181 }
4182 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004183 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004184 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004185#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004186 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004187 i = -1;
4188 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4189 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004190 ASN1_STRING *value;
4191
4192 value = X509_NAME_ENTRY_get_data(entry);
4193 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004194 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004195 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004196 if (order < 0) {
4197 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004198 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004199 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004200 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004201 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004202 }
4203 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004204 /* we must not free the SSL_CTX anymore below, since it's already in
4205 * the tree, so it will be discovered and cleaned in time.
4206 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004207
Emeric Brunfc0421f2012-09-07 17:30:07 +02004208#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004209 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004210 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4211 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004212 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004213 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004214 }
4215#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004216 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004217 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004218 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004219 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004220 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004221
William Lallemand9117de92019-10-04 00:29:42 +02004222 /* everything succeed, the ckch instance can be used */
4223 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004224 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004225
Emeric Brun054563d2019-10-17 13:16:58 +02004226 *ckchi = ckch_inst;
4227 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004228
4229error:
4230 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004231 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004232 struct sni_ctx *sc0, *sc0b;
4233
4234 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4235
4236 ebmb_delete(&sc0->name);
4237 LIST_DEL(&sc0->by_ckch_inst);
4238 free(sc0);
4239 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004240 free(ckch_inst);
4241 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004242 }
4243 /* We only created 1 SSL_CTX so we can free it there */
4244 SSL_CTX_free(ctx);
4245
Emeric Brun054563d2019-10-17 13:16:58 +02004246 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004247}
4248
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004249/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004250static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4251 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4252 char **sni_filter, int fcount, char **err)
4253{
4254 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004255 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004256
4257 /* we found the ckchs in the tree, we can use it directly */
4258 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004259 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 +02004260 else
Emeric Brun054563d2019-10-17 13:16:58 +02004261 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 +02004262
Emeric Brun054563d2019-10-17 13:16:58 +02004263 if (errcode & ERR_CODE)
4264 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004265
4266 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4267
4268 /* succeed, add the instance to the ckch_store's list of instance */
4269 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004270 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004271}
4272
4273
Willy Tarreaubbc91962019-10-16 16:42:19 +02004274/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004275int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004276{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004277 struct dirent **de_list;
4278 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004279 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004280 char *end;
4281 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004282 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004283 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004284#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004285 int is_bundle;
4286 int j;
4287#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004288 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004289 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004290 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004291 }
4292
yanbzhu08ce6ab2015-12-02 13:01:29 -05004293 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004294 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004295 ckchs = ckchs_load_cert_file(path, 0, err);
4296 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004297 return ERR_ALERT | ERR_FATAL;
4298
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004299 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004300 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004301
yanbzhu08ce6ab2015-12-02 13:01:29 -05004302 /* strip trailing slashes, including first one */
4303 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4304 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004305
yanbzhu08ce6ab2015-12-02 13:01:29 -05004306 n = scandir(path, &de_list, 0, alphasort);
4307 if (n < 0) {
4308 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4309 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004310 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004311 }
4312 else {
4313 for (i = 0; i < n; i++) {
4314 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004315
yanbzhu08ce6ab2015-12-02 13:01:29 -05004316 end = strrchr(de->d_name, '.');
4317 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4318 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004319
yanbzhu08ce6ab2015-12-02 13:01:29 -05004320 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4321 if (stat(fp, &buf) != 0) {
4322 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4323 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004324 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004325 goto ignore_entry;
4326 }
4327 if (!S_ISREG(buf.st_mode))
4328 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004329
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004330#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004331 is_bundle = 0;
4332 /* Check if current entry in directory is part of a multi-cert bundle */
4333
William Lallemand3af48e72020-02-03 17:15:52 +01004334 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
yanbzhu63ea8462015-12-09 13:35:14 -05004335 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4336 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4337 is_bundle = 1;
4338 break;
4339 }
4340 }
4341
4342 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004343 int dp_len;
4344
4345 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004346
4347 /* increment i and free de until we get to a non-bundle cert
4348 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004349 * this is important since ignore_entry will free de. This also
4350 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004351 */
Willy Tarreau05800522019-10-29 10:48:50 +01004352 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004353 free(de);
4354 i++;
4355 de = de_list[i];
4356 }
4357
Willy Tarreau05800522019-10-29 10:48:50 +01004358 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004359 if ((ckchs = ckchs_lookup(fp)) == NULL)
4360 ckchs = ckchs_load_cert_file(fp, 1, err);
4361 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004362 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004363 else
4364 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004365 /* Successfully processed the bundle */
4366 goto ignore_entry;
4367 }
4368 }
4369
4370#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004371 if ((ckchs = ckchs_lookup(fp)) == NULL)
4372 ckchs = ckchs_load_cert_file(fp, 0, err);
4373 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004374 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004375 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004376 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004377
yanbzhu08ce6ab2015-12-02 13:01:29 -05004378ignore_entry:
4379 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004380 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004381 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004382 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004383 return cfgerr;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004384
William Lallemand3af48e72020-02-03 17:15:52 +01004385 } else {
4386 /* stat failed */
Willy Tarreaubbc91962019-10-16 16:42:19 +02004387
William Lallemand3af48e72020-02-03 17:15:52 +01004388 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
4389 /* try to load a bundle if it is permitted */
4390 ckchs = ckchs_load_cert_file(path, 1, err);
4391 if (!ckchs)
4392 return ERR_ALERT | ERR_FATAL;
4393 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
4394 } else {
4395 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4396 err && *err ? *err : "", fp, strerror(errno));
4397 cfgerr |= ERR_ALERT | ERR_FATAL;
4398 }
4399 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004400
Emeric Brunfc0421f2012-09-07 17:30:07 +02004401 return cfgerr;
4402}
4403
Thierry Fournier383085f2013-01-24 14:15:43 +01004404/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4405 * done once. Zero is returned if the operation fails. No error is returned
4406 * if the random is said as not implemented, because we expect that openssl
4407 * will use another method once needed.
4408 */
4409static int ssl_initialize_random()
4410{
4411 unsigned char random;
4412 static int random_initialized = 0;
4413
4414 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4415 random_initialized = 1;
4416
4417 return random_initialized;
4418}
4419
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004420/* release ssl bind conf */
4421void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004422{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004423 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004424#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004425 free(conf->npn_str);
4426 conf->npn_str = NULL;
4427#endif
4428#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4429 free(conf->alpn_str);
4430 conf->alpn_str = NULL;
4431#endif
4432 free(conf->ca_file);
4433 conf->ca_file = NULL;
4434 free(conf->crl_file);
4435 conf->crl_file = NULL;
4436 free(conf->ciphers);
4437 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004438#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004439 free(conf->ciphersuites);
4440 conf->ciphersuites = NULL;
4441#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004442 free(conf->curves);
4443 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004444 free(conf->ecdhe);
4445 conf->ecdhe = NULL;
4446 }
4447}
4448
Willy Tarreaubbc91962019-10-16 16:42:19 +02004449/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004450int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4451{
4452 char thisline[CRT_LINESIZE];
4453 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004454 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004455 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004456 int linenum = 0;
4457 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004458 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004459
Willy Tarreauad1731d2013-04-02 17:35:58 +02004460 if ((f = fopen(file, "r")) == NULL) {
4461 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004462 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004463 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004464
4465 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004466 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004467 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004468 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004469 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004470 char *crt_path;
4471 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004472
4473 linenum++;
4474 end = line + strlen(line);
4475 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4476 /* Check if we reached the limit and the last char is not \n.
4477 * Watch out for the last line without the terminating '\n'!
4478 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004479 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4480 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004481 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004482 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004483 }
4484
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004485 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004486 newarg = 1;
4487 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004488 if (*line == '#' || *line == '\n' || *line == '\r') {
4489 /* end of string, end of loop */
4490 *line = 0;
4491 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004492 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004493 newarg = 1;
4494 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004495 } else if (*line == '[') {
4496 if (ssl_b) {
4497 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004498 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004499 break;
4500 }
4501 if (!arg) {
4502 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004503 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004504 break;
4505 }
4506 ssl_b = arg;
4507 newarg = 1;
4508 *line = 0;
4509 } else if (*line == ']') {
4510 if (ssl_e) {
4511 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004512 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004513 break;
4514 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004515 if (!ssl_b) {
4516 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004517 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004518 break;
4519 }
4520 ssl_e = arg;
4521 newarg = 1;
4522 *line = 0;
4523 } else if (newarg) {
4524 if (arg == MAX_CRT_ARGS) {
4525 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004526 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004527 break;
4528 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004529 newarg = 0;
4530 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004531 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004532 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004533 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004534 if (cfgerr)
4535 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004536 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004537
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004538 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004539 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004540 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004541
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004542 crt_path = args[0];
4543 if (*crt_path != '/' && global_ssl.crt_base) {
4544 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4545 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4546 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004547 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004548 break;
4549 }
4550 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4551 crt_path = path;
4552 }
4553
4554 ssl_conf = calloc(1, sizeof *ssl_conf);
4555 cur_arg = ssl_b ? ssl_b : 1;
4556 while (cur_arg < ssl_e) {
4557 newarg = 0;
4558 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4559 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4560 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004561 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004562 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4563 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4564 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004565 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004566 }
4567 cur_arg += 1 + ssl_bind_kws[i].skip;
4568 break;
4569 }
4570 }
4571 if (!cfgerr && !newarg) {
4572 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4573 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004574 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004575 break;
4576 }
4577 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004578
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004579 if (cfgerr) {
4580 ssl_sock_free_ssl_conf(ssl_conf);
4581 free(ssl_conf);
4582 ssl_conf = NULL;
4583 break;
4584 }
4585
William Lallemande3af8fb2019-10-08 11:36:53 +02004586 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004587 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004588 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004589 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004590 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004591 }
4592
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004593 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004594 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004595 else
4596 cfgerr |= ssl_sock_load_ckchs(crt_path, ckchs, bind_conf, ssl_conf, &args[cur_arg], arg - cur_arg - 1, err);
William Lallemandeed4bf22019-10-10 11:38:13 +02004597
Willy Tarreauad1731d2013-04-02 17:35:58 +02004598 if (cfgerr) {
4599 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004600 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004601 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004602 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004603 fclose(f);
4604 return cfgerr;
4605}
4606
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004607/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004608static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004609ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004610{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004611 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004612 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004613 SSL_OP_ALL | /* all known workarounds for bugs */
4614 SSL_OP_NO_SSLv2 |
4615 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004616 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004617 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004618 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004619 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004620 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004621 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004622 SSL_MODE_ENABLE_PARTIAL_WRITE |
4623 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004624 SSL_MODE_RELEASE_BUFFERS |
4625 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004626 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004627 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004628 int flags = MC_SSL_O_ALL;
4629 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004630
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004631 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004632 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004633
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004634 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004635 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4636 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4637 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004638 else
4639 flags = conf_ssl_methods->flags;
4640
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004641 min = conf_ssl_methods->min;
4642 max = conf_ssl_methods->max;
4643 /* start with TLSv10 to remove SSLv3 per default */
4644 if (!min && (!max || max >= CONF_TLSV10))
4645 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004646 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004647 if (min)
4648 flags |= (methodVersions[min].flag - 1);
4649 if (max)
4650 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004651 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004652 min = max = CONF_TLSV_NONE;
4653 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004654 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004655 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004656 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004657 if (min) {
4658 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004659 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4660 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4661 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4662 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004663 hole = 0;
4664 }
4665 max = i;
4666 }
4667 else {
4668 min = max = i;
4669 }
4670 }
4671 else {
4672 if (min)
4673 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004674 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004675 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004676 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4677 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004678 cfgerr += 1;
4679 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004680 /* save real min/max in bind_conf */
4681 conf_ssl_methods->min = min;
4682 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004683
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004684#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004685 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004686 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004687 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004688 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004689 else
4690 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4691 if (flags & methodVersions[i].flag)
4692 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004693#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004694 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004695 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4696 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004697#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004698
4699 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4700 options |= SSL_OP_NO_TICKET;
4701 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4702 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004703
4704#ifdef SSL_OP_NO_RENEGOTIATION
4705 options |= SSL_OP_NO_RENEGOTIATION;
4706#endif
4707
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004708 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004709
Willy Tarreau5db847a2019-05-09 14:13:35 +02004710#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004711 if (global_ssl.async)
4712 mode |= SSL_MODE_ASYNC;
4713#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004714 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004715 if (global_ssl.life_time)
4716 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004717
4718#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4719#ifdef OPENSSL_IS_BORINGSSL
4720 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4721 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05004722#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01004723 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004724 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004725 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4726 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004727#else
4728 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004729#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004730 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004731#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004732 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004733}
4734
William Lallemand4f45bb92017-10-30 20:08:51 +01004735
4736static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4737{
4738 if (first == block) {
4739 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4740 if (first->len > 0)
4741 sh_ssl_sess_tree_delete(sh_ssl_sess);
4742 }
4743}
4744
4745/* return first block from sh_ssl_sess */
4746static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4747{
4748 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4749
4750}
4751
4752/* store a session into the cache
4753 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4754 * data: asn1 encoded session
4755 * data_len: asn1 encoded session length
4756 * Returns 1 id session was stored (else 0)
4757 */
4758static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4759{
4760 struct shared_block *first;
4761 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4762
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004763 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004764 if (!first) {
4765 /* Could not retrieve enough free blocks to store that session */
4766 return 0;
4767 }
4768
4769 /* STORE the key in the first elem */
4770 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4771 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4772 first->len = sizeof(struct sh_ssl_sess_hdr);
4773
4774 /* it returns the already existing node
4775 or current node if none, never returns null */
4776 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4777 if (oldsh_ssl_sess != sh_ssl_sess) {
4778 /* NOTE: Row couldn't be in use because we lock read & write function */
4779 /* release the reserved row */
4780 shctx_row_dec_hot(ssl_shctx, first);
4781 /* replace the previous session already in the tree */
4782 sh_ssl_sess = oldsh_ssl_sess;
4783 /* ignore the previous session data, only use the header */
4784 first = sh_ssl_sess_first_block(sh_ssl_sess);
4785 shctx_row_inc_hot(ssl_shctx, first);
4786 first->len = sizeof(struct sh_ssl_sess_hdr);
4787 }
4788
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004789 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004790 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004791 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004792 }
4793
4794 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004795
4796 return 1;
4797}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004798
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004799/* SSL callback used when a new session is created while connecting to a server */
4800static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4801{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004802 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004803 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004804
Willy Tarreau07d94e42018-09-20 10:57:52 +02004805 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004806
Olivier Houcharde6060c52017-11-16 17:42:52 +01004807 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4808 int len;
4809 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004810
Olivier Houcharde6060c52017-11-16 17:42:52 +01004811 len = i2d_SSL_SESSION(sess, NULL);
4812 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4813 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4814 } else {
4815 free(s->ssl_ctx.reused_sess[tid].ptr);
4816 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4817 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4818 }
4819 if (s->ssl_ctx.reused_sess[tid].ptr) {
4820 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4821 &ptr);
4822 }
4823 } else {
4824 free(s->ssl_ctx.reused_sess[tid].ptr);
4825 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4826 }
4827
4828 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004829}
4830
Olivier Houcharde6060c52017-11-16 17:42:52 +01004831
William Lallemanded0b5ad2017-10-30 19:36:36 +01004832/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004833int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004834{
4835 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4836 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4837 unsigned char *p;
4838 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004839 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004840 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004841
4842 /* Session id is already stored in to key and session id is known
4843 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004844 * note: SSL_SESSION_set1_id is using
4845 * a memcpy so we need to use a different pointer
4846 * than sid_data or sid_ctx_data to avoid valgrind
4847 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004848 */
4849
4850 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004851
4852 /* copy value in an other buffer */
4853 memcpy(encid, sid_data, sid_length);
4854
4855 /* pad with 0 */
4856 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4857 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4858
4859 /* force length to zero to avoid ASN1 encoding */
4860 SSL_SESSION_set1_id(sess, encid, 0);
4861
4862 /* force length to zero to avoid ASN1 encoding */
4863 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004864
4865 /* check if buffer is large enough for the ASN1 encoded session */
4866 data_len = i2d_SSL_SESSION(sess, NULL);
4867 if (data_len > SHSESS_MAX_DATA_LEN)
4868 goto err;
4869
4870 p = encsess;
4871
4872 /* process ASN1 session encoding before the lock */
4873 i2d_SSL_SESSION(sess, &p);
4874
William Lallemanded0b5ad2017-10-30 19:36:36 +01004875
William Lallemanda3c77cf2017-10-30 23:44:40 +01004876 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004877 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004878 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004879 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004880err:
4881 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004882 SSL_SESSION_set1_id(sess, encid, sid_length);
4883 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004884
4885 return 0; /* do not increment session reference count */
4886}
4887
4888/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004889SSL_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 +01004890{
William Lallemand4f45bb92017-10-30 20:08:51 +01004891 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004892 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4893 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004894 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004895 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004896
4897 global.shctx_lookups++;
4898
4899 /* allow the session to be freed automatically by openssl */
4900 *do_copy = 0;
4901
4902 /* tree key is zeros padded sessionid */
4903 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4904 memcpy(tmpkey, key, key_len);
4905 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4906 key = tmpkey;
4907 }
4908
4909 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004910 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004911
4912 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004913 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4914 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004915 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004916 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004917 global.shctx_misses++;
4918 return NULL;
4919 }
4920
William Lallemand4f45bb92017-10-30 20:08:51 +01004921 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4922 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004923
William Lallemand4f45bb92017-10-30 20:08:51 +01004924 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 +01004925
William Lallemanda3c77cf2017-10-30 23:44:40 +01004926 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004927
4928 /* decode ASN1 session */
4929 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004930 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004931 /* Reset session id and session id contenxt */
4932 if (sess) {
4933 SSL_SESSION_set1_id(sess, key, key_len);
4934 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4935 }
4936
4937 return sess;
4938}
4939
William Lallemand4f45bb92017-10-30 20:08:51 +01004940
William Lallemanded0b5ad2017-10-30 19:36:36 +01004941/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004942void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004943{
William Lallemand4f45bb92017-10-30 20:08:51 +01004944 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004945 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4946 unsigned int sid_length;
4947 const unsigned char *sid_data;
4948 (void)ctx;
4949
4950 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4951 /* tree key is zeros padded sessionid */
4952 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4953 memcpy(tmpkey, sid_data, sid_length);
4954 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4955 sid_data = tmpkey;
4956 }
4957
William Lallemanda3c77cf2017-10-30 23:44:40 +01004958 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004959
4960 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004961 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4962 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004963 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004964 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004965 }
4966
4967 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004968 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004969}
4970
4971/* Set session cache mode to server and disable openssl internal cache.
4972 * Set shared cache callbacks on an ssl context.
4973 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004974void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004975{
4976 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4977
4978 if (!ssl_shctx) {
4979 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4980 return;
4981 }
4982
4983 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4984 SSL_SESS_CACHE_NO_INTERNAL |
4985 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4986
4987 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004988 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4989 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4990 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004991}
4992
William Lallemand8b453912019-11-21 15:48:10 +01004993/*
4994 * This function applies the SSL configuration on a SSL_CTX
4995 * It returns an error code and fills the <err> buffer
4996 */
4997int 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 +01004998{
4999 struct proxy *curproxy = bind_conf->frontend;
5000 int cfgerr = 0;
5001 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005002 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005003 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005004#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005005 const char *conf_ciphersuites;
5006#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005007 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005008
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005009 if (ssl_conf) {
5010 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5011 int i, min, max;
5012 int flags = MC_SSL_O_ALL;
5013
5014 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005015 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5016 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005017 if (min)
5018 flags |= (methodVersions[min].flag - 1);
5019 if (max)
5020 flags |= ~((methodVersions[max].flag << 1) - 1);
5021 min = max = CONF_TLSV_NONE;
5022 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5023 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5024 if (min)
5025 max = i;
5026 else
5027 min = max = i;
5028 }
5029 /* save real min/max */
5030 conf_ssl_methods->min = min;
5031 conf_ssl_methods->max = max;
5032 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005033 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5034 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005035 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005036 }
5037 }
5038
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005039 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005040 case SSL_SOCK_VERIFY_NONE:
5041 verify = SSL_VERIFY_NONE;
5042 break;
5043 case SSL_SOCK_VERIFY_OPTIONAL:
5044 verify = SSL_VERIFY_PEER;
5045 break;
5046 case SSL_SOCK_VERIFY_REQUIRED:
5047 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5048 break;
5049 }
5050 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5051 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005052 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5053 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5054 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005055 /* set CAfile to verify */
5056 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5057 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005058 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005059 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005060 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005061 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5062 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005063 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 +02005064 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005065 }
Emeric Brun850efd52014-01-29 12:24:34 +01005066 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005067 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5068 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005069 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005070 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005071#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005072 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005073 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5074
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005075 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005076 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5077 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005078 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005079 }
Emeric Brun561e5742012-10-02 15:20:55 +02005080 else {
5081 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5082 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005083 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005084#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005085 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005086 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005087#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005088 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005089 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005090 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5091 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005092 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005093 }
5094 }
5095#endif
5096
William Lallemand4f45bb92017-10-30 20:08:51 +01005097 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005098 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5099 if (conf_ciphers &&
5100 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005101 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5102 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005103 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005104 }
5105
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005106#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005107 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5108 if (conf_ciphersuites &&
5109 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005110 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5111 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005112 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005113 }
5114#endif
5115
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005116#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005117 /* If tune.ssl.default-dh-param has not been set,
5118 neither has ssl-default-dh-file and no static DH
5119 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005120 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005121 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005122 (ssl_dh_ptr_index == -1 ||
5123 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005124 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5125 const SSL_CIPHER * cipher = NULL;
5126 char cipher_description[128];
5127 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5128 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5129 which is not ephemeral DH. */
5130 const char dhe_description[] = " Kx=DH ";
5131 const char dhe_export_description[] = " Kx=DH(";
5132 int idx = 0;
5133 int dhe_found = 0;
5134 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005135
Remi Gacogne23d5d372014-10-10 17:04:26 +02005136 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005137
Remi Gacogne23d5d372014-10-10 17:04:26 +02005138 if (ssl) {
5139 ciphers = SSL_get_ciphers(ssl);
5140
5141 if (ciphers) {
5142 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5143 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5144 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5145 if (strstr(cipher_description, dhe_description) != NULL ||
5146 strstr(cipher_description, dhe_export_description) != NULL) {
5147 dhe_found = 1;
5148 break;
5149 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005150 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005151 }
5152 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005153 SSL_free(ssl);
5154 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005155 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005156
Lukas Tribus90132722014-08-18 00:56:33 +02005157 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005158 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",
5159 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005160 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005161 }
5162
Willy Tarreauef934602016-12-22 23:12:01 +01005163 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005164 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005165
Willy Tarreauef934602016-12-22 23:12:01 +01005166 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005167 if (local_dh_1024 == NULL) {
5168 local_dh_1024 = ssl_get_dh_1024();
5169 }
Willy Tarreauef934602016-12-22 23:12:01 +01005170 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005171 if (local_dh_2048 == NULL) {
5172 local_dh_2048 = ssl_get_dh_2048();
5173 }
Willy Tarreauef934602016-12-22 23:12:01 +01005174 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005175 if (local_dh_4096 == NULL) {
5176 local_dh_4096 = ssl_get_dh_4096();
5177 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005178 }
5179 }
5180 }
5181#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005182
Emeric Brunfc0421f2012-09-07 17:30:07 +02005183 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005184#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005185 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005186#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005187
Bernard Spil13c53f82018-02-15 13:34:58 +01005188#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005189 ssl_conf_cur = NULL;
5190 if (ssl_conf && ssl_conf->npn_str)
5191 ssl_conf_cur = ssl_conf;
5192 else if (bind_conf->ssl_conf.npn_str)
5193 ssl_conf_cur = &bind_conf->ssl_conf;
5194 if (ssl_conf_cur)
5195 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005196#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005197#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005198 ssl_conf_cur = NULL;
5199 if (ssl_conf && ssl_conf->alpn_str)
5200 ssl_conf_cur = ssl_conf;
5201 else if (bind_conf->ssl_conf.alpn_str)
5202 ssl_conf_cur = &bind_conf->ssl_conf;
5203 if (ssl_conf_cur)
5204 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005205#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005206#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005207 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5208 if (conf_curves) {
5209 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005210 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5211 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005212 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005213 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005214 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005215 }
5216#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005217#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005218 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005219 int i;
5220 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005221#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005222 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005223 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5224 NULL);
5225
5226 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005227 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005228 return cfgerr;
5229 }
5230#else
5231 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5232 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5233 ECDHE_DEFAULT_CURVE);
5234#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005235
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005236 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005237 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005238 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5239 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005240 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005241 }
5242 else {
5243 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5244 EC_KEY_free(ecdh);
5245 }
5246 }
5247#endif
5248
Emeric Brunfc0421f2012-09-07 17:30:07 +02005249 return cfgerr;
5250}
5251
Evan Broderbe554312013-06-27 00:05:25 -07005252static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5253{
5254 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5255 size_t prefixlen, suffixlen;
5256
5257 /* Trivial case */
5258 if (strcmp(pattern, hostname) == 0)
5259 return 1;
5260
Evan Broderbe554312013-06-27 00:05:25 -07005261 /* The rest of this logic is based on RFC 6125, section 6.4.3
5262 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5263
Emeric Bruna848dae2013-10-08 11:27:28 +02005264 pattern_wildcard = NULL;
5265 pattern_left_label_end = pattern;
5266 while (*pattern_left_label_end != '.') {
5267 switch (*pattern_left_label_end) {
5268 case 0:
5269 /* End of label not found */
5270 return 0;
5271 case '*':
5272 /* If there is more than one wildcards */
5273 if (pattern_wildcard)
5274 return 0;
5275 pattern_wildcard = pattern_left_label_end;
5276 break;
5277 }
5278 pattern_left_label_end++;
5279 }
5280
5281 /* If it's not trivial and there is no wildcard, it can't
5282 * match */
5283 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005284 return 0;
5285
5286 /* Make sure all labels match except the leftmost */
5287 hostname_left_label_end = strchr(hostname, '.');
5288 if (!hostname_left_label_end
5289 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5290 return 0;
5291
5292 /* Make sure the leftmost label of the hostname is long enough
5293 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005294 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005295 return 0;
5296
5297 /* Finally compare the string on either side of the
5298 * wildcard */
5299 prefixlen = pattern_wildcard - pattern;
5300 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005301 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5302 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005303 return 0;
5304
5305 return 1;
5306}
5307
5308static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5309{
5310 SSL *ssl;
5311 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005312 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005313 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005314 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005315
5316 int depth;
5317 X509 *cert;
5318 STACK_OF(GENERAL_NAME) *alt_names;
5319 int i;
5320 X509_NAME *cert_subject;
5321 char *str;
5322
5323 if (ok == 0)
5324 return ok;
5325
5326 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005327 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005328 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005329
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005330 /* We're checking if the provided hostnames match the desired one. The
5331 * desired hostname comes from the SNI we presented if any, or if not
5332 * provided then it may have been explicitly stated using a "verifyhost"
5333 * directive. If neither is set, we don't care about the name so the
5334 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005335 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005336 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005337 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005338 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005339 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005340 if (!servername)
5341 return ok;
5342 }
Evan Broderbe554312013-06-27 00:05:25 -07005343
5344 /* We only need to verify the CN on the actual server cert,
5345 * not the indirect CAs */
5346 depth = X509_STORE_CTX_get_error_depth(ctx);
5347 if (depth != 0)
5348 return ok;
5349
5350 /* At this point, the cert is *not* OK unless we can find a
5351 * hostname match */
5352 ok = 0;
5353
5354 cert = X509_STORE_CTX_get_current_cert(ctx);
5355 /* It seems like this might happen if verify peer isn't set */
5356 if (!cert)
5357 return ok;
5358
5359 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5360 if (alt_names) {
5361 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5362 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5363 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005364#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005365 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5366#else
Evan Broderbe554312013-06-27 00:05:25 -07005367 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005368#endif
Evan Broderbe554312013-06-27 00:05:25 -07005369 ok = ssl_sock_srv_hostcheck(str, servername);
5370 OPENSSL_free(str);
5371 }
5372 }
5373 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005374 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005375 }
5376
5377 cert_subject = X509_get_subject_name(cert);
5378 i = -1;
5379 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5380 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005381 ASN1_STRING *value;
5382 value = X509_NAME_ENTRY_get_data(entry);
5383 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005384 ok = ssl_sock_srv_hostcheck(str, servername);
5385 OPENSSL_free(str);
5386 }
5387 }
5388
Willy Tarreau71d058c2017-07-26 20:09:56 +02005389 /* report the mismatch and indicate if SNI was used or not */
5390 if (!ok && !conn->err_code)
5391 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005392 return ok;
5393}
5394
Emeric Brun94324a42012-10-11 14:00:19 +02005395/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005396int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005397{
Willy Tarreau03209342016-12-22 17:08:28 +01005398 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005399 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005400 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005401 SSL_OP_ALL | /* all known workarounds for bugs */
5402 SSL_OP_NO_SSLv2 |
5403 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005404 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005405 SSL_MODE_ENABLE_PARTIAL_WRITE |
5406 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005407 SSL_MODE_RELEASE_BUFFERS |
5408 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005409 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005410 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005411 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005412 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005413 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005414
Thierry Fournier383085f2013-01-24 14:15:43 +01005415 /* Make sure openssl opens /dev/urandom before the chroot */
5416 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005417 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005418 cfgerr++;
5419 }
5420
Willy Tarreaufce03112015-01-15 21:32:40 +01005421 /* Automatic memory computations need to know we use SSL there */
5422 global.ssl_used_backend = 1;
5423
5424 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005425 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005426 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005427 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5428 curproxy->id, srv->id,
5429 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005430 cfgerr++;
5431 return cfgerr;
5432 }
5433 }
Emeric Brun94324a42012-10-11 14:00:19 +02005434 if (srv->use_ssl)
5435 srv->xprt = &ssl_sock;
5436 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005437 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005438
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005439 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005440 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005441 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5442 proxy_type_str(curproxy), curproxy->id,
5443 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005444 cfgerr++;
5445 return cfgerr;
5446 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005447
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005448 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005449 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5450 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5451 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005452 else
5453 flags = conf_ssl_methods->flags;
5454
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005455 /* Real min and max should be determinate with configuration and openssl's capabilities */
5456 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005457 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005458 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005459 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005460
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005461 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005462 min = max = CONF_TLSV_NONE;
5463 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005464 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005465 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005466 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005467 if (min) {
5468 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005469 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5470 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5471 proxy_type_str(curproxy), curproxy->id, srv->id,
5472 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005473 hole = 0;
5474 }
5475 max = i;
5476 }
5477 else {
5478 min = max = i;
5479 }
5480 }
5481 else {
5482 if (min)
5483 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005484 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005485 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005486 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5487 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005488 cfgerr += 1;
5489 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005490
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005491#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005492 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005493 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005494 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005495 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005496 else
5497 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5498 if (flags & methodVersions[i].flag)
5499 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005500#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005501 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005502 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5503 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005504#endif
5505
5506 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5507 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005508 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005509
Willy Tarreau5db847a2019-05-09 14:13:35 +02005510#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005511 if (global_ssl.async)
5512 mode |= SSL_MODE_ASYNC;
5513#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005514 SSL_CTX_set_mode(ctx, mode);
5515 srv->ssl_ctx.ctx = ctx;
5516
Emeric Bruna7aa3092012-10-26 12:58:00 +02005517 if (srv->ssl_ctx.client_crt) {
5518 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 +01005519 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5520 proxy_type_str(curproxy), curproxy->id,
5521 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005522 cfgerr++;
5523 }
5524 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 +01005525 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5526 proxy_type_str(curproxy), curproxy->id,
5527 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005528 cfgerr++;
5529 }
5530 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005531 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5532 proxy_type_str(curproxy), curproxy->id,
5533 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005534 cfgerr++;
5535 }
5536 }
Emeric Brun94324a42012-10-11 14:00:19 +02005537
Emeric Brun850efd52014-01-29 12:24:34 +01005538 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5539 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005540 switch (srv->ssl_ctx.verify) {
5541 case SSL_SOCK_VERIFY_NONE:
5542 verify = SSL_VERIFY_NONE;
5543 break;
5544 case SSL_SOCK_VERIFY_REQUIRED:
5545 verify = SSL_VERIFY_PEER;
5546 break;
5547 }
Evan Broderbe554312013-06-27 00:05:25 -07005548 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005549 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005550 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005551 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005552 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005553 /* set CAfile to verify */
5554 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5555 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005556 curproxy->id, srv->id,
5557 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005558 cfgerr++;
5559 }
5560 }
Emeric Brun850efd52014-01-29 12:24:34 +01005561 else {
5562 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005563 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",
5564 curproxy->id, srv->id,
5565 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005566 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005567 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5568 curproxy->id, srv->id,
5569 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005570 cfgerr++;
5571 }
Emeric Brunef42d922012-10-11 16:11:36 +02005572#ifdef X509_V_FLAG_CRL_CHECK
5573 if (srv->ssl_ctx.crl_file) {
5574 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5575
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005576 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005577 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5578 curproxy->id, srv->id,
5579 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005580 cfgerr++;
5581 }
5582 else {
5583 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5584 }
5585 }
5586#endif
5587 }
5588
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005589 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5590 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5591 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005592 if (srv->ssl_ctx.ciphers &&
5593 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005594 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5595 curproxy->id, srv->id,
5596 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005597 cfgerr++;
5598 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005599
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005600#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005601 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005602 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005603 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5604 curproxy->id, srv->id,
5605 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5606 cfgerr++;
5607 }
5608#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005609#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5610 if (srv->ssl_ctx.npn_str)
5611 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5612#endif
5613#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5614 if (srv->ssl_ctx.alpn_str)
5615 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5616#endif
5617
Emeric Brun94324a42012-10-11 14:00:19 +02005618
5619 return cfgerr;
5620}
5621
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005622/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005623 * be NULL, in which case nothing is done. Returns the number of errors
5624 * encountered.
5625 */
Willy Tarreau03209342016-12-22 17:08:28 +01005626int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005627{
5628 struct ebmb_node *node;
5629 struct sni_ctx *sni;
5630 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005631 int errcode = 0;
5632 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005633
Willy Tarreaufce03112015-01-15 21:32:40 +01005634 /* Automatic memory computations need to know we use SSL there */
5635 global.ssl_used_frontend = 1;
5636
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005637 /* Make sure openssl opens /dev/urandom before the chroot */
5638 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005639 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005640 err++;
5641 }
5642 /* Create initial_ctx used to start the ssl connection before do switchctx */
5643 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005644 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005645 /* It should not be necessary to call this function, but it's
5646 necessary first to check and move all initialisation related
5647 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005648 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005649 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005650 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005651 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005652
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005653 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005654 while (node) {
5655 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005656 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5657 /* only initialize the CTX on its first occurrence and
5658 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005659 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005660 node = ebmb_next(node);
5661 }
5662
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005663 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005664 while (node) {
5665 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005666 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005667 /* only initialize the CTX on its first occurrence and
5668 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005669 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5670 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005671 node = ebmb_next(node);
5672 }
William Lallemand8b453912019-11-21 15:48:10 +01005673
5674 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005675 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005676 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005677 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005678 err++;
5679 }
5680
5681 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005682 return err;
5683}
5684
Willy Tarreau55d37912016-12-21 23:38:39 +01005685/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5686 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5687 * alerts are directly emitted since the rest of the stack does it below.
5688 */
5689int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5690{
5691 struct proxy *px = bind_conf->frontend;
5692 int alloc_ctx;
5693 int err;
5694
5695 if (!bind_conf->is_ssl) {
5696 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005697 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5698 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005699 }
5700 return 0;
5701 }
5702 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005703 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005704 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5705 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005706 }
5707 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005708 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5709 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005710 return -1;
5711 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005712 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005713 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005714 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005715 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005716 sizeof(*sh_ssl_sess_tree),
5717 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005718 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005719 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5720 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");
5721 else
5722 ha_alert("Unable to allocate SSL session cache.\n");
5723 return -1;
5724 }
5725 /* free block callback */
5726 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5727 /* init the root tree within the extra space */
5728 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5729 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005730 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005731 err = 0;
5732 /* initialize all certificate contexts */
5733 err += ssl_sock_prepare_all_ctx(bind_conf);
5734
5735 /* initialize CA variables if the certificates generation is enabled */
5736 err += ssl_sock_load_ca(bind_conf);
5737
5738 return -err;
5739}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005740
5741/* release ssl context allocated for servers. */
5742void ssl_sock_free_srv_ctx(struct server *srv)
5743{
Olivier Houchardc7566002018-11-20 23:33:50 +01005744#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5745 if (srv->ssl_ctx.alpn_str)
5746 free(srv->ssl_ctx.alpn_str);
5747#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005748#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005749 if (srv->ssl_ctx.npn_str)
5750 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005751#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005752 if (srv->ssl_ctx.ctx)
5753 SSL_CTX_free(srv->ssl_ctx.ctx);
5754}
5755
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005756/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005757 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5758 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005759void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005760{
5761 struct ebmb_node *node, *back;
5762 struct sni_ctx *sni;
5763
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005764 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005765 while (node) {
5766 sni = ebmb_entry(node, struct sni_ctx, name);
5767 back = ebmb_next(node);
5768 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005769 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005770 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005771 ssl_sock_free_ssl_conf(sni->conf);
5772 free(sni->conf);
5773 sni->conf = NULL;
5774 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005775 free(sni);
5776 node = back;
5777 }
5778
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005779 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005780 while (node) {
5781 sni = ebmb_entry(node, struct sni_ctx, name);
5782 back = ebmb_next(node);
5783 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005784 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005785 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005786 ssl_sock_free_ssl_conf(sni->conf);
5787 free(sni->conf);
5788 sni->conf = NULL;
5789 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005790 free(sni);
5791 node = back;
5792 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005793 SSL_CTX_free(bind_conf->initial_ctx);
5794 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005795 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005796 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005797}
5798
Willy Tarreau795cdab2016-12-22 17:30:54 +01005799/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5800void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5801{
5802 ssl_sock_free_ca(bind_conf);
5803 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005804 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005805 free(bind_conf->ca_sign_file);
5806 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005807 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005808 free(bind_conf->keys_ref->filename);
5809 free(bind_conf->keys_ref->tlskeys);
5810 LIST_DEL(&bind_conf->keys_ref->list);
5811 free(bind_conf->keys_ref);
5812 }
5813 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005814 bind_conf->ca_sign_pass = NULL;
5815 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005816}
5817
Christopher Faulet31af49d2015-06-09 17:29:50 +02005818/* Load CA cert file and private key used to generate certificates */
5819int
Willy Tarreau03209342016-12-22 17:08:28 +01005820ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005821{
Willy Tarreau03209342016-12-22 17:08:28 +01005822 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005823 FILE *fp;
5824 X509 *cacert = NULL;
5825 EVP_PKEY *capkey = NULL;
5826 int err = 0;
5827
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005828 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005829 return err;
5830
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005831#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005832 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005833 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005834 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005835 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005836 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005837#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005838
Christopher Faulet31af49d2015-06-09 17:29:50 +02005839 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005840 ha_alert("Proxy '%s': cannot enable certificate generation, "
5841 "no CA certificate File configured at [%s:%d].\n",
5842 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005843 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005844 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005845
5846 /* read in the CA certificate */
5847 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005848 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5849 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005850 goto load_error;
5851 }
5852 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005853 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5854 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005855 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005856 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005857 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005858 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005859 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5860 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005861 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005862 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005863
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005864 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005865 bind_conf->ca_sign_cert = cacert;
5866 bind_conf->ca_sign_pkey = capkey;
5867 return err;
5868
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005869 read_error:
5870 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005871 if (capkey) EVP_PKEY_free(capkey);
5872 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005873 load_error:
5874 bind_conf->generate_certs = 0;
5875 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005876 return err;
5877}
5878
5879/* Release CA cert and private key used to generate certificated */
5880void
5881ssl_sock_free_ca(struct bind_conf *bind_conf)
5882{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005883 if (bind_conf->ca_sign_pkey)
5884 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5885 if (bind_conf->ca_sign_cert)
5886 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005887 bind_conf->ca_sign_pkey = NULL;
5888 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005889}
5890
Emeric Brun46591952012-05-18 15:47:34 +02005891/*
5892 * This function is called if SSL * context is not yet allocated. The function
5893 * is designed to be called before any other data-layer operation and sets the
5894 * handshake flag on the connection. It is safe to call it multiple times.
5895 * It returns 0 on success and -1 in error case.
5896 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005897static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005898{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005899 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005900 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005901 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005902 return 0;
5903
Willy Tarreau3c728722014-01-23 13:50:42 +01005904 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005905 return 0;
5906
Olivier Houchard66ab4982019-02-26 18:37:15 +01005907 ctx = pool_alloc(ssl_sock_ctx_pool);
5908 if (!ctx) {
5909 conn->err_code = CO_ER_SSL_NO_MEM;
5910 return -1;
5911 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005912 ctx->wait_event.tasklet = tasklet_new();
5913 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005914 conn->err_code = CO_ER_SSL_NO_MEM;
5915 pool_free(ssl_sock_ctx_pool, ctx);
5916 return -1;
5917 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005918 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5919 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005920 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005921 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005922 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005923 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005924 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005925 ctx->xprt_st = 0;
5926 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005927
5928 /* Only work with sockets for now, this should be adapted when we'll
5929 * add QUIC support.
5930 */
5931 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005932 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005933 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5934 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005935 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005936
Willy Tarreau20879a02012-12-03 16:32:10 +01005937 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5938 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005939 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005940 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005941
Emeric Brun46591952012-05-18 15:47:34 +02005942 /* If it is in client mode initiate SSL session
5943 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005944 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005945 int may_retry = 1;
5946
5947 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005948 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005949 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5950 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005951 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005952 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005953 goto retry_connect;
5954 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005955 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005956 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005957 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005958 ctx->bio = BIO_new(ha_meth);
5959 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01005960 SSL_free(ctx->ssl);
5961 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005962 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005963 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005964 goto retry_connect;
5965 }
Emeric Brun55476152014-11-12 17:35:37 +01005966 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005967 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005968 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005969 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005970 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005971
Evan Broderbe554312013-06-27 00:05:25 -07005972 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005973 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5974 SSL_free(ctx->ssl);
5975 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005976 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005977 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005978 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005979 goto retry_connect;
5980 }
Emeric Brun55476152014-11-12 17:35:37 +01005981 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005982 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005983 }
5984
Olivier Houchard66ab4982019-02-26 18:37:15 +01005985 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005986 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5987 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5988 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 +01005989 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005990 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005991 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5992 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005993 } else if (sess) {
5994 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005995 }
5996 }
Evan Broderbe554312013-06-27 00:05:25 -07005997
Emeric Brun46591952012-05-18 15:47:34 +02005998 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005999 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006000
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006001 _HA_ATOMIC_ADD(&sslconns, 1);
6002 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006003 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006004 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006005 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006006 return 0;
6007 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006008 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006009 int may_retry = 1;
6010
6011 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006012 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006013 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6014 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006015 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006016 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006017 goto retry_accept;
6018 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006019 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006020 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006021 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006022 ctx->bio = BIO_new(ha_meth);
6023 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006024 SSL_free(ctx->ssl);
6025 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006026 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006027 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006028 goto retry_accept;
6029 }
Emeric Brun55476152014-11-12 17:35:37 +01006030 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006031 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006032 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006033 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006034 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006035
Emeric Brune1f38db2012-09-03 20:36:47 +02006036 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006037 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6038 SSL_free(ctx->ssl);
6039 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006040 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006041 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006042 goto retry_accept;
6043 }
Emeric Brun55476152014-11-12 17:35:37 +01006044 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006045 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006046 }
6047
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006048#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6049 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6050 b_alloc(&ctx->early_buf);
6051 SSL_set_max_early_data(ctx->ssl,
6052 /* Only allow early data if we managed to allocate
6053 * a buffer.
6054 */
6055 (!b_is_null(&ctx->early_buf)) ?
6056 global.tune.bufsize - global.tune.maxrewrite : 0);
6057 }
6058#endif
6059
Olivier Houchard66ab4982019-02-26 18:37:15 +01006060 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006061
Emeric Brun46591952012-05-18 15:47:34 +02006062 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006063 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006064#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006065 conn->flags |= CO_FL_EARLY_SSL_HS;
6066#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006067
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006068 _HA_ATOMIC_ADD(&sslconns, 1);
6069 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006070 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006071 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006072 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006073 return 0;
6074 }
6075 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006076 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006077err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006078 if (ctx && ctx->wait_event.tasklet)
6079 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006080 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006081 return -1;
6082}
6083
6084
6085/* This is the callback which is used when an SSL handshake is pending. It
6086 * updates the FD status if it wants some polling before being called again.
6087 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6088 * otherwise it returns non-zero and removes itself from the connection's
6089 * flags (the bit is provided in <flag> by the caller).
6090 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006091static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006092{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006093 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006094 int ret;
6095
Willy Tarreau3c728722014-01-23 13:50:42 +01006096 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006097 return 0;
6098
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006099 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006100 goto out_error;
6101
Willy Tarreau5db847a2019-05-09 14:13:35 +02006102#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006103 /*
6104 * Check if we have early data. If we do, we have to read them
6105 * before SSL_do_handshake() is called, And there's no way to
6106 * detect early data, except to try to read them
6107 */
6108 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006109 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006110
Olivier Houchard54907bb2019-12-19 15:02:39 +01006111 while (1) {
6112 ret = SSL_read_early_data(ctx->ssl,
6113 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6114 &read_data);
6115 if (ret == SSL_READ_EARLY_DATA_ERROR)
6116 goto check_error;
6117 if (read_data > 0) {
6118 conn->flags |= CO_FL_EARLY_DATA;
6119 b_add(&ctx->early_buf, read_data);
6120 }
6121 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6122 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6123 if (!b_data(&ctx->early_buf))
6124 b_free(&ctx->early_buf);
6125 break;
6126 }
6127 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006128 }
6129#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006130 /* If we use SSL_do_handshake to process a reneg initiated by
6131 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6132 * Usually SSL_write and SSL_read are used and process implicitly
6133 * the reneg handshake.
6134 * Here we use SSL_peek as a workaround for reneg.
6135 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006136 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006137 char c;
6138
Olivier Houchard66ab4982019-02-26 18:37:15 +01006139 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006140 if (ret <= 0) {
6141 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006142 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006143
Emeric Brun674b7432012-11-08 19:21:55 +01006144 if (ret == SSL_ERROR_WANT_WRITE) {
6145 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006146 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006147 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006148 return 0;
6149 }
6150 else if (ret == SSL_ERROR_WANT_READ) {
6151 /* handshake may have been completed but we have
6152 * no more data to read.
6153 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006154 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006155 ret = 1;
6156 goto reneg_ok;
6157 }
6158 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006159 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006160 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006161 return 0;
6162 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006163#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006164 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006165 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006166 return 0;
6167 }
6168#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006169 else if (ret == SSL_ERROR_SYSCALL) {
6170 /* if errno is null, then connection was successfully established */
6171 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6172 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006173 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006174#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6175 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006176 conn->err_code = CO_ER_SSL_HANDSHAKE;
6177#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006178 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006179#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006180 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006181 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006182 empty_handshake = state == TLS_ST_BEFORE;
6183#else
Lukas Tribus49799162019-07-08 14:29:15 +02006184 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6185 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006186#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006187 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006188 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006189 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006190 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6191 else
6192 conn->err_code = CO_ER_SSL_EMPTY;
6193 }
6194 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006195 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006196 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6197 else
6198 conn->err_code = CO_ER_SSL_ABORT;
6199 }
6200 }
6201 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006202 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006203 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006204 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006205 conn->err_code = CO_ER_SSL_HANDSHAKE;
6206 }
Lukas Tribus49799162019-07-08 14:29:15 +02006207#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006208 }
Emeric Brun674b7432012-11-08 19:21:55 +01006209 goto out_error;
6210 }
6211 else {
6212 /* Fail on all other handshake errors */
6213 /* Note: OpenSSL may leave unread bytes in the socket's
6214 * buffer, causing an RST to be emitted upon close() on
6215 * TCP sockets. We first try to drain possibly pending
6216 * data to avoid this as much as possible.
6217 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006218 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006219 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006220 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006221 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006222 goto out_error;
6223 }
6224 }
6225 /* read some data: consider handshake completed */
6226 goto reneg_ok;
6227 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006228 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006229check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006230 if (ret != 1) {
6231 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006232 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006233
6234 if (ret == SSL_ERROR_WANT_WRITE) {
6235 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006236 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006237 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006238 return 0;
6239 }
6240 else if (ret == SSL_ERROR_WANT_READ) {
6241 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006242 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006243 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6244 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006245 return 0;
6246 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006247#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006248 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006249 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006250 return 0;
6251 }
6252#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006253 else if (ret == SSL_ERROR_SYSCALL) {
6254 /* if errno is null, then connection was successfully established */
6255 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6256 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006257 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006258#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6259 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006260 conn->err_code = CO_ER_SSL_HANDSHAKE;
6261#else
6262 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006263#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006264 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006265 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006266 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006267#else
Lukas Tribus49799162019-07-08 14:29:15 +02006268 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6269 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006270#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006271 if (empty_handshake) {
6272 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006273 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006274 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6275 else
6276 conn->err_code = CO_ER_SSL_EMPTY;
6277 }
6278 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006279 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006280 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6281 else
6282 conn->err_code = CO_ER_SSL_ABORT;
6283 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006284 }
6285 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006286 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006287 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6288 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006289 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006290 }
Lukas Tribus49799162019-07-08 14:29:15 +02006291#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006292 }
Willy Tarreau89230192012-09-28 20:22:13 +02006293 goto out_error;
6294 }
Emeric Brun46591952012-05-18 15:47:34 +02006295 else {
6296 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006297 /* Note: OpenSSL may leave unread bytes in the socket's
6298 * buffer, causing an RST to be emitted upon close() on
6299 * TCP sockets. We first try to drain possibly pending
6300 * data to avoid this as much as possible.
6301 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006302 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006303 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006304 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006305 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006306 goto out_error;
6307 }
6308 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006309#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006310 else {
6311 /*
6312 * If the server refused the early data, we have to send a
6313 * 425 to the client, as we no longer have the data to sent
6314 * them again.
6315 */
6316 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006317 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006318 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6319 goto out_error;
6320 }
6321 }
6322 }
6323#endif
6324
Emeric Brun46591952012-05-18 15:47:34 +02006325
Emeric Brun674b7432012-11-08 19:21:55 +01006326reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006327
Willy Tarreau5db847a2019-05-09 14:13:35 +02006328#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006329 /* ASYNC engine API doesn't support moving read/write
6330 * buffers. So we disable ASYNC mode right after
6331 * the handshake to avoid buffer oveflows.
6332 */
6333 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006334 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006335#endif
Emeric Brun46591952012-05-18 15:47:34 +02006336 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006337 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006338 if (objt_server(conn->target)) {
6339 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6340 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6341 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006342 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006343 else {
6344 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6345 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6346 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6347 }
Emeric Brun46591952012-05-18 15:47:34 +02006348 }
6349
6350 /* The connection is now established at both layers, it's time to leave */
6351 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6352 return 1;
6353
6354 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006355 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006356 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006357 ERR_clear_error();
6358
Emeric Brun9fa89732012-10-04 17:09:56 +02006359 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006360 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6361 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6362 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006363 }
6364
Emeric Brun46591952012-05-18 15:47:34 +02006365 /* Fail on all other handshake errors */
6366 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006367 if (!conn->err_code)
6368 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006369 return 0;
6370}
6371
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006372/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6373 * event subscriber <es> is not allowed to change from a previous call as long
6374 * as at least one event is still subscribed. The <event_type> must only be a
6375 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6376 * unless the transport layer was already released.
6377 */
6378static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006379{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006380 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006381
Olivier Houchard0ff28652019-06-24 18:57:39 +02006382 if (!ctx)
6383 return -1;
6384
Willy Tarreau113d52b2020-01-10 09:20:26 +01006385 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6386 BUG_ON(ctx->subs && ctx->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006387 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006388
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006389 ctx->subs = es;
6390 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006391
6392 /* we may have to subscribe to lower layers for new events */
6393 event_type &= ~ctx->wait_event.events;
6394 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6395 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006396 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006397}
6398
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006399/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6400 * The <es> pointer is not allowed to differ from the one passed to the
6401 * subscribe() call. It always returns zero.
6402 */
6403static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006404{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006405 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006406
Willy Tarreau113d52b2020-01-10 09:20:26 +01006407 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006408 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006409
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006410 es->events &= ~event_type;
6411 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006412 ctx->subs = NULL;
6413
6414 /* If we subscribed, and we're not doing the handshake,
6415 * then we subscribed because the upper layer asked for it,
6416 * as the upper layer is no longer interested, we can
6417 * unsubscribe too.
6418 */
6419 event_type &= ctx->wait_event.events;
6420 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6421 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006422
6423 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006424}
6425
Olivier Houchard2e055482019-05-27 19:50:12 +02006426/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6427 * Returns 0 on success, and non-zero on failure.
6428 */
6429static 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)
6430{
6431 struct ssl_sock_ctx *ctx = xprt_ctx;
6432
6433 if (oldxprt_ops != NULL)
6434 *oldxprt_ops = ctx->xprt;
6435 if (oldxprt_ctx != NULL)
6436 *oldxprt_ctx = ctx->xprt_ctx;
6437 ctx->xprt = toadd_ops;
6438 ctx->xprt_ctx = toadd_ctx;
6439 return 0;
6440}
6441
Olivier Houchard5149b592019-05-23 17:47:36 +02006442/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6443 * return 0, otherwise just call the remove_xprt method from the underlying
6444 * XPRT.
6445 */
6446static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6447{
6448 struct ssl_sock_ctx *ctx = xprt_ctx;
6449
6450 if (ctx->xprt_ctx == toremove_ctx) {
6451 ctx->xprt_ctx = newctx;
6452 ctx->xprt = newops;
6453 return 0;
6454 }
6455 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6456}
6457
Olivier Houchardea8dd942019-05-20 14:02:16 +02006458static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6459{
6460 struct ssl_sock_ctx *ctx = context;
6461
6462 /* First if we're doing an handshake, try that */
6463 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6464 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6465 /* If we had an error, or the handshake is done and I/O is available,
6466 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006467 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006468 * we can't be sure conn_fd_handler() will be called again.
6469 */
6470 if ((ctx->conn->flags & CO_FL_ERROR) ||
6471 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6472 int ret = 0;
6473 int woke = 0;
6474
6475 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006476 if (ctx->subs) {
6477 tasklet_wakeup(ctx->subs->tasklet);
6478 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006479 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006480 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006481 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006482
Olivier Houchardea8dd942019-05-20 14:02:16 +02006483 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006484 * upper layers know. If we have no mux, create it,
6485 * and once we have a mux, call its wake method if we didn't
6486 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006487 */
6488 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006489 if (!ctx->conn->mux)
6490 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006491 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6492 ctx->conn->mux->wake(ctx->conn);
6493 return NULL;
6494 }
6495 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006496#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6497 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006498 else if (b_data(&ctx->early_buf) && ctx->subs &&
6499 ctx->subs->events & SUB_RETRY_RECV) {
6500 tasklet_wakeup(ctx->subs->tasklet);
6501 ctx->subs->events &= ~SUB_RETRY_RECV;
6502 if (!ctx->subs->events)
6503 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006504 }
6505#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006506 return NULL;
6507}
6508
Emeric Brun46591952012-05-18 15:47:34 +02006509/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006510 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006511 * buffer wraps, in which case a second call may be performed. The connection's
6512 * flags are updated with whatever special event is detected (error, read0,
6513 * empty). The caller is responsible for taking care of those events and
6514 * avoiding the call if inappropriate. The function does not call the
6515 * connection's polling update function, so the caller is responsible for this.
6516 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006517static 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 +02006518{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006519 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006520 ssize_t ret;
6521 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006522
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006523 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006524 goto out_error;
6525
Olivier Houchard54907bb2019-12-19 15:02:39 +01006526#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6527 if (b_data(&ctx->early_buf)) {
6528 try = b_contig_space(buf);
6529 if (try > b_data(&ctx->early_buf))
6530 try = b_data(&ctx->early_buf);
6531 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6532 b_add(buf, try);
6533 b_del(&ctx->early_buf, try);
6534 if (b_data(&ctx->early_buf) == 0)
6535 b_free(&ctx->early_buf);
6536 return try;
6537 }
6538#endif
6539
Willy Tarreau911db9b2020-01-23 16:27:54 +01006540 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006541 /* a handshake was requested */
6542 return 0;
6543
Emeric Brun46591952012-05-18 15:47:34 +02006544 /* read the largest possible block. For this, we perform only one call
6545 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6546 * in which case we accept to do it once again. A new attempt is made on
6547 * EINTR too.
6548 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006549 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006550
Willy Tarreau591d4452018-06-15 17:21:00 +02006551 try = b_contig_space(buf);
6552 if (!try)
6553 break;
6554
Willy Tarreauabf08d92014-01-14 11:31:27 +01006555 if (try > count)
6556 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006557
Olivier Houchard66ab4982019-02-26 18:37:15 +01006558 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006559
Emeric Brune1f38db2012-09-03 20:36:47 +02006560 if (conn->flags & CO_FL_ERROR) {
6561 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006562 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006563 }
Emeric Brun46591952012-05-18 15:47:34 +02006564 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006565 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006566 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006567 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006568 }
Emeric Brun46591952012-05-18 15:47:34 +02006569 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006570 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006571 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006572 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006573 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006574 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006575#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006576 /* Async mode can be re-enabled, because we're leaving data state.*/
6577 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006578 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006579#endif
Emeric Brun46591952012-05-18 15:47:34 +02006580 break;
6581 }
6582 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006583 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006584 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6585 SUB_RETRY_RECV,
6586 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006587 /* handshake is running, and it may need to re-enable read */
6588 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006589#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006590 /* Async mode can be re-enabled, because we're leaving data state.*/
6591 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006592 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006593#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006594 break;
6595 }
Emeric Brun46591952012-05-18 15:47:34 +02006596 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006597 } else if (ret == SSL_ERROR_ZERO_RETURN)
6598 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006599 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6600 * stack before shutting down the connection for
6601 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006602 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6603 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006604 /* otherwise it's a real error */
6605 goto out_error;
6606 }
6607 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006608 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006609 return done;
6610
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006611 clear_ssl_error:
6612 /* Clear openssl global errors stack */
6613 ssl_sock_dump_errors(conn);
6614 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006615 read0:
6616 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006617 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006618
Emeric Brun46591952012-05-18 15:47:34 +02006619 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006620 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006621 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006622 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006623 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006624 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006625}
6626
6627
Willy Tarreau787db9a2018-06-14 18:31:46 +02006628/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6629 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6630 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006631 * Only one call to send() is performed, unless the buffer wraps, in which case
6632 * a second call may be performed. The connection's flags are updated with
6633 * whatever special event is detected (error, empty). The caller is responsible
6634 * for taking care of those events and avoiding the call if inappropriate. The
6635 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006636 * is responsible for this. The buffer's output is not adjusted, it's up to the
6637 * caller to take care of this. It's up to the caller to update the buffer's
6638 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006639 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006640static 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 +02006641{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006642 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006643 ssize_t ret;
6644 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006645
6646 done = 0;
6647
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006648 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006649 goto out_error;
6650
Willy Tarreau911db9b2020-01-23 16:27:54 +01006651 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006652 /* a handshake was requested */
6653 return 0;
6654
6655 /* send the largest possible block. For this we perform only one call
6656 * to send() unless the buffer wraps and we exactly fill the first hunk,
6657 * in which case we accept to do it once again.
6658 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006659 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006660#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006661 size_t written_data;
6662#endif
6663
Willy Tarreau787db9a2018-06-14 18:31:46 +02006664 try = b_contig_data(buf, done);
6665 if (try > count)
6666 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006667
Willy Tarreau7bed9452014-02-02 02:00:24 +01006668 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006669 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006670 global_ssl.max_record && try > global_ssl.max_record) {
6671 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006672 }
6673 else {
6674 /* we need to keep the information about the fact that
6675 * we're not limiting the upcoming send(), because if it
6676 * fails, we'll have to retry with at least as many data.
6677 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006678 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006679 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006680
Willy Tarreau5db847a2019-05-09 14:13:35 +02006681#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006682 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006683 unsigned int max_early;
6684
Olivier Houchard522eea72017-11-03 16:27:47 +01006685 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006686 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006687 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006688 if (SSL_get0_session(ctx->ssl))
6689 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006690 else
6691 max_early = 0;
6692 }
6693
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006694 if (try + ctx->sent_early_data > max_early) {
6695 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006696 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006697 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006698 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006699 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006700 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006701 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006702 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006703 if (ret == 1) {
6704 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006705 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006706 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006707 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006708 /* Initiate the handshake, now */
6709 tasklet_wakeup(ctx->wait_event.tasklet);
6710 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006711
Olivier Houchardc2aae742017-09-22 18:26:28 +02006712 }
6713
6714 } else
6715#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006716 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006717
Emeric Brune1f38db2012-09-03 20:36:47 +02006718 if (conn->flags & CO_FL_ERROR) {
6719 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006720 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006721 }
Emeric Brun46591952012-05-18 15:47:34 +02006722 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006723 /* A send succeeded, so we can consider ourself connected */
6724 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006725 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006726 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006727 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006728 }
6729 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006730 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006731
Emeric Brun46591952012-05-18 15:47:34 +02006732 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006733 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006734 /* handshake is running, and it may need to re-enable write */
6735 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006736 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006737#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006738 /* Async mode can be re-enabled, because we're leaving data state.*/
6739 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006740 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006741#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006742 break;
6743 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006744
Emeric Brun46591952012-05-18 15:47:34 +02006745 break;
6746 }
6747 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006748 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006749 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006750 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6751 SUB_RETRY_RECV,
6752 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006753#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006754 /* Async mode can be re-enabled, because we're leaving data state.*/
6755 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006756 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006757#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006758 break;
6759 }
Emeric Brun46591952012-05-18 15:47:34 +02006760 goto out_error;
6761 }
6762 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006763 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006764 return done;
6765
6766 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006767 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006768 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006769 ERR_clear_error();
6770
Emeric Brun46591952012-05-18 15:47:34 +02006771 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006772 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006773}
6774
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006775static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006776
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006777 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006778
Olivier Houchardea8dd942019-05-20 14:02:16 +02006779
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006780 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006781 if (ctx->wait_event.events != 0)
6782 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6783 ctx->wait_event.events,
6784 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006785 if (ctx->subs) {
6786 ctx->subs->events = 0;
6787 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006788 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006789
Olivier Houchard692c1d02019-05-23 18:41:47 +02006790 if (ctx->xprt->close)
6791 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006792#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006793 if (global_ssl.async) {
6794 OSSL_ASYNC_FD all_fd[32], afd;
6795 size_t num_all_fds = 0;
6796 int i;
6797
Olivier Houchard66ab4982019-02-26 18:37:15 +01006798 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006799 if (num_all_fds > 32) {
6800 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6801 return;
6802 }
6803
Olivier Houchard66ab4982019-02-26 18:37:15 +01006804 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006805
6806 /* If an async job is pending, we must try to
6807 to catch the end using polling before calling
6808 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006809 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006810 for (i=0 ; i < num_all_fds ; i++) {
6811 /* switch on an handler designed to
6812 * handle the SSL_free
6813 */
6814 afd = all_fd[i];
6815 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006816 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006817 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006818 /* To ensure that the fd cache won't be used
6819 * and we'll catch a real RD event.
6820 */
6821 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006822 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006823 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006824 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006825 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006826 return;
6827 }
Emeric Brun3854e012017-05-17 20:42:48 +02006828 /* Else we can remove the fds from the fdtab
6829 * and call SSL_free.
6830 * note: we do a fd_remove and not a delete
6831 * because the fd is owned by the engine.
6832 * the engine is responsible to close
6833 */
6834 for (i=0 ; i < num_all_fds ; i++)
6835 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006836 }
6837#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006838 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006839 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006840 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006841 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006842 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006843 }
Emeric Brun46591952012-05-18 15:47:34 +02006844}
6845
6846/* This function tries to perform a clean shutdown on an SSL connection, and in
6847 * any case, flags the connection as reusable if no handshake was in progress.
6848 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006849static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006850{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006851 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006852
Willy Tarreau911db9b2020-01-23 16:27:54 +01006853 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006854 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006855 if (!clean)
6856 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006857 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006858 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006859 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006860 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006861 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006862 ERR_clear_error();
6863 }
Emeric Brun46591952012-05-18 15:47:34 +02006864}
6865
William Lallemandd4f946c2019-12-05 10:26:40 +01006866/* fill a buffer with the algorithm and size of a public key */
6867static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006868{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006869 int bits = 0;
6870 int sig = TLSEXT_signature_anonymous;
6871 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006872 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006873
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006874 pkey = X509_get_pubkey(crt);
6875 if (pkey) {
6876 bits = EVP_PKEY_bits(pkey);
6877 switch(EVP_PKEY_base_id(pkey)) {
6878 case EVP_PKEY_RSA:
6879 sig = TLSEXT_signature_rsa;
6880 break;
6881 case EVP_PKEY_EC:
6882 sig = TLSEXT_signature_ecdsa;
6883 break;
6884 case EVP_PKEY_DSA:
6885 sig = TLSEXT_signature_dsa;
6886 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006887 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006888 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006889 }
6890
6891 switch(sig) {
6892 case TLSEXT_signature_rsa:
6893 len = chunk_printf(out, "RSA%d", bits);
6894 break;
6895 case TLSEXT_signature_ecdsa:
6896 len = chunk_printf(out, "EC%d", bits);
6897 break;
6898 case TLSEXT_signature_dsa:
6899 len = chunk_printf(out, "DSA%d", bits);
6900 break;
6901 default:
6902 return 0;
6903 }
6904 if (len < 0)
6905 return 0;
6906 return 1;
6907}
6908
William Lallemandd4f946c2019-12-05 10:26:40 +01006909/* used for ppv2 pkey alog (can be used for logging) */
6910int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6911{
6912 struct ssl_sock_ctx *ctx;
6913 X509 *crt;
6914
6915 if (!ssl_sock_is_ssl(conn))
6916 return 0;
6917
6918 ctx = conn->xprt_ctx;
6919
6920 crt = SSL_get_certificate(ctx->ssl);
6921 if (!crt)
6922 return 0;
6923
6924 return cert_get_pkey_algo(crt, out);
6925}
6926
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006927/* used for ppv2 cert signature (can be used for logging) */
6928const char *ssl_sock_get_cert_sig(struct connection *conn)
6929{
Christopher Faulet82004142019-09-10 10:12:03 +02006930 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006931
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006932 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6933 X509 *crt;
6934
6935 if (!ssl_sock_is_ssl(conn))
6936 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006937 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006938 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006939 if (!crt)
6940 return NULL;
6941 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6942 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6943}
6944
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006945/* used for ppv2 authority */
6946const char *ssl_sock_get_sni(struct connection *conn)
6947{
6948#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006949 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006950
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006951 if (!ssl_sock_is_ssl(conn))
6952 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006953 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006954 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006955#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006956 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006957#endif
6958}
6959
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006960/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006961const char *ssl_sock_get_cipher_name(struct connection *conn)
6962{
Christopher Faulet82004142019-09-10 10:12:03 +02006963 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006964
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006965 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006966 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006967 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006968 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006969}
6970
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006971/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006972const char *ssl_sock_get_proto_version(struct connection *conn)
6973{
Christopher Faulet82004142019-09-10 10:12:03 +02006974 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006975
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006976 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006977 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006978 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006979 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006980}
6981
Willy Tarreau8d598402012-10-22 17:58:39 +02006982/* Extract a serial from a cert, and copy it to a chunk.
6983 * Returns 1 if serial is found and copied, 0 if no serial found and
6984 * -1 if output is not large enough.
6985 */
6986static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006987ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006988{
6989 ASN1_INTEGER *serial;
6990
6991 serial = X509_get_serialNumber(crt);
6992 if (!serial)
6993 return 0;
6994
6995 if (out->size < serial->length)
6996 return -1;
6997
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006998 memcpy(out->area, serial->data, serial->length);
6999 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007000 return 1;
7001}
7002
Emeric Brun43e79582014-10-29 19:03:26 +01007003/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007004 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7005 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007006 */
7007static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007008ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007009{
7010 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007011 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007012
7013 len =i2d_X509(crt, NULL);
7014 if (len <= 0)
7015 return 1;
7016
7017 if (out->size < len)
7018 return -1;
7019
7020 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007021 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007022 return 1;
7023}
7024
Emeric Brunce5ad802012-10-22 14:11:22 +02007025
Willy Tarreau83061a82018-07-13 11:56:34 +02007026/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007027 * Returns 1 if serial is found and copied, 0 if no valid time found
7028 * and -1 if output is not large enough.
7029 */
7030static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007031ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007032{
7033 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7034 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7035
7036 if (gentm->length < 12)
7037 return 0;
7038 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7039 return 0;
7040 if (out->size < gentm->length-2)
7041 return -1;
7042
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007043 memcpy(out->area, gentm->data+2, gentm->length-2);
7044 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007045 return 1;
7046 }
7047 else if (tm->type == V_ASN1_UTCTIME) {
7048 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7049
7050 if (utctm->length < 10)
7051 return 0;
7052 if (utctm->data[0] >= 0x35)
7053 return 0;
7054 if (out->size < utctm->length)
7055 return -1;
7056
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007057 memcpy(out->area, utctm->data, utctm->length);
7058 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007059 return 1;
7060 }
7061
7062 return 0;
7063}
7064
Emeric Brun87855892012-10-17 17:39:35 +02007065/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7066 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7067 */
7068static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007069ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7070 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007071{
7072 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007073 ASN1_OBJECT *obj;
7074 ASN1_STRING *data;
7075 const unsigned char *data_ptr;
7076 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007077 int i, j, n;
7078 int cur = 0;
7079 const char *s;
7080 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007081 int name_count;
7082
7083 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007084
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007085 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007086 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007087 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007088 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007089 else
7090 j = i;
7091
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007092 ne = X509_NAME_get_entry(a, j);
7093 obj = X509_NAME_ENTRY_get_object(ne);
7094 data = X509_NAME_ENTRY_get_data(ne);
7095 data_ptr = ASN1_STRING_get0_data(data);
7096 data_len = ASN1_STRING_length(data);
7097 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007098 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007099 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007100 s = tmp;
7101 }
7102
7103 if (chunk_strcasecmp(entry, s) != 0)
7104 continue;
7105
7106 if (pos < 0)
7107 cur--;
7108 else
7109 cur++;
7110
7111 if (cur != pos)
7112 continue;
7113
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007114 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007115 return -1;
7116
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007117 memcpy(out->area, data_ptr, data_len);
7118 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007119 return 1;
7120 }
7121
7122 return 0;
7123
William Lallemandd4f946c2019-12-05 10:26:40 +01007124}
7125
7126/*
7127 * Extract and format the DNS SAN extensions and copy result into a chuink
7128 * Return 0;
7129 */
7130#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7131static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7132{
7133 int i;
7134 char *str;
7135 STACK_OF(GENERAL_NAME) *names = NULL;
7136
7137 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7138 if (names) {
7139 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7140 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7141 if (i > 0)
7142 chunk_appendf(out, ", ");
7143 if (name->type == GEN_DNS) {
7144 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7145 chunk_appendf(out, "DNS:%s", str);
7146 OPENSSL_free(str);
7147 }
7148 }
7149 }
7150 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7151 }
7152 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007153}
William Lallemandd4f946c2019-12-05 10:26:40 +01007154#endif
Emeric Brun87855892012-10-17 17:39:35 +02007155
Elliot Otchet71f82972020-01-15 08:12:14 -05007156/*
7157 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7158 * Currently supports rfc2253 for returning LDAP V3 DNs.
7159 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7160 */
7161static int
7162ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7163{
7164 BIO *bio = NULL;
7165 int ret = 0;
7166 int data_len = 0;
7167
7168 if (chunk_strcmp(format, "rfc2253") == 0) {
7169 bio = BIO_new(BIO_s_mem());
7170 if (bio == NULL)
7171 goto out;
7172
7173 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7174 goto out;
7175
7176 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7177 goto out;
7178
7179 out->data = data_len;
7180
7181 ret = 1;
7182 }
7183out:
7184 if (bio)
7185 BIO_free(bio);
7186 return ret;
7187}
7188
Emeric Brun87855892012-10-17 17:39:35 +02007189/* Extract and format full DN from a X509_NAME and copy result into a chunk
7190 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7191 */
7192static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007193ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007194{
7195 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007196 ASN1_OBJECT *obj;
7197 ASN1_STRING *data;
7198 const unsigned char *data_ptr;
7199 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007200 int i, n, ln;
7201 int l = 0;
7202 const char *s;
7203 char *p;
7204 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007205 int name_count;
7206
7207
7208 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007209
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007210 out->data = 0;
7211 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007212 for (i = 0; i < name_count; i++) {
7213 ne = X509_NAME_get_entry(a, i);
7214 obj = X509_NAME_ENTRY_get_object(ne);
7215 data = X509_NAME_ENTRY_get_data(ne);
7216 data_ptr = ASN1_STRING_get0_data(data);
7217 data_len = ASN1_STRING_length(data);
7218 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007219 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007220 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007221 s = tmp;
7222 }
7223 ln = strlen(s);
7224
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007225 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007226 if (l > out->size)
7227 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007228 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007229
7230 *(p++)='/';
7231 memcpy(p, s, ln);
7232 p += ln;
7233 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007234 memcpy(p, data_ptr, data_len);
7235 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007236 }
7237
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007238 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007239 return 0;
7240
7241 return 1;
7242}
7243
Olivier Houchardab28a322018-12-21 19:45:40 +01007244void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7245{
7246#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007247 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007248
Olivier Houcharde488ea82019-06-28 14:10:33 +02007249 if (!ssl_sock_is_ssl(conn))
7250 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007251 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007252 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007253#endif
7254}
7255
Willy Tarreau119a4082016-12-22 21:58:38 +01007256/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7257 * to disable SNI.
7258 */
Willy Tarreau63076412015-07-10 11:33:32 +02007259void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7260{
7261#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007262 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007263
Willy Tarreau119a4082016-12-22 21:58:38 +01007264 char *prev_name;
7265
Willy Tarreau63076412015-07-10 11:33:32 +02007266 if (!ssl_sock_is_ssl(conn))
7267 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007268 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007269
Willy Tarreau119a4082016-12-22 21:58:38 +01007270 /* if the SNI changes, we must destroy the reusable context so that a
7271 * new connection will present a new SNI. As an optimization we could
7272 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7273 * server.
7274 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007275 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007276 if ((!prev_name && hostname) ||
7277 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007278 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007279
Olivier Houchard66ab4982019-02-26 18:37:15 +01007280 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007281#endif
7282}
7283
Emeric Brun0abf8362014-06-24 18:26:41 +02007284/* Extract peer certificate's common name into the chunk dest
7285 * Returns
7286 * the len of the extracted common name
7287 * or 0 if no CN found in DN
7288 * or -1 on error case (i.e. no peer certificate)
7289 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007290int ssl_sock_get_remote_common_name(struct connection *conn,
7291 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007292{
Christopher Faulet82004142019-09-10 10:12:03 +02007293 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007294 X509 *crt = NULL;
7295 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007296 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007297 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007298 .area = (char *)&find_cn,
7299 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007300 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007301 int result = -1;
David Safb76832014-05-08 23:42:08 -04007302
7303 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007304 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007305 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007306
7307 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007308 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007309 if (!crt)
7310 goto out;
7311
7312 name = X509_get_subject_name(crt);
7313 if (!name)
7314 goto out;
David Safb76832014-05-08 23:42:08 -04007315
Emeric Brun0abf8362014-06-24 18:26:41 +02007316 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7317out:
David Safb76832014-05-08 23:42:08 -04007318 if (crt)
7319 X509_free(crt);
7320
7321 return result;
7322}
7323
Dave McCowan328fb582014-07-30 10:39:13 -04007324/* returns 1 if client passed a certificate for this session, 0 if not */
7325int ssl_sock_get_cert_used_sess(struct connection *conn)
7326{
Christopher Faulet82004142019-09-10 10:12:03 +02007327 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007328 X509 *crt = NULL;
7329
7330 if (!ssl_sock_is_ssl(conn))
7331 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007332 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007333
7334 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007335 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007336 if (!crt)
7337 return 0;
7338
7339 X509_free(crt);
7340 return 1;
7341}
7342
7343/* returns 1 if client passed a certificate for this connection, 0 if not */
7344int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007345{
Christopher Faulet82004142019-09-10 10:12:03 +02007346 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007347
David Safb76832014-05-08 23:42:08 -04007348 if (!ssl_sock_is_ssl(conn))
7349 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007350 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007351 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007352}
7353
7354/* returns result from SSL verify */
7355unsigned int ssl_sock_get_verify_result(struct connection *conn)
7356{
Christopher Faulet82004142019-09-10 10:12:03 +02007357 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007358
David Safb76832014-05-08 23:42:08 -04007359 if (!ssl_sock_is_ssl(conn))
7360 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007361 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007362 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007363}
7364
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007365/* Returns the application layer protocol name in <str> and <len> when known.
7366 * Zero is returned if the protocol name was not found, otherwise non-zero is
7367 * returned. The string is allocated in the SSL context and doesn't have to be
7368 * freed by the caller. NPN is also checked if available since older versions
7369 * of openssl (1.0.1) which are more common in field only support this one.
7370 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007371static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007372{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007373#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7374 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007375 struct ssl_sock_ctx *ctx = xprt_ctx;
7376 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007377 return 0;
7378
7379 *str = NULL;
7380
7381#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007382 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007383 if (*str)
7384 return 1;
7385#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007386#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007387 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007388 if (*str)
7389 return 1;
7390#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007391#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007392 return 0;
7393}
7394
Willy Tarreau7875d092012-09-10 08:20:03 +02007395/***** Below are some sample fetching functions for ACL/patterns *****/
7396
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007397static int
7398smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7399{
7400 struct connection *conn;
7401
7402 conn = objt_conn(smp->sess->origin);
7403 if (!conn || conn->xprt != &ssl_sock)
7404 return 0;
7405
7406 smp->flags = 0;
7407 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007408#ifdef OPENSSL_IS_BORINGSSL
7409 {
7410 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7411 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7412 SSL_early_data_accepted(ctx->ssl));
7413 }
7414#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007415 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007416 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007417#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007418 return 1;
7419}
7420
Emeric Brune64aef12012-09-21 13:15:06 +02007421/* boolean, returns true if client cert was present */
7422static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007423smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007424{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007425 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007426 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007427
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007428 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007429 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007430 return 0;
7431
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007432 ctx = conn->xprt_ctx;
7433
Willy Tarreau911db9b2020-01-23 16:27:54 +01007434 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007435 smp->flags |= SMP_F_MAY_CHANGE;
7436 return 0;
7437 }
7438
7439 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007440 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007441 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007442
7443 return 1;
7444}
7445
Emeric Brun43e79582014-10-29 19:03:26 +01007446/* binary, returns a certificate in a binary chunk (der/raw).
7447 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7448 * should be use.
7449 */
7450static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007451smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007452{
7453 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7454 X509 *crt = NULL;
7455 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007456 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007457 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007458 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007459
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007460 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007461 if (!conn || conn->xprt != &ssl_sock)
7462 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007463 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007464
Willy Tarreau911db9b2020-01-23 16:27:54 +01007465 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007466 smp->flags |= SMP_F_MAY_CHANGE;
7467 return 0;
7468 }
7469
7470 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007471 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007472 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007473 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007474
7475 if (!crt)
7476 goto out;
7477
7478 smp_trash = get_trash_chunk();
7479 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7480 goto out;
7481
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007482 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007483 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007484 ret = 1;
7485out:
7486 /* SSL_get_peer_certificate, it increase X509 * ref count */
7487 if (cert_peer && crt)
7488 X509_free(crt);
7489 return ret;
7490}
7491
Emeric Brunba841a12014-04-30 17:05:08 +02007492/* binary, returns serial of certificate in a binary chunk.
7493 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7494 * should be use.
7495 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007496static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007497smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007498{
Emeric Brunba841a12014-04-30 17:05:08 +02007499 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007500 X509 *crt = NULL;
7501 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007502 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007503 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007504 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007505
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007506 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007507 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007508 return 0;
7509
Olivier Houchard66ab4982019-02-26 18:37:15 +01007510 ctx = conn->xprt_ctx;
7511
Willy Tarreau911db9b2020-01-23 16:27:54 +01007512 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007513 smp->flags |= SMP_F_MAY_CHANGE;
7514 return 0;
7515 }
7516
Emeric Brunba841a12014-04-30 17:05:08 +02007517 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007518 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007519 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007520 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007521
Willy Tarreau8d598402012-10-22 17:58:39 +02007522 if (!crt)
7523 goto out;
7524
Willy Tarreau47ca5452012-12-23 20:22:19 +01007525 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007526 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7527 goto out;
7528
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007529 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007530 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007531 ret = 1;
7532out:
Emeric Brunba841a12014-04-30 17:05:08 +02007533 /* SSL_get_peer_certificate, it increase X509 * ref count */
7534 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007535 X509_free(crt);
7536 return ret;
7537}
Emeric Brune64aef12012-09-21 13:15:06 +02007538
Emeric Brunba841a12014-04-30 17:05:08 +02007539/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7540 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7541 * should be use.
7542 */
James Votha051b4a2013-05-14 20:37:59 +02007543static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007544smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007545{
Emeric Brunba841a12014-04-30 17:05:08 +02007546 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007547 X509 *crt = NULL;
7548 const EVP_MD *digest;
7549 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007550 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007551 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007552 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007553
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007554 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007555 if (!conn || conn->xprt != &ssl_sock)
7556 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007557 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007558
Willy Tarreau911db9b2020-01-23 16:27:54 +01007559 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02007560 smp->flags |= SMP_F_MAY_CHANGE;
7561 return 0;
7562 }
7563
Emeric Brunba841a12014-04-30 17:05:08 +02007564 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007565 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007566 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007567 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007568 if (!crt)
7569 goto out;
7570
7571 smp_trash = get_trash_chunk();
7572 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007573 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7574 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007575
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007576 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007577 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007578 ret = 1;
7579out:
Emeric Brunba841a12014-04-30 17:05:08 +02007580 /* SSL_get_peer_certificate, it increase X509 * ref count */
7581 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007582 X509_free(crt);
7583 return ret;
7584}
7585
Emeric Brunba841a12014-04-30 17:05:08 +02007586/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7587 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7588 * should be use.
7589 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007590static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007591smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007592{
Emeric Brunba841a12014-04-30 17:05:08 +02007593 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007594 X509 *crt = NULL;
7595 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007596 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007597 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007598 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007599
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007600 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007601 if (!conn || conn->xprt != &ssl_sock)
7602 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007603 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007604
Willy Tarreau911db9b2020-01-23 16:27:54 +01007605 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007606 smp->flags |= SMP_F_MAY_CHANGE;
7607 return 0;
7608 }
7609
Emeric Brunba841a12014-04-30 17:05:08 +02007610 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007611 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007612 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007613 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007614 if (!crt)
7615 goto out;
7616
Willy Tarreau47ca5452012-12-23 20:22:19 +01007617 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007618 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007619 goto out;
7620
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007621 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007622 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007623 ret = 1;
7624out:
Emeric Brunba841a12014-04-30 17:05:08 +02007625 /* SSL_get_peer_certificate, it increase X509 * ref count */
7626 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007627 X509_free(crt);
7628 return ret;
7629}
7630
Emeric Brunba841a12014-04-30 17:05:08 +02007631/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7632 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7633 * should be use.
7634 */
Emeric Brun87855892012-10-17 17:39:35 +02007635static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007636smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007637{
Emeric Brunba841a12014-04-30 17:05:08 +02007638 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007639 X509 *crt = NULL;
7640 X509_NAME *name;
7641 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007642 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007643 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007644 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007645
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007646 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007647 if (!conn || conn->xprt != &ssl_sock)
7648 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007649 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007650
Willy Tarreau911db9b2020-01-23 16:27:54 +01007651 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02007652 smp->flags |= SMP_F_MAY_CHANGE;
7653 return 0;
7654 }
7655
Emeric Brunba841a12014-04-30 17:05:08 +02007656 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007657 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007658 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007659 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007660 if (!crt)
7661 goto out;
7662
7663 name = X509_get_issuer_name(crt);
7664 if (!name)
7665 goto out;
7666
Willy Tarreau47ca5452012-12-23 20:22:19 +01007667 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007668 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007669 int pos = 1;
7670
7671 if (args[1].type == ARGT_SINT)
7672 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007673
7674 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7675 goto out;
7676 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007677 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7678 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7679 goto out;
7680 }
Emeric Brun87855892012-10-17 17:39:35 +02007681 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7682 goto out;
7683
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007684 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007685 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007686 ret = 1;
7687out:
Emeric Brunba841a12014-04-30 17:05:08 +02007688 /* SSL_get_peer_certificate, it increase X509 * ref count */
7689 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007690 X509_free(crt);
7691 return ret;
7692}
7693
Emeric Brunba841a12014-04-30 17:05:08 +02007694/* string, returns notbefore date in ASN1_UTCTIME format.
7695 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7696 * should be use.
7697 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007698static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007699smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007700{
Emeric Brunba841a12014-04-30 17:05:08 +02007701 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007702 X509 *crt = NULL;
7703 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007704 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007705 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007706 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007707
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007708 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007709 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007710 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007711 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007712
Willy Tarreau911db9b2020-01-23 16:27:54 +01007713 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007714 smp->flags |= SMP_F_MAY_CHANGE;
7715 return 0;
7716 }
7717
Emeric Brunba841a12014-04-30 17:05:08 +02007718 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007719 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007720 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007721 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007722 if (!crt)
7723 goto out;
7724
Willy Tarreau47ca5452012-12-23 20:22:19 +01007725 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007726 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007727 goto out;
7728
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007729 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007730 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007731 ret = 1;
7732out:
Emeric Brunba841a12014-04-30 17:05:08 +02007733 /* SSL_get_peer_certificate, it increase X509 * ref count */
7734 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007735 X509_free(crt);
7736 return ret;
7737}
7738
Emeric Brunba841a12014-04-30 17:05:08 +02007739/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7740 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7741 * should be use.
7742 */
Emeric Brun87855892012-10-17 17:39:35 +02007743static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007744smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007745{
Emeric Brunba841a12014-04-30 17:05:08 +02007746 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007747 X509 *crt = NULL;
7748 X509_NAME *name;
7749 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007750 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007751 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007752 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007753
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007754 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007755 if (!conn || conn->xprt != &ssl_sock)
7756 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007757 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007758
Willy Tarreau911db9b2020-01-23 16:27:54 +01007759 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02007760 smp->flags |= SMP_F_MAY_CHANGE;
7761 return 0;
7762 }
7763
Emeric Brunba841a12014-04-30 17:05:08 +02007764 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007765 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007766 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007767 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007768 if (!crt)
7769 goto out;
7770
7771 name = X509_get_subject_name(crt);
7772 if (!name)
7773 goto out;
7774
Willy Tarreau47ca5452012-12-23 20:22:19 +01007775 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007776 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007777 int pos = 1;
7778
7779 if (args[1].type == ARGT_SINT)
7780 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007781
7782 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7783 goto out;
7784 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007785 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7786 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7787 goto out;
7788 }
Emeric Brun87855892012-10-17 17:39:35 +02007789 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7790 goto out;
7791
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007792 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007793 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007794 ret = 1;
7795out:
Emeric Brunba841a12014-04-30 17:05:08 +02007796 /* SSL_get_peer_certificate, it increase X509 * ref count */
7797 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007798 X509_free(crt);
7799 return ret;
7800}
Emeric Brun9143d372012-12-20 15:44:16 +01007801
7802/* integer, returns true if current session use a client certificate */
7803static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007804smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007805{
7806 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007807 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007808 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007809
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007810 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007811 if (!conn || conn->xprt != &ssl_sock)
7812 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007813 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007814
Willy Tarreau911db9b2020-01-23 16:27:54 +01007815 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01007816 smp->flags |= SMP_F_MAY_CHANGE;
7817 return 0;
7818 }
7819
7820 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007821 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007822 if (crt) {
7823 X509_free(crt);
7824 }
7825
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007826 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007827 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007828 return 1;
7829}
7830
Emeric Brunba841a12014-04-30 17:05:08 +02007831/* integer, returns the certificate version
7832 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7833 * should be use.
7834 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007835static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007836smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007837{
Emeric Brunba841a12014-04-30 17:05:08 +02007838 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007839 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007840 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007841 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007842
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007843 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007844 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007845 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007846 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007847
Willy Tarreau911db9b2020-01-23 16:27:54 +01007848 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007849 smp->flags |= SMP_F_MAY_CHANGE;
7850 return 0;
7851 }
7852
Emeric Brunba841a12014-04-30 17:05:08 +02007853 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007854 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007855 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007856 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007857 if (!crt)
7858 return 0;
7859
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007860 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007861 /* SSL_get_peer_certificate increase X509 * ref count */
7862 if (cert_peer)
7863 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007864 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007865
7866 return 1;
7867}
7868
Emeric Brunba841a12014-04-30 17:05:08 +02007869/* string, returns the certificate's signature algorithm.
7870 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7871 * should be use.
7872 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007873static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007874smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007875{
Emeric Brunba841a12014-04-30 17:05:08 +02007876 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007877 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007878 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007879 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007880 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007881 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007882
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007883 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007884 if (!conn || conn->xprt != &ssl_sock)
7885 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007886 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007887
Willy Tarreau911db9b2020-01-23 16:27:54 +01007888 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007889 smp->flags |= SMP_F_MAY_CHANGE;
7890 return 0;
7891 }
7892
Emeric Brunba841a12014-04-30 17:05:08 +02007893 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007894 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007895 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007896 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007897 if (!crt)
7898 return 0;
7899
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007900 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7901 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007902
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007903 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7904 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007905 /* SSL_get_peer_certificate increase X509 * ref count */
7906 if (cert_peer)
7907 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007908 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007909 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007910
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007911 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007912 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007913 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007914 /* SSL_get_peer_certificate increase X509 * ref count */
7915 if (cert_peer)
7916 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007917
7918 return 1;
7919}
7920
Emeric Brunba841a12014-04-30 17:05:08 +02007921/* string, returns the certificate's key algorithm.
7922 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7923 * should be use.
7924 */
Emeric Brun521a0112012-10-22 12:22:55 +02007925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007926smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007927{
Emeric Brunba841a12014-04-30 17:05:08 +02007928 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007929 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007930 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007931 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007932 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007933 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007934
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007935 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007936 if (!conn || conn->xprt != &ssl_sock)
7937 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007938 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007939
Willy Tarreau911db9b2020-01-23 16:27:54 +01007940 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02007941 smp->flags |= SMP_F_MAY_CHANGE;
7942 return 0;
7943 }
7944
Emeric Brunba841a12014-04-30 17:05:08 +02007945 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007946 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007947 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007948 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007949 if (!crt)
7950 return 0;
7951
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007952 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7953 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007954
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007955 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7956 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007957 /* SSL_get_peer_certificate increase X509 * ref count */
7958 if (cert_peer)
7959 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007960 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007961 }
Emeric Brun521a0112012-10-22 12:22:55 +02007962
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007963 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007964 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007965 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007966 if (cert_peer)
7967 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007968
7969 return 1;
7970}
7971
Emeric Brun645ae792014-04-30 14:21:06 +02007972/* boolean, returns true if front conn. transport layer is SSL.
7973 * This function is also usable on backend conn if the fetch keyword 5th
7974 * char is 'b'.
7975 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007976static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007977smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007978{
Emeric Bruneb8def92018-02-19 15:59:48 +01007979 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7980 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007981
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007982 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007983 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007984 return 1;
7985}
7986
Emeric Brun2525b6b2012-10-18 15:59:43 +02007987/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007989smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007990{
7991#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007992 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007993 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007994
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007995 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007996 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007997 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007998 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007999 return 1;
8000#else
8001 return 0;
8002#endif
8003}
8004
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008005/* boolean, returns true if client session has been resumed.
8006 * This function is also usable on backend conn if the fetch keyword 5th
8007 * char is 'b'.
8008 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008009static int
8010smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8011{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008012 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8013 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008014 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008015
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008016
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008017 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008018 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008019 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008020 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008021 return 1;
8022}
8023
Emeric Brun645ae792014-04-30 14:21:06 +02008024/* string, returns the used cipher if front conn. transport layer is SSL.
8025 * This function is also usable on backend conn if the fetch keyword 5th
8026 * char is 'b'.
8027 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008028static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008029smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008030{
Emeric Bruneb8def92018-02-19 15:59:48 +01008031 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8032 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008033 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008034
Willy Tarreaube508f12016-03-10 11:47:01 +01008035 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008036 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008037 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008038 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008039
Olivier Houchard66ab4982019-02-26 18:37:15 +01008040 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008041 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008042 return 0;
8043
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008044 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008045 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008046 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008047
8048 return 1;
8049}
8050
Emeric Brun645ae792014-04-30 14:21:06 +02008051/* integer, returns the algoritm's keysize if front conn. transport layer
8052 * is SSL.
8053 * This function is also usable on backend conn if the fetch keyword 5th
8054 * char is 'b'.
8055 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008056static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008057smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008058{
Emeric Bruneb8def92018-02-19 15:59:48 +01008059 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8060 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008061 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008062 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008063
Emeric Brun589fcad2012-10-16 14:13:26 +02008064 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008065 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008066 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008067 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008068
Olivier Houchard66ab4982019-02-26 18:37:15 +01008069 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008070 return 0;
8071
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008072 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008073 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008074
8075 return 1;
8076}
8077
Emeric Brun645ae792014-04-30 14:21:06 +02008078/* integer, returns the used keysize if front conn. transport layer is SSL.
8079 * This function is also usable on backend conn if the fetch keyword 5th
8080 * char is 'b'.
8081 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008083smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008084{
Emeric Bruneb8def92018-02-19 15:59:48 +01008085 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8086 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008087 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008088
Emeric Brun589fcad2012-10-16 14:13:26 +02008089 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008090 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8091 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008092 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008093
Olivier Houchard66ab4982019-02-26 18:37:15 +01008094 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008095 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008096 return 0;
8097
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008098 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008099
8100 return 1;
8101}
8102
Bernard Spil13c53f82018-02-15 13:34:58 +01008103#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008104static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008105smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008106{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008107 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008108 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008109
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008110 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008111 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008112
Olivier Houchard6b77f492018-11-22 18:18:29 +01008113 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8114 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008115 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8116 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008117 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008118
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008119 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008120 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008121 (const unsigned char **)&smp->data.u.str.area,
8122 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008123
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008124 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008125 return 0;
8126
8127 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008128}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008129#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008130
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008131#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008133smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008134{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008135 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008136 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008137
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008138 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008139 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008140
Olivier Houchard6b77f492018-11-22 18:18:29 +01008141 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8142 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8143
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008144 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008145 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008146 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008147
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008148 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008149 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008150 (const unsigned char **)&smp->data.u.str.area,
8151 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02008152
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008153 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008154 return 0;
8155
8156 return 1;
8157}
8158#endif
8159
Emeric Brun645ae792014-04-30 14:21:06 +02008160/* string, returns the used protocol if front conn. transport layer is SSL.
8161 * This function is also usable on backend conn if the fetch keyword 5th
8162 * char is 'b'.
8163 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008164static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008165smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008166{
Emeric Bruneb8def92018-02-19 15:59:48 +01008167 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8168 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008169 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008170
Emeric Brun589fcad2012-10-16 14:13:26 +02008171 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008172 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8173 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008174 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008175
Olivier Houchard66ab4982019-02-26 18:37:15 +01008176 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008177 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008178 return 0;
8179
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008180 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008181 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008182 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008183
8184 return 1;
8185}
8186
Willy Tarreau87b09662015-04-03 00:22:06 +02008187/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008188 * This function is also usable on backend conn if the fetch keyword 5th
8189 * char is 'b'.
8190 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008191#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008192static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008193smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008194{
Emeric Bruneb8def92018-02-19 15:59:48 +01008195 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8196 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008197 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008198 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008199
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008200 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008201 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008202
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008203 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8204 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008205 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008206
Olivier Houchard66ab4982019-02-26 18:37:15 +01008207 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008208 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008209 return 0;
8210
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008211 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
8212 (unsigned int *)&smp->data.u.str.data);
8213 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008214 return 0;
8215
8216 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008217}
Patrick Hemmer41966772018-04-28 19:15:48 -04008218#endif
8219
Emeric Brunfe68f682012-10-16 14:59:28 +02008220
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008221#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008222static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008223smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8224{
8225 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8226 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8227 struct buffer *data;
8228 struct ssl_sock_ctx *ctx;
8229
8230 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8231 return 0;
8232 ctx = conn->xprt_ctx;
8233
8234 data = get_trash_chunk();
8235 if (kw[7] == 'c')
8236 data->data = SSL_get_client_random(ctx->ssl,
8237 (unsigned char *) data->area,
8238 data->size);
8239 else
8240 data->data = SSL_get_server_random(ctx->ssl,
8241 (unsigned char *) data->area,
8242 data->size);
8243 if (!data->data)
8244 return 0;
8245
8246 smp->flags = 0;
8247 smp->data.type = SMP_T_BIN;
8248 smp->data.u.str = *data;
8249
8250 return 1;
8251}
8252
8253static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008254smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8255{
8256 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8257 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8258 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008259 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008260 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008261
8262 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8263 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008264 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008265
Olivier Houchard66ab4982019-02-26 18:37:15 +01008266 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008267 if (!ssl_sess)
8268 return 0;
8269
8270 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008271 data->data = SSL_SESSION_get_master_key(ssl_sess,
8272 (unsigned char *) data->area,
8273 data->size);
8274 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008275 return 0;
8276
8277 smp->flags = 0;
8278 smp->data.type = SMP_T_BIN;
8279 smp->data.u.str = *data;
8280
8281 return 1;
8282}
8283#endif
8284
Patrick Hemmer41966772018-04-28 19:15:48 -04008285#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008286static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008287smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008288{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008289 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008290 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008291
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008292 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008293 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008294
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008295 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008296 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8297 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008298 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008299
Olivier Houchard66ab4982019-02-26 18:37:15 +01008300 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008301 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008302 return 0;
8303
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008304 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008305 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008306}
Patrick Hemmer41966772018-04-28 19:15:48 -04008307#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008308
David Sc1ad52e2014-04-08 18:48:47 -04008309static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008310smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8311{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008312 struct connection *conn;
8313 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008314 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008315
8316 conn = objt_conn(smp->sess->origin);
8317 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8318 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008319 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008320
Olivier Houchard66ab4982019-02-26 18:37:15 +01008321 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008322 if (!capture)
8323 return 0;
8324
8325 smp->flags = SMP_F_CONST;
8326 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008327 smp->data.u.str.area = capture->ciphersuite;
8328 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008329 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008330}
8331
8332static int
8333smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8334{
Willy Tarreau83061a82018-07-13 11:56:34 +02008335 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008336
8337 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8338 return 0;
8339
8340 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008341 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008342 smp->data.type = SMP_T_BIN;
8343 smp->data.u.str = *data;
8344 return 1;
8345}
8346
8347static int
8348smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8349{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008350 struct connection *conn;
8351 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008352 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008353
8354 conn = objt_conn(smp->sess->origin);
8355 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8356 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008357 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008358
Olivier Houchard66ab4982019-02-26 18:37:15 +01008359 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008360 if (!capture)
8361 return 0;
8362
8363 smp->data.type = SMP_T_SINT;
8364 smp->data.u.sint = capture->xxh64;
8365 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008366}
8367
8368static int
8369smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8370{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008371#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008372 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008373 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008374
8375 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8376 return 0;
8377
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008378 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008379 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008380 const char *str;
8381 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008382 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008383 uint16_t id = (bin[0] << 8) | bin[1];
8384#if defined(OPENSSL_IS_BORINGSSL)
8385 cipher = SSL_get_cipher_by_value(id);
8386#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008387 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008388 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8389 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008390#endif
8391 str = SSL_CIPHER_get_name(cipher);
8392 if (!str || strcmp(str, "(NONE)") == 0)
8393 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008394 else
8395 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8396 }
8397 smp->data.type = SMP_T_STR;
8398 smp->data.u.str = *data;
8399 return 1;
8400#else
8401 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8402#endif
8403}
8404
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008405#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008406static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008407smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008408{
Emeric Bruneb8def92018-02-19 15:59:48 +01008409 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8410 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008411 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008412 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008413 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008414
8415 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008416 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8417 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008418 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008419
Willy Tarreau911db9b2020-01-23 16:27:54 +01008420 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008421 smp->flags |= SMP_F_MAY_CHANGE;
8422 return 0;
8423 }
8424
8425 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008426 if (!SSL_session_reused(ctx->ssl))
8427 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008428 finished_trash->area,
8429 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008430 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008431 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008432 finished_trash->area,
8433 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008434
8435 if (!finished_len)
8436 return 0;
8437
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008438 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008439 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008440 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008441
8442 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008443}
Patrick Hemmer41966772018-04-28 19:15:48 -04008444#endif
David Sc1ad52e2014-04-08 18:48:47 -04008445
Emeric Brun2525b6b2012-10-18 15:59:43 +02008446/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008448smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008449{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008450 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008451 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008452
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008453 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008454 if (!conn || conn->xprt != &ssl_sock)
8455 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008456 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008457
Willy Tarreau911db9b2020-01-23 16:27:54 +01008458 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008459 smp->flags = SMP_F_MAY_CHANGE;
8460 return 0;
8461 }
8462
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008463 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008464 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008465 smp->flags = 0;
8466
8467 return 1;
8468}
8469
Emeric Brun2525b6b2012-10-18 15:59:43 +02008470/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008471static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008472smp_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 +02008473{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008474 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008475 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008476
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008477 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008478 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008479 return 0;
8480
Willy Tarreau911db9b2020-01-23 16:27:54 +01008481 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008482 smp->flags = SMP_F_MAY_CHANGE;
8483 return 0;
8484 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008485 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008486
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008487 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008488 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008489 smp->flags = 0;
8490
8491 return 1;
8492}
8493
Emeric Brun2525b6b2012-10-18 15:59:43 +02008494/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008495static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008496smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008497{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008498 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008499 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008500
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008501 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008502 if (!conn || conn->xprt != &ssl_sock)
8503 return 0;
8504
Willy Tarreau911db9b2020-01-23 16:27:54 +01008505 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008506 smp->flags = SMP_F_MAY_CHANGE;
8507 return 0;
8508 }
8509
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008510 ctx = conn->xprt_ctx;
8511
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008512 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008513 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008514 smp->flags = 0;
8515
8516 return 1;
8517}
8518
Emeric Brun2525b6b2012-10-18 15:59:43 +02008519/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008520static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008521smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008522{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008523 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008524 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008525
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008526 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008527 if (!conn || conn->xprt != &ssl_sock)
8528 return 0;
8529
Willy Tarreau911db9b2020-01-23 16:27:54 +01008530 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008531 smp->flags = SMP_F_MAY_CHANGE;
8532 return 0;
8533 }
8534
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008535 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008536 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008537 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008538
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008539 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008540 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008541 smp->flags = 0;
8542
8543 return 1;
8544}
8545
Emeric Brunfb510ea2012-10-05 12:00:26 +02008546/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008547static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Emeric Brund94b3fe2012-09-20 18:23:56 +02008548{
8549 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008550 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008551 return ERR_ALERT | ERR_FATAL;
8552 }
8553
Willy Tarreauef934602016-12-22 23:12:01 +01008554 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8555 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008556 else
8557 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008558
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008559 if (!ssl_store_load_locations_file(conf->ca_file)) {
8560 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8561 return ERR_ALERT | ERR_FATAL;
8562 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008563 return 0;
8564}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008565static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8566{
8567 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8568}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008569
Christopher Faulet31af49d2015-06-09 17:29:50 +02008570/* parse the "ca-sign-file" bind keyword */
8571static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8572{
8573 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008574 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008575 return ERR_ALERT | ERR_FATAL;
8576 }
8577
Willy Tarreauef934602016-12-22 23:12:01 +01008578 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8579 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008580 else
8581 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8582
8583 return 0;
8584}
8585
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008586/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008587static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8588{
8589 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008590 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008591 return ERR_ALERT | ERR_FATAL;
8592 }
8593 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8594 return 0;
8595}
8596
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008597/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008598static 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 +02008599{
8600 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008601 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008602 return ERR_ALERT | ERR_FATAL;
8603 }
8604
Emeric Brun76d88952012-10-05 15:47:31 +02008605 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008606 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008607 return 0;
8608}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008609static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8610{
8611 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8612}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008613
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008614#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008615/* parse the "ciphersuites" bind keyword */
8616static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8617{
8618 if (!*args[cur_arg + 1]) {
8619 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8620 return ERR_ALERT | ERR_FATAL;
8621 }
8622
8623 free(conf->ciphersuites);
8624 conf->ciphersuites = strdup(args[cur_arg + 1]);
8625 return 0;
8626}
8627static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8628{
8629 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8630}
8631#endif
8632
Willy Tarreaubbc91962019-10-16 16:42:19 +02008633/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008634static 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 +02008635{
Willy Tarreau38011032013-08-13 16:59:39 +02008636 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008637
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008638 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008639 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008640 return ERR_ALERT | ERR_FATAL;
8641 }
8642
Willy Tarreauef934602016-12-22 23:12:01 +01008643 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8644 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008645 memprintf(err, "'%s' : path too long", args[cur_arg]);
8646 return ERR_ALERT | ERR_FATAL;
8647 }
Willy Tarreauef934602016-12-22 23:12:01 +01008648 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008649 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008650 }
8651
Willy Tarreaubbc91962019-10-16 16:42:19 +02008652 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008653}
8654
Willy Tarreaubbc91962019-10-16 16:42:19 +02008655/* 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 +01008656static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8657{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008658 int err_code;
8659
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008660 if (!*args[cur_arg + 1]) {
8661 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8662 return ERR_ALERT | ERR_FATAL;
8663 }
8664
Willy Tarreaubbc91962019-10-16 16:42:19 +02008665 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8666 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008667 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008668
Willy Tarreaubbc91962019-10-16 16:42:19 +02008669 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008670}
8671
Emeric Brunfb510ea2012-10-05 12:00:26 +02008672/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008673static 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 +02008674{
Emeric Brun051cdab2012-10-02 19:25:50 +02008675#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008676 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008677 return ERR_ALERT | ERR_FATAL;
8678#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008679 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008680 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008681 return ERR_ALERT | ERR_FATAL;
8682 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008683
Willy Tarreauef934602016-12-22 23:12:01 +01008684 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8685 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008686 else
8687 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008688
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008689 if (!ssl_store_load_locations_file(conf->crl_file)) {
8690 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8691 return ERR_ALERT | ERR_FATAL;
8692 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008693 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008694#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008695}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008696static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8697{
8698 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8699}
Emeric Brun2b58d042012-09-20 17:10:03 +02008700
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008701/* parse the "curves" bind keyword keyword */
8702static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8703{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008704#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008705 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008706 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008707 return ERR_ALERT | ERR_FATAL;
8708 }
8709 conf->curves = strdup(args[cur_arg + 1]);
8710 return 0;
8711#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008712 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008713 return ERR_ALERT | ERR_FATAL;
8714#endif
8715}
8716static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8717{
8718 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8719}
8720
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008721/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008722static 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 +02008723{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008724#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008725 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008726 return ERR_ALERT | ERR_FATAL;
8727#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008728 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 +02008729 return ERR_ALERT | ERR_FATAL;
8730#else
8731 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008732 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008733 return ERR_ALERT | ERR_FATAL;
8734 }
8735
8736 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008737
8738 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008739#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008740}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008741static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8742{
8743 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8744}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008745
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008746/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008747static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8748{
8749 int code;
8750 char *p = args[cur_arg + 1];
8751 unsigned long long *ignerr = &conf->crt_ignerr;
8752
8753 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008754 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008755 return ERR_ALERT | ERR_FATAL;
8756 }
8757
8758 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8759 ignerr = &conf->ca_ignerr;
8760
8761 if (strcmp(p, "all") == 0) {
8762 *ignerr = ~0ULL;
8763 return 0;
8764 }
8765
8766 while (p) {
8767 code = atoi(p);
8768 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008769 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8770 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008771 return ERR_ALERT | ERR_FATAL;
8772 }
8773 *ignerr |= 1ULL << code;
8774 p = strchr(p, ',');
8775 if (p)
8776 p++;
8777 }
8778
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008779 return 0;
8780}
8781
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008782/* parse tls_method_options "no-xxx" and "force-xxx" */
8783static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008784{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008785 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008786 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008787 p = strchr(arg, '-');
8788 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008789 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008790 p++;
8791 if (!strcmp(p, "sslv3"))
8792 v = CONF_SSLV3;
8793 else if (!strcmp(p, "tlsv10"))
8794 v = CONF_TLSV10;
8795 else if (!strcmp(p, "tlsv11"))
8796 v = CONF_TLSV11;
8797 else if (!strcmp(p, "tlsv12"))
8798 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008799 else if (!strcmp(p, "tlsv13"))
8800 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008801 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008802 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008803 if (!strncmp(arg, "no-", 3))
8804 methods->flags |= methodVersions[v].flag;
8805 else if (!strncmp(arg, "force-", 6))
8806 methods->min = methods->max = v;
8807 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008808 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008809 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008810 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008811 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008812 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008813}
8814
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008815static 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 +02008816{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008817 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008818}
8819
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008820static 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 +02008821{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008822 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8823}
8824
8825/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8826static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8827{
8828 uint16_t i, v = 0;
8829 char *argv = args[cur_arg + 1];
8830 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008831 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008832 return ERR_ALERT | ERR_FATAL;
8833 }
8834 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8835 if (!strcmp(argv, methodVersions[i].name))
8836 v = i;
8837 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008838 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008839 return ERR_ALERT | ERR_FATAL;
8840 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008841 if (!strcmp("ssl-min-ver", args[cur_arg]))
8842 methods->min = v;
8843 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8844 methods->max = v;
8845 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008846 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008847 return ERR_ALERT | ERR_FATAL;
8848 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008849 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008850}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008851
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008852static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8853{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008854#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008855 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 +02008856#endif
8857 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8858}
8859
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008860static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8861{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008862 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008863}
8864
8865static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8866{
8867 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8868}
8869
Emeric Brun2d0c4822012-10-02 13:45:20 +02008870/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008871static 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 +02008872{
Emeric Brun89675492012-10-05 13:48:26 +02008873 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008874 return 0;
8875}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008876
Olivier Houchardc2aae742017-09-22 18:26:28 +02008877/* parse the "allow-0rtt" bind keyword */
8878static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8879{
8880 conf->early_data = 1;
8881 return 0;
8882}
8883
8884static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8885{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008886 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008887 return 0;
8888}
8889
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008890/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008891static 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 +02008892{
Bernard Spil13c53f82018-02-15 13:34:58 +01008893#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008894 char *p1, *p2;
8895
8896 if (!*args[cur_arg + 1]) {
8897 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8898 return ERR_ALERT | ERR_FATAL;
8899 }
8900
8901 free(conf->npn_str);
8902
Willy Tarreau3724da12016-02-12 17:11:12 +01008903 /* the NPN string is built as a suite of (<len> <name>)*,
8904 * so we reuse each comma to store the next <len> and need
8905 * one more for the end of the string.
8906 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008907 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008908 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008909 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8910
8911 /* replace commas with the name length */
8912 p1 = conf->npn_str;
8913 p2 = p1 + 1;
8914 while (1) {
8915 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8916 if (!p2)
8917 p2 = p1 + 1 + strlen(p1 + 1);
8918
8919 if (p2 - (p1 + 1) > 255) {
8920 *p2 = '\0';
8921 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8922 return ERR_ALERT | ERR_FATAL;
8923 }
8924
8925 *p1 = p2 - (p1 + 1);
8926 p1 = p2;
8927
8928 if (!*p2)
8929 break;
8930
8931 *(p2++) = '\0';
8932 }
8933 return 0;
8934#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008935 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008936 return ERR_ALERT | ERR_FATAL;
8937#endif
8938}
8939
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008940static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8941{
8942 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8943}
8944
Willy Tarreauab861d32013-04-02 02:30:41 +02008945/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008946static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Willy Tarreauab861d32013-04-02 02:30:41 +02008947{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008948#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008949 char *p1, *p2;
8950
8951 if (!*args[cur_arg + 1]) {
8952 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8953 return ERR_ALERT | ERR_FATAL;
8954 }
8955
8956 free(conf->alpn_str);
8957
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008958 /* the ALPN string is built as a suite of (<len> <name>)*,
8959 * so we reuse each comma to store the next <len> and need
8960 * one more for the end of the string.
8961 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008962 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008963 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008964 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8965
8966 /* replace commas with the name length */
8967 p1 = conf->alpn_str;
8968 p2 = p1 + 1;
8969 while (1) {
8970 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8971 if (!p2)
8972 p2 = p1 + 1 + strlen(p1 + 1);
8973
8974 if (p2 - (p1 + 1) > 255) {
8975 *p2 = '\0';
8976 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8977 return ERR_ALERT | ERR_FATAL;
8978 }
8979
8980 *p1 = p2 - (p1 + 1);
8981 p1 = p2;
8982
8983 if (!*p2)
8984 break;
8985
8986 *(p2++) = '\0';
8987 }
8988 return 0;
8989#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008990 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008991 return ERR_ALERT | ERR_FATAL;
8992#endif
8993}
8994
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008995static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8996{
8997 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8998}
8999
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009000/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009001static 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 +02009002{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009003 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009004 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009005
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009006 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9007 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009008#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009009 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9010 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9011#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009012 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009013 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9014 if (!conf->ssl_conf.ssl_methods.min)
9015 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9016 if (!conf->ssl_conf.ssl_methods.max)
9017 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009018
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009019 return 0;
9020}
9021
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009022/* parse the "prefer-client-ciphers" bind keyword */
9023static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9024{
9025 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9026 return 0;
9027}
9028
Christopher Faulet31af49d2015-06-09 17:29:50 +02009029/* parse the "generate-certificates" bind keyword */
9030static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9031{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009032#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009033 conf->generate_certs = 1;
9034#else
9035 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9036 err && *err ? *err : "");
9037#endif
9038 return 0;
9039}
9040
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009041/* parse the "strict-sni" bind keyword */
9042static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9043{
9044 conf->strict_sni = 1;
9045 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009046}
9047
9048/* parse the "tls-ticket-keys" bind keyword */
9049static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9050{
9051#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009052 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009053 int i = 0;
9054 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009055 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009056
9057 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009058 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009059 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009060 }
9061
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009062 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009063 if (keys_ref) {
9064 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009065 conf->keys_ref = keys_ref;
9066 return 0;
9067 }
9068
Christopher Faulete566f3d2019-10-21 09:55:49 +02009069 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009070 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009071 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009072 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009073 }
9074
Emeric Brun9e754772019-01-10 17:51:55 +01009075 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009076 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009077 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009078 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009079 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009080
9081 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009082 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009083 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009084 }
9085
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009086 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009087 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009088 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009089 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009090 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009091
Emeric Brun9e754772019-01-10 17:51:55 +01009092 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009093 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9094 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009095 int dec_size;
9096
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009097 /* Strip newline characters from the end */
9098 if(thisline[len - 1] == '\n')
9099 thisline[--len] = 0;
9100
9101 if(thisline[len - 1] == '\r')
9102 thisline[--len] = 0;
9103
Emeric Brun9e754772019-01-10 17:51:55 +01009104 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9105 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009106 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009107 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009108 }
Emeric Brun9e754772019-01-10 17:51:55 +01009109 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9110 keys_ref->key_size_bits = 128;
9111 }
9112 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9113 keys_ref->key_size_bits = 256;
9114 }
9115 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9116 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9117 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009118 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009119 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009120 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009121 i++;
9122 }
9123
9124 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009125 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 +02009126 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009127 }
9128
9129 fclose(f);
9130
9131 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009132 i -= 2;
9133 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009134 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009135 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009136 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009137 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009138
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009139 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9140
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009141 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009142
9143 fail:
9144 if (f)
9145 fclose(f);
9146 if (keys_ref) {
9147 free(keys_ref->filename);
9148 free(keys_ref->tlskeys);
9149 free(keys_ref);
9150 }
9151 return ERR_ALERT | ERR_FATAL;
9152
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009153#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009154 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009155 return ERR_ALERT | ERR_FATAL;
9156#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009157}
9158
Emeric Brund94b3fe2012-09-20 18:23:56 +02009159/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009160static 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 +02009161{
9162 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009163 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009164 return ERR_ALERT | ERR_FATAL;
9165 }
9166
9167 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009168 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009169 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009170 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009171 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009172 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009173 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009174 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9175 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009176 return ERR_ALERT | ERR_FATAL;
9177 }
9178
9179 return 0;
9180}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009181static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9182{
9183 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9184}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009185
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009186/* parse the "no-ca-names" bind keyword */
9187static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9188{
9189 conf->no_ca_names = 1;
9190 return 0;
9191}
9192static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9193{
9194 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9195}
9196
Willy Tarreau92faadf2012-10-10 23:04:25 +02009197/************** "server" keywords ****************/
9198
Olivier Houchardc7566002018-11-20 23:33:50 +01009199/* parse the "npn" bind keyword */
9200static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9201{
9202#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9203 char *p1, *p2;
9204
9205 if (!*args[*cur_arg + 1]) {
9206 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9207 return ERR_ALERT | ERR_FATAL;
9208 }
9209
9210 free(newsrv->ssl_ctx.npn_str);
9211
9212 /* the NPN string is built as a suite of (<len> <name>)*,
9213 * so we reuse each comma to store the next <len> and need
9214 * one more for the end of the string.
9215 */
9216 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9217 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9218 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9219 newsrv->ssl_ctx.npn_len);
9220
9221 /* replace commas with the name length */
9222 p1 = newsrv->ssl_ctx.npn_str;
9223 p2 = p1 + 1;
9224 while (1) {
9225 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9226 newsrv->ssl_ctx.npn_len - (p1 + 1));
9227 if (!p2)
9228 p2 = p1 + 1 + strlen(p1 + 1);
9229
9230 if (p2 - (p1 + 1) > 255) {
9231 *p2 = '\0';
9232 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9233 return ERR_ALERT | ERR_FATAL;
9234 }
9235
9236 *p1 = p2 - (p1 + 1);
9237 p1 = p2;
9238
9239 if (!*p2)
9240 break;
9241
9242 *(p2++) = '\0';
9243 }
9244 return 0;
9245#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009246 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009247 return ERR_ALERT | ERR_FATAL;
9248#endif
9249}
9250
Olivier Houchard92150142018-12-21 19:47:01 +01009251/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009252static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9253{
9254#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9255 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009256 char **alpn_str;
9257 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009258
Olivier Houchard92150142018-12-21 19:47:01 +01009259 if (*args[*cur_arg] == 'c') {
9260 alpn_str = &newsrv->check.alpn_str;
9261 alpn_len = &newsrv->check.alpn_len;
9262 } else {
9263 alpn_str = &newsrv->ssl_ctx.alpn_str;
9264 alpn_len = &newsrv->ssl_ctx.alpn_len;
9265
9266 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009267 if (!*args[*cur_arg + 1]) {
9268 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9269 return ERR_ALERT | ERR_FATAL;
9270 }
9271
Olivier Houchard92150142018-12-21 19:47:01 +01009272 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009273
9274 /* the ALPN string is built as a suite of (<len> <name>)*,
9275 * so we reuse each comma to store the next <len> and need
9276 * one more for the end of the string.
9277 */
Olivier Houchard92150142018-12-21 19:47:01 +01009278 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9279 *alpn_str = calloc(1, *alpn_len + 1);
9280 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009281
9282 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009283 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009284 p2 = p1 + 1;
9285 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009286 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009287 if (!p2)
9288 p2 = p1 + 1 + strlen(p1 + 1);
9289
9290 if (p2 - (p1 + 1) > 255) {
9291 *p2 = '\0';
9292 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9293 return ERR_ALERT | ERR_FATAL;
9294 }
9295
9296 *p1 = p2 - (p1 + 1);
9297 p1 = p2;
9298
9299 if (!*p2)
9300 break;
9301
9302 *(p2++) = '\0';
9303 }
9304 return 0;
9305#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009306 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009307 return ERR_ALERT | ERR_FATAL;
9308#endif
9309}
9310
Emeric Brunef42d922012-10-11 16:11:36 +02009311/* parse the "ca-file" server keyword */
9312static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9313{
9314 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009315 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009316 return ERR_ALERT | ERR_FATAL;
9317 }
9318
Willy Tarreauef934602016-12-22 23:12:01 +01009319 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9320 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009321 else
9322 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9323
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009324 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9325 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9326 return ERR_ALERT | ERR_FATAL;
9327 }
Emeric Brunef42d922012-10-11 16:11:36 +02009328 return 0;
9329}
9330
Olivier Houchard9130a962017-10-17 17:33:43 +02009331/* parse the "check-sni" server keyword */
9332static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9333{
9334 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009335 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009336 return ERR_ALERT | ERR_FATAL;
9337 }
9338
9339 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9340 if (!newsrv->check.sni) {
9341 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9342 return ERR_ALERT | ERR_FATAL;
9343 }
9344 return 0;
9345
9346}
9347
Willy Tarreau92faadf2012-10-10 23:04:25 +02009348/* parse the "check-ssl" server keyword */
9349static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9350{
9351 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009352 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9353 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009354#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009355 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9356 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9357#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009358 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009359 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9360 if (!newsrv->ssl_ctx.methods.min)
9361 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9362 if (!newsrv->ssl_ctx.methods.max)
9363 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9364
Willy Tarreau92faadf2012-10-10 23:04:25 +02009365 return 0;
9366}
9367
9368/* parse the "ciphers" server keyword */
9369static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9370{
9371 if (!*args[*cur_arg + 1]) {
9372 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9373 return ERR_ALERT | ERR_FATAL;
9374 }
9375
9376 free(newsrv->ssl_ctx.ciphers);
9377 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9378 return 0;
9379}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009380
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009381#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009382/* parse the "ciphersuites" server keyword */
9383static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9384{
9385 if (!*args[*cur_arg + 1]) {
9386 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9387 return ERR_ALERT | ERR_FATAL;
9388 }
9389
9390 free(newsrv->ssl_ctx.ciphersuites);
9391 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9392 return 0;
9393}
9394#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009395
Emeric Brunef42d922012-10-11 16:11:36 +02009396/* parse the "crl-file" server keyword */
9397static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9398{
9399#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009400 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009401 return ERR_ALERT | ERR_FATAL;
9402#else
9403 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009404 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009405 return ERR_ALERT | ERR_FATAL;
9406 }
9407
Willy Tarreauef934602016-12-22 23:12:01 +01009408 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9409 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009410 else
9411 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9412
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009413 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9414 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9415 return ERR_ALERT | ERR_FATAL;
9416 }
Emeric Brunef42d922012-10-11 16:11:36 +02009417 return 0;
9418#endif
9419}
9420
Emeric Bruna7aa3092012-10-26 12:58:00 +02009421/* parse the "crt" server keyword */
9422static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9423{
9424 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009425 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009426 return ERR_ALERT | ERR_FATAL;
9427 }
9428
Willy Tarreauef934602016-12-22 23:12:01 +01009429 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009430 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009431 else
9432 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9433
9434 return 0;
9435}
Emeric Brunef42d922012-10-11 16:11:36 +02009436
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009437/* parse the "no-check-ssl" server keyword */
9438static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9439{
9440 newsrv->check.use_ssl = 0;
9441 free(newsrv->ssl_ctx.ciphers);
9442 newsrv->ssl_ctx.ciphers = NULL;
9443 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9444 return 0;
9445}
9446
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009447/* parse the "no-send-proxy-v2-ssl" server keyword */
9448static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9449{
9450 newsrv->pp_opts &= ~SRV_PP_V2;
9451 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9452 return 0;
9453}
9454
9455/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9456static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9457{
9458 newsrv->pp_opts &= ~SRV_PP_V2;
9459 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9460 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9461 return 0;
9462}
9463
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009464/* parse the "no-ssl" server keyword */
9465static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9466{
9467 newsrv->use_ssl = 0;
9468 free(newsrv->ssl_ctx.ciphers);
9469 newsrv->ssl_ctx.ciphers = NULL;
9470 return 0;
9471}
9472
Olivier Houchard522eea72017-11-03 16:27:47 +01009473/* parse the "allow-0rtt" server keyword */
9474static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9475{
9476 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9477 return 0;
9478}
9479
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009480/* parse the "no-ssl-reuse" server keyword */
9481static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9482{
9483 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9484 return 0;
9485}
9486
Emeric Brunf9c5c472012-10-11 15:28:34 +02009487/* parse the "no-tls-tickets" server keyword */
9488static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9489{
9490 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9491 return 0;
9492}
David Safb76832014-05-08 23:42:08 -04009493/* parse the "send-proxy-v2-ssl" server keyword */
9494static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9495{
9496 newsrv->pp_opts |= SRV_PP_V2;
9497 newsrv->pp_opts |= SRV_PP_V2_SSL;
9498 return 0;
9499}
9500
9501/* parse the "send-proxy-v2-ssl-cn" server keyword */
9502static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9503{
9504 newsrv->pp_opts |= SRV_PP_V2;
9505 newsrv->pp_opts |= SRV_PP_V2_SSL;
9506 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9507 return 0;
9508}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009509
Willy Tarreau732eac42015-07-09 11:40:25 +02009510/* parse the "sni" server keyword */
9511static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9512{
9513#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9514 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9515 return ERR_ALERT | ERR_FATAL;
9516#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009517 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009518
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009519 arg = args[*cur_arg + 1];
9520 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009521 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9522 return ERR_ALERT | ERR_FATAL;
9523 }
9524
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009525 free(newsrv->sni_expr);
9526 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009527
Willy Tarreau732eac42015-07-09 11:40:25 +02009528 return 0;
9529#endif
9530}
9531
Willy Tarreau92faadf2012-10-10 23:04:25 +02009532/* parse the "ssl" server keyword */
9533static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9534{
9535 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009536 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9537 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009538#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009539 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9540 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9541#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009542 return 0;
9543}
9544
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009545/* parse the "ssl-reuse" server keyword */
9546static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9547{
9548 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9549 return 0;
9550}
9551
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009552/* parse the "tls-tickets" server keyword */
9553static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9554{
9555 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9556 return 0;
9557}
9558
Emeric Brunef42d922012-10-11 16:11:36 +02009559/* parse the "verify" server keyword */
9560static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9561{
9562 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009563 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009564 return ERR_ALERT | ERR_FATAL;
9565 }
9566
9567 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009568 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009569 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009570 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009571 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009572 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9573 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009574 return ERR_ALERT | ERR_FATAL;
9575 }
9576
Evan Broderbe554312013-06-27 00:05:25 -07009577 return 0;
9578}
9579
9580/* parse the "verifyhost" server keyword */
9581static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9582{
9583 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009584 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009585 return ERR_ALERT | ERR_FATAL;
9586 }
9587
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009588 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009589 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9590
Emeric Brunef42d922012-10-11 16:11:36 +02009591 return 0;
9592}
9593
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009594/* parse the "ssl-default-bind-options" keyword in global section */
9595static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9596 struct proxy *defpx, const char *file, int line,
9597 char **err) {
9598 int i = 1;
9599
9600 if (*(args[i]) == 0) {
9601 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9602 return -1;
9603 }
9604 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009605 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009606 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009607 else if (!strcmp(args[i], "prefer-client-ciphers"))
9608 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009609 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9610 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9611 i++;
9612 else {
9613 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9614 return -1;
9615 }
9616 }
9617 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009618 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9619 return -1;
9620 }
9621 i++;
9622 }
9623 return 0;
9624}
9625
9626/* parse the "ssl-default-server-options" keyword in global section */
9627static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9628 struct proxy *defpx, const char *file, int line,
9629 char **err) {
9630 int i = 1;
9631
9632 if (*(args[i]) == 0) {
9633 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9634 return -1;
9635 }
9636 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009637 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009638 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009639 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9640 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9641 i++;
9642 else {
9643 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9644 return -1;
9645 }
9646 }
9647 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009648 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9649 return -1;
9650 }
9651 i++;
9652 }
9653 return 0;
9654}
9655
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009656/* parse the "ca-base" / "crt-base" keywords in global section.
9657 * Returns <0 on alert, >0 on warning, 0 on success.
9658 */
9659static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9660 struct proxy *defpx, const char *file, int line,
9661 char **err)
9662{
9663 char **target;
9664
Willy Tarreauef934602016-12-22 23:12:01 +01009665 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009666
9667 if (too_many_args(1, args, err, NULL))
9668 return -1;
9669
9670 if (*target) {
9671 memprintf(err, "'%s' already specified.", args[0]);
9672 return -1;
9673 }
9674
9675 if (*(args[1]) == 0) {
9676 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9677 return -1;
9678 }
9679 *target = strdup(args[1]);
9680 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009681}
9682
9683/* parse the "ssl-mode-async" keyword in global section.
9684 * Returns <0 on alert, >0 on warning, 0 on success.
9685 */
9686static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9687 struct proxy *defpx, const char *file, int line,
9688 char **err)
9689{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009690#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009691 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009692 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009693 return 0;
9694#else
9695 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9696 return -1;
9697#endif
9698}
9699
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009700#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009701static int ssl_check_async_engine_count(void) {
9702 int err_code = 0;
9703
Emeric Brun3854e012017-05-17 20:42:48 +02009704 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009705 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009706 err_code = ERR_ABORT;
9707 }
9708 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009709}
9710
Grant Zhang872f9c22017-01-21 01:10:18 +00009711/* parse the "ssl-engine" keyword in global section.
9712 * Returns <0 on alert, >0 on warning, 0 on success.
9713 */
9714static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9715 struct proxy *defpx, const char *file, int line,
9716 char **err)
9717{
9718 char *algo;
9719 int ret = -1;
9720
9721 if (*(args[1]) == 0) {
9722 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9723 return ret;
9724 }
9725
9726 if (*(args[2]) == 0) {
9727 /* if no list of algorithms is given, it defaults to ALL */
9728 algo = strdup("ALL");
9729 goto add_engine;
9730 }
9731
9732 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9733 if (strcmp(args[2], "algo") != 0) {
9734 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9735 return ret;
9736 }
9737
9738 if (*(args[3]) == 0) {
9739 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9740 return ret;
9741 }
9742 algo = strdup(args[3]);
9743
9744add_engine:
9745 if (ssl_init_single_engine(args[1], algo)==0) {
9746 openssl_engines_initialized++;
9747 ret = 0;
9748 }
9749 free(algo);
9750 return ret;
9751}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009752#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009753
Willy Tarreauf22e9682016-12-21 23:23:19 +01009754/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9755 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9756 */
9757static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9758 struct proxy *defpx, const char *file, int line,
9759 char **err)
9760{
9761 char **target;
9762
Willy Tarreauef934602016-12-22 23:12:01 +01009763 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009764
9765 if (too_many_args(1, args, err, NULL))
9766 return -1;
9767
9768 if (*(args[1]) == 0) {
9769 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9770 return -1;
9771 }
9772
9773 free(*target);
9774 *target = strdup(args[1]);
9775 return 0;
9776}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009777
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009778#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009779/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9780 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9781 */
9782static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9783 struct proxy *defpx, const char *file, int line,
9784 char **err)
9785{
9786 char **target;
9787
9788 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9789
9790 if (too_many_args(1, args, err, NULL))
9791 return -1;
9792
9793 if (*(args[1]) == 0) {
9794 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9795 return -1;
9796 }
9797
9798 free(*target);
9799 *target = strdup(args[1]);
9800 return 0;
9801}
9802#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009803
Willy Tarreau9ceda382016-12-21 23:13:03 +01009804/* parse various global tune.ssl settings consisting in positive integers.
9805 * Returns <0 on alert, >0 on warning, 0 on success.
9806 */
9807static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9808 struct proxy *defpx, const char *file, int line,
9809 char **err)
9810{
9811 int *target;
9812
9813 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9814 target = &global.tune.sslcachesize;
9815 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009816 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009817 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009818 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009819 else if (strcmp(args[0], "maxsslconn") == 0)
9820 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009821 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9822 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009823 else {
9824 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9825 return -1;
9826 }
9827
9828 if (too_many_args(1, args, err, NULL))
9829 return -1;
9830
9831 if (*(args[1]) == 0) {
9832 memprintf(err, "'%s' expects an integer argument.", args[0]);
9833 return -1;
9834 }
9835
9836 *target = atoi(args[1]);
9837 if (*target < 0) {
9838 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9839 return -1;
9840 }
9841 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009842}
9843
9844static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9845 struct proxy *defpx, const char *file, int line,
9846 char **err)
9847{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009848 int ret;
9849
9850 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9851 if (ret != 0)
9852 return ret;
9853
Willy Tarreaubafbe012017-11-24 17:34:44 +01009854 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009855 memprintf(err, "'%s' is already configured.", args[0]);
9856 return -1;
9857 }
9858
Willy Tarreaubafbe012017-11-24 17:34:44 +01009859 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9860 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009861 memprintf(err, "Out of memory error.");
9862 return -1;
9863 }
9864 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009865}
9866
9867/* parse "ssl.force-private-cache".
9868 * Returns <0 on alert, >0 on warning, 0 on success.
9869 */
9870static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9871 struct proxy *defpx, const char *file, int line,
9872 char **err)
9873{
9874 if (too_many_args(0, args, err, NULL))
9875 return -1;
9876
Willy Tarreauef934602016-12-22 23:12:01 +01009877 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009878 return 0;
9879}
9880
9881/* parse "ssl.lifetime".
9882 * Returns <0 on alert, >0 on warning, 0 on success.
9883 */
9884static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9885 struct proxy *defpx, const char *file, int line,
9886 char **err)
9887{
9888 const char *res;
9889
9890 if (too_many_args(1, args, err, NULL))
9891 return -1;
9892
9893 if (*(args[1]) == 0) {
9894 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9895 return -1;
9896 }
9897
Willy Tarreauef934602016-12-22 23:12:01 +01009898 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009899 if (res == PARSE_TIME_OVER) {
9900 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9901 args[1], args[0]);
9902 return -1;
9903 }
9904 else if (res == PARSE_TIME_UNDER) {
9905 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9906 args[1], args[0]);
9907 return -1;
9908 }
9909 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009910 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9911 return -1;
9912 }
9913 return 0;
9914}
9915
9916#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009917/* parse "ssl-dh-param-file".
9918 * Returns <0 on alert, >0 on warning, 0 on success.
9919 */
9920static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9921 struct proxy *defpx, const char *file, int line,
9922 char **err)
9923{
9924 if (too_many_args(1, args, err, NULL))
9925 return -1;
9926
9927 if (*(args[1]) == 0) {
9928 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9929 return -1;
9930 }
9931
9932 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9933 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9934 return -1;
9935 }
9936 return 0;
9937}
9938
Willy Tarreau9ceda382016-12-21 23:13:03 +01009939/* parse "ssl.default-dh-param".
9940 * Returns <0 on alert, >0 on warning, 0 on success.
9941 */
9942static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9943 struct proxy *defpx, const char *file, int line,
9944 char **err)
9945{
9946 if (too_many_args(1, args, err, NULL))
9947 return -1;
9948
9949 if (*(args[1]) == 0) {
9950 memprintf(err, "'%s' expects an integer argument.", args[0]);
9951 return -1;
9952 }
9953
Willy Tarreauef934602016-12-22 23:12:01 +01009954 global_ssl.default_dh_param = atoi(args[1]);
9955 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009956 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9957 return -1;
9958 }
9959 return 0;
9960}
9961#endif
9962
William Lallemand3af48e72020-02-03 17:15:52 +01009963
9964/*
9965 * parse "ssl-load-extra-files".
9966 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
9967 */
9968static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
9969 struct proxy *defpx, const char *file, int line,
9970 char **err)
9971{
9972 int i;
9973 int gf = SSL_GF_NONE;
9974
9975 if (*(args[1]) == 0)
9976 goto err_arg;
9977
9978 for (i = 1; *args[i]; i++) {
9979
9980 if (!strcmp("bundle", args[i])) {
9981 gf |= SSL_GF_BUNDLE;
9982
9983 } else if (!strcmp("sctl", args[i])) {
9984 gf |= SSL_GF_SCTL;
9985
9986 } else if (!strcmp("ocsp", args[i])){
9987 gf |= SSL_GF_OCSP;
9988
9989 } else if (!strcmp("issuer", args[i])){
9990 gf |= SSL_GF_OCSP_ISSUER;
9991
9992 } else if (!strcmp("none", args[i])) {
9993 if (gf != SSL_GF_NONE)
9994 goto err_alone;
9995 gf = SSL_GF_NONE;
9996 i++;
9997 break;
9998
9999 } else if (!strcmp("all", args[i])) {
10000 if (gf != SSL_GF_NONE)
10001 goto err_alone;
10002 gf = SSL_GF_ALL;
10003 i++;
10004 break;
10005 } else {
10006 goto err_arg;
10007 }
10008 }
10009 /* break from loop but there are still arguments */
10010 if (*args[i])
10011 goto err_alone;
10012
10013 global_ssl.extra_files = gf;
10014
10015 return 0;
10016
10017err_alone:
10018 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10019 return -1;
10020
10021err_arg:
10022 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10023 return -1;
10024}
10025
Willy Tarreau9ceda382016-12-21 23:13:03 +010010026
William Lallemand32af2032016-10-29 18:09:35 +020010027/* This function is used with TLS ticket keys management. It permits to browse
10028 * each reference. The variable <getnext> must contain the current node,
10029 * <end> point to the root node.
10030 */
10031#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10032static inline
10033struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10034{
10035 struct tls_keys_ref *ref = getnext;
10036
10037 while (1) {
10038
10039 /* Get next list entry. */
10040 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10041
10042 /* If the entry is the last of the list, return NULL. */
10043 if (&ref->list == end)
10044 return NULL;
10045
10046 return ref;
10047 }
10048}
10049
10050static inline
10051struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10052{
10053 int id;
10054 char *error;
10055
10056 /* If the reference starts by a '#', this is numeric id. */
10057 if (reference[0] == '#') {
10058 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10059 id = strtol(reference + 1, &error, 10);
10060 if (*error != '\0')
10061 return NULL;
10062
10063 /* Perform the unique id lookup. */
10064 return tlskeys_ref_lookupid(id);
10065 }
10066
10067 /* Perform the string lookup. */
10068 return tlskeys_ref_lookup(reference);
10069}
10070#endif
10071
10072
10073#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10074
10075static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10076
10077static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10078 return cli_io_handler_tlskeys_files(appctx);
10079}
10080
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010081/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10082 * (next index to be dumped), and cli.p0 (next key reference).
10083 */
William Lallemand32af2032016-10-29 18:09:35 +020010084static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10085
10086 struct stream_interface *si = appctx->owner;
10087
10088 switch (appctx->st2) {
10089 case STAT_ST_INIT:
10090 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010091 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010092 * later and restart at the state "STAT_ST_INIT".
10093 */
10094 chunk_reset(&trash);
10095
10096 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10097 chunk_appendf(&trash, "# id secret\n");
10098 else
10099 chunk_appendf(&trash, "# id (file)\n");
10100
Willy Tarreau06d80a92017-10-19 14:32:15 +020010101 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010102 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010103 return 0;
10104 }
10105
William Lallemand32af2032016-10-29 18:09:35 +020010106 /* Now, we start the browsing of the references lists.
10107 * Note that the following call to LIST_ELEM return bad pointer. The only
10108 * available field of this pointer is <list>. It is used with the function
10109 * tlskeys_list_get_next() for retruning the first available entry
10110 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010111 if (appctx->ctx.cli.p0 == NULL) {
10112 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10113 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010114 }
10115
10116 appctx->st2 = STAT_ST_LIST;
10117 /* fall through */
10118
10119 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010120 while (appctx->ctx.cli.p0) {
10121 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010122
10123 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010124 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010125 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010126
10127 if (appctx->ctx.cli.i1 == 0)
10128 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10129
William Lallemand32af2032016-10-29 18:09:35 +020010130 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010131 int head;
10132
10133 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10134 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010135 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010136 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010137
10138 chunk_reset(t2);
10139 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010140 if (ref->key_size_bits == 128) {
10141 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10142 sizeof(struct tls_sess_key_128),
10143 t2->area, t2->size);
10144 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10145 t2->area);
10146 }
10147 else if (ref->key_size_bits == 256) {
10148 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10149 sizeof(struct tls_sess_key_256),
10150 t2->area, t2->size);
10151 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10152 t2->area);
10153 }
10154 else {
10155 /* This case should never happen */
10156 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10157 }
William Lallemand32af2032016-10-29 18:09:35 +020010158
Willy Tarreau06d80a92017-10-19 14:32:15 +020010159 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010160 /* let's try again later from this stream. We add ourselves into
10161 * this stream's users so that it can remove us upon termination.
10162 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010163 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010164 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010165 return 0;
10166 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010167 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010168 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010169 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010170 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010171 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010172 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010173 /* let's try again later from this stream. We add ourselves into
10174 * this stream's users so that it can remove us upon termination.
10175 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010176 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010177 return 0;
10178 }
10179
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010180 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010181 break;
10182
10183 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010184 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010185 }
10186
10187 appctx->st2 = STAT_ST_FIN;
10188 /* fall through */
10189
10190 default:
10191 appctx->st2 = STAT_ST_FIN;
10192 return 1;
10193 }
10194 return 0;
10195}
10196
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010197/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010198static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010199{
William Lallemand32af2032016-10-29 18:09:35 +020010200 /* no parameter, shows only file list */
10201 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010202 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010203 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010204 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010205 }
10206
10207 if (args[2][0] == '*') {
10208 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010209 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010210 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010211 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010212 if (!appctx->ctx.cli.p0)
10213 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010214 }
William Lallemand32af2032016-10-29 18:09:35 +020010215 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010216 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010217}
10218
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010219static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010220{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010221 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010222 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010223
William Lallemand32af2032016-10-29 18:09:35 +020010224 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010225 if (!*args[3] || !*args[4])
10226 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 +020010227
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010228 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010229 if (!ref)
10230 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010231
Willy Tarreau1c913e42018-08-22 05:26:57 +020010232 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010233 if (ret < 0)
10234 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010235
Willy Tarreau1c913e42018-08-22 05:26:57 +020010236 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010237 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10238 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010239
Willy Tarreau9d008692019-08-09 11:21:01 +020010240 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010241}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010242#endif
William Lallemand32af2032016-10-29 18:09:35 +020010243
William Lallemand44b35322019-10-17 16:28:40 +020010244
10245/* Type of SSL payloads that can be updated over the CLI */
10246
10247enum {
10248 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010249#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010250 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010251#endif
William Lallemand44b35322019-10-17 16:28:40 +020010252 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010253#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010254 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010255#endif
William Lallemand44b35322019-10-17 16:28:40 +020010256 CERT_TYPE_MAX,
10257};
10258
10259struct {
10260 const char *ext;
10261 int type;
10262 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10263 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010264} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010265 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +020010266#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010267 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010268#endif
10269#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010270 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010271#endif
William Lallemand44b35322019-10-17 16:28:40 +020010272 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010273 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010274};
10275
William Lallemand430413e2019-10-28 14:30:47 +010010276/* states of the CLI IO handler for 'set ssl cert' */
10277enum {
10278 SETCERT_ST_INIT = 0,
10279 SETCERT_ST_GEN,
10280 SETCERT_ST_INSERT,
10281 SETCERT_ST_FIN,
10282};
William Lallemand8f840d72019-10-23 10:53:05 +020010283
William Lallemandd4f946c2019-12-05 10:26:40 +010010284/* release function of the `show ssl cert' command */
10285static void cli_release_show_cert(struct appctx *appctx)
10286{
10287 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10288}
10289
10290/* IO handler of "show ssl cert <filename>" */
10291static int cli_io_handler_show_cert(struct appctx *appctx)
10292{
10293 struct buffer *trash = alloc_trash_chunk();
10294 struct ebmb_node *node;
10295 struct stream_interface *si = appctx->owner;
10296 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010010297
10298 if (trash == NULL)
10299 return 1;
10300
10301 if (!appctx->ctx.ssl.old_ckchs) {
10302 if (ckchs_transaction.old_ckchs) {
10303 ckchs = ckchs_transaction.old_ckchs;
10304 chunk_appendf(trash, "# transaction\n");
10305 if (!ckchs->multi) {
10306 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010307#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010308 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010010309 int n;
10310
William Lallemandd4f946c2019-12-05 10:26:40 +010010311 chunk_appendf(trash, "*%s:", ckchs->path);
10312 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10313 if (ckchs->ckch[n].cert)
10314 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10315 }
10316 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010317#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010318 }
10319 }
10320 }
10321
10322 if (!appctx->ctx.cli.p0) {
10323 chunk_appendf(trash, "# filename\n");
10324 node = ebmb_first(&ckchs_tree);
10325 } else {
10326 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10327 }
10328 while (node) {
10329 ckchs = ebmb_entry(node, struct ckch_store, node);
10330 if (!ckchs->multi) {
10331 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010332#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010333 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010010334 int n;
10335
William Lallemandd4f946c2019-12-05 10:26:40 +010010336 chunk_appendf(trash, "%s:", ckchs->path);
10337 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10338 if (ckchs->ckch[n].cert)
10339 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10340 }
10341 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010342#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010343 }
10344
10345 node = ebmb_next(node);
10346 if (ci_putchk(si_ic(si), trash) == -1) {
10347 si_rx_room_blk(si);
10348 goto yield;
10349 }
10350 }
10351
10352 appctx->ctx.cli.p0 = NULL;
10353 free_trash_chunk(trash);
10354 return 1;
10355yield:
10356
10357 free_trash_chunk(trash);
10358 appctx->ctx.cli.p0 = ckchs;
10359 return 0; /* should come back */
10360}
10361
10362/* IO handler of the details "show ssl cert <filename>" */
10363static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10364{
10365 struct stream_interface *si = appctx->owner;
10366 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10367 struct buffer *out = alloc_trash_chunk();
10368 struct buffer *tmp = alloc_trash_chunk();
10369 X509_NAME *name = NULL;
10370 int write = -1;
10371 BIO *bio = NULL;
10372
10373 if (!tmp || !out)
10374 goto end;
10375
10376 if (!ckchs->multi) {
10377 chunk_appendf(out, "Filename: ");
10378 if (ckchs == ckchs_transaction.new_ckchs)
10379 chunk_appendf(out, "*");
10380 chunk_appendf(out, "%s\n", ckchs->path);
10381 chunk_appendf(out, "Serial: ");
10382 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10383 goto end;
10384 dump_binary(out, tmp->area, tmp->data);
10385 chunk_appendf(out, "\n");
10386
10387 chunk_appendf(out, "notBefore: ");
10388 chunk_reset(tmp);
10389 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10390 goto end;
10391 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10392 goto end;
10393 write = BIO_read(bio, tmp->area, tmp->size-1);
10394 tmp->area[write] = '\0';
10395 BIO_free(bio);
10396 chunk_appendf(out, "%s\n", tmp->area);
10397
10398 chunk_appendf(out, "notAfter: ");
10399 chunk_reset(tmp);
10400 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10401 goto end;
10402 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10403 goto end;
10404 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10405 goto end;
10406 tmp->area[write] = '\0';
10407 BIO_free(bio);
10408 chunk_appendf(out, "%s\n", tmp->area);
10409
10410
10411 chunk_appendf(out, "Issuer: ");
10412 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10413 goto end;
10414 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10415 goto end;
10416 *(tmp->area + tmp->data) = '\0';
10417 chunk_appendf(out, "%s\n", tmp->area);
10418
10419 chunk_appendf(out, "Subject: ");
10420 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10421 goto end;
10422 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10423 goto end;
10424 *(tmp->area + tmp->data) = '\0';
10425 chunk_appendf(out, "%s\n", tmp->area);
10426
10427
10428#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10429 chunk_appendf(out, "Subject Alternative Name: ");
10430 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10431 goto end;
10432 *(out->area + out->data) = '\0';
10433 chunk_appendf(out, "\n");
10434#endif
10435 chunk_reset(tmp);
10436 chunk_appendf(out, "Algorithm: ");
10437 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10438 goto end;
10439 chunk_appendf(out, "%s\n", tmp->area);
10440
10441 chunk_reset(tmp);
10442 chunk_appendf(out, "SHA1 FingerPrint: ");
10443 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area,
10444 (unsigned int *)&tmp->data) == 0)
10445 goto end;
10446 dump_binary(out, tmp->area, tmp->data);
10447 chunk_appendf(out, "\n");
10448 }
10449
10450 if (ci_putchk(si_ic(si), out) == -1) {
10451 si_rx_room_blk(si);
10452 goto yield;
10453 }
10454
10455end:
10456 free_trash_chunk(tmp);
10457 free_trash_chunk(out);
10458 return 1;
10459yield:
10460 free_trash_chunk(tmp);
10461 free_trash_chunk(out);
10462 return 0; /* should come back */
10463}
10464
10465/* parsing function for 'show ssl cert [certfile]' */
10466static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10467{
10468 struct ckch_store *ckchs;
10469
10470 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10471 return cli_err(appctx, "Can't allocate memory!\n");
10472
10473 /* The operations on the CKCH architecture are locked so we can
10474 * manipulate ckch_store and ckch_inst */
10475 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10476 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10477
10478 /* check if there is a certificate to lookup */
10479 if (*args[3]) {
10480 if (*args[3] == '*') {
10481 if (!ckchs_transaction.new_ckchs)
10482 goto error;
10483
10484 ckchs = ckchs_transaction.new_ckchs;
10485
10486 if (strcmp(args[3] + 1, ckchs->path))
10487 goto error;
10488
10489 } else {
10490 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10491 goto error;
10492
10493 }
10494
10495 if (ckchs->multi)
10496 goto error;
10497
10498 appctx->ctx.cli.p0 = ckchs;
10499 /* use the IO handler that shows details */
10500 appctx->io_handler = cli_io_handler_show_cert_detail;
10501 }
10502
10503 return 0;
10504
10505error:
10506 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10507 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10508}
10509
William Lallemand430413e2019-10-28 14:30:47 +010010510/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010511static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010512{
10513 struct ckch_store *new_ckchs;
10514 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010515
William Lallemand430413e2019-10-28 14:30:47 +010010516 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010517
William Lallemand430413e2019-10-28 14:30:47 +010010518 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010519 /* 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 +010010520 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010521
William Lallemandbeea2a42019-10-30 17:45:33 +010010522 if (!new_ckchs)
10523 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010524
William Lallemandbeea2a42019-10-30 17:45:33 +010010525 /* if the allocation failed, we need to free everything from the temporary list */
10526 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10527 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010528
William Lallemandbeea2a42019-10-30 17:45:33 +010010529 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10530 if (sc0->order == 0) /* we only free if it's the first inserted */
10531 SSL_CTX_free(sc0->ctx);
10532 LIST_DEL(&sc0->by_ckch_inst);
10533 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010534 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010535 LIST_DEL(&ckchi->by_ckchs);
10536 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010537 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010538 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010539 }
10540}
10541
10542
10543/*
10544 * This function tries to create the new ckch_inst and their SNIs
10545 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010546static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010547{
10548 struct stream_interface *si = appctx->owner;
10549 int y = 0;
10550 char *err = NULL;
10551 int errcode = 0;
10552 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10553 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010554 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010555 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010556
William Lallemand33cc76f2019-10-31 11:43:45 +010010557 if (trash == NULL)
10558 goto error;
10559
William Lallemand8f840d72019-10-23 10:53:05 +020010560 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10561 goto error;
10562
William Lallemand430413e2019-10-28 14:30:47 +010010563 while (1) {
10564 switch (appctx->st2) {
10565 case SETCERT_ST_INIT:
10566 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010567 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010568 if (ci_putchk(si_ic(si), trash) == -1) {
10569 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010570 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010571 }
10572 appctx->st2 = SETCERT_ST_GEN;
10573 /* fallthrough */
10574 case SETCERT_ST_GEN:
10575 /*
10576 * This state generates the ckch instances with their
10577 * sni_ctxs and SSL_CTX.
10578 *
William Lallemand430413e2019-10-28 14:30:47 +010010579 * Since the SSL_CTX generation can be CPU consumer, we
10580 * yield every 10 instances.
10581 */
William Lallemand8f840d72019-10-23 10:53:05 +020010582
William Lallemandbeea2a42019-10-30 17:45:33 +010010583 old_ckchs = appctx->ctx.ssl.old_ckchs;
10584 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010585
William Lallemandbeea2a42019-10-30 17:45:33 +010010586 if (!new_ckchs)
10587 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010588
William Lallemandbeea2a42019-10-30 17:45:33 +010010589 /* get the next ckchi to regenerate */
10590 ckchi = appctx->ctx.ssl.next_ckchi;
10591 /* we didn't start yet, set it to the first elem */
10592 if (ckchi == NULL)
10593 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010594
William Lallemandbeea2a42019-10-30 17:45:33 +010010595 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10596 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10597 struct ckch_inst *new_inst;
William Lallemand8f840d72019-10-23 10:53:05 +020010598
William Lallemandbeea2a42019-10-30 17:45:33 +010010599 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10600 if (y >= 10) {
10601 /* save the next ckchi to compute */
10602 appctx->ctx.ssl.next_ckchi = ckchi;
10603 goto yield;
10604 }
William Lallemand8f840d72019-10-23 10:53:05 +020010605
William Lallemandbeea2a42019-10-30 17:45:33 +010010606 if (new_ckchs->multi)
10607 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10608 else
10609 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
William Lallemand8f840d72019-10-23 10:53:05 +020010610
William Lallemandbeea2a42019-10-30 17:45:33 +010010611 if (errcode & ERR_CODE)
10612 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010613
William Lallemand21724f02019-11-04 17:56:13 +010010614 /* if the previous ckchi was used as the default */
10615 if (ckchi->is_default)
10616 new_inst->is_default = 1;
10617
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010618 /* we need to initialize the SSL_CTX generated */
10619 /* TODO: the prepare_ctx function need to be reworked to be safer there */
10620 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10621 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10622 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10623 if (errcode & ERR_CODE)
10624 goto error;
10625 }
10626 }
10627
10628
William Lallemandbeea2a42019-10-30 17:45:33 +010010629 /* display one dot per new instance */
10630 chunk_appendf(trash, ".");
10631 /* link the new ckch_inst to the duplicate */
10632 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10633 y++;
10634 }
William Lallemand430413e2019-10-28 14:30:47 +010010635 appctx->st2 = SETCERT_ST_INSERT;
10636 /* fallthrough */
10637 case SETCERT_ST_INSERT:
10638 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010639
William Lallemandbeea2a42019-10-30 17:45:33 +010010640 old_ckchs = appctx->ctx.ssl.old_ckchs;
10641 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010642
William Lallemandbeea2a42019-10-30 17:45:33 +010010643 if (!new_ckchs)
10644 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010645
William Lallemand21724f02019-11-04 17:56:13 +010010646 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010647 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10648 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10649 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10650 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10651 }
William Lallemand8f840d72019-10-23 10:53:05 +020010652
William Lallemandbeea2a42019-10-30 17:45:33 +010010653 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10654 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010655
William Lallemandbeea2a42019-10-30 17:45:33 +010010656 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10657 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10658 ebmb_delete(&sc0->name);
10659 LIST_DEL(&sc0->by_ckch_inst);
10660 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010661 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010662 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10663 LIST_DEL(&ckchi->by_ckchs);
10664 free(ckchi);
10665 }
William Lallemand8f840d72019-10-23 10:53:05 +020010666
William Lallemandbeea2a42019-10-30 17:45:33 +010010667 /* Replace the old ckchs by the new one */
10668 ebmb_delete(&old_ckchs->node);
10669 ckchs_free(old_ckchs);
10670 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010671 appctx->st2 = SETCERT_ST_FIN;
10672 /* fallthrough */
10673 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010674 /* we achieved the transaction, we can set everything to NULL */
10675 free(ckchs_transaction.path);
10676 ckchs_transaction.path = NULL;
10677 ckchs_transaction.new_ckchs = NULL;
10678 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010679 goto end;
10680 }
William Lallemand8f840d72019-10-23 10:53:05 +020010681 }
William Lallemand430413e2019-10-28 14:30:47 +010010682end:
William Lallemand8f840d72019-10-23 10:53:05 +020010683
William Lallemanded442432019-11-21 16:41:07 +010010684 chunk_appendf(trash, "\n");
10685 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010686 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010687 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010688 if (ci_putchk(si_ic(si), trash) == -1)
10689 si_rx_room_blk(si);
10690 free_trash_chunk(trash);
10691 /* success: call the release function and don't come back */
10692 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010693yield:
10694 /* store the state */
10695 if (ci_putchk(si_ic(si), trash) == -1)
10696 si_rx_room_blk(si);
10697 free_trash_chunk(trash);
10698 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010699 return 0; /* should come back */
10700
10701error:
10702 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010703 if (trash) {
10704 chunk_appendf(trash, "\n%sFailed!\n", err);
10705 if (ci_putchk(si_ic(si), trash) == -1)
10706 si_rx_room_blk(si);
10707 free_trash_chunk(trash);
10708 }
William Lallemand430413e2019-10-28 14:30:47 +010010709 /* error: call the release function and don't come back */
10710 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010711}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010712
10713/*
10714 * Parsing function of 'commit ssl cert'
10715 */
10716static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10717{
10718 char *err = NULL;
10719
William Lallemand230662a2019-12-03 13:32:54 +010010720 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10721 return 1;
10722
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010723 if (!*args[3])
10724 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10725
10726 /* The operations on the CKCH architecture are locked so we can
10727 * manipulate ckch_store and ckch_inst */
10728 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10729 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10730
10731 if (!ckchs_transaction.path) {
10732 memprintf(&err, "No ongoing transaction! !\n");
10733 goto error;
10734 }
10735
10736 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10737 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10738 goto error;
10739 }
10740
10741 /* init the appctx structure */
10742 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010743 appctx->ctx.ssl.next_ckchi = NULL;
10744 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10745 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10746
10747 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10748 return 0;
10749
10750error:
10751
10752 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10753 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10754
10755 return cli_dynerr(appctx, err);
10756}
10757
10758
William Lallemand8f840d72019-10-23 10:53:05 +020010759/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010760 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010761 */
William Lallemand150bfa82019-09-19 17:12:49 +020010762static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10763{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010764 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010765 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010766 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010767 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010768 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010769 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010770 char *end;
10771 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010772 struct cert_key_and_chain *ckch;
10773 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010774
William Lallemand230662a2019-12-03 13:32:54 +010010775 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10776 return 1;
10777
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010778 if ((buf = alloc_trash_chunk()) == NULL)
10779 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010780
10781 if (!*args[3] || !payload)
10782 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10783
10784 /* The operations on the CKCH architecture are locked so we can
10785 * manipulate ckch_store and ckch_inst */
10786 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10787 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10788
William Lallemand8f840d72019-10-23 10:53:05 +020010789 if (!chunk_strcpy(buf, args[3])) {
10790 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10791 errcode |= ERR_ALERT | ERR_FATAL;
10792 goto end;
10793 }
10794
William Lallemand44b35322019-10-17 16:28:40 +020010795 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010796 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010797 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010798 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10799 *end = '\0';
10800 type = cert_exts[i].type;
10801 break;
10802 }
10803 }
10804
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010805 appctx->ctx.ssl.old_ckchs = NULL;
10806 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010807
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010808 /* if there is an ongoing transaction */
10809 if (ckchs_transaction.path) {
10810 /* 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 +020010811#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010812 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010813 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010814 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010815
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010816 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010817 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010818 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010819 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010820 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10821 bundle = j; /* keep the type of certificate so we insert it at the right place */
10822 *end = '\0'; /* it's a bundle let's end the string*/
10823 break;
10824 }
William Lallemand150bfa82019-09-19 17:12:49 +020010825 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010826 if (bundle < 0) {
10827 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);
10828 errcode |= ERR_ALERT | ERR_FATAL;
10829 goto end;
10830 }
10831 }
10832#endif
10833
10834 /* if there is an ongoing transaction, check if this is the same file */
10835 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10836 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10837 errcode |= ERR_ALERT | ERR_FATAL;
10838 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010839 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010840
10841 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10842
10843 } else {
10844 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10845
10846 /* lookup for the certificate in the tree:
10847 * check if this is used as a bundle AND as a unique certificate */
10848 for (i = 0; i < 2; i++) {
10849
10850 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10851 /* only the bundle name is in the tree and you should
10852 * never update a bundle name, only a filename */
10853 if (bundle < 0 && find_ckchs[i]->multi) {
10854 /* we tried to look for a non-bundle and we found a bundle */
10855 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10856 err ? err : "", args[3], args[3]);
10857 errcode |= ERR_ALERT | ERR_FATAL;
10858 goto end;
10859 }
William Lallemand3246d942019-11-04 14:02:11 +010010860 /* If we want a bundle but this is not a bundle
10861 * example: When you try to update <file>.rsa, but
10862 * <file> is a regular file */
10863 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10864 find_ckchs[i] = NULL;
10865 break;
10866 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010867 }
10868#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10869 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010870 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010871 int j;
10872
10873 /* check if it was used in a bundle by removing the
10874 * .dsa/.rsa/.ecdsa at the end of the filename */
10875 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010876 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010877 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10878 bundle = j; /* keep the type of certificate so we insert it at the right place */
10879 *end = '\0'; /* it's a bundle let's end the string*/
10880 break;
10881 }
10882 }
William Lallemand37031b82019-11-04 13:38:53 +010010883 if (bundle < 0) /* we didn't find a bundle extension */
10884 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010885 }
William Lallemand963b2e72019-10-14 11:38:36 +020010886#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010887 /* bundles are not supported here, so we don't need to lookup again */
10888 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010889#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010890 }
10891
10892 if (find_ckchs[0] && find_ckchs[1]) {
10893 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",
10894 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10895 errcode |= ERR_ALERT | ERR_FATAL;
10896 goto end;
10897 }
10898
10899 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010900 }
10901
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010902 if (!appctx->ctx.ssl.old_ckchs) {
10903 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010904 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010905 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010906 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010907 }
10908
William Lallemand8a7fdf02019-11-04 10:59:32 +010010909 if (!appctx->ctx.ssl.path) {
10910 /* this is a new transaction, set the path of the transaction */
10911 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10912 if (!appctx->ctx.ssl.path) {
10913 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10914 errcode |= ERR_ALERT | ERR_FATAL;
10915 goto end;
10916 }
10917 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010918
10919 old_ckchs = appctx->ctx.ssl.old_ckchs;
10920
10921 /* TODO: handle filters */
10922 if (old_ckchs->filters) {
10923 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10924 err ? err : "");
10925 errcode |= ERR_ALERT | ERR_FATAL;
10926 goto end;
10927 }
10928
10929 /* duplicate the ckch store */
10930 new_ckchs = ckchs_dup(old_ckchs);
10931 if (!new_ckchs) {
10932 memprintf(&err, "%sCannot allocate memory!\n",
10933 err ? err : "");
10934 errcode |= ERR_ALERT | ERR_FATAL;
10935 goto end;
10936 }
10937
10938 if (!new_ckchs->multi)
10939 ckch = new_ckchs->ckch;
10940 else
10941 ckch = &new_ckchs->ckch[bundle];
10942
10943 /* appply the change on the duplicate */
10944 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10945 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10946 errcode |= ERR_ALERT | ERR_FATAL;
10947 goto end;
10948 }
10949
10950 appctx->ctx.ssl.new_ckchs = new_ckchs;
10951
10952 /* we succeed, we can save the ckchs in the transaction */
10953
10954 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010955 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010956 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10957 ckchs_transaction.path = appctx->ctx.ssl.path;
10958 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10959 } else {
10960 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10961
10962 }
10963
10964 /* free the previous ckchs if there was a transaction */
10965 ckchs_free(ckchs_transaction.new_ckchs);
10966
10967 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10968
10969
William Lallemand8f840d72019-10-23 10:53:05 +020010970 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010971
William Lallemand8f840d72019-10-23 10:53:05 +020010972end:
10973 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010974
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010975 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010976
10977 ckchs_free(appctx->ctx.ssl.new_ckchs);
10978 appctx->ctx.ssl.new_ckchs = NULL;
10979
10980 appctx->ctx.ssl.old_ckchs = NULL;
10981
10982 free(appctx->ctx.ssl.path);
10983 appctx->ctx.ssl.path = NULL;
10984
10985 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010986 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010987 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010988
10989 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10990 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010991 }
William Lallemand8f840d72019-10-23 10:53:05 +020010992 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010993}
10994
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010995/* parsing function of 'abort ssl cert' */
10996static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
10997{
10998 char *err = NULL;
10999
William Lallemand230662a2019-12-03 13:32:54 +010011000 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11001 return 1;
11002
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011003 if (!*args[3])
11004 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
11005
11006 /* The operations on the CKCH architecture are locked so we can
11007 * manipulate ckch_store and ckch_inst */
11008 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11009 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
11010
11011 if (!ckchs_transaction.path) {
11012 memprintf(&err, "No ongoing transaction!\n");
11013 goto error;
11014 }
11015
11016 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
11017 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
11018 goto error;
11019 }
11020
11021 /* Only free the ckchs there, because the SNI and instances were not generated yet */
11022 ckchs_free(ckchs_transaction.new_ckchs);
11023 ckchs_transaction.new_ckchs = NULL;
11024 ckchs_free(ckchs_transaction.old_ckchs);
11025 ckchs_transaction.old_ckchs = NULL;
11026 free(ckchs_transaction.path);
11027 ckchs_transaction.path = NULL;
11028
11029 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11030
11031 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
11032 return cli_dynmsg(appctx, LOG_NOTICE, err);
11033
11034error:
11035 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11036
11037 return cli_dynerr(appctx, err);
11038}
11039
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020011040static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020011041{
11042#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
11043 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020011044 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020011045
11046 if (!payload)
11047 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020011048
11049 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020011050 if (!*payload)
11051 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020011052
11053 /* remove \r and \n from the payload */
11054 for (i = 0, j = 0; payload[i]; i++) {
11055 if (payload[i] == '\r' || payload[i] == '\n')
11056 continue;
11057 payload[j++] = payload[i];
11058 }
11059 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020011060
Willy Tarreau1c913e42018-08-22 05:26:57 +020011061 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020011062 if (ret < 0)
11063 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011064
Willy Tarreau1c913e42018-08-22 05:26:57 +020011065 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020011066 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020011067 if (err)
11068 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
11069 else
11070 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011071 }
Willy Tarreau9d008692019-08-09 11:21:01 +020011072
11073 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020011074#else
Willy Tarreau9d008692019-08-09 11:21:01 +020011075 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 +020011076#endif
11077
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011078}
11079
Willy Tarreau86a394e2019-05-09 14:15:32 +020011080#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011081static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
11082{
11083 switch (arg->type) {
11084 case ARGT_STR:
11085 smp->data.type = SMP_T_STR;
11086 smp->data.u.str = arg->data.str;
11087 return 1;
11088 case ARGT_VAR:
11089 if (!vars_get_by_desc(&arg->data.var, smp))
11090 return 0;
11091 if (!sample_casts[smp->data.type][SMP_T_STR])
11092 return 0;
11093 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
11094 return 0;
11095 return 1;
11096 default:
11097 return 0;
11098 }
11099}
11100
11101static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
11102 const char *file, int line, char **err)
11103{
11104 switch(args[0].data.sint) {
11105 case 128:
11106 case 192:
11107 case 256:
11108 break;
11109 default:
11110 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11111 return 0;
11112 }
11113 /* Try to decode a variable. */
11114 vars_check_arg(&args[1], NULL);
11115 vars_check_arg(&args[2], NULL);
11116 vars_check_arg(&args[3], NULL);
11117 return 1;
11118}
11119
11120/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11121static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11122{
11123 struct sample nonce, key, aead_tag;
11124 struct buffer *smp_trash, *smp_trash_alloc;
11125 EVP_CIPHER_CTX *ctx;
11126 int dec_size, ret;
11127
11128 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11129 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11130 return 0;
11131
11132 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11133 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11134 return 0;
11135
11136 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11137 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11138 return 0;
11139
11140 smp_trash = get_trash_chunk();
11141 smp_trash_alloc = alloc_trash_chunk();
11142 if (!smp_trash_alloc)
11143 return 0;
11144
11145 ctx = EVP_CIPHER_CTX_new();
11146
11147 if (!ctx)
11148 goto err;
11149
11150 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11151 if (dec_size < 0)
11152 goto err;
11153 smp_trash->data = dec_size;
11154
11155 /* Set cipher type and mode */
11156 switch(arg_p[0].data.sint) {
11157 case 128:
11158 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11159 break;
11160 case 192:
11161 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11162 break;
11163 case 256:
11164 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11165 break;
11166 }
11167
11168 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11169
11170 /* Initialise IV */
11171 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11172 goto err;
11173
11174 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11175 if (dec_size < 0)
11176 goto err;
11177 smp_trash->data = dec_size;
11178
11179 /* Initialise key */
11180 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11181 goto err;
11182
11183 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11184 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11185 goto err;
11186
11187 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11188 if (dec_size < 0)
11189 goto err;
11190 smp_trash_alloc->data = dec_size;
11191 dec_size = smp_trash->data;
11192
11193 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11194 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11195
11196 if (ret <= 0)
11197 goto err;
11198
11199 smp->data.u.str.data = dec_size + smp_trash->data;
11200 smp->data.u.str.area = smp_trash->area;
11201 smp->data.type = SMP_T_BIN;
11202 smp->flags &= ~SMP_F_CONST;
11203 free_trash_chunk(smp_trash_alloc);
11204 return 1;
11205
11206err:
11207 free_trash_chunk(smp_trash_alloc);
11208 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011209}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011210# endif
William Lallemand32af2032016-10-29 18:09:35 +020011211
Elliot Otchet71f82972020-01-15 08:12:14 -050011212/* Argument validation functions */
11213
11214/* This function is used to validate the arguments passed to any "x_dn" ssl
11215 * keywords. These keywords support specifying a third parameter that must be
11216 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11217 */
11218int val_dnfmt(struct arg *arg, char **err_msg)
11219{
11220 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11221 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11222 return 0;
11223 }
11224 return 1;
11225}
11226
William Lallemand32af2032016-10-29 18:09:35 +020011227/* register cli keywords */
11228static struct cli_kw_list cli_kws = {{ },{
11229#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11230 { { "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 +020011231 { { "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 +020011232#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011233 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011234 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11235 { { "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 +010011236 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
William Lallemandd4f946c2019-12-05 10:26:40 +010011237 { { "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 Lallemand32af2032016-10-29 18:09:35 +020011238 { { NULL }, NULL, NULL, NULL }
11239}};
11240
Willy Tarreau0108d902018-11-25 19:14:37 +010011241INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011242
Willy Tarreau7875d092012-09-10 08:20:03 +020011243/* Note: must not be declared <const> as its list will be overwritten.
11244 * Please take care of keeping this list alphabetically sorted.
11245 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011246static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011247 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011248 { "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 +010011249#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011250 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011251#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011252 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011253#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11254 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11255#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011256 { "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 +020011257 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011258 { "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 +020011259 { "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 +020011260#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011261 { "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 -040011262#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011263#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011264 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11265 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011266 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11267#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011268 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11269 { "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 +010011270 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011271 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011272 { "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 +020011273 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11274 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11275 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11276 { "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 -050011277 { "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 +020011278 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11279 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011280 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011281 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11282 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011283 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011284 { "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 +020011285 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11286 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11287 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11288 { "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 -050011289 { "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 +020011290 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011291 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011292 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011293 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011294 { "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 +010011295 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011296 { "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 +020011297 { "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 +010011298 { "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 +020011299 { "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 +010011300#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011301 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011302#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011303#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011304 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011305#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011306 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011307#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011308 { "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 -040011309#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011310 { "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 +020011311#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011312 { "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 -040011313#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011314#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011315 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11316 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011317 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11318#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011319#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011320 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011321#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011322 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11323 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11324 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11325 { "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 +020011326 { NULL, NULL, 0, 0, 0 },
11327}};
11328
Willy Tarreau0108d902018-11-25 19:14:37 +010011329INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11330
Willy Tarreau7875d092012-09-10 08:20:03 +020011331/* Note: must not be declared <const> as its list will be overwritten.
11332 * Please take care of keeping this list alphabetically sorted.
11333 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011334static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011335 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11336 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011337 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011338}};
11339
Willy Tarreau0108d902018-11-25 19:14:37 +010011340INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11341
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011342/* Note: must not be declared <const> as its list will be overwritten.
11343 * Please take care of keeping this list alphabetically sorted, doing so helps
11344 * all code contributors.
11345 * Optional keywords are also declared with a NULL ->parse() function so that
11346 * the config parser can report an appropriate error when a known keyword was
11347 * not enabled.
11348 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011349static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011350 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011351 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11352 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11353 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011354#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011355 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11356#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011357 { "crl-file", ssl_bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +010011358 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011359 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011360 { "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 +010011361 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011362 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11363 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011364 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11365 { NULL, NULL, 0 },
11366};
11367
Willy Tarreau0108d902018-11-25 19:14:37 +010011368/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11369
Willy Tarreau51fb7652012-09-18 18:24:39 +020011370static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011371 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011372 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11373 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11374 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11375 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11376 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11377 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011378#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011379 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11380#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011381 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11382 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11383 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11384 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11385 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11386 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11387 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11388 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11389 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11390 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011391 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011392 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011393 { "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 +020011394 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11395 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11396 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11397 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011398 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011399 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11400 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011401 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11402 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011403 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11404 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11405 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11406 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11407 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011408 { NULL, NULL, 0 },
11409}};
Emeric Brun46591952012-05-18 15:47:34 +020011410
Willy Tarreau0108d902018-11-25 19:14:37 +010011411INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11412
Willy Tarreau92faadf2012-10-10 23:04:25 +020011413/* Note: must not be declared <const> as its list will be overwritten.
11414 * Please take care of keeping this list alphabetically sorted, doing so helps
11415 * all code contributors.
11416 * Optional keywords are also declared with a NULL ->parse() function so that
11417 * the config parser can report an appropriate error when a known keyword was
11418 * not enabled.
11419 */
11420static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011421 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011422 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011423 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011424 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011425 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011426 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11427 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011428#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011429 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11430#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011431 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11432 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11433 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11434 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11435 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11436 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11437 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11438 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11439 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11440 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11441 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11442 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11443 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11444 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11445 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11446 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11447 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11448 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011449 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011450 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11451 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11452 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11453 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11454 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11455 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11456 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11457 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11458 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11459 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011460 { NULL, NULL, 0, 0 },
11461}};
11462
Willy Tarreau0108d902018-11-25 19:14:37 +010011463INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11464
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011465static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011466 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11467 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011468 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011469 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11470 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011471#ifndef OPENSSL_NO_DH
11472 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11473#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011474 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011475#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011476 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011477#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011478 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11479#ifndef OPENSSL_NO_DH
11480 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11481#endif
11482 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11483 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11484 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11485 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011486 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011487 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11488 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011489#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011490 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11491 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11492#endif
William Lallemand3af48e72020-02-03 17:15:52 +010011493 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011494 { 0, NULL, NULL },
11495}};
11496
Willy Tarreau0108d902018-11-25 19:14:37 +010011497INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11498
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011499/* Note: must not be declared <const> as its list will be overwritten */
11500static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011501#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011502 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11503#endif
11504 { NULL, NULL, 0, 0, 0 },
11505}};
11506
11507INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11508
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011509/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011510static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011511 .snd_buf = ssl_sock_from_buf,
11512 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011513 .subscribe = ssl_subscribe,
11514 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011515 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011516 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011517 .rcv_pipe = NULL,
11518 .snd_pipe = NULL,
11519 .shutr = NULL,
11520 .shutw = ssl_sock_shutw,
11521 .close = ssl_sock_close,
11522 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011523 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011524 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011525 .prepare_srv = ssl_sock_prepare_srv_ctx,
11526 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011527 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011528 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011529};
11530
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011531enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11532 struct session *sess, struct stream *s, int flags)
11533{
11534 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011535 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011536
11537 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011538 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011539
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011540 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011541 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011542 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011543 s->req.flags |= CF_READ_NULL;
11544 return ACT_RET_YIELD;
11545 }
11546 }
11547 return (ACT_RET_CONT);
11548}
11549
11550static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11551{
11552 rule->action_ptr = ssl_action_wait_for_hs;
11553
11554 return ACT_RET_PRS_OK;
11555}
11556
11557static struct action_kw_list http_req_actions = {ILH, {
11558 { "wait-for-handshake", ssl_parse_wait_for_hs },
11559 { /* END */ }
11560}};
11561
Willy Tarreau0108d902018-11-25 19:14:37 +010011562INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11563
Willy Tarreau5db847a2019-05-09 14:13:35 +020011564#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011565
11566static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11567{
11568 if (ptr) {
11569 chunk_destroy(ptr);
11570 free(ptr);
11571 }
11572}
11573
11574#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011575static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11576{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011577 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011578}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011579
Emeric Brun46591952012-05-18 15:47:34 +020011580__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011581static void __ssl_sock_init(void)
11582{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011583#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011584 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011585 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011586#endif
Emeric Brun46591952012-05-18 15:47:34 +020011587
Willy Tarreauef934602016-12-22 23:12:01 +010011588 if (global_ssl.listen_default_ciphers)
11589 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11590 if (global_ssl.connect_default_ciphers)
11591 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011592#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011593 if (global_ssl.listen_default_ciphersuites)
11594 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11595 if (global_ssl.connect_default_ciphersuites)
11596 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11597#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011598
Willy Tarreau13e14102016-12-22 20:25:26 +010011599 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011600#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011601 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011602#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011603#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011604 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011605 n = sk_SSL_COMP_num(cm);
11606 while (n--) {
11607 (void) sk_SSL_COMP_pop(cm);
11608 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011609#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011610
Willy Tarreau5db847a2019-05-09 14:13:35 +020011611#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011612 ssl_locking_init();
11613#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011614#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011615 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11616#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011617 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011618 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 +020011619#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011620 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011621 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011622#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011623#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11624 hap_register_post_check(tlskeys_finalize_config);
11625#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011626
11627 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11628 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11629
11630#ifndef OPENSSL_NO_DH
11631 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11632 hap_register_post_deinit(ssl_free_dh);
11633#endif
11634#ifndef OPENSSL_NO_ENGINE
11635 hap_register_post_deinit(ssl_free_engines);
11636#endif
11637 /* Load SSL string for the verbose & debug mode. */
11638 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011639 ha_meth = BIO_meth_new(0x666, "ha methods");
11640 BIO_meth_set_write(ha_meth, ha_ssl_write);
11641 BIO_meth_set_read(ha_meth, ha_ssl_read);
11642 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11643 BIO_meth_set_create(ha_meth, ha_ssl_new);
11644 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11645 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11646 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011647
11648 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011649}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011650
Willy Tarreau80713382018-11-26 10:19:54 +010011651/* Compute and register the version string */
11652static void ssl_register_build_options()
11653{
11654 char *ptr = NULL;
11655 int i;
11656
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011657 memprintf(&ptr, "Built with OpenSSL version : "
11658#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011659 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011660#else /* OPENSSL_IS_BORINGSSL */
11661 OPENSSL_VERSION_TEXT
11662 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011663 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011664 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011665#endif
11666 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011667#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011668 "no (library version too old)"
11669#elif defined(OPENSSL_NO_TLSEXT)
11670 "no (disabled via OPENSSL_NO_TLSEXT)"
11671#else
11672 "yes"
11673#endif
11674 "", ptr);
11675
11676 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11677#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11678 "yes"
11679#else
11680#ifdef OPENSSL_NO_TLSEXT
11681 "no (because of OPENSSL_NO_TLSEXT)"
11682#else
11683 "no (version might be too old, 0.9.8f min needed)"
11684#endif
11685#endif
11686 "", ptr);
11687
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011688 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11689 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11690 if (methodVersions[i].option)
11691 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011692
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011693 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011694}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011695
Willy Tarreau80713382018-11-26 10:19:54 +010011696INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011697
Emeric Brun46591952012-05-18 15:47:34 +020011698
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011699#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011700void ssl_free_engines(void) {
11701 struct ssl_engine_list *wl, *wlb;
11702 /* free up engine list */
11703 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11704 ENGINE_finish(wl->e);
11705 ENGINE_free(wl->e);
11706 LIST_DEL(&wl->list);
11707 free(wl);
11708 }
11709}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011710#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011711
Remi Gacogned3a23c32015-05-28 16:39:47 +020011712#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011713void ssl_free_dh(void) {
11714 if (local_dh_1024) {
11715 DH_free(local_dh_1024);
11716 local_dh_1024 = NULL;
11717 }
11718 if (local_dh_2048) {
11719 DH_free(local_dh_2048);
11720 local_dh_2048 = NULL;
11721 }
11722 if (local_dh_4096) {
11723 DH_free(local_dh_4096);
11724 local_dh_4096 = NULL;
11725 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011726 if (global_dh) {
11727 DH_free(global_dh);
11728 global_dh = NULL;
11729 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011730}
11731#endif
11732
11733__attribute__((destructor))
11734static void __ssl_sock_deinit(void)
11735{
11736#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011737 if (ssl_ctx_lru_tree) {
11738 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011739 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011740 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011741#endif
11742
Willy Tarreau5db847a2019-05-09 14:13:35 +020011743#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011744 ERR_remove_state(0);
11745 ERR_free_strings();
11746
11747 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011748#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011749
Willy Tarreau5db847a2019-05-09 14:13:35 +020011750#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011751 CRYPTO_cleanup_all_ex_data();
11752#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011753 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011754}
11755
11756
Emeric Brun46591952012-05-18 15:47:34 +020011757/*
11758 * Local variables:
11759 * c-indent-level: 8
11760 * c-basic-offset: 8
11761 * End:
11762 */