blob: 651afa3a7af4bab6f9d3440e3638f7590f90a895 [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>
Olivier Houchardccaa7de2017-10-02 11:51:03 +020080#include <proto/proto_http.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
127/* ssl_methods versions */
128enum {
129 CONF_TLSV_NONE = 0,
130 CONF_TLSV_MIN = 1,
131 CONF_SSLV3 = 1,
132 CONF_TLSV10 = 2,
133 CONF_TLSV11 = 3,
134 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200135 CONF_TLSV13 = 5,
136 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137};
138
Emeric Brun850efd52014-01-29 12:24:34 +0100139/* server and bind verify method, it uses a global value as default */
140enum {
141 SSL_SOCK_VERIFY_DEFAULT = 0,
142 SSL_SOCK_VERIFY_REQUIRED = 1,
143 SSL_SOCK_VERIFY_OPTIONAL = 2,
144 SSL_SOCK_VERIFY_NONE = 3,
145};
146
William Lallemand3f85c9a2017-10-09 16:30:50 +0200147
Willy Tarreau71b734c2014-01-28 15:19:44 +0100148int sslconns = 0;
149int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100150static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100151int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200152
Willy Tarreauef934602016-12-22 23:12:01 +0100153static struct {
154 char *crt_base; /* base directory path for certificates */
155 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000156 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100157
158 char *listen_default_ciphers;
159 char *connect_default_ciphers;
Willy Tarreau5db847a2019-05-09 14:13:35 +0200160#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200161 char *listen_default_ciphersuites;
162 char *connect_default_ciphersuites;
163#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100164 int listen_default_ssloptions;
165 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200166 struct tls_version_filter listen_default_sslmethods;
167 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100168
169 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
170 unsigned int life_time; /* SSL session lifetime in seconds */
171 unsigned int max_record; /* SSL max record size */
172 unsigned int default_dh_param; /* SSL maximum DH parameter size */
173 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100174 int capture_cipherlist; /* Size of the cipherlist buffer. */
Willy Tarreauef934602016-12-22 23:12:01 +0100175} global_ssl = {
176#ifdef LISTEN_DEFAULT_CIPHERS
177 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
178#endif
179#ifdef CONNECT_DEFAULT_CIPHERS
180 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
181#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +0200182#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200183#ifdef LISTEN_DEFAULT_CIPHERSUITES
184 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
185#endif
186#ifdef CONNECT_DEFAULT_CIPHERSUITES
187 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
188#endif
189#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100190 .listen_default_ssloptions = BC_SSL_O_NONE,
191 .connect_default_ssloptions = SRV_SSL_O_NONE,
192
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200193 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
194 .listen_default_sslmethods.min = CONF_TLSV_NONE,
195 .listen_default_sslmethods.max = CONF_TLSV_NONE,
196 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
197 .connect_default_sslmethods.min = CONF_TLSV_NONE,
198 .connect_default_sslmethods.max = CONF_TLSV_NONE,
199
Willy Tarreauef934602016-12-22 23:12:01 +0100200#ifdef DEFAULT_SSL_MAX_RECORD
201 .max_record = DEFAULT_SSL_MAX_RECORD,
202#endif
203 .default_dh_param = SSL_DEFAULT_DH_PARAM,
204 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100205 .capture_cipherlist = 0,
Willy Tarreauef934602016-12-22 23:12:01 +0100206};
207
Olivier Houcharda8955d52019-04-07 22:00:38 +0200208static BIO_METHOD *ha_meth;
209
Olivier Houchard66ab4982019-02-26 18:37:15 +0100210struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200211 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100212 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200213 BIO *bio;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100214 struct xprt_ops *xprt;
215 void *xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100216 int xprt_st; /* transport layer state, initialized to zero */
217 int tmp_early_data; /* 1st byte of early data, if any */
218 int sent_early_data; /* Amount of early data we sent so far */
219
Olivier Houchard66ab4982019-02-26 18:37:15 +0100220};
221
222DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
223
Olivier Houcharda8955d52019-04-07 22:00:38 +0200224/* Methods to implement OpenSSL BIO */
225static int ha_ssl_write(BIO *h, const char *buf, int num)
226{
227 struct buffer tmpbuf;
228 struct ssl_sock_ctx *ctx;
229 int ret;
230
231 ctx = BIO_get_data(h);
232 tmpbuf.size = num;
233 tmpbuf.area = (void *)(uintptr_t)buf;
234 tmpbuf.data = num;
235 tmpbuf.head = 0;
236 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200237 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200238 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200239 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200240 } else if (ret == 0)
241 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200242 return ret;
243}
244
245static int ha_ssl_gets(BIO *h, char *buf, int size)
246{
247
248 return 0;
249}
250
251static int ha_ssl_puts(BIO *h, const char *str)
252{
253
254 return ha_ssl_write(h, str, strlen(str));
255}
256
257static int ha_ssl_read(BIO *h, char *buf, int size)
258{
259 struct buffer tmpbuf;
260 struct ssl_sock_ctx *ctx;
261 int ret;
262
263 ctx = BIO_get_data(h);
264 tmpbuf.size = size;
265 tmpbuf.area = buf;
266 tmpbuf.data = 0;
267 tmpbuf.head = 0;
268 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200269 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200270 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200271 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200272 } else if (ret == 0)
273 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200274
275 return ret;
276}
277
278static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
279{
280 int ret = 0;
281 switch (cmd) {
282 case BIO_CTRL_DUP:
283 case BIO_CTRL_FLUSH:
284 ret = 1;
285 break;
286 }
287 return ret;
288}
289
290static int ha_ssl_new(BIO *h)
291{
292 BIO_set_init(h, 1);
293 BIO_set_data(h, NULL);
294 BIO_clear_flags(h, ~0);
295 return 1;
296}
297
298static int ha_ssl_free(BIO *data)
299{
300
301 return 1;
302}
303
304
Willy Tarreau5db847a2019-05-09 14:13:35 +0200305#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100306
Emeric Brun821bb9b2017-06-15 16:37:39 +0200307static HA_RWLOCK_T *ssl_rwlocks;
308
309
310unsigned long ssl_id_function(void)
311{
312 return (unsigned long)tid;
313}
314
315void ssl_locking_function(int mode, int n, const char * file, int line)
316{
317 if (mode & CRYPTO_LOCK) {
318 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100319 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200320 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100321 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200322 }
323 else {
324 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100325 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200326 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200328 }
329}
330
331static int ssl_locking_init(void)
332{
333 int i;
334
335 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
336 if (!ssl_rwlocks)
337 return -1;
338
339 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100340 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200341
342 CRYPTO_set_id_callback(ssl_id_function);
343 CRYPTO_set_locking_callback(ssl_locking_function);
344
345 return 0;
346}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100347
Emeric Brun821bb9b2017-06-15 16:37:39 +0200348#endif
349
350
351
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100352/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100353struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100354 unsigned long long int xxh64;
355 unsigned char ciphersuite_len;
356 char ciphersuite[0];
357};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100358struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100359static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200360static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100361
Emmanuel Hocdet96b78342017-10-31 15:46:07 +0100362static int ssl_pkey_info_index = -1;
363
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200364#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
365struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
366#endif
367
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200368#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000369static unsigned int openssl_engines_initialized;
370struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
371struct ssl_engine_list {
372 struct list list;
373 ENGINE *e;
374};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200375#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000376
Remi Gacogne8de54152014-07-15 11:36:40 +0200377#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200378static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200379static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200380static DH *local_dh_1024 = NULL;
381static DH *local_dh_2048 = NULL;
382static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100383static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200384#endif /* OPENSSL_NO_DH */
385
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100386#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200387/* X509V3 Extensions that will be added on generated certificates */
388#define X509V3_EXT_SIZE 5
389static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
390 "basicConstraints",
391 "nsComment",
392 "subjectKeyIdentifier",
393 "authorityKeyIdentifier",
394 "keyUsage",
395};
396static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
397 "CA:FALSE",
398 "\"OpenSSL Generated Certificate\"",
399 "hash",
400 "keyid,issuer:always",
401 "nonRepudiation,digitalSignature,keyEncipherment"
402};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200403/* LRU cache to store generated certificate */
404static struct lru64_head *ssl_ctx_lru_tree = NULL;
405static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200406static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100407__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200408
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200409#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
410
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100411static struct ssl_bind_kw ssl_bind_kws[];
412
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200413#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500414/* The order here matters for picking a default context,
415 * keep the most common keytype at the bottom of the list
416 */
417const char *SSL_SOCK_KEYTYPE_NAMES[] = {
418 "dsa",
419 "ecdsa",
420 "rsa"
421};
422#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100423#else
424#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500425#endif
426
William Lallemandc3cd35f2017-11-28 11:04:43 +0100427static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100428static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
429
430#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
431
432#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
433 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
434
435#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
436 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200437
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100438/*
439 * This function gives the detail of the SSL error. It is used only
440 * if the debug mode and the verbose mode are activated. It dump all
441 * the SSL error until the stack was empty.
442 */
443static forceinline void ssl_sock_dump_errors(struct connection *conn)
444{
445 unsigned long ret;
446
447 if (unlikely(global.mode & MODE_DEBUG)) {
448 while(1) {
449 ret = ERR_get_error();
450 if (ret == 0)
451 return;
452 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200453 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100454 ERR_func_error_string(ret), ERR_reason_error_string(ret));
455 }
456 }
457}
458
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200459#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
yanbzhube2774d2015-12-10 15:07:30 -0500460/*
461 * struct alignment works here such that the key.key is the same as key_data
462 * Do not change the placement of key_data
463 */
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200464struct certificate_ocsp {
465 struct ebmb_node key;
466 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
Willy Tarreau83061a82018-07-13 11:56:34 +0200467 struct buffer response;
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200468 long expire;
469};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200470
yanbzhube2774d2015-12-10 15:07:30 -0500471struct ocsp_cbk_arg {
472 int is_single;
473 int single_kt;
474 union {
475 struct certificate_ocsp *s_ocsp;
476 /*
477 * m_ocsp will have multiple entries dependent on key type
478 * Entry 0 - DSA
479 * Entry 1 - ECDSA
480 * Entry 2 - RSA
481 */
482 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
483 };
484};
485
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200486#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000487static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
488{
489 int err_code = ERR_ABORT;
490 ENGINE *engine;
491 struct ssl_engine_list *el;
492
493 /* grab the structural reference to the engine */
494 engine = ENGINE_by_id(engine_id);
495 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100496 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000497 goto fail_get;
498 }
499
500 if (!ENGINE_init(engine)) {
501 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100502 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000503 goto fail_init;
504 }
505
506 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100507 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000508 goto fail_set_method;
509 }
510
511 el = calloc(1, sizeof(*el));
512 el->e = engine;
513 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100514 nb_engines++;
515 if (global_ssl.async)
516 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000517 return 0;
518
519fail_set_method:
520 /* release the functional reference from ENGINE_init() */
521 ENGINE_finish(engine);
522
523fail_init:
524 /* release the structural reference from ENGINE_by_id() */
525 ENGINE_free(engine);
526
527fail_get:
528 return err_code;
529}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200530#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000531
Willy Tarreau5db847a2019-05-09 14:13:35 +0200532#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200533/*
534 * openssl async fd handler
535 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200536void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000537{
538 struct connection *conn = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000539
Emeric Brun3854e012017-05-17 20:42:48 +0200540 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000541 * to poll this fd until it is requested
542 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000543 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000544 fd_cant_recv(fd);
545
546 /* crypto engine is available, let's notify the associated
547 * connection that it can pursue its processing.
548 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000549 __conn_sock_want_recv(conn);
550 __conn_sock_want_send(conn);
551 conn_update_sock_polling(conn);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000552}
553
Emeric Brun3854e012017-05-17 20:42:48 +0200554/*
555 * openssl async delayed SSL_free handler
556 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200557void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000558{
559 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200560 OSSL_ASYNC_FD all_fd[32];
561 size_t num_all_fds = 0;
562 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000563
Emeric Brun3854e012017-05-17 20:42:48 +0200564 /* We suppose that the async job for a same SSL *
565 * are serialized. So if we are awake it is
566 * because the running job has just finished
567 * and we can remove all async fds safely
568 */
569 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
570 if (num_all_fds > 32) {
571 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
572 return;
573 }
574
575 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
576 for (i=0 ; i < num_all_fds ; i++)
577 fd_remove(all_fd[i]);
578
579 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000580 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100581 _HA_ATOMIC_SUB(&sslconns, 1);
582 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000583}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000584/*
Emeric Brun3854e012017-05-17 20:42:48 +0200585 * function used to manage a returned SSL_ERROR_WANT_ASYNC
586 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000587 */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200588static inline void ssl_async_process_fds(struct connection *conn, SSL *ssl)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000589{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100590 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200591 OSSL_ASYNC_FD del_fd[32];
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000592 size_t num_add_fds = 0;
593 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200594 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000595
596 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
597 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200598 if (num_add_fds > 32 || num_del_fds > 32) {
599 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 +0000600 return;
601 }
602
Emeric Brun3854e012017-05-17 20:42:48 +0200603 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000604
Emeric Brun3854e012017-05-17 20:42:48 +0200605 /* We remove unused fds from the fdtab */
606 for (i=0 ; i < num_del_fds ; i++)
607 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000608
Emeric Brun3854e012017-05-17 20:42:48 +0200609 /* We add new fds to the fdtab */
610 for (i=0 ; i < num_add_fds ; i++) {
Willy Tarreaua9786b62018-01-25 07:22:13 +0100611 fd_insert(add_fd[i], conn, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000612 }
613
Emeric Brun3854e012017-05-17 20:42:48 +0200614 num_add_fds = 0;
615 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
616 if (num_add_fds > 32) {
617 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
618 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000619 }
Emeric Brun3854e012017-05-17 20:42:48 +0200620
621 /* We activate the polling for all known async fds */
622 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000623 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200624 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000625 /* To ensure that the fd cache won't be used
626 * We'll prefer to catch a real RD event
627 * because handling an EAGAIN on this fd will
628 * result in a context switch and also
629 * some engines uses a fd in blocking mode.
630 */
631 fd_cant_recv(add_fd[i]);
632 }
Emeric Brun3854e012017-05-17 20:42:48 +0200633
634 /* We must also prevent the conn_handler
635 * to be called until a read event was
636 * polled on an async fd
637 */
638 __conn_sock_stop_both(conn);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000639}
640#endif
641
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200642/*
643 * This function returns the number of seconds elapsed
644 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
645 * date presented un ASN1_GENERALIZEDTIME.
646 *
647 * In parsing error case, it returns -1.
648 */
649static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
650{
651 long epoch;
652 char *p, *end;
653 const unsigned short month_offset[12] = {
654 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
655 };
656 int year, month;
657
658 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
659
660 p = (char *)d->data;
661 end = p + d->length;
662
663 if (end - p < 4) return -1;
664 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
665 p += 4;
666 if (end - p < 2) return -1;
667 month = 10 * (p[0] - '0') + p[1] - '0';
668 if (month < 1 || month > 12) return -1;
669 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
670 We consider leap years and the current month (<marsh or not) */
671 epoch = ( ((year - 1970) * 365)
672 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
673 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
674 + month_offset[month-1]
675 ) * 24 * 60 * 60;
676 p += 2;
677 if (end - p < 2) return -1;
678 /* Add the number of seconds of completed days of current month */
679 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
680 p += 2;
681 if (end - p < 2) return -1;
682 /* Add the completed hours of the current day */
683 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
684 p += 2;
685 if (end - p < 2) return -1;
686 /* Add the completed minutes of the current hour */
687 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
688 p += 2;
689 if (p == end) return -1;
690 /* Test if there is available seconds */
691 if (p[0] < '0' || p[0] > '9')
692 goto nosec;
693 if (end - p < 2) return -1;
694 /* Add the seconds of the current minute */
695 epoch += 10 * (p[0] - '0') + p[1] - '0';
696 p += 2;
697 if (p == end) return -1;
698 /* Ignore seconds float part if present */
699 if (p[0] == '.') {
700 do {
701 if (++p == end) return -1;
702 } while (p[0] >= '0' && p[0] <= '9');
703 }
704
705nosec:
706 if (p[0] == 'Z') {
707 if (end - p != 1) return -1;
708 return epoch;
709 }
710 else if (p[0] == '+') {
711 if (end - p != 5) return -1;
712 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700713 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 +0200714 }
715 else if (p[0] == '-') {
716 if (end - p != 5) return -1;
717 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700718 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 +0200719 }
720
721 return -1;
722}
723
Emeric Brun1d3865b2014-06-20 15:37:32 +0200724static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200725
726/* This function starts to check if the OCSP response (in DER format) contained
727 * in chunk 'ocsp_response' is valid (else exits on error).
728 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
729 * contained in the OCSP Response and exits on error if no match.
730 * If it's a valid OCSP Response:
731 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
732 * pointed by 'ocsp'.
733 * If 'ocsp' is NULL, the function looks up into the OCSP response's
734 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
735 * from the response) and exits on error if not found. Finally, If an OCSP response is
736 * already present in the container, it will be overwritten.
737 *
738 * Note: OCSP response containing more than one OCSP Single response is not
739 * considered valid.
740 *
741 * Returns 0 on success, 1 in error case.
742 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200743static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
744 struct certificate_ocsp *ocsp,
745 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200746{
747 OCSP_RESPONSE *resp;
748 OCSP_BASICRESP *bs = NULL;
749 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200750 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200752 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200753 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200754 int reason;
755 int ret = 1;
756
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
758 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200759 if (!resp) {
760 memprintf(err, "Unable to parse OCSP response");
761 goto out;
762 }
763
764 rc = OCSP_response_status(resp);
765 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
766 memprintf(err, "OCSP response status not successful");
767 goto out;
768 }
769
770 bs = OCSP_response_get1_basic(resp);
771 if (!bs) {
772 memprintf(err, "Failed to get basic response from OCSP Response");
773 goto out;
774 }
775
776 count_sr = OCSP_resp_count(bs);
777 if (count_sr > 1) {
778 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
779 goto out;
780 }
781
782 sr = OCSP_resp_get0(bs, 0);
783 if (!sr) {
784 memprintf(err, "Failed to get OCSP single response");
785 goto out;
786 }
787
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200788 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
789
Emeric Brun4147b2e2014-06-16 18:36:30 +0200790 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200791 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200792 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200793 goto out;
794 }
795
Emeric Brun13a6b482014-06-20 15:44:34 +0200796 if (!nextupd) {
797 memprintf(err, "OCSP single response: missing nextupdate");
798 goto out;
799 }
800
Emeric Brunc8b27b62014-06-19 14:16:17 +0200801 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200802 if (!rc) {
803 memprintf(err, "OCSP single response: no longer valid.");
804 goto out;
805 }
806
807 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200808 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200809 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
810 goto out;
811 }
812 }
813
814 if (!ocsp) {
815 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
816 unsigned char *p;
817
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200818 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200819 if (!rc) {
820 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
821 goto out;
822 }
823
824 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
825 memprintf(err, "OCSP single response: Certificate ID too long");
826 goto out;
827 }
828
829 p = key;
830 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200831 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200832 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
833 if (!ocsp) {
834 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
835 goto out;
836 }
837 }
838
839 /* According to comments on "chunk_dup", the
840 previous chunk buffer will be freed */
841 if (!chunk_dup(&ocsp->response, ocsp_response)) {
842 memprintf(err, "OCSP response: Memory allocation error");
843 goto out;
844 }
845
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200846 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
847
Emeric Brun4147b2e2014-06-16 18:36:30 +0200848 ret = 0;
849out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100850 ERR_clear_error();
851
Emeric Brun4147b2e2014-06-16 18:36:30 +0200852 if (bs)
853 OCSP_BASICRESP_free(bs);
854
855 if (resp)
856 OCSP_RESPONSE_free(resp);
857
858 return ret;
859}
860/*
861 * External function use to update the OCSP response in the OCSP response's
862 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
863 * to update in DER format.
864 *
865 * Returns 0 on success, 1 in error case.
866 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200867int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200868{
869 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
870}
871
872/*
873 * This function load the OCSP Resonse in DER format contained in file at
874 * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response'
875 *
876 * Returns 0 on success, 1 in error case.
877 */
878static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err)
879{
880 int fd = -1;
881 int r = 0;
882 int ret = 1;
883
884 fd = open(ocsp_path, O_RDONLY);
885 if (fd == -1) {
886 memprintf(err, "Error opening OCSP response file");
887 goto end;
888 }
889
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200890 trash.data = 0;
891 while (trash.data < trash.size) {
892 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200893 if (r < 0) {
894 if (errno == EINTR)
895 continue;
896
897 memprintf(err, "Error reading OCSP response from file");
898 goto end;
899 }
900 else if (r == 0) {
901 break;
902 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200903 trash.data += r;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200904 }
905
906 close(fd);
907 fd = -1;
908
909 ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err);
910end:
911 if (fd != -1)
912 close(fd);
913
914 return ret;
915}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100916#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +0200917
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100918#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
919static 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)
920{
Christopher Faulet16f45c82018-02-16 11:23:49 +0100921 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +0100922 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100923 struct connection *conn;
924 int head;
925 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100926 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100927
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200928 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +0200929 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100930 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
931
932 keys = ref->tlskeys;
933 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100934
935 if (enc) {
936 memcpy(key_name, keys[head].name, 16);
937
938 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +0100939 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100940
Emeric Brun9e754772019-01-10 17:51:55 +0100941 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100942
Emeric Brun9e754772019-01-10 17:51:55 +0100943 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
944 goto end;
945
Willy Tarreau9356dac2019-05-10 09:22:53 +0200946 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100947 ret = 1;
948 }
949 else if (ref->key_size_bits == 256 ) {
950
951 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
952 goto end;
953
Willy Tarreau9356dac2019-05-10 09:22:53 +0200954 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100955 ret = 1;
956 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100957 } else {
958 for (i = 0; i < TLS_TICKETS_NO; i++) {
959 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
960 goto found;
961 }
Christopher Faulet16f45c82018-02-16 11:23:49 +0100962 ret = 0;
963 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100964
Christopher Faulet16f45c82018-02-16 11:23:49 +0100965 found:
Emeric Brun9e754772019-01-10 17:51:55 +0100966 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200967 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 +0100968 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
969 goto end;
970 /* 2 for key renewal, 1 if current key is still valid */
971 ret = i ? 2 : 1;
972 }
973 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200974 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 +0100975 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
976 goto end;
977 /* 2 for key renewal, 1 if current key is still valid */
978 ret = i ? 2 : 1;
979 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100980 }
Emeric Brun9e754772019-01-10 17:51:55 +0100981
Christopher Faulet16f45c82018-02-16 11:23:49 +0100982 end:
983 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
984 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200985}
986
987struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
988{
989 struct tls_keys_ref *ref;
990
991 list_for_each_entry(ref, &tlskeys_reference, list)
992 if (ref->filename && strcmp(filename, ref->filename) == 0)
993 return ref;
994 return NULL;
995}
996
997struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
998{
999 struct tls_keys_ref *ref;
1000
1001 list_for_each_entry(ref, &tlskeys_reference, list)
1002 if (ref->unique_id == unique_id)
1003 return ref;
1004 return NULL;
1005}
1006
Emeric Brun9e754772019-01-10 17:51:55 +01001007/* Update the key into ref: if keysize doesnt
1008 * match existing ones, this function returns -1
1009 * else it returns 0 on success.
1010 */
1011int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001012 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001013{
Emeric Brun9e754772019-01-10 17:51:55 +01001014 if (ref->key_size_bits == 128) {
1015 if (tlskey->data != sizeof(struct tls_sess_key_128))
1016 return -1;
1017 }
1018 else if (ref->key_size_bits == 256) {
1019 if (tlskey->data != sizeof(struct tls_sess_key_256))
1020 return -1;
1021 }
1022 else
1023 return -1;
1024
Christopher Faulet16f45c82018-02-16 11:23:49 +01001025 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1027 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001028 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1029 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001030
1031 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001032}
1033
Willy Tarreau83061a82018-07-13 11:56:34 +02001034int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001035{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001036 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1037
1038 if(!ref) {
1039 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1040 return 1;
1041 }
Emeric Brun9e754772019-01-10 17:51:55 +01001042 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1043 memprintf(err, "Invalid key size");
1044 return 1;
1045 }
1046
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001047 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001048}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001049
1050/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001051 * automatic ids. It's called just after the basic checks. It returns
1052 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001053 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001054static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001055{
1056 int i = 0;
1057 struct tls_keys_ref *ref, *ref2, *ref3;
1058 struct list tkr = LIST_HEAD_INIT(tkr);
1059
1060 list_for_each_entry(ref, &tlskeys_reference, list) {
1061 if (ref->unique_id == -1) {
1062 /* Look for the first free id. */
1063 while (1) {
1064 list_for_each_entry(ref2, &tlskeys_reference, list) {
1065 if (ref2->unique_id == i) {
1066 i++;
1067 break;
1068 }
1069 }
1070 if (&ref2->list == &tlskeys_reference)
1071 break;
1072 }
1073
1074 /* Uses the unique id and increment it for the next entry. */
1075 ref->unique_id = i;
1076 i++;
1077 }
1078 }
1079
1080 /* This sort the reference list by id. */
1081 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1082 LIST_DEL(&ref->list);
1083 list_for_each_entry(ref3, &tkr, list) {
1084 if (ref->unique_id < ref3->unique_id) {
1085 LIST_ADDQ(&ref3->list, &ref->list);
1086 break;
1087 }
1088 }
1089 if (&ref3->list == &tkr)
1090 LIST_ADDQ(&tkr, &ref->list);
1091 }
1092
1093 /* swap root */
1094 LIST_ADD(&tkr, &tlskeys_reference);
1095 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001096 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001097}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001098#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1099
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001100#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001101int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1102{
1103 switch (evp_keytype) {
1104 case EVP_PKEY_RSA:
1105 return 2;
1106 case EVP_PKEY_DSA:
1107 return 0;
1108 case EVP_PKEY_EC:
1109 return 1;
1110 }
1111
1112 return -1;
1113}
1114
Emeric Brun4147b2e2014-06-16 18:36:30 +02001115/*
1116 * Callback used to set OCSP status extension content in server hello.
1117 */
1118int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1119{
yanbzhube2774d2015-12-10 15:07:30 -05001120 struct certificate_ocsp *ocsp;
1121 struct ocsp_cbk_arg *ocsp_arg;
1122 char *ssl_buf;
1123 EVP_PKEY *ssl_pkey;
1124 int key_type;
1125 int index;
1126
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001127 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001128
1129 ssl_pkey = SSL_get_privatekey(ssl);
1130 if (!ssl_pkey)
1131 return SSL_TLSEXT_ERR_NOACK;
1132
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001133 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001134
1135 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1136 ocsp = ocsp_arg->s_ocsp;
1137 else {
1138 /* For multiple certs per context, we have to find the correct OCSP response based on
1139 * the certificate type
1140 */
1141 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1142
1143 if (index < 0)
1144 return SSL_TLSEXT_ERR_NOACK;
1145
1146 ocsp = ocsp_arg->m_ocsp[index];
1147
1148 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001149
1150 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001151 !ocsp->response.area ||
1152 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001153 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001154 return SSL_TLSEXT_ERR_NOACK;
1155
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001156 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001157 if (!ssl_buf)
1158 return SSL_TLSEXT_ERR_NOACK;
1159
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001160 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1161 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001162
1163 return SSL_TLSEXT_ERR_OK;
1164}
1165
1166/*
1167 * This function enables the handling of OCSP status extension on 'ctx' if a
1168 * file name 'cert_path' suffixed using ".ocsp" is present.
1169 * To enable OCSP status extension, the issuer's certificate is mandatory.
1170 * It should be present in the certificate's extra chain builded from file
1171 * 'cert_path'. If not found, the issuer certificate is loaded from a file
1172 * named 'cert_path' suffixed using '.issuer'.
1173 *
1174 * In addition, ".ocsp" file content is loaded as a DER format of an OCSP
1175 * response. If file is empty or content is not a valid OCSP response,
1176 * OCSP status extension is enabled but OCSP response is ignored (a warning
1177 * is displayed).
1178 *
1179 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001180 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001181 */
1182static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path)
1183{
1184
1185 BIO *in = NULL;
1186 X509 *x, *xi = NULL, *issuer = NULL;
1187 STACK_OF(X509) *chain = NULL;
1188 OCSP_CERTID *cid = NULL;
1189 SSL *ssl;
1190 char ocsp_path[MAXPATHLEN+1];
1191 int i, ret = -1;
1192 struct stat st;
1193 struct certificate_ocsp *ocsp = NULL, *iocsp;
1194 char *warn = NULL;
1195 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001196 pem_password_cb *passwd_cb;
1197 void *passwd_cb_userdata;
1198 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001199
1200 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1201
1202 if (stat(ocsp_path, &st))
1203 return 1;
1204
1205 ssl = SSL_new(ctx);
1206 if (!ssl)
1207 goto out;
1208
1209 x = SSL_get_certificate(ssl);
1210 if (!x)
1211 goto out;
1212
1213 /* Try to lookup for issuer in certificate extra chain */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001214 SSL_CTX_get_extra_chain_certs(ctx, &chain);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001215 for (i = 0; i < sk_X509_num(chain); i++) {
1216 issuer = sk_X509_value(chain, i);
1217 if (X509_check_issued(issuer, x) == X509_V_OK)
1218 break;
1219 else
1220 issuer = NULL;
1221 }
1222
1223 /* If not found try to load issuer from a suffixed file */
1224 if (!issuer) {
1225 char issuer_path[MAXPATHLEN+1];
1226
1227 in = BIO_new(BIO_s_file());
1228 if (!in)
1229 goto out;
1230
1231 snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path);
1232 if (BIO_read_filename(in, issuer_path) <= 0)
1233 goto out;
1234
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001235 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
1236 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
1237
1238 xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001239 if (!xi)
1240 goto out;
1241
1242 if (X509_check_issued(xi, x) != X509_V_OK)
1243 goto out;
1244
1245 issuer = xi;
1246 }
1247
1248 cid = OCSP_cert_to_id(0, x, issuer);
1249 if (!cid)
1250 goto out;
1251
1252 i = i2d_OCSP_CERTID(cid, NULL);
1253 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1254 goto out;
1255
Vincent Bernat02779b62016-04-03 13:48:43 +02001256 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001257 if (!ocsp)
1258 goto out;
1259
1260 p = ocsp->key_data;
1261 i2d_OCSP_CERTID(cid, &p);
1262
1263 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1264 if (iocsp == ocsp)
1265 ocsp = NULL;
1266
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001267#ifndef SSL_CTX_get_tlsext_status_cb
1268# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1269 *cb = (void (*) (void))ctx->tlsext_status_cb;
1270#endif
1271 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1272
1273 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001274 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001275 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001276
1277 cb_arg->is_single = 1;
1278 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001279
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001280 pkey = X509_get_pubkey(x);
1281 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1282 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001283
1284 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1285 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1286 } else {
1287 /*
1288 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1289 * Update that cb_arg with the new cert's staple
1290 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001291 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001292 struct certificate_ocsp *tmp_ocsp;
1293 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001294 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001295 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001296
1297#ifdef SSL_CTX_get_tlsext_status_arg
1298 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1299#else
1300 cb_arg = ctx->tlsext_status_arg;
1301#endif
yanbzhube2774d2015-12-10 15:07:30 -05001302
1303 /*
1304 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1305 * the order of operations below matter, take care when changing it
1306 */
1307 tmp_ocsp = cb_arg->s_ocsp;
1308 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1309 cb_arg->s_ocsp = NULL;
1310 cb_arg->m_ocsp[index] = tmp_ocsp;
1311 cb_arg->is_single = 0;
1312 cb_arg->single_kt = 0;
1313
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001314 pkey = X509_get_pubkey(x);
1315 key_type = EVP_PKEY_base_id(pkey);
1316 EVP_PKEY_free(pkey);
1317
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001318 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001319 if (index >= 0 && !cb_arg->m_ocsp[index])
1320 cb_arg->m_ocsp[index] = iocsp;
1321
1322 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001323
1324 ret = 0;
1325
1326 warn = NULL;
1327 if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) {
1328 memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001330 }
1331
1332out:
1333 if (ssl)
1334 SSL_free(ssl);
1335
1336 if (in)
1337 BIO_free(in);
1338
1339 if (xi)
1340 X509_free(xi);
1341
1342 if (cid)
1343 OCSP_CERTID_free(cid);
1344
1345 if (ocsp)
1346 free(ocsp);
1347
1348 if (warn)
1349 free(warn);
1350
1351
1352 return ret;
1353}
1354
1355#endif
1356
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001357#ifdef OPENSSL_IS_BORINGSSL
1358static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path)
1359{
1360 char ocsp_path[MAXPATHLEN+1];
1361 struct stat st;
1362 int fd = -1, r = 0;
1363
1364 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1365 if (stat(ocsp_path, &st))
1366 return 0;
1367
1368 fd = open(ocsp_path, O_RDONLY);
1369 if (fd == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001370 ha_warning("Error opening OCSP response file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001371 return -1;
1372 }
1373
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001374 trash.data = 0;
1375 while (trash.data < trash.size) {
1376 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001377 if (r < 0) {
1378 if (errno == EINTR)
1379 continue;
Christopher Faulet767a84b2017-11-24 16:50:31 +01001380 ha_warning("Error reading OCSP response from file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001381 close(fd);
1382 return -1;
1383 }
1384 else if (r == 0) {
1385 break;
1386 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001387 trash.data += r;
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001388 }
1389 close(fd);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001390 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area,
1391 trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001392}
1393#endif
1394
Willy Tarreau5db847a2019-05-09 14:13:35 +02001395#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001396
1397#define CT_EXTENSION_TYPE 18
1398
1399static int sctl_ex_index = -1;
1400
1401/*
1402 * Try to parse Signed Certificate Timestamp List structure. This function
1403 * makes only basic test if the data seems like SCTL. No signature validation
1404 * is performed.
1405 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001406static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001407{
1408 int ret = 1;
1409 int len, pos, sct_len;
1410 unsigned char *data;
1411
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001412 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001413 goto out;
1414
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001415 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001416 len = (data[0] << 8) | data[1];
1417
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001418 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001419 goto out;
1420
1421 data = data + 2;
1422 pos = 0;
1423 while (pos < len) {
1424 if (len - pos < 2)
1425 goto out;
1426
1427 sct_len = (data[pos] << 8) | data[pos + 1];
1428 if (pos + sct_len + 2 > len)
1429 goto out;
1430
1431 pos += sct_len + 2;
1432 }
1433
1434 ret = 0;
1435
1436out:
1437 return ret;
1438}
1439
Willy Tarreau83061a82018-07-13 11:56:34 +02001440static int ssl_sock_load_sctl_from_file(const char *sctl_path,
1441 struct buffer **sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001442{
1443 int fd = -1;
1444 int r = 0;
1445 int ret = 1;
1446
1447 *sctl = NULL;
1448
1449 fd = open(sctl_path, O_RDONLY);
1450 if (fd == -1)
1451 goto end;
1452
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001453 trash.data = 0;
1454 while (trash.data < trash.size) {
1455 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001456 if (r < 0) {
1457 if (errno == EINTR)
1458 continue;
1459
1460 goto end;
1461 }
1462 else if (r == 0) {
1463 break;
1464 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001465 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001466 }
1467
1468 ret = ssl_sock_parse_sctl(&trash);
1469 if (ret)
1470 goto end;
1471
Vincent Bernat02779b62016-04-03 13:48:43 +02001472 *sctl = calloc(1, sizeof(**sctl));
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001473 if (!chunk_dup(*sctl, &trash)) {
1474 free(*sctl);
1475 *sctl = NULL;
1476 goto end;
1477 }
1478
1479end:
1480 if (fd != -1)
1481 close(fd);
1482
1483 return ret;
1484}
1485
1486int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1487{
Willy Tarreau83061a82018-07-13 11:56:34 +02001488 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001489
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 *out = (unsigned char *) sctl->area;
1491 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001492
1493 return 1;
1494}
1495
1496int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1497{
1498 return 1;
1499}
1500
1501static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path)
1502{
1503 char sctl_path[MAXPATHLEN+1];
1504 int ret = -1;
1505 struct stat st;
Willy Tarreau83061a82018-07-13 11:56:34 +02001506 struct buffer *sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001507
1508 snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path);
1509
1510 if (stat(sctl_path, &st))
1511 return 1;
1512
1513 if (ssl_sock_load_sctl_from_file(sctl_path, &sctl))
1514 goto out;
1515
1516 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) {
1517 free(sctl);
1518 goto out;
1519 }
1520
1521 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1522
1523 ret = 0;
1524
1525out:
1526 return ret;
1527}
1528
1529#endif
1530
Emeric Brune1f38db2012-09-03 20:36:47 +02001531void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1532{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001533 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001534 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001535 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001536 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001537
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001538#ifndef SSL_OP_NO_RENEGOTIATION
1539 /* Please note that BoringSSL defines this macro to zero so don't
1540 * change this to #if and do not assign a default value to this macro!
1541 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001542 if (where & SSL_CB_HANDSHAKE_START) {
1543 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001544 if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001545 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001546 conn->err_code = CO_ER_SSL_RENEG;
1547 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001548 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001549#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001550
1551 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001552 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001553 /* Long certificate chains optimz
1554 If write and read bios are differents, we
1555 consider that the buffering was activated,
1556 so we rise the output buffer size from 4k
1557 to 16k */
1558 write_bio = SSL_get_wbio(ssl);
1559 if (write_bio != SSL_get_rbio(ssl)) {
1560 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001561 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001562 }
1563 }
1564 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001565}
1566
Emeric Brune64aef12012-09-21 13:15:06 +02001567/* Callback is called for each certificate of the chain during a verify
1568 ok is set to 1 if preverify detect no error on current certificate.
1569 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001570int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001571{
1572 SSL *ssl;
1573 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001574 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001575 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001576
1577 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001578 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001579
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001580 ctx = conn->xprt_ctx;
1581
1582 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001583
Emeric Brun81c00f02012-09-21 14:31:21 +02001584 if (ok) /* no errors */
1585 return ok;
1586
1587 depth = X509_STORE_CTX_get_error_depth(x_store);
1588 err = X509_STORE_CTX_get_error(x_store);
1589
1590 /* check if CA error needs to be ignored */
1591 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001592 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1593 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1594 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001595 }
1596
Willy Tarreau07d94e42018-09-20 10:57:52 +02001597 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001598 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001599 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001600 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001601 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001602
Willy Tarreau20879a02012-12-03 16:32:10 +01001603 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001604 return 0;
1605 }
1606
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001607 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1608 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001609
Emeric Brun81c00f02012-09-21 14:31:21 +02001610 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001611 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001612 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001613 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001614 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001615 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001616
Willy Tarreau20879a02012-12-03 16:32:10 +01001617 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001618 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001619}
1620
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001621static inline
1622void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001623 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001624{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001625 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001626 unsigned char *msg;
1627 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001628 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001629
1630 /* This function is called for "from client" and "to server"
1631 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001632 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001633 */
1634
1635 /* "write_p" is set to 0 is the bytes are received messages,
1636 * otherwise it is set to 1.
1637 */
1638 if (write_p != 0)
1639 return;
1640
1641 /* content_type contains the type of message received or sent
1642 * according with the SSL/TLS protocol spec. This message is
1643 * encoded with one byte. The value 256 (two bytes) is used
1644 * for designing the SSL/TLS record layer. According with the
1645 * rfc6101, the expected message (other than 256) are:
1646 * - change_cipher_spec(20)
1647 * - alert(21)
1648 * - handshake(22)
1649 * - application_data(23)
1650 * - (255)
1651 * We are interessed by the handshake and specially the client
1652 * hello.
1653 */
1654 if (content_type != 22)
1655 return;
1656
1657 /* The message length is at least 4 bytes, containing the
1658 * message type and the message length.
1659 */
1660 if (len < 4)
1661 return;
1662
1663 /* First byte of the handshake message id the type of
1664 * message. The konwn types are:
1665 * - hello_request(0)
1666 * - client_hello(1)
1667 * - server_hello(2)
1668 * - certificate(11)
1669 * - server_key_exchange (12)
1670 * - certificate_request(13)
1671 * - server_hello_done(14)
1672 * We are interested by the client hello.
1673 */
1674 msg = (unsigned char *)buf;
1675 if (msg[0] != 1)
1676 return;
1677
1678 /* Next three bytes are the length of the message. The total length
1679 * must be this decoded length + 4. If the length given as argument
1680 * is not the same, we abort the protocol dissector.
1681 */
1682 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1683 if (len < rec_len + 4)
1684 return;
1685 msg += 4;
1686 end = msg + rec_len;
1687 if (end < msg)
1688 return;
1689
1690 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1691 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001692 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1693 */
1694 msg += 1 + 1 + 4 + 28;
1695 if (msg > end)
1696 return;
1697
1698 /* Next, is session id:
1699 * if present, we have to jump by length + 1 for the size information
1700 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001701 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001702 if (msg[0] > 0)
1703 msg += msg[0];
1704 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001705 if (msg > end)
1706 return;
1707
1708 /* Next two bytes are the ciphersuite length. */
1709 if (msg + 2 > end)
1710 return;
1711 rec_len = (msg[0] << 8) + msg[1];
1712 msg += 2;
1713 if (msg + rec_len > end || msg + rec_len < msg)
1714 return;
1715
Willy Tarreaubafbe012017-11-24 17:34:44 +01001716 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001717 if (!capture)
1718 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001719 /* Compute the xxh64 of the ciphersuite. */
1720 capture->xxh64 = XXH64(msg, rec_len, 0);
1721
1722 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001723 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1724 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001725 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001726
1727 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001728}
1729
Emeric Brun29f037d2014-04-25 19:05:36 +02001730/* Callback is called for ssl protocol analyse */
1731void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1732{
Emeric Brun29f037d2014-04-25 19:05:36 +02001733#ifdef TLS1_RT_HEARTBEAT
1734 /* test heartbeat received (write_p is set to 0
1735 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001736 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001737 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001738 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001739 const unsigned char *p = buf;
1740 unsigned int payload;
1741
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001742 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001743
1744 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1745 if (*p != TLS1_HB_REQUEST)
1746 return;
1747
Willy Tarreauaeed6722014-04-25 23:59:58 +02001748 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001749 goto kill_it;
1750
1751 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001752 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001753 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001754 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001755 /* We have a clear heartbleed attack (CVE-2014-0160), the
1756 * advertised payload is larger than the advertised packet
1757 * length, so we have garbage in the buffer between the
1758 * payload and the end of the buffer (p+len). We can't know
1759 * if the SSL stack is patched, and we don't know if we can
1760 * safely wipe out the area between p+3+len and payload.
1761 * So instead, we prevent the response from being sent by
1762 * setting the max_send_fragment to 0 and we report an SSL
1763 * error, which will kill this connection. It will be reported
1764 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001765 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1766 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001767 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001768 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1769 return;
1770 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001771#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001772 if (global_ssl.capture_cipherlist > 0)
1773 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001774}
1775
Bernard Spil13c53f82018-02-15 13:34:58 +01001776#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001777static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1778 const unsigned char *in, unsigned int inlen,
1779 void *arg)
1780{
1781 struct server *srv = arg;
1782
1783 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1784 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1785 return SSL_TLSEXT_ERR_OK;
1786 return SSL_TLSEXT_ERR_NOACK;
1787}
1788#endif
1789
1790#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001791/* This callback is used so that the server advertises the list of
1792 * negociable protocols for NPN.
1793 */
1794static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1795 unsigned int *len, void *arg)
1796{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001797 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001798
1799 *data = (const unsigned char *)conf->npn_str;
1800 *len = conf->npn_len;
1801 return SSL_TLSEXT_ERR_OK;
1802}
1803#endif
1804
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001805#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001806/* This callback is used so that the server advertises the list of
1807 * negociable protocols for ALPN.
1808 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001809static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1810 unsigned char *outlen,
1811 const unsigned char *server,
1812 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001813{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001814 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001815
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001816 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1817 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1818 return SSL_TLSEXT_ERR_NOACK;
1819 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001820 return SSL_TLSEXT_ERR_OK;
1821}
1822#endif
1823
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001824#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001825#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001826
Christopher Faulet30548802015-06-11 13:39:32 +02001827/* Create a X509 certificate with the specified servername and serial. This
1828 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001829static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001830ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001831{
Christopher Faulet7969a332015-10-09 11:15:03 +02001832 X509 *cacert = bind_conf->ca_sign_cert;
1833 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001834 SSL_CTX *ssl_ctx = NULL;
1835 X509 *newcrt = NULL;
1836 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001837 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001838 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001839 X509_NAME *name;
1840 const EVP_MD *digest;
1841 X509V3_CTX ctx;
1842 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001843 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001844
Christopher Faulet48a83322017-07-28 16:56:09 +02001845 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001846#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001847 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1848#else
1849 tmp_ssl = SSL_new(bind_conf->default_ctx);
1850 if (tmp_ssl)
1851 pkey = SSL_get_privatekey(tmp_ssl);
1852#endif
1853 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001854 goto mkcert_error;
1855
1856 /* Create the certificate */
1857 if (!(newcrt = X509_new()))
1858 goto mkcert_error;
1859
1860 /* Set version number for the certificate (X509v3) and the serial
1861 * number */
1862 if (X509_set_version(newcrt, 2L) != 1)
1863 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001864 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001865
1866 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001867 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1868 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001869 goto mkcert_error;
1870
1871 /* set public key in the certificate */
1872 if (X509_set_pubkey(newcrt, pkey) != 1)
1873 goto mkcert_error;
1874
1875 /* Set issuer name from the CA */
1876 if (!(name = X509_get_subject_name(cacert)))
1877 goto mkcert_error;
1878 if (X509_set_issuer_name(newcrt, name) != 1)
1879 goto mkcert_error;
1880
1881 /* Set the subject name using the same, but the CN */
1882 name = X509_NAME_dup(name);
1883 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1884 (const unsigned char *)servername,
1885 -1, -1, 0) != 1) {
1886 X509_NAME_free(name);
1887 goto mkcert_error;
1888 }
1889 if (X509_set_subject_name(newcrt, name) != 1) {
1890 X509_NAME_free(name);
1891 goto mkcert_error;
1892 }
1893 X509_NAME_free(name);
1894
1895 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001896 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001897 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1898 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1899 X509_EXTENSION *ext;
1900
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001901 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001902 goto mkcert_error;
1903 if (!X509_add_ext(newcrt, ext, -1)) {
1904 X509_EXTENSION_free(ext);
1905 goto mkcert_error;
1906 }
1907 X509_EXTENSION_free(ext);
1908 }
1909
1910 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001911
1912 key_type = EVP_PKEY_base_id(capkey);
1913
1914 if (key_type == EVP_PKEY_DSA)
1915 digest = EVP_sha1();
1916 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001917 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001918 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001919 digest = EVP_sha256();
1920 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001921#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001922 int nid;
1923
1924 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1925 goto mkcert_error;
1926 if (!(digest = EVP_get_digestbynid(nid)))
1927 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001928#else
1929 goto mkcert_error;
1930#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001931 }
1932
Christopher Faulet31af49d2015-06-09 17:29:50 +02001933 if (!(X509_sign(newcrt, capkey, digest)))
1934 goto mkcert_error;
1935
1936 /* Create and set the new SSL_CTX */
1937 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1938 goto mkcert_error;
1939 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1940 goto mkcert_error;
1941 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1942 goto mkcert_error;
1943 if (!SSL_CTX_check_private_key(ssl_ctx))
1944 goto mkcert_error;
1945
1946 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001947
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001948#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001949 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001950#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001951#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1952 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001953 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001954 EC_KEY *ecc;
1955 int nid;
1956
1957 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1958 goto end;
1959 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1960 goto end;
1961 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1962 EC_KEY_free(ecc);
1963 }
1964#endif
1965 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001966 return ssl_ctx;
1967
1968 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001969 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001970 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001971 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1972 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001973 return NULL;
1974}
1975
Christopher Faulet7969a332015-10-09 11:15:03 +02001976SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001977ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001978{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001979 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001980 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001981
Olivier Houchard66ab4982019-02-26 18:37:15 +01001982 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001983}
1984
Christopher Faulet30548802015-06-11 13:39:32 +02001985/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001986 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001987SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001988ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001989{
1990 struct lru64 *lru = NULL;
1991
1992 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001993 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001994 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001995 if (lru && lru->domain) {
1996 if (ssl)
1997 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001998 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001999 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002000 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002001 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002002 }
2003 return NULL;
2004}
2005
Emeric Brun821bb9b2017-06-15 16:37:39 +02002006/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2007 * function is not thread-safe, it should only be used to check if a certificate
2008 * exists in the lru cache (with no warranty it will not be removed by another
2009 * thread). It is kept for backward compatibility. */
2010SSL_CTX *
2011ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2012{
2013 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2014}
2015
Christopher Fauletd2cab922015-07-28 16:03:47 +02002016/* Set a certificate int the LRU cache used to store generated
2017 * certificate. Return 0 on success, otherwise -1 */
2018int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002019ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002020{
2021 struct lru64 *lru = NULL;
2022
2023 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002024 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002025 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002026 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002027 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002028 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002029 }
Christopher Faulet30548802015-06-11 13:39:32 +02002030 if (lru->domain && lru->data)
2031 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002032 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002033 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002034 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002035 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002036 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002037}
2038
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002039/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002040unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002041ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002042{
2043 return XXH32(data, len, ssl_ctx_lru_seed);
2044}
2045
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002046/* Generate a cert and immediately assign it to the SSL session so that the cert's
2047 * refcount is maintained regardless of the cert's presence in the LRU cache.
2048 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002049static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002050ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002051{
2052 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002053 SSL_CTX *ssl_ctx = NULL;
2054 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002055 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002056
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002057 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002058 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002059 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002060 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002061 if (lru && lru->domain)
2062 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002063 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002064 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002065 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002066 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002067 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002068 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002069 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002070 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002071 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002072 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002073 SSL_set_SSL_CTX(ssl, ssl_ctx);
2074 /* No LRU cache, this CTX will be released as soon as the session dies */
2075 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002076 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002077 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002078 return 0;
2079}
2080static int
2081ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2082{
2083 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002084 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002085
2086 conn_get_to_addr(conn);
2087 if (conn->flags & CO_FL_ADDR_TO_SET) {
2088 key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002089 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002090 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002091 }
2092 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002093}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002094#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002095
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002096#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002097typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2098
2099static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002100{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002101#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002102 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002103 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2104#endif
2105}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002106static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2107 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002108 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2109}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002110static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002111#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002112 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002113 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2114#endif
2115}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002116static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002117#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002118 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002119 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2120#endif
2121}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002122/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002123static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2124/* Unusable in this context. */
2125static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2126static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2127static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2128static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2129static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002130#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002131typedef enum { SET_MIN, SET_MAX } set_context_func;
2132
2133static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2134 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002135 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2136}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002137static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2138 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2139 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2140}
2141static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2142 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002143 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2144}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002145static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2146 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2147 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2148}
2149static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2150 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002151 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2152}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002153static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2154 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2155 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2156}
2157static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2158 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002159 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2160}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002161static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2162 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2163 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2164}
2165static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002166#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002167 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002168 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2169#endif
2170}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002171static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2172#if SSL_OP_NO_TLSv1_3
2173 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2174 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002175#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002176}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002177#endif
2178static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2179static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002180
2181static struct {
2182 int option;
2183 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002184 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2185 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002186 const char *name;
2187} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002188 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2189 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2190 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2191 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2192 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2193 {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 +02002194};
2195
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002196static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2197{
2198 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2199 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2200 SSL_set_SSL_CTX(ssl, ctx);
2201}
2202
Willy Tarreau5db847a2019-05-09 14:13:35 +02002203#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002204
2205static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2206{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002207 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002208 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002209
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002210 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2211 return SSL_TLSEXT_ERR_OK;
2212 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002213}
2214
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002215#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002216static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2217{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002218 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002219#else
2220static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2221{
2222#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002223 struct connection *conn;
2224 struct bind_conf *s;
2225 const uint8_t *extension_data;
2226 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002227 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002228
2229 char *wildp = NULL;
2230 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002231 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002232 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002233 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002234 int i;
2235
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002236 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002237 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002238
Olivier Houchard9679ac92017-10-27 14:58:08 +02002239 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002240 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002241#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002242 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2243 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002244#else
2245 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2246#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002247 /*
2248 * The server_name extension was given too much extensibility when it
2249 * was written, so parsing the normal case is a bit complex.
2250 */
2251 size_t len;
2252 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002253 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002254 /* Extract the length of the supplied list of names. */
2255 len = (*extension_data++) << 8;
2256 len |= *extension_data++;
2257 if (len + 2 != extension_len)
2258 goto abort;
2259 /*
2260 * The list in practice only has a single element, so we only consider
2261 * the first one.
2262 */
2263 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2264 goto abort;
2265 extension_len = len - 1;
2266 /* Now we can finally pull out the byte array with the actual hostname. */
2267 if (extension_len <= 2)
2268 goto abort;
2269 len = (*extension_data++) << 8;
2270 len |= *extension_data++;
2271 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2272 || memchr(extension_data, 0, len) != NULL)
2273 goto abort;
2274 servername = extension_data;
2275 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002276 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002277#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2278 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002279 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002280 }
2281#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002282 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002283 if (!s->strict_sni) {
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002284 ssl_sock_switchctx_set(ssl, s->default_ctx);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002285 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002286 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002287 goto abort;
2288 }
2289
2290 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002291#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002292 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002293#else
2294 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2295#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002296 uint8_t sign;
2297 size_t len;
2298 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002299 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002300 len = (*extension_data++) << 8;
2301 len |= *extension_data++;
2302 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002303 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002304 if (len % 2 != 0)
2305 goto abort;
2306 for (; len > 0; len -= 2) {
2307 extension_data++; /* hash */
2308 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002309 switch (sign) {
2310 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002311 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002312 break;
2313 case TLSEXT_signature_ecdsa:
2314 has_ecdsa_sig = 1;
2315 break;
2316 default:
2317 continue;
2318 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002319 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002320 break;
2321 }
2322 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002323 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002324 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002325 }
2326 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002327 const SSL_CIPHER *cipher;
2328 size_t len;
2329 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002330 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002331#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002332 len = ctx->cipher_suites_len;
2333 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002334#else
2335 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2336#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002337 if (len % 2 != 0)
2338 goto abort;
2339 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002340#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002341 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002342 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002343#else
2344 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2345#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002346 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002347 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002348 break;
2349 }
2350 }
2351 }
2352
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002353 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002354 trash.area[i] = tolower(servername[i]);
2355 if (!wildp && (trash.area[i] == '.'))
2356 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002357 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002358 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002359
2360 /* lookup in full qualified names */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002361 node = ebst_lookup(&s->sni_ctx, trash.area);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002362
2363 /* lookup a not neg filter */
2364 for (n = node; n; n = ebmb_next_dup(n)) {
2365 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002366 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002367 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002368 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002370 break;
2371 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002372 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002373 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002374 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002375 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002376 if (!node_anonymous)
2377 node_anonymous = n;
2378 break;
2379 }
2380 }
2381 }
2382 if (wildp) {
2383 /* lookup in wildcards names */
2384 node = ebst_lookup(&s->sni_w_ctx, wildp);
2385 for (n = node; n; n = ebmb_next_dup(n)) {
2386 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002387 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002388 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002389 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002390 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002391 break;
2392 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002393 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002394 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002395 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002396 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002397 if (!node_anonymous)
2398 node_anonymous = n;
2399 break;
2400 }
2401 }
2402 }
2403 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002404 /* select by key_signature priority order */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002405 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2406 : ((has_rsa_sig && node_rsa) ? node_rsa
2407 : (node_anonymous ? node_anonymous
2408 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2409 : node_rsa /* no rsa signature case (far far away) */
2410 )));
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002411 if (node) {
2412 /* switch ctx */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02002413 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002414 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002415 if (conf) {
2416 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2417 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2418 if (conf->early_data)
2419 allow_early = 1;
2420 }
2421 goto allow_early;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002422 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002423#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002424 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002425 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002426 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002427 }
2428#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002429 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002430 /* no certificate match, is the default_ctx */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002431 ssl_sock_switchctx_set(ssl, s->default_ctx);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002432 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002433allow_early:
2434#ifdef OPENSSL_IS_BORINGSSL
2435 if (allow_early)
2436 SSL_set_early_data_enabled(ssl, 1);
2437#else
2438 if (!allow_early)
2439 SSL_set_max_early_data(ssl, 0);
2440#endif
2441 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002442 abort:
2443 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2444 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002445#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002446 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002447#else
2448 *al = SSL_AD_UNRECOGNIZED_NAME;
2449 return 0;
2450#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002451}
2452
2453#else /* OPENSSL_IS_BORINGSSL */
2454
Emeric Brunfc0421f2012-09-07 17:30:07 +02002455/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2456 * warning when no match is found, which implies the default (first) cert
2457 * will keep being used.
2458 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002459static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002460{
2461 const char *servername;
2462 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002463 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002464 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002465 int i;
2466 (void)al; /* shut gcc stupid warning */
2467
2468 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002469 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002470#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002471 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2472 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002473#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002474 if (s->strict_sni)
2475 return SSL_TLSEXT_ERR_ALERT_FATAL;
2476 ssl_sock_switchctx_set(ssl, s->default_ctx);
2477 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002478 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002479
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002480 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002481 if (!servername[i])
2482 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002483 trash.area[i] = tolower(servername[i]);
2484 if (!wildp && (trash.area[i] == '.'))
2485 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002486 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002487 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002488
2489 /* lookup in full qualified names */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002490 node = ebst_lookup(&s->sni_ctx, trash.area);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002491
2492 /* lookup a not neg filter */
2493 for (n = node; n; n = ebmb_next_dup(n)) {
2494 if (!container_of(n, struct sni_ctx, name)->neg) {
2495 node = n;
2496 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002497 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002498 }
2499 if (!node && wildp) {
2500 /* lookup in wildcards names */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002501 node = ebst_lookup(&s->sni_w_ctx, wildp);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002502 }
2503 if (!node || container_of(node, struct sni_ctx, name)->neg) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002504#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002505 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2506 /* switch ctx done in ssl_sock_generate_certificate */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002507 return SSL_TLSEXT_ERR_OK;
2508 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002509#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002510 if (s->strict_sni)
2511 return SSL_TLSEXT_ERR_ALERT_FATAL;
2512 ssl_sock_switchctx_set(ssl, s->default_ctx);
2513 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002514 }
2515
2516 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002517 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002518 return SSL_TLSEXT_ERR_OK;
2519}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002520#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002521#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2522
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002523#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002524
2525static DH * ssl_get_dh_1024(void)
2526{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002527 static unsigned char dh1024_p[]={
2528 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2529 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2530 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2531 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2532 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2533 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2534 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2535 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2536 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2537 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2538 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2539 };
2540 static unsigned char dh1024_g[]={
2541 0x02,
2542 };
2543
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002544 BIGNUM *p;
2545 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002546 DH *dh = DH_new();
2547 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002548 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2549 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002550
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002551 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002552 DH_free(dh);
2553 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002554 } else {
2555 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002556 }
2557 }
2558 return dh;
2559}
2560
2561static DH *ssl_get_dh_2048(void)
2562{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002563 static unsigned char dh2048_p[]={
2564 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2565 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2566 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2567 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2568 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2569 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2570 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2571 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2572 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2573 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2574 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2575 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2576 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2577 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2578 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2579 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2580 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2581 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2582 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2583 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2584 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2585 0xB7,0x1F,0x77,0xF3,
2586 };
2587 static unsigned char dh2048_g[]={
2588 0x02,
2589 };
2590
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002591 BIGNUM *p;
2592 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002593 DH *dh = DH_new();
2594 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002595 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2596 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002597
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002598 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002599 DH_free(dh);
2600 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002601 } else {
2602 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002603 }
2604 }
2605 return dh;
2606}
2607
2608static DH *ssl_get_dh_4096(void)
2609{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002610 static unsigned char dh4096_p[]={
2611 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2612 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2613 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2614 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2615 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2616 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2617 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2618 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2619 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2620 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2621 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2622 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2623 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2624 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2625 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2626 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2627 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2628 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2629 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2630 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2631 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2632 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2633 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2634 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2635 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2636 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2637 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2638 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2639 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2640 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2641 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2642 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2643 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2644 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2645 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2646 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2647 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2648 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2649 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2650 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2651 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2652 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2653 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002654 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002655 static unsigned char dh4096_g[]={
2656 0x02,
2657 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002658
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002659 BIGNUM *p;
2660 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002661 DH *dh = DH_new();
2662 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002663 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2664 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002665
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002666 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002667 DH_free(dh);
2668 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002669 } else {
2670 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002671 }
2672 }
2673 return dh;
2674}
2675
2676/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002677 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002678static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2679{
2680 DH *dh = NULL;
2681 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002682 int type;
2683
2684 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002685
2686 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2687 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2688 */
2689 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2690 keylen = EVP_PKEY_bits(pkey);
2691 }
2692
Willy Tarreauef934602016-12-22 23:12:01 +01002693 if (keylen > global_ssl.default_dh_param) {
2694 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002695 }
2696
Remi Gacogned3a341a2015-05-29 16:26:17 +02002697 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002698 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002699 }
2700 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002701 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002702 }
2703 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002704 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002705 }
2706
2707 return dh;
2708}
2709
Remi Gacogne47783ef2015-05-29 15:53:22 +02002710static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002711{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002712 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002713 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002714
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002715 if (in == NULL)
2716 goto end;
2717
Remi Gacogne47783ef2015-05-29 15:53:22 +02002718 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002719 goto end;
2720
Remi Gacogne47783ef2015-05-29 15:53:22 +02002721 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2722
2723end:
2724 if (in)
2725 BIO_free(in);
2726
Emeric Brune1b4ed42018-08-16 15:14:12 +02002727 ERR_clear_error();
2728
Remi Gacogne47783ef2015-05-29 15:53:22 +02002729 return dh;
2730}
2731
2732int ssl_sock_load_global_dh_param_from_file(const char *filename)
2733{
2734 global_dh = ssl_sock_get_dh_from_file(filename);
2735
2736 if (global_dh) {
2737 return 0;
2738 }
2739
2740 return -1;
2741}
2742
2743/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
Joseph Herlant017b3da2018-11-15 09:07:59 -08002744 if an error occurred, and 0 if parameter not found. */
Remi Gacogne47783ef2015-05-29 15:53:22 +02002745int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
2746{
2747 int ret = -1;
2748 DH *dh = ssl_sock_get_dh_from_file(file);
2749
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002750 if (dh) {
2751 ret = 1;
2752 SSL_CTX_set_tmp_dh(ctx, dh);
Remi Gacogne4f902b82015-05-28 16:23:00 +02002753
2754 if (ssl_dh_ptr_index >= 0) {
2755 /* store a pointer to the DH params to avoid complaining about
2756 ssl-default-dh-param not being set for this SSL_CTX */
2757 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2758 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002759 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02002760 else if (global_dh) {
2761 SSL_CTX_set_tmp_dh(ctx, global_dh);
2762 ret = 0; /* DH params not found */
2763 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002764 else {
Emeric Brun41fdb3c2013-04-26 11:05:44 +02002765 /* Clear openssl global errors stack */
2766 ERR_clear_error();
2767
Willy Tarreauef934602016-12-22 23:12:01 +01002768 if (global_ssl.default_dh_param <= 1024) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002769 /* we are limited to DH parameter of 1024 bits anyway */
Remi Gacognec7e12632016-07-02 16:26:10 +02002770 if (local_dh_1024 == NULL)
2771 local_dh_1024 = ssl_get_dh_1024();
2772
Remi Gacogne8de54152014-07-15 11:36:40 +02002773 if (local_dh_1024 == NULL)
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002774 goto end;
Willy Tarreau6e774b42014-04-25 21:35:23 +02002775
Remi Gacogne8de54152014-07-15 11:36:40 +02002776 SSL_CTX_set_tmp_dh(ctx, local_dh_1024);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002777 }
2778 else {
2779 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2780 }
Willy Tarreau6e774b42014-04-25 21:35:23 +02002781
Emeric Brun41fdb3c2013-04-26 11:05:44 +02002782 ret = 0; /* DH params not found */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002783 }
Emeric Brun644cde02012-12-14 11:21:13 +01002784
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002785end:
2786 if (dh)
2787 DH_free(dh);
2788
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002789 return ret;
2790}
2791#endif
2792
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002793static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf,
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002794 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002795{
2796 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002797 int wild = 0, neg = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002798 struct ebmb_node *node;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002799
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002800 if (*name == '!') {
2801 neg = 1;
2802 name++;
2803 }
2804 if (*name == '*') {
2805 wild = 1;
2806 name++;
2807 }
2808 /* !* filter is a nop */
2809 if (neg && wild)
2810 return order;
2811 if (*name) {
2812 int j, len;
2813 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002814 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002815 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002816 if (j >= trash.size)
2817 return order;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002818 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002819
2820 /* Check for duplicates. */
2821 if (wild)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002822 node = ebst_lookup(&s->sni_w_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002823 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002824 node = ebst_lookup(&s->sni_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002825 for (; node; node = ebmb_next_dup(node)) {
2826 sc = ebmb_entry(node, struct sni_ctx, name);
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002827 if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg)
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002828 return order;
2829 }
2830
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002831 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002832 if (!sc)
2833 return order;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002835 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002836 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002837 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002838 sc->order = order++;
2839 sc->neg = neg;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01002840 if (kinfo.sig != TLSEXT_signature_anonymous)
2841 SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002842 if (wild)
2843 ebst_insert(&s->sni_w_ctx, &sc->name);
2844 else
2845 ebst_insert(&s->sni_ctx, &sc->name);
2846 }
2847 return order;
2848}
2849
yanbzhu488a4d22015-12-01 15:16:07 -05002850
2851/* The following code is used for loading multiple crt files into
2852 * SSL_CTX's based on CN/SAN
2853 */
Willy Tarreau5db847a2019-05-09 14:13:35 +02002854#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05002855/* This is used to preload the certifcate, private key
2856 * and Cert Chain of a file passed in via the crt
2857 * argument
2858 *
2859 * This way, we do not have to read the file multiple times
2860 */
2861struct cert_key_and_chain {
2862 X509 *cert;
2863 EVP_PKEY *key;
2864 unsigned int num_chain_certs;
2865 /* This is an array of X509 pointers */
2866 X509 **chain_certs;
2867};
2868
yanbzhu08ce6ab2015-12-02 13:01:29 -05002869#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
2870
2871struct key_combo_ctx {
2872 SSL_CTX *ctx;
2873 int order;
2874};
2875
2876/* Map used for processing multiple keypairs for a single purpose
2877 *
2878 * This maps CN/SNI name to certificate type
2879 */
2880struct sni_keytype {
2881 int keytypes; /* BITMASK for keytypes */
2882 struct ebmb_node name; /* node holding the servername value */
2883};
2884
2885
yanbzhu488a4d22015-12-01 15:16:07 -05002886/* Frees the contents of a cert_key_and_chain
2887 */
2888static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
2889{
2890 int i;
2891
2892 if (!ckch)
2893 return;
2894
2895 /* Free the certificate and set pointer to NULL */
2896 if (ckch->cert)
2897 X509_free(ckch->cert);
2898 ckch->cert = NULL;
2899
2900 /* Free the key and set pointer to NULL */
2901 if (ckch->key)
2902 EVP_PKEY_free(ckch->key);
2903 ckch->key = NULL;
2904
2905 /* Free each certificate in the chain */
2906 for (i = 0; i < ckch->num_chain_certs; i++) {
2907 if (ckch->chain_certs[i])
2908 X509_free(ckch->chain_certs[i]);
2909 }
2910
2911 /* Free the chain obj itself and set to NULL */
2912 if (ckch->num_chain_certs > 0) {
2913 free(ckch->chain_certs);
2914 ckch->num_chain_certs = 0;
2915 ckch->chain_certs = NULL;
2916 }
2917
2918}
2919
2920/* checks if a key and cert exists in the ckch
2921 */
2922static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
2923{
2924 return (ckch->cert != NULL && ckch->key != NULL);
2925}
2926
2927
2928/* Loads the contents of a crt file (path) into a cert_key_and_chain
2929 * This allows us to carry the contents of the file without having to
2930 * read the file multiple times.
2931 *
2932 * returns:
2933 * 0 on Success
2934 * 1 on SSL Failure
2935 * 2 on file not found
2936 */
2937static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
2938{
2939
2940 BIO *in;
2941 X509 *ca = NULL;
2942 int ret = 1;
2943
2944 ssl_sock_free_cert_key_and_chain_contents(ckch);
2945
2946 in = BIO_new(BIO_s_file());
2947 if (in == NULL)
2948 goto end;
2949
2950 if (BIO_read_filename(in, path) <= 0)
2951 goto end;
2952
yanbzhu488a4d22015-12-01 15:16:07 -05002953 /* Read Private Key */
2954 ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
2955 if (ckch->key == NULL) {
2956 memprintf(err, "%sunable to load private key from file '%s'.\n",
2957 err && *err ? *err : "", path);
2958 goto end;
2959 }
2960
Willy Tarreaubb137a82016-04-06 19:02:38 +02002961 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02002962 if (BIO_reset(in) == -1) {
2963 memprintf(err, "%san error occurred while reading the file '%s'.\n",
2964 err && *err ? *err : "", path);
2965 goto end;
2966 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02002967
2968 /* Read Certificate */
2969 ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
2970 if (ckch->cert == NULL) {
2971 memprintf(err, "%sunable to load certificate from file '%s'.\n",
2972 err && *err ? *err : "", path);
2973 goto end;
2974 }
2975
yanbzhu488a4d22015-12-01 15:16:07 -05002976 /* Read Certificate Chain */
2977 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
2978 /* Grow the chain certs */
2979 ckch->num_chain_certs++;
2980 ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *)));
2981
2982 /* use - 1 here since we just incremented it above */
2983 ckch->chain_certs[ckch->num_chain_certs - 1] = ca;
2984 }
2985 ret = ERR_get_error();
2986 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
2987 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
2988 err && *err ? *err : "", path);
2989 ret = 1;
2990 goto end;
2991 }
2992
2993 ret = 0;
2994
2995end:
2996
2997 ERR_clear_error();
2998 if (in)
2999 BIO_free(in);
3000
3001 /* Something went wrong in one of the reads */
3002 if (ret != 0)
3003 ssl_sock_free_cert_key_and_chain_contents(ckch);
3004
3005 return ret;
3006}
3007
3008/* Loads the info in ckch into ctx
3009 * Currently, this does not process any information about ocsp, dhparams or
3010 * sctl
3011 * Returns
3012 * 0 on success
3013 * 1 on failure
3014 */
3015static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3016{
3017 int i = 0;
3018
3019 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3020 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3021 err && *err ? *err : "", path);
3022 return 1;
3023 }
3024
3025 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3026 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3027 err && *err ? *err : "", path);
3028 return 1;
3029 }
3030
yanbzhu488a4d22015-12-01 15:16:07 -05003031 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
3032 for (i = 0; i < ckch->num_chain_certs; i++) {
3033 if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003034 memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3035 err && *err ? *err : "", (i+1), path);
yanbzhu488a4d22015-12-01 15:16:07 -05003036 return 1;
3037 }
3038 }
3039
3040 if (SSL_CTX_check_private_key(ctx) <= 0) {
3041 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3042 err && *err ? *err : "", path);
3043 return 1;
3044 }
3045
3046 return 0;
3047}
3048
yanbzhu08ce6ab2015-12-02 13:01:29 -05003049
3050static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
3051{
3052 struct sni_keytype *s_kt = NULL;
3053 struct ebmb_node *node;
3054 int i;
3055
3056 for (i = 0; i < trash.size; i++) {
3057 if (!str[i])
3058 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003059 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003060 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003061 trash.area[i] = 0;
3062 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003063 if (!node) {
3064 /* CN not found in tree */
3065 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3066 /* Using memcpy here instead of strncpy.
3067 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3068 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3069 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003070 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003071 s_kt->keytypes = 0;
3072 ebst_insert(sni_keytypes, &s_kt->name);
3073 } else {
3074 /* CN found in tree */
3075 s_kt = container_of(node, struct sni_keytype, name);
3076 }
3077
3078 /* Mark that this CN has the keytype of key_index via keytypes mask */
3079 s_kt->keytypes |= 1<<key_index;
3080
3081}
3082
3083
3084/* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files.
3085 * If any are found, group these files into a set of SSL_CTX*
3086 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3087 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003088 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003089 *
3090 * Returns
3091 * 0 on success
3092 * 1 on failure
3093 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003094static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3095 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003096{
3097 char fp[MAXPATHLEN+1] = {0};
3098 int n = 0;
3099 int i = 0;
3100 struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} };
3101 struct eb_root sni_keytypes_map = { {0} };
3102 struct ebmb_node *node;
3103 struct ebmb_node *next;
3104 /* Array of SSL_CTX pointers corresponding to each possible combo
3105 * of keytypes
3106 */
3107 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
3108 int rv = 0;
3109 X509_NAME *xname = NULL;
3110 char *str = NULL;
3111#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3112 STACK_OF(GENERAL_NAME) *names = NULL;
3113#endif
3114
3115 /* Load all possible certs and keys */
3116 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3117 struct stat buf;
3118
3119 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3120 if (stat(fp, &buf) == 0) {
3121 if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) {
3122 rv = 1;
3123 goto end;
3124 }
3125 }
3126 }
3127
3128 /* Process each ckch and update keytypes for each CN/SAN
3129 * for example, if CN/SAN www.a.com is associated with
3130 * certs with keytype 0 and 2, then at the end of the loop,
3131 * www.a.com will have:
3132 * keyindex = 0 | 1 | 4 = 5
3133 */
3134 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3135
3136 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3137 continue;
3138
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003139 if (fcount) {
Willy Tarreau24b892f2016-06-20 23:01:57 +02003140 for (i = 0; i < fcount; i++)
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003141 ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3142 } else {
3143 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3144 * so the line that contains logic is marked via comments
3145 */
3146 xname = X509_get_subject_name(certs_and_keys[n].cert);
3147 i = -1;
3148 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3149 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003150 ASN1_STRING *value;
3151 value = X509_NAME_ENTRY_get_data(entry);
3152 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003153 /* Important line is here */
3154 ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003155
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003156 OPENSSL_free(str);
3157 str = NULL;
3158 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003159 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003160
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003161 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003162#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003163 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3164 if (names) {
3165 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3166 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003167
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003168 if (name->type == GEN_DNS) {
3169 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3170 /* Important line is here */
3171 ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003172
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003173 OPENSSL_free(str);
3174 str = NULL;
3175 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003176 }
3177 }
3178 }
3179 }
3180#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3181 }
3182
3183 /* If no files found, return error */
3184 if (eb_is_empty(&sni_keytypes_map)) {
3185 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3186 err && *err ? *err : "", path);
3187 rv = 1;
3188 goto end;
3189 }
3190
3191 /* We now have a map of CN/SAN to keytypes that are loaded in
3192 * Iterate through the map to create the SSL_CTX's (if needed)
3193 * and add each CTX to the SNI tree
3194 *
3195 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003196 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003197 * combination is denoted by the key in the map. Each key
3198 * has a value between 1 and 2^n - 1. Conveniently, the array
3199 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3200 * entry in the array to correspond to the unique combo (key)
3201 * associated with i. This unique key combo (i) will be associated
3202 * with combos[i-1]
3203 */
3204
3205 node = ebmb_first(&sni_keytypes_map);
3206 while (node) {
3207 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003208 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003209 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003210
3211 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3212 i = container_of(node, struct sni_keytype, name)->keytypes;
3213 cur_ctx = key_combos[i-1].ctx;
3214
3215 if (cur_ctx == NULL) {
3216 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003217 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003218 if (cur_ctx == NULL) {
3219 memprintf(err, "%sunable to allocate SSL context.\n",
3220 err && *err ? *err : "");
3221 rv = 1;
3222 goto end;
3223 }
3224
yanbzhube2774d2015-12-10 15:07:30 -05003225 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003226 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3227 if (i & (1<<n)) {
3228 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003229 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3230 if (ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err) != 0) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003231 SSL_CTX_free(cur_ctx);
3232 rv = 1;
3233 goto end;
3234 }
yanbzhube2774d2015-12-10 15:07:30 -05003235
3236#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
3237 /* Load OCSP Info into context */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003238 if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) {
yanbzhube2774d2015-12-10 15:07:30 -05003239 if (err)
3240 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",
Bertrand Jacquin5424ee02016-11-13 16:37:14 +00003241 *err ? *err : "", cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003242 SSL_CTX_free(cur_ctx);
3243 rv = 1;
3244 goto end;
3245 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003246#elif (defined OPENSSL_IS_BORINGSSL)
3247 ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003248#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003249 }
3250 }
3251
3252 /* Load DH params into the ctx to support DHE keys */
3253#ifndef OPENSSL_NO_DH
3254 if (ssl_dh_ptr_index >= 0)
3255 SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL);
3256
3257 rv = ssl_sock_load_dh_params(cur_ctx, NULL);
3258 if (rv < 0) {
3259 if (err)
3260 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3261 *err ? *err : "", path);
3262 rv = 1;
3263 goto end;
3264 }
3265#endif
3266
3267 /* Update key_combos */
3268 key_combos[i-1].ctx = cur_ctx;
3269 }
3270
3271 /* Update SNI Tree */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003272 key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf,
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003273 kinfo, str, key_combos[i-1].order);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003274 node = ebmb_next(node);
3275 }
3276
3277
3278 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3279 if (!bind_conf->default_ctx) {
3280 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3281 if (key_combos[i].ctx) {
3282 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003283 bind_conf->default_ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003284 break;
3285 }
3286 }
3287 }
3288
3289end:
3290
3291 if (names)
3292 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3293
3294 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3295 ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]);
3296
3297 node = ebmb_first(&sni_keytypes_map);
3298 while (node) {
3299 next = ebmb_next(node);
3300 ebmb_delete(node);
3301 node = next;
3302 }
3303
3304 return rv;
3305}
3306#else
3307/* This is a dummy, that just logs an error and returns error */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003308static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3309 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003310{
3311 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3312 err && *err ? *err : "", path, strerror(errno));
3313 return 1;
3314}
3315
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003316#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003317
Emeric Brunfc0421f2012-09-07 17:30:07 +02003318/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
3319 * an early error happens and the caller must call SSL_CTX_free() by itelf.
3320 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003321static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s,
3322 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003323{
3324 BIO *in;
3325 X509 *x = NULL, *ca;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003326 int i, err;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003327 int ret = -1;
3328 int order = 0;
3329 X509_NAME *xname;
3330 char *str;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003331 pem_password_cb *passwd_cb;
3332 void *passwd_cb_userdata;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003333 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003334 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003335
Emeric Brunfc0421f2012-09-07 17:30:07 +02003336#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3337 STACK_OF(GENERAL_NAME) *names;
3338#endif
3339
3340 in = BIO_new(BIO_s_file());
3341 if (in == NULL)
3342 goto end;
3343
3344 if (BIO_read_filename(in, file) <= 0)
3345 goto end;
3346
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003347
3348 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
3349 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
3350
3351 x = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003352 if (x == NULL)
3353 goto end;
3354
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003355 pkey = X509_get_pubkey(x);
3356 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003357 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003358 switch(EVP_PKEY_base_id(pkey)) {
3359 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003360 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003361 break;
3362 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003363 kinfo.sig = TLSEXT_signature_ecdsa;
3364 break;
3365 case EVP_PKEY_DSA:
3366 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003367 break;
3368 }
3369 EVP_PKEY_free(pkey);
3370 }
3371
Emeric Brun50bcecc2013-04-22 13:05:23 +02003372 if (fcount) {
3373 while (fcount--)
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003374 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, sni_filter[fcount], order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003375 }
3376 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003377#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003378 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
3379 if (names) {
3380 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3381 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3382 if (name->type == GEN_DNS) {
3383 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003384 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003385 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003386 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003387 }
3388 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003389 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003390 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003391#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003392 xname = X509_get_subject_name(x);
3393 i = -1;
3394 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3395 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003396 ASN1_STRING *value;
3397
3398 value = X509_NAME_ENTRY_get_data(entry);
3399 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003400 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003401 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003402 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003403 }
3404 }
3405
3406 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
3407 if (!SSL_CTX_use_certificate(ctx, x))
3408 goto end;
3409
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003410#ifdef SSL_CTX_clear_extra_chain_certs
3411 SSL_CTX_clear_extra_chain_certs(ctx);
3412#else
Emeric Brunfc0421f2012-09-07 17:30:07 +02003413 if (ctx->extra_certs != NULL) {
3414 sk_X509_pop_free(ctx->extra_certs, X509_free);
3415 ctx->extra_certs = NULL;
3416 }
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003417#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003418
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003419 while ((ca = PEM_read_bio_X509(in, NULL, passwd_cb, passwd_cb_userdata))) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003420 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3421 X509_free(ca);
3422 goto end;
3423 }
3424 }
3425
3426 err = ERR_get_error();
3427 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
3428 /* we successfully reached the last cert in the file */
3429 ret = 1;
3430 }
3431 ERR_clear_error();
3432
3433end:
3434 if (x)
3435 X509_free(x);
3436
3437 if (in)
3438 BIO_free(in);
3439
3440 return ret;
3441}
3442
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003443static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3444 char **sni_filter, int fcount, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003445{
3446 int ret;
3447 SSL_CTX *ctx;
3448
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003449 ctx = SSL_CTX_new(SSLv23_server_method());
Emeric Brunfc0421f2012-09-07 17:30:07 +02003450 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003451 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3452 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003453 return 1;
3454 }
3455
3456 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003457 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
3458 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003459 SSL_CTX_free(ctx);
3460 return 1;
3461 }
3462
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003463 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, ssl_conf, sni_filter, fcount);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003464 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003465 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
3466 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003467 if (ret < 0) /* serious error, must do that ourselves */
3468 SSL_CTX_free(ctx);
3469 return 1;
3470 }
Emeric Brun61694ab2012-10-26 13:35:33 +02003471
3472 if (SSL_CTX_check_private_key(ctx) <= 0) {
3473 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3474 err && *err ? *err : "", path);
3475 return 1;
3476 }
3477
Emeric Brunfc0421f2012-09-07 17:30:07 +02003478 /* we must not free the SSL_CTX anymore below, since it's already in
3479 * the tree, so it will be discovered and cleaned in time.
3480 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003481#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +02003482 /* store a NULL pointer to indicate we have not yet loaded
3483 a custom DH param file */
3484 if (ssl_dh_ptr_index >= 0) {
3485 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3486 }
3487
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003488 ret = ssl_sock_load_dh_params(ctx, path);
3489 if (ret < 0) {
3490 if (err)
3491 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3492 *err ? *err : "", path);
3493 return 1;
3494 }
3495#endif
3496
Lukas Tribuse4e30f72014-12-09 16:32:51 +01003497#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02003498 ret = ssl_sock_load_ocsp(ctx, path);
3499 if (ret < 0) {
3500 if (err)
3501 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",
3502 *err ? *err : "", path);
3503 return 1;
3504 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003505#elif (defined OPENSSL_IS_BORINGSSL)
3506 ssl_sock_set_ocsp_response_from_file(ctx, path);
Emeric Brun4147b2e2014-06-16 18:36:30 +02003507#endif
3508
Willy Tarreau5db847a2019-05-09 14:13:35 +02003509#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003510 if (sctl_ex_index >= 0) {
3511 ret = ssl_sock_load_sctl(ctx, path);
3512 if (ret < 0) {
3513 if (err)
3514 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3515 *err ? *err : "", path);
3516 return 1;
3517 }
3518 }
3519#endif
3520
Emeric Brunfc0421f2012-09-07 17:30:07 +02003521#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003522 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003523 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3524 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +02003525 return 1;
3526 }
3527#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003528 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003529 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003530 bind_conf->default_ssl_conf = ssl_conf;
3531 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003532
3533 return 0;
3534}
3535
Willy Tarreau03209342016-12-22 17:08:28 +01003536int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003537{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003538 struct dirent **de_list;
3539 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003540 DIR *dir;
3541 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01003542 char *end;
3543 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02003544 int cfgerr = 0;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003545#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003546 int is_bundle;
3547 int j;
3548#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003549
yanbzhu08ce6ab2015-12-02 13:01:29 -05003550 if (stat(path, &buf) == 0) {
3551 dir = opendir(path);
3552 if (!dir)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003553 return ssl_sock_load_cert_file(path, bind_conf, NULL, NULL, 0, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003554
yanbzhu08ce6ab2015-12-02 13:01:29 -05003555 /* strip trailing slashes, including first one */
3556 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
3557 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003558
yanbzhu08ce6ab2015-12-02 13:01:29 -05003559 n = scandir(path, &de_list, 0, alphasort);
3560 if (n < 0) {
3561 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
3562 err && *err ? *err : "", path, strerror(errno));
3563 cfgerr++;
3564 }
3565 else {
3566 for (i = 0; i < n; i++) {
3567 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02003568
yanbzhu08ce6ab2015-12-02 13:01:29 -05003569 end = strrchr(de->d_name, '.');
3570 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
3571 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003572
yanbzhu08ce6ab2015-12-02 13:01:29 -05003573 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
3574 if (stat(fp, &buf) != 0) {
3575 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3576 err && *err ? *err : "", fp, strerror(errno));
3577 cfgerr++;
3578 goto ignore_entry;
3579 }
3580 if (!S_ISREG(buf.st_mode))
3581 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05003582
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003583#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003584 is_bundle = 0;
3585 /* Check if current entry in directory is part of a multi-cert bundle */
3586
3587 if (end) {
3588 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
3589 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
3590 is_bundle = 1;
3591 break;
3592 }
3593 }
3594
3595 if (is_bundle) {
3596 char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */
3597 int dp_len;
3598
3599 dp_len = end - de->d_name;
3600 snprintf(dp, dp_len + 1, "%s", de->d_name);
3601
3602 /* increment i and free de until we get to a non-bundle cert
3603 * Note here that we look at de_list[i + 1] before freeing de
3604 * this is important since ignore_entry will free de
3605 */
3606 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) {
3607 free(de);
3608 i++;
3609 de = de_list[i];
3610 }
3611
3612 snprintf(fp, sizeof(fp), "%s/%s", path, dp);
Emeric Bruneb155b62018-08-16 15:11:12 +02003613 cfgerr += ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05003614
3615 /* Successfully processed the bundle */
3616 goto ignore_entry;
3617 }
3618 }
3619
3620#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003621 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003622ignore_entry:
3623 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003624 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003625 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003626 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003627 closedir(dir);
3628 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003629 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003630
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003631 cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003632
Emeric Brunfc0421f2012-09-07 17:30:07 +02003633 return cfgerr;
3634}
3635
Thierry Fournier383085f2013-01-24 14:15:43 +01003636/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3637 * done once. Zero is returned if the operation fails. No error is returned
3638 * if the random is said as not implemented, because we expect that openssl
3639 * will use another method once needed.
3640 */
3641static int ssl_initialize_random()
3642{
3643 unsigned char random;
3644 static int random_initialized = 0;
3645
3646 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3647 random_initialized = 1;
3648
3649 return random_initialized;
3650}
3651
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003652/* release ssl bind conf */
3653void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003654{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003655 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01003656#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003657 free(conf->npn_str);
3658 conf->npn_str = NULL;
3659#endif
3660#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
3661 free(conf->alpn_str);
3662 conf->alpn_str = NULL;
3663#endif
3664 free(conf->ca_file);
3665 conf->ca_file = NULL;
3666 free(conf->crl_file);
3667 conf->crl_file = NULL;
3668 free(conf->ciphers);
3669 conf->ciphers = NULL;
Willy Tarreau5db847a2019-05-09 14:13:35 +02003670#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02003671 free(conf->ciphersuites);
3672 conf->ciphersuites = NULL;
3673#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003674 free(conf->curves);
3675 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003676 free(conf->ecdhe);
3677 conf->ecdhe = NULL;
3678 }
3679}
3680
3681int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
3682{
3683 char thisline[CRT_LINESIZE];
3684 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003685 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05003686 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003687 int linenum = 0;
3688 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003689
Willy Tarreauad1731d2013-04-02 17:35:58 +02003690 if ((f = fopen(file, "r")) == NULL) {
3691 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003692 return 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003693 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003694
3695 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003696 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003697 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003698 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003699 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003700 char *crt_path;
3701 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003702
3703 linenum++;
3704 end = line + strlen(line);
3705 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
3706 /* Check if we reached the limit and the last char is not \n.
3707 * Watch out for the last line without the terminating '\n'!
3708 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02003709 memprintf(err, "line %d too long in file '%s', limit is %d characters",
3710 linenum, file, (int)sizeof(thisline)-1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003711 cfgerr = 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003712 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003713 }
3714
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003715 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003716 newarg = 1;
3717 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003718 if (*line == '#' || *line == '\n' || *line == '\r') {
3719 /* end of string, end of loop */
3720 *line = 0;
3721 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003722 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02003723 newarg = 1;
3724 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003725 } else if (*line == '[') {
3726 if (ssl_b) {
3727 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
3728 cfgerr = 1;
3729 break;
3730 }
3731 if (!arg) {
3732 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
3733 cfgerr = 1;
3734 break;
3735 }
3736 ssl_b = arg;
3737 newarg = 1;
3738 *line = 0;
3739 } else if (*line == ']') {
3740 if (ssl_e) {
3741 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Emeric Brun50bcecc2013-04-22 13:05:23 +02003742 cfgerr = 1;
3743 break;
3744 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003745 if (!ssl_b) {
3746 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
3747 cfgerr = 1;
3748 break;
3749 }
3750 ssl_e = arg;
3751 newarg = 1;
3752 *line = 0;
3753 } else if (newarg) {
3754 if (arg == MAX_CRT_ARGS) {
3755 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
3756 cfgerr = 1;
3757 break;
3758 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003759 newarg = 0;
3760 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003761 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003762 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003763 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003764 if (cfgerr)
3765 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003766 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003767
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003768 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003769 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003770 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003771
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003772 crt_path = args[0];
3773 if (*crt_path != '/' && global_ssl.crt_base) {
3774 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
3775 memprintf(err, "'%s' : path too long on line %d in file '%s'",
3776 crt_path, linenum, file);
3777 cfgerr = 1;
3778 break;
3779 }
3780 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
3781 crt_path = path;
3782 }
3783
3784 ssl_conf = calloc(1, sizeof *ssl_conf);
3785 cur_arg = ssl_b ? ssl_b : 1;
3786 while (cur_arg < ssl_e) {
3787 newarg = 0;
3788 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
3789 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
3790 newarg = 1;
3791 cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
3792 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
3793 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
3794 args[cur_arg], linenum, file);
3795 cfgerr = 1;
3796 }
3797 cur_arg += 1 + ssl_bind_kws[i].skip;
3798 break;
3799 }
3800 }
3801 if (!cfgerr && !newarg) {
3802 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
3803 args[cur_arg], linenum, file);
3804 cfgerr = 1;
3805 break;
3806 }
3807 }
3808 if (cfgerr) {
3809 ssl_sock_free_ssl_conf(ssl_conf);
3810 free(ssl_conf);
3811 ssl_conf = NULL;
3812 break;
3813 }
3814
3815 if (stat(crt_path, &buf) == 0) {
3816 cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf,
3817 &args[cur_arg], arg - cur_arg - 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003818 } else {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003819 cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf,
3820 &args[cur_arg], arg - cur_arg - 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003821 }
3822
Willy Tarreauad1731d2013-04-02 17:35:58 +02003823 if (cfgerr) {
3824 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003825 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003826 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003827 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003828 fclose(f);
3829 return cfgerr;
3830}
3831
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003832/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003833static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003834ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003835{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003836 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003837 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003838 SSL_OP_ALL | /* all known workarounds for bugs */
3839 SSL_OP_NO_SSLv2 |
3840 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003841 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003842 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003843 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02003844 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003845 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003846 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003847 SSL_MODE_ENABLE_PARTIAL_WRITE |
3848 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003849 SSL_MODE_RELEASE_BUFFERS |
3850 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02003851 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003852 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003853 int flags = MC_SSL_O_ALL;
3854 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003855
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003856 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003857 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003858
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003859 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003860 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3861 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3862 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003863 else
3864 flags = conf_ssl_methods->flags;
3865
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003866 min = conf_ssl_methods->min;
3867 max = conf_ssl_methods->max;
3868 /* start with TLSv10 to remove SSLv3 per default */
3869 if (!min && (!max || max >= CONF_TLSV10))
3870 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003871 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003872 if (min)
3873 flags |= (methodVersions[min].flag - 1);
3874 if (max)
3875 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003876 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003877 min = max = CONF_TLSV_NONE;
3878 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003879 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003880 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003881 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003882 if (min) {
3883 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003884 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
3885 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3886 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3887 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003888 hole = 0;
3889 }
3890 max = i;
3891 }
3892 else {
3893 min = max = i;
3894 }
3895 }
3896 else {
3897 if (min)
3898 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003899 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003900 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003901 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3902 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003903 cfgerr += 1;
3904 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003905 /* save real min/max in bind_conf */
3906 conf_ssl_methods->min = min;
3907 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003908
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003909#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003910 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08003911 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003912 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003913 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003914 else
3915 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
3916 if (flags & methodVersions[i].flag)
3917 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003918#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003919 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003920 methodVersions[min].ctx_set_version(ctx, SET_MIN);
3921 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02003922#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003923
3924 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
3925 options |= SSL_OP_NO_TICKET;
3926 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
3927 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08003928
3929#ifdef SSL_OP_NO_RENEGOTIATION
3930 options |= SSL_OP_NO_RENEGOTIATION;
3931#endif
3932
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003933 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003934
Willy Tarreau5db847a2019-05-09 14:13:35 +02003935#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003936 if (global_ssl.async)
3937 mode |= SSL_MODE_ASYNC;
3938#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003939 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003940 if (global_ssl.life_time)
3941 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003942
3943#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3944#ifdef OPENSSL_IS_BORINGSSL
3945 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
3946 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02003947#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard51088ce2019-01-02 18:46:41 +01003948 if (bind_conf->ssl_conf.early_data) {
3949 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
3950 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
3951 }
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02003952 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
3953 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003954#else
3955 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003956#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02003957 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003958#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003959 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003960}
3961
William Lallemand4f45bb92017-10-30 20:08:51 +01003962
3963static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
3964{
3965 if (first == block) {
3966 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
3967 if (first->len > 0)
3968 sh_ssl_sess_tree_delete(sh_ssl_sess);
3969 }
3970}
3971
3972/* return first block from sh_ssl_sess */
3973static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
3974{
3975 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
3976
3977}
3978
3979/* store a session into the cache
3980 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
3981 * data: asn1 encoded session
3982 * data_len: asn1 encoded session length
3983 * Returns 1 id session was stored (else 0)
3984 */
3985static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
3986{
3987 struct shared_block *first;
3988 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
3989
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02003990 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01003991 if (!first) {
3992 /* Could not retrieve enough free blocks to store that session */
3993 return 0;
3994 }
3995
3996 /* STORE the key in the first elem */
3997 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
3998 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
3999 first->len = sizeof(struct sh_ssl_sess_hdr);
4000
4001 /* it returns the already existing node
4002 or current node if none, never returns null */
4003 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4004 if (oldsh_ssl_sess != sh_ssl_sess) {
4005 /* NOTE: Row couldn't be in use because we lock read & write function */
4006 /* release the reserved row */
4007 shctx_row_dec_hot(ssl_shctx, first);
4008 /* replace the previous session already in the tree */
4009 sh_ssl_sess = oldsh_ssl_sess;
4010 /* ignore the previous session data, only use the header */
4011 first = sh_ssl_sess_first_block(sh_ssl_sess);
4012 shctx_row_inc_hot(ssl_shctx, first);
4013 first->len = sizeof(struct sh_ssl_sess_hdr);
4014 }
4015
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004016 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004017 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004018 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004019 }
4020
4021 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004022
4023 return 1;
4024}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004025
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004026/* SSL callback used when a new session is created while connecting to a server */
4027static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4028{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004029 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004030 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004031
Willy Tarreau07d94e42018-09-20 10:57:52 +02004032 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004033
Olivier Houcharde6060c52017-11-16 17:42:52 +01004034 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4035 int len;
4036 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004037
Olivier Houcharde6060c52017-11-16 17:42:52 +01004038 len = i2d_SSL_SESSION(sess, NULL);
4039 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4040 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4041 } else {
4042 free(s->ssl_ctx.reused_sess[tid].ptr);
4043 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4044 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4045 }
4046 if (s->ssl_ctx.reused_sess[tid].ptr) {
4047 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4048 &ptr);
4049 }
4050 } else {
4051 free(s->ssl_ctx.reused_sess[tid].ptr);
4052 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4053 }
4054
4055 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004056}
4057
Olivier Houcharde6060c52017-11-16 17:42:52 +01004058
William Lallemanded0b5ad2017-10-30 19:36:36 +01004059/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004060int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004061{
4062 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4063 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4064 unsigned char *p;
4065 int data_len;
4066 unsigned int sid_length, sid_ctx_length;
4067 const unsigned char *sid_data;
4068 const unsigned char *sid_ctx_data;
4069
4070 /* Session id is already stored in to key and session id is known
4071 * so we dont store it to keep size.
4072 */
4073
4074 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4075 sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length);
4076 SSL_SESSION_set1_id(sess, sid_data, 0);
4077 SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0);
4078
4079 /* check if buffer is large enough for the ASN1 encoded session */
4080 data_len = i2d_SSL_SESSION(sess, NULL);
4081 if (data_len > SHSESS_MAX_DATA_LEN)
4082 goto err;
4083
4084 p = encsess;
4085
4086 /* process ASN1 session encoding before the lock */
4087 i2d_SSL_SESSION(sess, &p);
4088
4089 memcpy(encid, sid_data, sid_length);
4090 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4091 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4092
William Lallemanda3c77cf2017-10-30 23:44:40 +01004093 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004094 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004095 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004096 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004097err:
4098 /* reset original length values */
4099 SSL_SESSION_set1_id(sess, sid_data, sid_length);
4100 SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length);
4101
4102 return 0; /* do not increment session reference count */
4103}
4104
4105/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004106SSL_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 +01004107{
William Lallemand4f45bb92017-10-30 20:08:51 +01004108 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004109 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4110 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004111 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004112 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004113
4114 global.shctx_lookups++;
4115
4116 /* allow the session to be freed automatically by openssl */
4117 *do_copy = 0;
4118
4119 /* tree key is zeros padded sessionid */
4120 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4121 memcpy(tmpkey, key, key_len);
4122 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4123 key = tmpkey;
4124 }
4125
4126 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004127 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004128
4129 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004130 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4131 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004132 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004133 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004134 global.shctx_misses++;
4135 return NULL;
4136 }
4137
William Lallemand4f45bb92017-10-30 20:08:51 +01004138 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4139 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004140
William Lallemand4f45bb92017-10-30 20:08:51 +01004141 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 +01004142
William Lallemanda3c77cf2017-10-30 23:44:40 +01004143 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004144
4145 /* decode ASN1 session */
4146 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004147 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004148 /* Reset session id and session id contenxt */
4149 if (sess) {
4150 SSL_SESSION_set1_id(sess, key, key_len);
4151 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4152 }
4153
4154 return sess;
4155}
4156
William Lallemand4f45bb92017-10-30 20:08:51 +01004157
William Lallemanded0b5ad2017-10-30 19:36:36 +01004158/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004159void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004160{
William Lallemand4f45bb92017-10-30 20:08:51 +01004161 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004162 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4163 unsigned int sid_length;
4164 const unsigned char *sid_data;
4165 (void)ctx;
4166
4167 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4168 /* tree key is zeros padded sessionid */
4169 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4170 memcpy(tmpkey, sid_data, sid_length);
4171 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4172 sid_data = tmpkey;
4173 }
4174
William Lallemanda3c77cf2017-10-30 23:44:40 +01004175 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004176
4177 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004178 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4179 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004180 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004181 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004182 }
4183
4184 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004185 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004186}
4187
4188/* Set session cache mode to server and disable openssl internal cache.
4189 * Set shared cache callbacks on an ssl context.
4190 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004191void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004192{
4193 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4194
4195 if (!ssl_shctx) {
4196 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4197 return;
4198 }
4199
4200 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4201 SSL_SESS_CACHE_NO_INTERNAL |
4202 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4203
4204 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004205 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4206 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4207 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004208}
4209
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004210int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx)
4211{
4212 struct proxy *curproxy = bind_conf->frontend;
4213 int cfgerr = 0;
4214 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004215 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004216 const char *conf_ciphers;
Willy Tarreau5db847a2019-05-09 14:13:35 +02004217#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004218 const char *conf_ciphersuites;
4219#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004220 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004221
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004222 if (ssl_conf) {
4223 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4224 int i, min, max;
4225 int flags = MC_SSL_O_ALL;
4226
4227 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004228 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4229 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004230 if (min)
4231 flags |= (methodVersions[min].flag - 1);
4232 if (max)
4233 flags |= ~((methodVersions[max].flag << 1) - 1);
4234 min = max = CONF_TLSV_NONE;
4235 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4236 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4237 if (min)
4238 max = i;
4239 else
4240 min = max = i;
4241 }
4242 /* save real min/max */
4243 conf_ssl_methods->min = min;
4244 conf_ssl_methods->max = max;
4245 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004246 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4247 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004248 cfgerr += 1;
4249 }
4250 }
4251
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004252 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004253 case SSL_SOCK_VERIFY_NONE:
4254 verify = SSL_VERIFY_NONE;
4255 break;
4256 case SSL_SOCK_VERIFY_OPTIONAL:
4257 verify = SSL_VERIFY_PEER;
4258 break;
4259 case SSL_SOCK_VERIFY_REQUIRED:
4260 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4261 break;
4262 }
4263 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4264 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004265 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
4266 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
4267 if (ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004268 /* load CAfile to verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004269 if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004270 ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
4271 curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004272 cfgerr++;
4273 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004274 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
4275 /* set CA names for client cert request, function returns void */
4276 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file));
4277 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004278 }
Emeric Brun850efd52014-01-29 12:24:34 +01004279 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004280 ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4281 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun850efd52014-01-29 12:24:34 +01004282 cfgerr++;
4283 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004284#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004285 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004286 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4287
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004288 if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004289 ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4290 curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004291 cfgerr++;
4292 }
Emeric Brun561e5742012-10-02 15:20:55 +02004293 else {
4294 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4295 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004296 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004297#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004298 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004299 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004300#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004301 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004302 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004303 ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4304 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004305 cfgerr++;
4306 }
4307 }
4308#endif
4309
William Lallemand4f45bb92017-10-30 20:08:51 +01004310 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004311 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4312 if (conf_ciphers &&
4313 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004314 ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4315 curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004316 cfgerr++;
4317 }
4318
Willy Tarreau5db847a2019-05-09 14:13:35 +02004319#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004320 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4321 if (conf_ciphersuites &&
4322 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
4323 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4324 curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
4325 cfgerr++;
4326 }
4327#endif
4328
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004329#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004330 /* If tune.ssl.default-dh-param has not been set,
4331 neither has ssl-default-dh-file and no static DH
4332 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004333 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004334 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004335 (ssl_dh_ptr_index == -1 ||
4336 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004337 STACK_OF(SSL_CIPHER) * ciphers = NULL;
4338 const SSL_CIPHER * cipher = NULL;
4339 char cipher_description[128];
4340 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
4341 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
4342 which is not ephemeral DH. */
4343 const char dhe_description[] = " Kx=DH ";
4344 const char dhe_export_description[] = " Kx=DH(";
4345 int idx = 0;
4346 int dhe_found = 0;
4347 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004348
Remi Gacogne23d5d372014-10-10 17:04:26 +02004349 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004350
Remi Gacogne23d5d372014-10-10 17:04:26 +02004351 if (ssl) {
4352 ciphers = SSL_get_ciphers(ssl);
4353
4354 if (ciphers) {
4355 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
4356 cipher = sk_SSL_CIPHER_value(ciphers, idx);
4357 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
4358 if (strstr(cipher_description, dhe_description) != NULL ||
4359 strstr(cipher_description, dhe_export_description) != NULL) {
4360 dhe_found = 1;
4361 break;
4362 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02004363 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004364 }
4365 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02004366 SSL_free(ssl);
4367 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004368 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004369
Lukas Tribus90132722014-08-18 00:56:33 +02004370 if (dhe_found) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004371 ha_warning("Setting 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");
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004372 }
4373
Willy Tarreauef934602016-12-22 23:12:01 +01004374 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004375 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004376
Willy Tarreauef934602016-12-22 23:12:01 +01004377 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004378 if (local_dh_1024 == NULL) {
4379 local_dh_1024 = ssl_get_dh_1024();
4380 }
Willy Tarreauef934602016-12-22 23:12:01 +01004381 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004382 if (local_dh_2048 == NULL) {
4383 local_dh_2048 = ssl_get_dh_2048();
4384 }
Willy Tarreauef934602016-12-22 23:12:01 +01004385 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004386 if (local_dh_4096 == NULL) {
4387 local_dh_4096 = ssl_get_dh_4096();
4388 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004389 }
4390 }
4391 }
4392#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004393
Emeric Brunfc0421f2012-09-07 17:30:07 +02004394 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004395#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004396 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004397#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004398
Bernard Spil13c53f82018-02-15 13:34:58 +01004399#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004400 ssl_conf_cur = NULL;
4401 if (ssl_conf && ssl_conf->npn_str)
4402 ssl_conf_cur = ssl_conf;
4403 else if (bind_conf->ssl_conf.npn_str)
4404 ssl_conf_cur = &bind_conf->ssl_conf;
4405 if (ssl_conf_cur)
4406 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004407#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004408#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004409 ssl_conf_cur = NULL;
4410 if (ssl_conf && ssl_conf->alpn_str)
4411 ssl_conf_cur = ssl_conf;
4412 else if (bind_conf->ssl_conf.alpn_str)
4413 ssl_conf_cur = &bind_conf->ssl_conf;
4414 if (ssl_conf_cur)
4415 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004416#endif
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004417#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004418 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4419 if (conf_curves) {
4420 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004421 ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4422 curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004423 cfgerr++;
4424 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004425#if defined(SSL_CTX_set_ecdh_auto)
4426 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
4427#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004428 }
4429#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004430#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004431 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02004432 int i;
4433 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004434#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004435 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004436 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4437 NULL);
4438
4439 if (ecdhe == NULL) {
4440 SSL_CTX_set_dh_auto(ctx, 1);
4441 return cfgerr;
4442 }
4443#else
4444 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4445 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4446 ECDHE_DEFAULT_CURVE);
4447#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004448
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004449 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004450 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004451 ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4452 curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02004453 cfgerr++;
4454 }
4455 else {
4456 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4457 EC_KEY_free(ecdh);
4458 }
4459 }
4460#endif
4461
Emeric Brunfc0421f2012-09-07 17:30:07 +02004462 return cfgerr;
4463}
4464
Evan Broderbe554312013-06-27 00:05:25 -07004465static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4466{
4467 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4468 size_t prefixlen, suffixlen;
4469
4470 /* Trivial case */
4471 if (strcmp(pattern, hostname) == 0)
4472 return 1;
4473
Evan Broderbe554312013-06-27 00:05:25 -07004474 /* The rest of this logic is based on RFC 6125, section 6.4.3
4475 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4476
Emeric Bruna848dae2013-10-08 11:27:28 +02004477 pattern_wildcard = NULL;
4478 pattern_left_label_end = pattern;
4479 while (*pattern_left_label_end != '.') {
4480 switch (*pattern_left_label_end) {
4481 case 0:
4482 /* End of label not found */
4483 return 0;
4484 case '*':
4485 /* If there is more than one wildcards */
4486 if (pattern_wildcard)
4487 return 0;
4488 pattern_wildcard = pattern_left_label_end;
4489 break;
4490 }
4491 pattern_left_label_end++;
4492 }
4493
4494 /* If it's not trivial and there is no wildcard, it can't
4495 * match */
4496 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004497 return 0;
4498
4499 /* Make sure all labels match except the leftmost */
4500 hostname_left_label_end = strchr(hostname, '.');
4501 if (!hostname_left_label_end
4502 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
4503 return 0;
4504
4505 /* Make sure the leftmost label of the hostname is long enough
4506 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004507 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004508 return 0;
4509
4510 /* Finally compare the string on either side of the
4511 * wildcard */
4512 prefixlen = pattern_wildcard - pattern;
4513 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02004514 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
4515 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004516 return 0;
4517
4518 return 1;
4519}
4520
4521static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4522{
4523 SSL *ssl;
4524 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004525 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004526 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004527 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004528
4529 int depth;
4530 X509 *cert;
4531 STACK_OF(GENERAL_NAME) *alt_names;
4532 int i;
4533 X509_NAME *cert_subject;
4534 char *str;
4535
4536 if (ok == 0)
4537 return ok;
4538
4539 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004540 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004541 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07004542
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004543 /* We're checking if the provided hostnames match the desired one. The
4544 * desired hostname comes from the SNI we presented if any, or if not
4545 * provided then it may have been explicitly stated using a "verifyhost"
4546 * directive. If neither is set, we don't care about the name so the
4547 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004548 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004549 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004550 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004551 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004552 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004553 if (!servername)
4554 return ok;
4555 }
Evan Broderbe554312013-06-27 00:05:25 -07004556
4557 /* We only need to verify the CN on the actual server cert,
4558 * not the indirect CAs */
4559 depth = X509_STORE_CTX_get_error_depth(ctx);
4560 if (depth != 0)
4561 return ok;
4562
4563 /* At this point, the cert is *not* OK unless we can find a
4564 * hostname match */
4565 ok = 0;
4566
4567 cert = X509_STORE_CTX_get_current_cert(ctx);
4568 /* It seems like this might happen if verify peer isn't set */
4569 if (!cert)
4570 return ok;
4571
4572 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4573 if (alt_names) {
4574 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4575 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4576 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004577#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004578 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4579#else
Evan Broderbe554312013-06-27 00:05:25 -07004580 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004581#endif
Evan Broderbe554312013-06-27 00:05:25 -07004582 ok = ssl_sock_srv_hostcheck(str, servername);
4583 OPENSSL_free(str);
4584 }
4585 }
4586 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004587 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004588 }
4589
4590 cert_subject = X509_get_subject_name(cert);
4591 i = -1;
4592 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4593 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004594 ASN1_STRING *value;
4595 value = X509_NAME_ENTRY_get_data(entry);
4596 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004597 ok = ssl_sock_srv_hostcheck(str, servername);
4598 OPENSSL_free(str);
4599 }
4600 }
4601
Willy Tarreau71d058c2017-07-26 20:09:56 +02004602 /* report the mismatch and indicate if SNI was used or not */
4603 if (!ok && !conn->err_code)
4604 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004605 return ok;
4606}
4607
Emeric Brun94324a42012-10-11 14:00:19 +02004608/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004609int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004610{
Willy Tarreau03209342016-12-22 17:08:28 +01004611 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004612 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004613 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004614 SSL_OP_ALL | /* all known workarounds for bugs */
4615 SSL_OP_NO_SSLv2 |
4616 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004617 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004618 SSL_MODE_ENABLE_PARTIAL_WRITE |
4619 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004620 SSL_MODE_RELEASE_BUFFERS |
4621 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004622 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004623 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004624 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004625 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004626 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004627
Thierry Fournier383085f2013-01-24 14:15:43 +01004628 /* Make sure openssl opens /dev/urandom before the chroot */
4629 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004630 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01004631 cfgerr++;
4632 }
4633
Willy Tarreaufce03112015-01-15 21:32:40 +01004634 /* Automatic memory computations need to know we use SSL there */
4635 global.ssl_used_backend = 1;
4636
4637 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004638 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004639 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004640 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
4641 curproxy->id, srv->id,
4642 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004643 cfgerr++;
4644 return cfgerr;
4645 }
4646 }
Emeric Brun94324a42012-10-11 14:00:19 +02004647 if (srv->use_ssl)
4648 srv->xprt = &ssl_sock;
4649 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01004650 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004651
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004652 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004653 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004654 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4655 proxy_type_str(curproxy), curproxy->id,
4656 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02004657 cfgerr++;
4658 return cfgerr;
4659 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004660
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004661 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004662 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4663 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4664 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004665 else
4666 flags = conf_ssl_methods->flags;
4667
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004668 /* Real min and max should be determinate with configuration and openssl's capabilities */
4669 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004670 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004671 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004672 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004673
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004674 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004675 min = max = CONF_TLSV_NONE;
4676 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004677 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004678 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004679 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004680 if (min) {
4681 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004682 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
4683 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4684 proxy_type_str(curproxy), curproxy->id, srv->id,
4685 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004686 hole = 0;
4687 }
4688 max = i;
4689 }
4690 else {
4691 min = max = i;
4692 }
4693 }
4694 else {
4695 if (min)
4696 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004697 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004698 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004699 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4700 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004701 cfgerr += 1;
4702 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004703
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004704#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004705 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004706 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004707 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004708 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004709 else
4710 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4711 if (flags & methodVersions[i].flag)
4712 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004713#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004714 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004715 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4716 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004717#endif
4718
4719 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4720 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004721 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004722
Willy Tarreau5db847a2019-05-09 14:13:35 +02004723#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004724 if (global_ssl.async)
4725 mode |= SSL_MODE_ASYNC;
4726#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004727 SSL_CTX_set_mode(ctx, mode);
4728 srv->ssl_ctx.ctx = ctx;
4729
Emeric Bruna7aa3092012-10-26 12:58:00 +02004730 if (srv->ssl_ctx.client_crt) {
4731 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 +01004732 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4733 proxy_type_str(curproxy), curproxy->id,
4734 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004735 cfgerr++;
4736 }
4737 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 +01004738 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4739 proxy_type_str(curproxy), curproxy->id,
4740 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004741 cfgerr++;
4742 }
4743 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004744 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4745 proxy_type_str(curproxy), curproxy->id,
4746 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004747 cfgerr++;
4748 }
4749 }
Emeric Brun94324a42012-10-11 14:00:19 +02004750
Emeric Brun850efd52014-01-29 12:24:34 +01004751 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4752 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004753 switch (srv->ssl_ctx.verify) {
4754 case SSL_SOCK_VERIFY_NONE:
4755 verify = SSL_VERIFY_NONE;
4756 break;
4757 case SSL_SOCK_VERIFY_REQUIRED:
4758 verify = SSL_VERIFY_PEER;
4759 break;
4760 }
Evan Broderbe554312013-06-27 00:05:25 -07004761 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004762 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004763 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004764 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004765 if (srv->ssl_ctx.ca_file) {
4766 /* load CAfile to verify */
4767 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004768 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
4769 curproxy->id, srv->id,
4770 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004771 cfgerr++;
4772 }
4773 }
Emeric Brun850efd52014-01-29 12:24:34 +01004774 else {
4775 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01004776 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",
4777 curproxy->id, srv->id,
4778 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004779 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01004780 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
4781 curproxy->id, srv->id,
4782 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004783 cfgerr++;
4784 }
Emeric Brunef42d922012-10-11 16:11:36 +02004785#ifdef X509_V_FLAG_CRL_CHECK
4786 if (srv->ssl_ctx.crl_file) {
4787 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4788
4789 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004790 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
4791 curproxy->id, srv->id,
4792 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004793 cfgerr++;
4794 }
4795 else {
4796 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4797 }
4798 }
4799#endif
4800 }
4801
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004802 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
4803 SSL_SESS_CACHE_NO_INTERNAL_STORE);
4804 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02004805 if (srv->ssl_ctx.ciphers &&
4806 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004807 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4808 curproxy->id, srv->id,
4809 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02004810 cfgerr++;
4811 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004812
Willy Tarreau5db847a2019-05-09 14:13:35 +02004813#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004814 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00004815 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004816 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
4817 curproxy->id, srv->id,
4818 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
4819 cfgerr++;
4820 }
4821#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01004822#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4823 if (srv->ssl_ctx.npn_str)
4824 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
4825#endif
4826#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4827 if (srv->ssl_ctx.alpn_str)
4828 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
4829#endif
4830
Emeric Brun94324a42012-10-11 14:00:19 +02004831
4832 return cfgerr;
4833}
4834
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004835/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004836 * be NULL, in which case nothing is done. Returns the number of errors
4837 * encountered.
4838 */
Willy Tarreau03209342016-12-22 17:08:28 +01004839int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004840{
4841 struct ebmb_node *node;
4842 struct sni_ctx *sni;
4843 int err = 0;
4844
Willy Tarreaufce03112015-01-15 21:32:40 +01004845 /* Automatic memory computations need to know we use SSL there */
4846 global.ssl_used_frontend = 1;
4847
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004848 /* Make sure openssl opens /dev/urandom before the chroot */
4849 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004850 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004851 err++;
4852 }
4853 /* Create initial_ctx used to start the ssl connection before do switchctx */
4854 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004855 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004856 /* It should not be necessary to call this function, but it's
4857 necessary first to check and move all initialisation related
4858 to initial_ctx in ssl_sock_initial_ctx. */
4859 err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx);
4860 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004861 if (bind_conf->default_ctx)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004862 err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx);
Emeric Brun0bed9942014-10-30 19:25:24 +01004863
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004864 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004865 while (node) {
4866 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004867 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4868 /* only initialize the CTX on its first occurrence and
4869 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004870 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004871 node = ebmb_next(node);
4872 }
4873
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004874 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004875 while (node) {
4876 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004877 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4878 /* only initialize the CTX on its first occurrence and
4879 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004880 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004881 node = ebmb_next(node);
4882 }
4883 return err;
4884}
4885
Willy Tarreau55d37912016-12-21 23:38:39 +01004886/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4887 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4888 * alerts are directly emitted since the rest of the stack does it below.
4889 */
4890int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4891{
4892 struct proxy *px = bind_conf->frontend;
4893 int alloc_ctx;
4894 int err;
4895
4896 if (!bind_conf->is_ssl) {
4897 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004898 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
4899 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01004900 }
4901 return 0;
4902 }
4903 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004904 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004905 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
4906 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004907 }
4908 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004909 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
4910 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004911 return -1;
4912 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004913 }
William Lallemandc61c0b32017-12-04 18:46:39 +01004914 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004915 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02004916 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01004917 sizeof(*sh_ssl_sess_tree),
4918 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02004919 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004920 if (alloc_ctx == SHCTX_E_INIT_LOCK)
4921 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");
4922 else
4923 ha_alert("Unable to allocate SSL session cache.\n");
4924 return -1;
4925 }
4926 /* free block callback */
4927 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
4928 /* init the root tree within the extra space */
4929 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
4930 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01004931 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004932 err = 0;
4933 /* initialize all certificate contexts */
4934 err += ssl_sock_prepare_all_ctx(bind_conf);
4935
4936 /* initialize CA variables if the certificates generation is enabled */
4937 err += ssl_sock_load_ca(bind_conf);
4938
4939 return -err;
4940}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004941
4942/* release ssl context allocated for servers. */
4943void ssl_sock_free_srv_ctx(struct server *srv)
4944{
Olivier Houchardc7566002018-11-20 23:33:50 +01004945#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4946 if (srv->ssl_ctx.alpn_str)
4947 free(srv->ssl_ctx.alpn_str);
4948#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01004949#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01004950 if (srv->ssl_ctx.npn_str)
4951 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01004952#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004953 if (srv->ssl_ctx.ctx)
4954 SSL_CTX_free(srv->ssl_ctx.ctx);
4955}
4956
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004957/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004958 * be NULL, in which case nothing is done. The default_ctx is nullified too.
4959 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004960void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004961{
4962 struct ebmb_node *node, *back;
4963 struct sni_ctx *sni;
4964
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004965 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004966 while (node) {
4967 sni = ebmb_entry(node, struct sni_ctx, name);
4968 back = ebmb_next(node);
4969 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004970 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02004971 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004972 ssl_sock_free_ssl_conf(sni->conf);
4973 free(sni->conf);
4974 sni->conf = NULL;
4975 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004976 free(sni);
4977 node = back;
4978 }
4979
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004980 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004981 while (node) {
4982 sni = ebmb_entry(node, struct sni_ctx, name);
4983 back = ebmb_next(node);
4984 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004985 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02004986 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004987 ssl_sock_free_ssl_conf(sni->conf);
4988 free(sni->conf);
4989 sni->conf = NULL;
4990 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004991 free(sni);
4992 node = back;
4993 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004994 SSL_CTX_free(bind_conf->initial_ctx);
4995 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004996 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004997 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02004998}
4999
Willy Tarreau795cdab2016-12-22 17:30:54 +01005000/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5001void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5002{
5003 ssl_sock_free_ca(bind_conf);
5004 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005005 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005006 free(bind_conf->ca_sign_file);
5007 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005008 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005009 free(bind_conf->keys_ref->filename);
5010 free(bind_conf->keys_ref->tlskeys);
5011 LIST_DEL(&bind_conf->keys_ref->list);
5012 free(bind_conf->keys_ref);
5013 }
5014 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005015 bind_conf->ca_sign_pass = NULL;
5016 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005017}
5018
Christopher Faulet31af49d2015-06-09 17:29:50 +02005019/* Load CA cert file and private key used to generate certificates */
5020int
Willy Tarreau03209342016-12-22 17:08:28 +01005021ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005022{
Willy Tarreau03209342016-12-22 17:08:28 +01005023 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005024 FILE *fp;
5025 X509 *cacert = NULL;
5026 EVP_PKEY *capkey = NULL;
5027 int err = 0;
5028
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005029 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005030 return err;
5031
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005032#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005033 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005034 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005035 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005036 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005037 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005038#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005039
Christopher Faulet31af49d2015-06-09 17:29:50 +02005040 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005041 ha_alert("Proxy '%s': cannot enable certificate generation, "
5042 "no CA certificate File configured at [%s:%d].\n",
5043 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005044 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005045 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005046
5047 /* read in the CA certificate */
5048 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005049 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5050 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005051 goto load_error;
5052 }
5053 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005054 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5055 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005056 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005057 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005058 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005059 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005060 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5061 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005062 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005063 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005064
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005065 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005066 bind_conf->ca_sign_cert = cacert;
5067 bind_conf->ca_sign_pkey = capkey;
5068 return err;
5069
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005070 read_error:
5071 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005072 if (capkey) EVP_PKEY_free(capkey);
5073 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005074 load_error:
5075 bind_conf->generate_certs = 0;
5076 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005077 return err;
5078}
5079
5080/* Release CA cert and private key used to generate certificated */
5081void
5082ssl_sock_free_ca(struct bind_conf *bind_conf)
5083{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005084 if (bind_conf->ca_sign_pkey)
5085 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5086 if (bind_conf->ca_sign_cert)
5087 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005088 bind_conf->ca_sign_pkey = NULL;
5089 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005090}
5091
Emeric Brun46591952012-05-18 15:47:34 +02005092/*
5093 * This function is called if SSL * context is not yet allocated. The function
5094 * is designed to be called before any other data-layer operation and sets the
5095 * handshake flag on the connection. It is safe to call it multiple times.
5096 * It returns 0 on success and -1 in error case.
5097 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005098static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005099{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005100 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005101 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005102 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005103 return 0;
5104
Willy Tarreau3c728722014-01-23 13:50:42 +01005105 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005106 return 0;
5107
Olivier Houchard66ab4982019-02-26 18:37:15 +01005108 ctx = pool_alloc(ssl_sock_ctx_pool);
5109 if (!ctx) {
5110 conn->err_code = CO_ER_SSL_NO_MEM;
5111 return -1;
5112 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005113 ctx->sent_early_data = 0;
5114 ctx->tmp_early_data = -1;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005115 ctx->conn = conn;
5116
5117 /* Only work with sockets for now, this should be adapted when we'll
5118 * add QUIC support.
5119 */
5120 ctx->xprt = xprt_get(XPRT_RAW);
5121 if (ctx->xprt->init)
5122 ctx->xprt->init(conn, &ctx->xprt_ctx);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005123
Willy Tarreau20879a02012-12-03 16:32:10 +01005124 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5125 conn->err_code = CO_ER_SSL_TOO_MANY;
Willy Tarreau403edff2012-09-06 11:58:37 +02005126 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01005127 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005128
Emeric Brun46591952012-05-18 15:47:34 +02005129 /* If it is in client mode initiate SSL session
5130 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005131 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005132 int may_retry = 1;
5133
5134 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005135 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005136 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5137 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005138 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005139 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005140 goto retry_connect;
5141 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005142 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005143 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005144 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005145 ctx->bio = BIO_new(ha_meth);
5146 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005147 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005148 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005149 goto retry_connect;
5150 }
Emeric Brun55476152014-11-12 17:35:37 +01005151 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005152 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005153 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005154 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005155 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005156
Evan Broderbe554312013-06-27 00:05:25 -07005157 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005158 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5159 SSL_free(ctx->ssl);
5160 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005161 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005162 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005163 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005164 goto retry_connect;
5165 }
Emeric Brun55476152014-11-12 17:35:37 +01005166 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005167 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005168 }
5169
Olivier Houchard66ab4982019-02-26 18:37:15 +01005170 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005171 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5172 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5173 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 +01005174 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005175 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005176 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5177 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005178 } else if (sess) {
5179 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005180 }
5181 }
Evan Broderbe554312013-06-27 00:05:25 -07005182
Emeric Brun46591952012-05-18 15:47:34 +02005183 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005184 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005185
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005186 _HA_ATOMIC_ADD(&sslconns, 1);
5187 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005188 *xprt_ctx = ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005189 return 0;
5190 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005191 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005192 int may_retry = 1;
5193
5194 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005195 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005196 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5197 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005198 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005199 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005200 goto retry_accept;
5201 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005202 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005203 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005204 }
Emeric Brun46591952012-05-18 15:47:34 +02005205
Olivier Houcharda8955d52019-04-07 22:00:38 +02005206 ctx->bio = BIO_new(ha_meth);
5207 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005208 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005209 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005210 goto retry_accept;
5211 }
Emeric Brun55476152014-11-12 17:35:37 +01005212 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005213 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005214 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005215 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005216 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005217
Emeric Brune1f38db2012-09-03 20:36:47 +02005218 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005219 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5220 SSL_free(ctx->ssl);
5221 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005222 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005223 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005224 goto retry_accept;
5225 }
Emeric Brun55476152014-11-12 17:35:37 +01005226 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005227 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005228 }
5229
Olivier Houchard66ab4982019-02-26 18:37:15 +01005230 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005231
Emeric Brun46591952012-05-18 15:47:34 +02005232 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005233 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005234#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005235 conn->flags |= CO_FL_EARLY_SSL_HS;
5236#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005237
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005238 _HA_ATOMIC_ADD(&sslconns, 1);
5239 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005240 *xprt_ctx = ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005241 return 0;
5242 }
5243 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005244 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005245err:
5246 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005247 return -1;
5248}
5249
5250
5251/* This is the callback which is used when an SSL handshake is pending. It
5252 * updates the FD status if it wants some polling before being called again.
5253 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5254 * otherwise it returns non-zero and removes itself from the connection's
5255 * flags (the bit is provided in <flag> by the caller).
5256 */
5257int ssl_sock_handshake(struct connection *conn, unsigned int flag)
5258{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005259 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005260 int ret;
5261
Willy Tarreau3c728722014-01-23 13:50:42 +01005262 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005263 return 0;
5264
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005265 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005266 goto out_error;
5267
Willy Tarreau5db847a2019-05-09 14:13:35 +02005268#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005269 /*
5270 * Check if we have early data. If we do, we have to read them
5271 * before SSL_do_handshake() is called, And there's no way to
5272 * detect early data, except to try to read them
5273 */
5274 if (conn->flags & CO_FL_EARLY_SSL_HS) {
5275 size_t read_data;
5276
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005277 ret = SSL_read_early_data(ctx->ssl, &ctx->tmp_early_data,
Olivier Houchardc2aae742017-09-22 18:26:28 +02005278 1, &read_data);
5279 if (ret == SSL_READ_EARLY_DATA_ERROR)
5280 goto check_error;
5281 if (ret == SSL_READ_EARLY_DATA_SUCCESS) {
5282 conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN);
5283 return 1;
5284 } else
5285 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5286 }
5287#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005288 /* If we use SSL_do_handshake to process a reneg initiated by
5289 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5290 * Usually SSL_write and SSL_read are used and process implicitly
5291 * the reneg handshake.
5292 * Here we use SSL_peek as a workaround for reneg.
5293 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005294 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005295 char c;
5296
Olivier Houchard66ab4982019-02-26 18:37:15 +01005297 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005298 if (ret <= 0) {
5299 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005300 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005301
Emeric Brun674b7432012-11-08 19:21:55 +01005302 if (ret == SSL_ERROR_WANT_WRITE) {
5303 /* SSL handshake needs to write, L4 connection may not be ready */
5304 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01005305 __conn_sock_want_send(conn);
Willy Tarreau585744b2017-08-24 14:31:19 +02005306 fd_cant_send(conn->handle.fd);
Emeric Brun674b7432012-11-08 19:21:55 +01005307 return 0;
5308 }
5309 else if (ret == SSL_ERROR_WANT_READ) {
5310 /* handshake may have been completed but we have
5311 * no more data to read.
5312 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005313 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005314 ret = 1;
5315 goto reneg_ok;
5316 }
5317 /* SSL handshake needs to read, L4 connection is ready */
5318 if (conn->flags & CO_FL_WAIT_L4_CONN)
5319 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5320 __conn_sock_stop_send(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01005321 __conn_sock_want_recv(conn);
Willy Tarreau585744b2017-08-24 14:31:19 +02005322 fd_cant_recv(conn->handle.fd);
Emeric Brun674b7432012-11-08 19:21:55 +01005323 return 0;
5324 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005325#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005326 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005327 ssl_async_process_fds(conn, ctx->ssl);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005328 return 0;
5329 }
5330#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005331 else if (ret == SSL_ERROR_SYSCALL) {
5332 /* if errno is null, then connection was successfully established */
5333 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5334 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005335 if (!conn->err_code) {
Emeric Brun77e89192018-08-16 11:36:40 +02005336#ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005337 conn->err_code = CO_ER_SSL_HANDSHAKE;
5338#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005339 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005340#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005341 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005342 empty_handshake = state == TLS_ST_BEFORE;
5343#else
Ilya Shipitsin54832b92019-05-05 23:27:54 +05005344 empty_handshake = SSL_state((SSL *)ctx->ssl) == SSL_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005345#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005346 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005347 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005348 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005349 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5350 else
5351 conn->err_code = CO_ER_SSL_EMPTY;
5352 }
5353 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005354 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005355 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5356 else
5357 conn->err_code = CO_ER_SSL_ABORT;
5358 }
5359 }
5360 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005361 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005362 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005363 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005364 conn->err_code = CO_ER_SSL_HANDSHAKE;
5365 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005366#endif
Willy Tarreau20879a02012-12-03 16:32:10 +01005367 }
Emeric Brun674b7432012-11-08 19:21:55 +01005368 goto out_error;
5369 }
5370 else {
5371 /* Fail on all other handshake errors */
5372 /* Note: OpenSSL may leave unread bytes in the socket's
5373 * buffer, causing an RST to be emitted upon close() on
5374 * TCP sockets. We first try to drain possibly pending
5375 * data to avoid this as much as possible.
5376 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005377 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005378 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005379 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005380 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005381 goto out_error;
5382 }
5383 }
5384 /* read some data: consider handshake completed */
5385 goto reneg_ok;
5386 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005387 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005388check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005389 if (ret != 1) {
5390 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005391 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005392
5393 if (ret == SSL_ERROR_WANT_WRITE) {
5394 /* SSL handshake needs to write, L4 connection may not be ready */
5395 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01005396 __conn_sock_want_send(conn);
Willy Tarreau585744b2017-08-24 14:31:19 +02005397 fd_cant_send(conn->handle.fd);
Emeric Brun46591952012-05-18 15:47:34 +02005398 return 0;
5399 }
5400 else if (ret == SSL_ERROR_WANT_READ) {
5401 /* SSL handshake needs to read, L4 connection is ready */
5402 if (conn->flags & CO_FL_WAIT_L4_CONN)
5403 conn->flags &= ~CO_FL_WAIT_L4_CONN;
5404 __conn_sock_stop_send(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01005405 __conn_sock_want_recv(conn);
Willy Tarreau585744b2017-08-24 14:31:19 +02005406 fd_cant_recv(conn->handle.fd);
Emeric Brun46591952012-05-18 15:47:34 +02005407 return 0;
5408 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005409#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005410 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005411 ssl_async_process_fds(conn, ctx->ssl);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005412 return 0;
5413 }
5414#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005415 else if (ret == SSL_ERROR_SYSCALL) {
5416 /* if errno is null, then connection was successfully established */
5417 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5418 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005419 if (!conn->err_code) {
Emeric Brun77e89192018-08-16 11:36:40 +02005420#ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005421 conn->err_code = CO_ER_SSL_HANDSHAKE;
5422#else
5423 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005424#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005425 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005426 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005427#else
Ilya Shipitsin54832b92019-05-05 23:27:54 +05005428 empty_handshake = SSL_state((SSL *)ctx->ssl) == SSL_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005429#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005430 if (empty_handshake) {
5431 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005432 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005433 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5434 else
5435 conn->err_code = CO_ER_SSL_EMPTY;
5436 }
5437 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005438 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005439 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5440 else
5441 conn->err_code = CO_ER_SSL_ABORT;
5442 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005443 }
5444 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005445 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005446 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5447 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005448 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005449 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005450#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005451 }
Willy Tarreau89230192012-09-28 20:22:13 +02005452 goto out_error;
5453 }
Emeric Brun46591952012-05-18 15:47:34 +02005454 else {
5455 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005456 /* Note: OpenSSL may leave unread bytes in the socket's
5457 * buffer, causing an RST to be emitted upon close() on
5458 * TCP sockets. We first try to drain possibly pending
5459 * data to avoid this as much as possible.
5460 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005461 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005462 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005463 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005464 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005465 goto out_error;
5466 }
5467 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005468#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01005469 else {
5470 /*
5471 * If the server refused the early data, we have to send a
5472 * 425 to the client, as we no longer have the data to sent
5473 * them again.
5474 */
5475 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005476 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005477 conn->err_code = CO_ER_SSL_EARLY_FAILED;
5478 goto out_error;
5479 }
5480 }
5481 }
5482#endif
5483
Emeric Brun46591952012-05-18 15:47:34 +02005484
Emeric Brun674b7432012-11-08 19:21:55 +01005485reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00005486
Willy Tarreau5db847a2019-05-09 14:13:35 +02005487#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005488 /* ASYNC engine API doesn't support moving read/write
5489 * buffers. So we disable ASYNC mode right after
5490 * the handshake to avoid buffer oveflows.
5491 */
5492 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005493 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005494#endif
Emeric Brun46591952012-05-18 15:47:34 +02005495 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005496 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005497 if (objt_server(conn->target)) {
5498 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
5499 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
5500 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02005501 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005502 else {
5503 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
5504 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
5505 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
5506 }
Emeric Brun46591952012-05-18 15:47:34 +02005507 }
5508
Emmanuel Hocdetca6a9572017-11-23 12:40:07 +01005509#ifdef OPENSSL_IS_BORINGSSL
Olivier Houchard66ab4982019-02-26 18:37:15 +01005510 if ((conn->flags & CO_FL_EARLY_SSL_HS) && !SSL_in_early_data(ctx->ssl))
Emmanuel Hocdetca6a9572017-11-23 12:40:07 +01005511 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5512#endif
Emeric Brun46591952012-05-18 15:47:34 +02005513 /* The connection is now established at both layers, it's time to leave */
5514 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
5515 return 1;
5516
5517 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005518 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005519 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005520 ERR_clear_error();
5521
Emeric Brun9fa89732012-10-04 17:09:56 +02005522 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02005523 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5524 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5525 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02005526 }
5527
Emeric Brun46591952012-05-18 15:47:34 +02005528 /* Fail on all other handshake errors */
5529 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01005530 if (!conn->err_code)
5531 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005532 return 0;
5533}
5534
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005535static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005536{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005537
5538 return conn_subscribe(conn, NULL, event_type, param);
Olivier Houcharddf357842019-03-21 16:30:07 +01005539}
5540
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005541static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005542{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005543
5544 return conn_unsubscribe(conn, NULL, event_type, param);
Olivier Houcharddf357842019-03-21 16:30:07 +01005545}
5546
Emeric Brun46591952012-05-18 15:47:34 +02005547/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01005548 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02005549 * buffer wraps, in which case a second call may be performed. The connection's
5550 * flags are updated with whatever special event is detected (error, read0,
5551 * empty). The caller is responsible for taking care of those events and
5552 * avoiding the call if inappropriate. The function does not call the
5553 * connection's polling update function, so the caller is responsible for this.
5554 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005555static 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 +02005556{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005557 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02005558 ssize_t ret;
5559 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02005560
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005561 conn_refresh_polling_flags(conn);
5562
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005563 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005564 goto out_error;
5565
5566 if (conn->flags & CO_FL_HANDSHAKE)
5567 /* a handshake was requested */
5568 return 0;
5569
Emeric Brun46591952012-05-18 15:47:34 +02005570 /* read the largest possible block. For this, we perform only one call
5571 * to recv() unless the buffer wraps and we exactly fill the first hunk,
5572 * in which case we accept to do it once again. A new attempt is made on
5573 * EINTR too.
5574 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01005575 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005576 int need_out = 0;
5577
Willy Tarreau591d4452018-06-15 17:21:00 +02005578 try = b_contig_space(buf);
5579 if (!try)
5580 break;
5581
Willy Tarreauabf08d92014-01-14 11:31:27 +01005582 if (try > count)
5583 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02005584
Olivier Houchardc2aae742017-09-22 18:26:28 +02005585 if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005586 ctx->tmp_early_data != -1) {
5587 *b_tail(buf) = ctx->tmp_early_data;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005588 done++;
5589 try--;
5590 count--;
Olivier Houchardacd14032018-06-28 18:17:23 +02005591 b_add(buf, 1);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005592 ctx->tmp_early_data = -1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005593 continue;
5594 }
Willy Tarreauabf08d92014-01-14 11:31:27 +01005595
Willy Tarreau5db847a2019-05-09 14:13:35 +02005596#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005597 if (conn->flags & CO_FL_EARLY_SSL_HS) {
5598 size_t read_length;
5599
Olivier Houchard66ab4982019-02-26 18:37:15 +01005600 ret = SSL_read_early_data(ctx->ssl,
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02005601 b_tail(buf), try, &read_length);
Olivier Houchard522eea72017-11-03 16:27:47 +01005602 if (ret == SSL_READ_EARLY_DATA_SUCCESS &&
5603 read_length > 0)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005604 conn->flags |= CO_FL_EARLY_DATA;
5605 if (ret == SSL_READ_EARLY_DATA_SUCCESS ||
5606 ret == SSL_READ_EARLY_DATA_FINISH) {
5607 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5608 /*
5609 * We're done reading the early data,
5610 * let's make the handshake
5611 */
5612 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5613 conn->flags |= CO_FL_SSL_WAIT_HS;
5614 need_out = 1;
5615 if (read_length == 0)
5616 break;
5617 }
5618 ret = read_length;
5619 }
5620 } else
5621#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005622 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetca6a9572017-11-23 12:40:07 +01005623#ifdef OPENSSL_IS_BORINGSSL
5624 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005625 if (SSL_in_early_data(ctx->ssl)) {
Emmanuel Hocdetca6a9572017-11-23 12:40:07 +01005626 if (ret > 0)
5627 conn->flags |= CO_FL_EARLY_DATA;
5628 } else {
Emmanuel Hocdetcebd7962017-11-27 16:14:40 +01005629 conn->flags &= ~(CO_FL_EARLY_SSL_HS);
Emmanuel Hocdetca6a9572017-11-23 12:40:07 +01005630 }
5631 }
5632#endif
Emeric Brune1f38db2012-09-03 20:36:47 +02005633 if (conn->flags & CO_FL_ERROR) {
5634 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005635 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005636 }
Emeric Brun46591952012-05-18 15:47:34 +02005637 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02005638 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005639 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005640 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005641 }
Emeric Brun46591952012-05-18 15:47:34 +02005642 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005643 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005644 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005645 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02005646 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01005647 __conn_sock_want_send(conn);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005648#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005649 /* Async mode can be re-enabled, because we're leaving data state.*/
5650 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005651 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005652#endif
Emeric Brun46591952012-05-18 15:47:34 +02005653 break;
5654 }
5655 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005656 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01005657 /* handshake is running, and it may need to re-enable read */
5658 conn->flags |= CO_FL_SSL_WAIT_HS;
5659 __conn_sock_want_recv(conn);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005660#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005661 /* Async mode can be re-enabled, because we're leaving data state.*/
5662 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005663 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005664#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005665 break;
5666 }
Emeric Brun46591952012-05-18 15:47:34 +02005667 /* we need to poll for retry a read later */
Willy Tarreau585744b2017-08-24 14:31:19 +02005668 fd_cant_recv(conn->handle.fd);
Emeric Brun46591952012-05-18 15:47:34 +02005669 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005670 } else if (ret == SSL_ERROR_ZERO_RETURN)
5671 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005672 /* For SSL_ERROR_SYSCALL, make sure to clear the error
5673 * stack before shutting down the connection for
5674 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005675 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
5676 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02005677 /* otherwise it's a real error */
5678 goto out_error;
5679 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005680 if (need_out)
5681 break;
Emeric Brun46591952012-05-18 15:47:34 +02005682 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005683 leave:
5684 conn_cond_update_sock_polling(conn);
Emeric Brun46591952012-05-18 15:47:34 +02005685 return done;
5686
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005687 clear_ssl_error:
5688 /* Clear openssl global errors stack */
5689 ssl_sock_dump_errors(conn);
5690 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02005691 read0:
5692 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005693 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005694
Emeric Brun46591952012-05-18 15:47:34 +02005695 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005696 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01005697 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005698 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005699 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005700 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005701}
5702
5703
Willy Tarreau787db9a2018-06-14 18:31:46 +02005704/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5705 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5706 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02005707 * Only one call to send() is performed, unless the buffer wraps, in which case
5708 * a second call may be performed. The connection's flags are updated with
5709 * whatever special event is detected (error, empty). The caller is responsible
5710 * for taking care of those events and avoiding the call if inappropriate. The
5711 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02005712 * is responsible for this. The buffer's output is not adjusted, it's up to the
5713 * caller to take care of this. It's up to the caller to update the buffer's
5714 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02005715 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005716static 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 +02005717{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005718 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005719 ssize_t ret;
5720 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02005721
5722 done = 0;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005723 conn_refresh_polling_flags(conn);
Emeric Brun46591952012-05-18 15:47:34 +02005724
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005725 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005726 goto out_error;
5727
Olivier Houchard010941f2019-05-03 20:56:19 +02005728 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005729 /* a handshake was requested */
5730 return 0;
5731
5732 /* send the largest possible block. For this we perform only one call
5733 * to send() unless the buffer wraps and we exactly fill the first hunk,
5734 * in which case we accept to do it once again.
5735 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02005736 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02005737#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005738 size_t written_data;
5739#endif
5740
Willy Tarreau787db9a2018-06-14 18:31:46 +02005741 try = b_contig_data(buf, done);
5742 if (try > count)
5743 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01005744
Willy Tarreau7bed9452014-02-02 02:00:24 +01005745 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005746 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01005747 global_ssl.max_record && try > global_ssl.max_record) {
5748 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005749 }
5750 else {
5751 /* we need to keep the information about the fact that
5752 * we're not limiting the upcoming send(), because if it
5753 * fails, we'll have to retry with at least as many data.
5754 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005755 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005756 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01005757
Willy Tarreau5db847a2019-05-09 14:13:35 +02005758#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02005759 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005760 unsigned int max_early;
5761
Olivier Houchard522eea72017-11-03 16:27:47 +01005762 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01005763 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01005764 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005765 if (SSL_get0_session(ctx->ssl))
5766 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01005767 else
5768 max_early = 0;
5769 }
5770
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005771 if (try + ctx->sent_early_data > max_early) {
5772 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01005773 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02005774 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005775 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01005776 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005777 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005778 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005779 if (ret == 1) {
5780 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005781 ctx->sent_early_data += ret;
Olivier Houchard010941f2019-05-03 20:56:19 +02005782 if (objt_server(conn->target))
Olivier Houchard522eea72017-11-03 16:27:47 +01005783 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard522eea72017-11-03 16:27:47 +01005784
Olivier Houchardc2aae742017-09-22 18:26:28 +02005785 }
5786
5787 } else
5788#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005789 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01005790
Emeric Brune1f38db2012-09-03 20:36:47 +02005791 if (conn->flags & CO_FL_ERROR) {
5792 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005793 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005794 }
Emeric Brun46591952012-05-18 15:47:34 +02005795 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01005796 /* A send succeeded, so we can consier ourself connected */
5797 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005798 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005799 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005800 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005801 }
5802 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005803 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005804
Emeric Brun46591952012-05-18 15:47:34 +02005805 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005806 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01005807 /* handshake is running, and it may need to re-enable write */
5808 conn->flags |= CO_FL_SSL_WAIT_HS;
5809 __conn_sock_want_send(conn);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005810#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005811 /* Async mode can be re-enabled, because we're leaving data state.*/
5812 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005813 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005814#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005815 break;
5816 }
Emeric Brun46591952012-05-18 15:47:34 +02005817 /* we need to poll to retry a write later */
Willy Tarreau585744b2017-08-24 14:31:19 +02005818 fd_cant_send(conn->handle.fd);
Emeric Brun46591952012-05-18 15:47:34 +02005819 break;
5820 }
5821 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005822 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02005823 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01005824 __conn_sock_want_recv(conn);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005825#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005826 /* Async mode can be re-enabled, because we're leaving data state.*/
5827 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005828 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005829#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005830 break;
5831 }
Emeric Brun46591952012-05-18 15:47:34 +02005832 goto out_error;
5833 }
5834 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005835 leave:
5836 conn_cond_update_sock_polling(conn);
Emeric Brun46591952012-05-18 15:47:34 +02005837 return done;
5838
5839 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005840 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005841 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005842 ERR_clear_error();
5843
Emeric Brun46591952012-05-18 15:47:34 +02005844 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005845 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005846}
5847
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005848static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02005849
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005850 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005851
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005852 if (ctx) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02005853#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02005854 if (global_ssl.async) {
5855 OSSL_ASYNC_FD all_fd[32], afd;
5856 size_t num_all_fds = 0;
5857 int i;
5858
Olivier Houchard66ab4982019-02-26 18:37:15 +01005859 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02005860 if (num_all_fds > 32) {
5861 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
5862 return;
5863 }
5864
Olivier Houchard66ab4982019-02-26 18:37:15 +01005865 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02005866
5867 /* If an async job is pending, we must try to
5868 to catch the end using polling before calling
5869 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005870 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02005871 for (i=0 ; i < num_all_fds ; i++) {
5872 /* switch on an handler designed to
5873 * handle the SSL_free
5874 */
5875 afd = all_fd[i];
5876 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005877 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02005878 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00005879 /* To ensure that the fd cache won't be used
5880 * and we'll catch a real RD event.
5881 */
5882 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02005883 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005884 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005885 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005886 return;
5887 }
Emeric Brun3854e012017-05-17 20:42:48 +02005888 /* Else we can remove the fds from the fdtab
5889 * and call SSL_free.
5890 * note: we do a fd_remove and not a delete
5891 * because the fd is owned by the engine.
5892 * the engine is responsible to close
5893 */
5894 for (i=0 ; i < num_all_fds ; i++)
5895 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005896 }
5897#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005898 SSL_free(ctx->ssl);
5899 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005900 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005901 }
Emeric Brun46591952012-05-18 15:47:34 +02005902}
5903
5904/* This function tries to perform a clean shutdown on an SSL connection, and in
5905 * any case, flags the connection as reusable if no handshake was in progress.
5906 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005907static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02005908{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005909 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005910
Emeric Brun46591952012-05-18 15:47:34 +02005911 if (conn->flags & CO_FL_HANDSHAKE)
5912 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01005913 if (!clean)
5914 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005915 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005916 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005917 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01005918 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005919 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005920 ERR_clear_error();
5921 }
Emeric Brun46591952012-05-18 15:47:34 +02005922}
5923
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005924/* used for ppv2 pkey alog (can be used for logging) */
Willy Tarreau83061a82018-07-13 11:56:34 +02005925int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005926{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005927 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005928 struct pkey_info *pkinfo;
5929 int bits = 0;
5930 int sig = TLSEXT_signature_anonymous;
5931 int len = -1;
5932
5933 if (!ssl_sock_is_ssl(conn))
5934 return 0;
5935
Olivier Houchard66ab4982019-02-26 18:37:15 +01005936 pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005937 if (pkinfo) {
5938 sig = pkinfo->sig;
5939 bits = pkinfo->bits;
5940 } else {
5941 /* multicert and generated cert have no pkey info */
5942 X509 *crt;
5943 EVP_PKEY *pkey;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005944 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005945 if (!crt)
5946 return 0;
5947 pkey = X509_get_pubkey(crt);
5948 if (pkey) {
5949 bits = EVP_PKEY_bits(pkey);
5950 switch(EVP_PKEY_base_id(pkey)) {
5951 case EVP_PKEY_RSA:
5952 sig = TLSEXT_signature_rsa;
5953 break;
5954 case EVP_PKEY_EC:
5955 sig = TLSEXT_signature_ecdsa;
5956 break;
5957 case EVP_PKEY_DSA:
5958 sig = TLSEXT_signature_dsa;
5959 break;
5960 }
5961 EVP_PKEY_free(pkey);
5962 }
5963 }
5964
5965 switch(sig) {
5966 case TLSEXT_signature_rsa:
5967 len = chunk_printf(out, "RSA%d", bits);
5968 break;
5969 case TLSEXT_signature_ecdsa:
5970 len = chunk_printf(out, "EC%d", bits);
5971 break;
5972 case TLSEXT_signature_dsa:
5973 len = chunk_printf(out, "DSA%d", bits);
5974 break;
5975 default:
5976 return 0;
5977 }
5978 if (len < 0)
5979 return 0;
5980 return 1;
5981}
5982
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005983/* used for ppv2 cert signature (can be used for logging) */
5984const char *ssl_sock_get_cert_sig(struct connection *conn)
5985{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005986 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
5987
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005988 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
5989 X509 *crt;
5990
5991 if (!ssl_sock_is_ssl(conn))
5992 return NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005993 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005994 if (!crt)
5995 return NULL;
5996 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
5997 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
5998}
5999
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006000/* used for ppv2 authority */
6001const char *ssl_sock_get_sni(struct connection *conn)
6002{
6003#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Olivier Houchard66ab4982019-02-26 18:37:15 +01006004 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6005
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006006 if (!ssl_sock_is_ssl(conn))
6007 return NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006008 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006009#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006010 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006011#endif
6012}
6013
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006014/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006015const char *ssl_sock_get_cipher_name(struct connection *conn)
6016{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006017 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6018
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006019 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006020 return NULL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006021
Olivier Houchard66ab4982019-02-26 18:37:15 +01006022 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006023}
6024
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006025/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006026const char *ssl_sock_get_proto_version(struct connection *conn)
6027{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006028 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6029
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006030 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006031 return NULL;
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006032
Olivier Houchard66ab4982019-02-26 18:37:15 +01006033 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006034}
6035
Willy Tarreau8d598402012-10-22 17:58:39 +02006036/* Extract a serial from a cert, and copy it to a chunk.
6037 * Returns 1 if serial is found and copied, 0 if no serial found and
6038 * -1 if output is not large enough.
6039 */
6040static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006041ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006042{
6043 ASN1_INTEGER *serial;
6044
6045 serial = X509_get_serialNumber(crt);
6046 if (!serial)
6047 return 0;
6048
6049 if (out->size < serial->length)
6050 return -1;
6051
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006052 memcpy(out->area, serial->data, serial->length);
6053 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006054 return 1;
6055}
6056
Emeric Brun43e79582014-10-29 19:03:26 +01006057/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006058 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6059 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006060 */
6061static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006062ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006063{
6064 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006065 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006066
6067 len =i2d_X509(crt, NULL);
6068 if (len <= 0)
6069 return 1;
6070
6071 if (out->size < len)
6072 return -1;
6073
6074 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006075 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006076 return 1;
6077}
6078
Emeric Brunce5ad802012-10-22 14:11:22 +02006079
Willy Tarreau83061a82018-07-13 11:56:34 +02006080/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006081 * Returns 1 if serial is found and copied, 0 if no valid time found
6082 * and -1 if output is not large enough.
6083 */
6084static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006085ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006086{
6087 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6088 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6089
6090 if (gentm->length < 12)
6091 return 0;
6092 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6093 return 0;
6094 if (out->size < gentm->length-2)
6095 return -1;
6096
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006097 memcpy(out->area, gentm->data+2, gentm->length-2);
6098 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02006099 return 1;
6100 }
6101 else if (tm->type == V_ASN1_UTCTIME) {
6102 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
6103
6104 if (utctm->length < 10)
6105 return 0;
6106 if (utctm->data[0] >= 0x35)
6107 return 0;
6108 if (out->size < utctm->length)
6109 return -1;
6110
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006111 memcpy(out->area, utctm->data, utctm->length);
6112 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02006113 return 1;
6114 }
6115
6116 return 0;
6117}
6118
Emeric Brun87855892012-10-17 17:39:35 +02006119/* Extract an entry from a X509_NAME and copy its value to an output chunk.
6120 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
6121 */
6122static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006123ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
6124 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006125{
6126 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006127 ASN1_OBJECT *obj;
6128 ASN1_STRING *data;
6129 const unsigned char *data_ptr;
6130 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006131 int i, j, n;
6132 int cur = 0;
6133 const char *s;
6134 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006135 int name_count;
6136
6137 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006138
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006139 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006140 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02006141 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006142 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02006143 else
6144 j = i;
6145
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006146 ne = X509_NAME_get_entry(a, j);
6147 obj = X509_NAME_ENTRY_get_object(ne);
6148 data = X509_NAME_ENTRY_get_data(ne);
6149 data_ptr = ASN1_STRING_get0_data(data);
6150 data_len = ASN1_STRING_length(data);
6151 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006152 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006153 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006154 s = tmp;
6155 }
6156
6157 if (chunk_strcasecmp(entry, s) != 0)
6158 continue;
6159
6160 if (pos < 0)
6161 cur--;
6162 else
6163 cur++;
6164
6165 if (cur != pos)
6166 continue;
6167
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006168 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02006169 return -1;
6170
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006171 memcpy(out->area, data_ptr, data_len);
6172 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006173 return 1;
6174 }
6175
6176 return 0;
6177
6178}
6179
6180/* Extract and format full DN from a X509_NAME and copy result into a chunk
6181 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
6182 */
6183static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006184ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006185{
6186 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006187 ASN1_OBJECT *obj;
6188 ASN1_STRING *data;
6189 const unsigned char *data_ptr;
6190 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006191 int i, n, ln;
6192 int l = 0;
6193 const char *s;
6194 char *p;
6195 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006196 int name_count;
6197
6198
6199 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006200
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006201 out->data = 0;
6202 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006203 for (i = 0; i < name_count; i++) {
6204 ne = X509_NAME_get_entry(a, i);
6205 obj = X509_NAME_ENTRY_get_object(ne);
6206 data = X509_NAME_ENTRY_get_data(ne);
6207 data_ptr = ASN1_STRING_get0_data(data);
6208 data_len = ASN1_STRING_length(data);
6209 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006210 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006211 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006212 s = tmp;
6213 }
6214 ln = strlen(s);
6215
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006216 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006217 if (l > out->size)
6218 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006219 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02006220
6221 *(p++)='/';
6222 memcpy(p, s, ln);
6223 p += ln;
6224 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006225 memcpy(p, data_ptr, data_len);
6226 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006227 }
6228
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006229 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02006230 return 0;
6231
6232 return 1;
6233}
6234
Olivier Houchardab28a322018-12-21 19:45:40 +01006235void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6236{
6237#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006238 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6239
6240 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006241#endif
6242}
6243
Willy Tarreau119a4082016-12-22 21:58:38 +01006244/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
6245 * to disable SNI.
6246 */
Willy Tarreau63076412015-07-10 11:33:32 +02006247void ssl_sock_set_servername(struct connection *conn, const char *hostname)
6248{
6249#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Olivier Houchard66ab4982019-02-26 18:37:15 +01006250 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6251
Willy Tarreau119a4082016-12-22 21:58:38 +01006252 char *prev_name;
6253
Willy Tarreau63076412015-07-10 11:33:32 +02006254 if (!ssl_sock_is_ssl(conn))
6255 return;
6256
Willy Tarreau119a4082016-12-22 21:58:38 +01006257 /* if the SNI changes, we must destroy the reusable context so that a
6258 * new connection will present a new SNI. As an optimization we could
6259 * later imagine having a small cache of ssl_ctx to hold a few SNI per
6260 * server.
6261 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006262 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01006263 if ((!prev_name && hostname) ||
6264 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006265 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01006266
Olivier Houchard66ab4982019-02-26 18:37:15 +01006267 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02006268#endif
6269}
6270
Emeric Brun0abf8362014-06-24 18:26:41 +02006271/* Extract peer certificate's common name into the chunk dest
6272 * Returns
6273 * the len of the extracted common name
6274 * or 0 if no CN found in DN
6275 * or -1 on error case (i.e. no peer certificate)
6276 */
Willy Tarreau83061a82018-07-13 11:56:34 +02006277int ssl_sock_get_remote_common_name(struct connection *conn,
6278 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04006279{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006280 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04006281 X509 *crt = NULL;
6282 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04006283 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02006284 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006285 .area = (char *)&find_cn,
6286 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04006287 };
Emeric Brun0abf8362014-06-24 18:26:41 +02006288 int result = -1;
David Safb76832014-05-08 23:42:08 -04006289
6290 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02006291 goto out;
David Safb76832014-05-08 23:42:08 -04006292
6293 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006294 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006295 if (!crt)
6296 goto out;
6297
6298 name = X509_get_subject_name(crt);
6299 if (!name)
6300 goto out;
David Safb76832014-05-08 23:42:08 -04006301
Emeric Brun0abf8362014-06-24 18:26:41 +02006302 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6303out:
David Safb76832014-05-08 23:42:08 -04006304 if (crt)
6305 X509_free(crt);
6306
6307 return result;
6308}
6309
Dave McCowan328fb582014-07-30 10:39:13 -04006310/* returns 1 if client passed a certificate for this session, 0 if not */
6311int ssl_sock_get_cert_used_sess(struct connection *conn)
6312{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006313 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006314 X509 *crt = NULL;
6315
6316 if (!ssl_sock_is_ssl(conn))
6317 return 0;
6318
6319 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006320 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006321 if (!crt)
6322 return 0;
6323
6324 X509_free(crt);
6325 return 1;
6326}
6327
6328/* returns 1 if client passed a certificate for this connection, 0 if not */
6329int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006330{
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006331 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6332
David Safb76832014-05-08 23:42:08 -04006333 if (!ssl_sock_is_ssl(conn))
6334 return 0;
6335
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006336 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006337}
6338
6339/* returns result from SSL verify */
6340unsigned int ssl_sock_get_verify_result(struct connection *conn)
6341{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006342 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6343
David Safb76832014-05-08 23:42:08 -04006344 if (!ssl_sock_is_ssl(conn))
6345 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
6346
Olivier Houchard66ab4982019-02-26 18:37:15 +01006347 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006348}
6349
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006350/* Returns the application layer protocol name in <str> and <len> when known.
6351 * Zero is returned if the protocol name was not found, otherwise non-zero is
6352 * returned. The string is allocated in the SSL context and doesn't have to be
6353 * freed by the caller. NPN is also checked if available since older versions
6354 * of openssl (1.0.1) which are more common in field only support this one.
6355 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006356static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006357{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006358#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6359 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006360 struct ssl_sock_ctx *ctx = xprt_ctx;
6361 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006362 return 0;
6363
6364 *str = NULL;
6365
6366#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006367 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006368 if (*str)
6369 return 1;
6370#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006371#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006372 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006373 if (*str)
6374 return 1;
6375#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006376#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006377 return 0;
6378}
6379
Willy Tarreau7875d092012-09-10 08:20:03 +02006380/***** Below are some sample fetching functions for ACL/patterns *****/
6381
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006382static int
6383smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
6384{
6385 struct connection *conn;
6386
6387 conn = objt_conn(smp->sess->origin);
6388 if (!conn || conn->xprt != &ssl_sock)
6389 return 0;
6390
6391 smp->flags = 0;
6392 smp->data.type = SMP_T_BOOL;
Olivier Houchard25ae45a2017-11-29 19:51:19 +01006393 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
6394 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006395
6396 return 1;
6397}
6398
Emeric Brune64aef12012-09-21 13:15:06 +02006399/* boolean, returns true if client cert was present */
6400static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006401smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02006402{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006403 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006404 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006405
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006406 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006407 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02006408 return 0;
6409
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006410 ctx = conn->xprt_ctx;
6411
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006412 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02006413 smp->flags |= SMP_F_MAY_CHANGE;
6414 return 0;
6415 }
6416
6417 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006418 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006419 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02006420
6421 return 1;
6422}
6423
Emeric Brun43e79582014-10-29 19:03:26 +01006424/* binary, returns a certificate in a binary chunk (der/raw).
6425 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6426 * should be use.
6427 */
6428static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006429smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01006430{
6431 int cert_peer = (kw[4] == 'c') ? 1 : 0;
6432 X509 *crt = NULL;
6433 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006434 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01006435 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006436 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006437
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006438 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01006439 if (!conn || conn->xprt != &ssl_sock)
6440 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006441 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006442
6443 if (!(conn->flags & CO_FL_CONNECTED)) {
6444 smp->flags |= SMP_F_MAY_CHANGE;
6445 return 0;
6446 }
6447
6448 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006449 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006450 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006451 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006452
6453 if (!crt)
6454 goto out;
6455
6456 smp_trash = get_trash_chunk();
6457 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
6458 goto out;
6459
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006460 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006461 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01006462 ret = 1;
6463out:
6464 /* SSL_get_peer_certificate, it increase X509 * ref count */
6465 if (cert_peer && crt)
6466 X509_free(crt);
6467 return ret;
6468}
6469
Emeric Brunba841a12014-04-30 17:05:08 +02006470/* binary, returns serial of certificate in a binary chunk.
6471 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6472 * should be use.
6473 */
Willy Tarreau8d598402012-10-22 17:58:39 +02006474static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006475smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02006476{
Emeric Brunba841a12014-04-30 17:05:08 +02006477 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02006478 X509 *crt = NULL;
6479 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006480 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006481 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006482 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006483
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006484 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006485 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02006486 return 0;
6487
Olivier Houchard66ab4982019-02-26 18:37:15 +01006488 ctx = conn->xprt_ctx;
6489
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006490 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02006491 smp->flags |= SMP_F_MAY_CHANGE;
6492 return 0;
6493 }
6494
Emeric Brunba841a12014-04-30 17:05:08 +02006495 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006496 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006497 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006498 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006499
Willy Tarreau8d598402012-10-22 17:58:39 +02006500 if (!crt)
6501 goto out;
6502
Willy Tarreau47ca5452012-12-23 20:22:19 +01006503 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02006504 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
6505 goto out;
6506
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006507 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006508 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02006509 ret = 1;
6510out:
Emeric Brunba841a12014-04-30 17:05:08 +02006511 /* SSL_get_peer_certificate, it increase X509 * ref count */
6512 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02006513 X509_free(crt);
6514 return ret;
6515}
Emeric Brune64aef12012-09-21 13:15:06 +02006516
Emeric Brunba841a12014-04-30 17:05:08 +02006517/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
6518 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6519 * should be use.
6520 */
James Votha051b4a2013-05-14 20:37:59 +02006521static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006522smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02006523{
Emeric Brunba841a12014-04-30 17:05:08 +02006524 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02006525 X509 *crt = NULL;
6526 const EVP_MD *digest;
6527 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006528 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006529 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006530 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02006531
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006532 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006533 if (!conn || conn->xprt != &ssl_sock)
6534 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006535 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006536
6537 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02006538 smp->flags |= SMP_F_MAY_CHANGE;
6539 return 0;
6540 }
6541
Emeric Brunba841a12014-04-30 17:05:08 +02006542 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006543 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006544 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006545 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02006546 if (!crt)
6547 goto out;
6548
6549 smp_trash = get_trash_chunk();
6550 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006551 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
6552 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02006553
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006554 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006555 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02006556 ret = 1;
6557out:
Emeric Brunba841a12014-04-30 17:05:08 +02006558 /* SSL_get_peer_certificate, it increase X509 * ref count */
6559 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02006560 X509_free(crt);
6561 return ret;
6562}
6563
Emeric Brunba841a12014-04-30 17:05:08 +02006564/* string, returns certificate's notafter date in ASN1_UTCTIME format.
6565 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6566 * should be use.
6567 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006568static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006569smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006570{
Emeric Brunba841a12014-04-30 17:05:08 +02006571 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006572 X509 *crt = NULL;
6573 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006574 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006575 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006576 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006577
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006578 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006579 if (!conn || conn->xprt != &ssl_sock)
6580 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006581 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006582
6583 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006584 smp->flags |= SMP_F_MAY_CHANGE;
6585 return 0;
6586 }
6587
Emeric Brunba841a12014-04-30 17:05:08 +02006588 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006589 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006590 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006591 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006592 if (!crt)
6593 goto out;
6594
Willy Tarreau47ca5452012-12-23 20:22:19 +01006595 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006596 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006597 goto out;
6598
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006599 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006600 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006601 ret = 1;
6602out:
Emeric Brunba841a12014-04-30 17:05:08 +02006603 /* SSL_get_peer_certificate, it increase X509 * ref count */
6604 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006605 X509_free(crt);
6606 return ret;
6607}
6608
Emeric Brunba841a12014-04-30 17:05:08 +02006609/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
6610 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6611 * should be use.
6612 */
Emeric Brun87855892012-10-17 17:39:35 +02006613static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006614smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006615{
Emeric Brunba841a12014-04-30 17:05:08 +02006616 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006617 X509 *crt = NULL;
6618 X509_NAME *name;
6619 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006620 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006621 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006622 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02006623
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006624 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006625 if (!conn || conn->xprt != &ssl_sock)
6626 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006627 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006628
6629 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02006630 smp->flags |= SMP_F_MAY_CHANGE;
6631 return 0;
6632 }
6633
Emeric Brunba841a12014-04-30 17:05:08 +02006634 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006635 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006636 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006637 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006638 if (!crt)
6639 goto out;
6640
6641 name = X509_get_issuer_name(crt);
6642 if (!name)
6643 goto out;
6644
Willy Tarreau47ca5452012-12-23 20:22:19 +01006645 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02006646 if (args && args[0].type == ARGT_STR) {
6647 int pos = 1;
6648
6649 if (args[1].type == ARGT_SINT)
6650 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006651
6652 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6653 goto out;
6654 }
6655 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6656 goto out;
6657
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006658 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006659 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006660 ret = 1;
6661out:
Emeric Brunba841a12014-04-30 17:05:08 +02006662 /* SSL_get_peer_certificate, it increase X509 * ref count */
6663 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006664 X509_free(crt);
6665 return ret;
6666}
6667
Emeric Brunba841a12014-04-30 17:05:08 +02006668/* string, returns notbefore date in ASN1_UTCTIME format.
6669 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6670 * should be use.
6671 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006672static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006673smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006674{
Emeric Brunba841a12014-04-30 17:05:08 +02006675 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006676 X509 *crt = NULL;
6677 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006678 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006679 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006680 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006681
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006682 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006683 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02006684 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006685 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006686
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006687 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006688 smp->flags |= SMP_F_MAY_CHANGE;
6689 return 0;
6690 }
6691
Emeric Brunba841a12014-04-30 17:05:08 +02006692 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006693 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006694 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006695 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006696 if (!crt)
6697 goto out;
6698
Willy Tarreau47ca5452012-12-23 20:22:19 +01006699 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006700 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006701 goto out;
6702
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006703 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006704 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006705 ret = 1;
6706out:
Emeric Brunba841a12014-04-30 17:05:08 +02006707 /* SSL_get_peer_certificate, it increase X509 * ref count */
6708 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006709 X509_free(crt);
6710 return ret;
6711}
6712
Emeric Brunba841a12014-04-30 17:05:08 +02006713/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
6714 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6715 * should be use.
6716 */
Emeric Brun87855892012-10-17 17:39:35 +02006717static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006718smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006719{
Emeric Brunba841a12014-04-30 17:05:08 +02006720 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006721 X509 *crt = NULL;
6722 X509_NAME *name;
6723 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006724 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006725 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006726 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02006727
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006728 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006729 if (!conn || conn->xprt != &ssl_sock)
6730 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006731 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006732
6733 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02006734 smp->flags |= SMP_F_MAY_CHANGE;
6735 return 0;
6736 }
6737
Emeric Brunba841a12014-04-30 17:05:08 +02006738 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006739 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006740 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006741 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006742 if (!crt)
6743 goto out;
6744
6745 name = X509_get_subject_name(crt);
6746 if (!name)
6747 goto out;
6748
Willy Tarreau47ca5452012-12-23 20:22:19 +01006749 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02006750 if (args && args[0].type == ARGT_STR) {
6751 int pos = 1;
6752
6753 if (args[1].type == ARGT_SINT)
6754 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006755
6756 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6757 goto out;
6758 }
6759 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6760 goto out;
6761
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006762 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006763 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006764 ret = 1;
6765out:
Emeric Brunba841a12014-04-30 17:05:08 +02006766 /* SSL_get_peer_certificate, it increase X509 * ref count */
6767 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006768 X509_free(crt);
6769 return ret;
6770}
Emeric Brun9143d372012-12-20 15:44:16 +01006771
6772/* integer, returns true if current session use a client certificate */
6773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006774smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01006775{
6776 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006777 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006778 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01006779
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006780 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006781 if (!conn || conn->xprt != &ssl_sock)
6782 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006783 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006784
6785 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01006786 smp->flags |= SMP_F_MAY_CHANGE;
6787 return 0;
6788 }
6789
6790 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006791 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01006792 if (crt) {
6793 X509_free(crt);
6794 }
6795
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006796 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006797 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01006798 return 1;
6799}
6800
Emeric Brunba841a12014-04-30 17:05:08 +02006801/* integer, returns the certificate version
6802 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6803 * should be use.
6804 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02006805static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006806smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02006807{
Emeric Brunba841a12014-04-30 17:05:08 +02006808 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02006809 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006810 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006811 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006812
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006813 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006814 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02006815 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006816 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02006817
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006818 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02006819 smp->flags |= SMP_F_MAY_CHANGE;
6820 return 0;
6821 }
6822
Emeric Brunba841a12014-04-30 17:05:08 +02006823 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006824 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006825 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006826 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02006827 if (!crt)
6828 return 0;
6829
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006830 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02006831 /* SSL_get_peer_certificate increase X509 * ref count */
6832 if (cert_peer)
6833 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006834 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02006835
6836 return 1;
6837}
6838
Emeric Brunba841a12014-04-30 17:05:08 +02006839/* string, returns the certificate's signature algorithm.
6840 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6841 * should be use.
6842 */
Emeric Brun7f56e742012-10-19 18:15:40 +02006843static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006844smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02006845{
Emeric Brunba841a12014-04-30 17:05:08 +02006846 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02006847 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006848 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02006849 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006850 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006851 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02006852
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006853 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006854 if (!conn || conn->xprt != &ssl_sock)
6855 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006856 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006857
6858 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02006859 smp->flags |= SMP_F_MAY_CHANGE;
6860 return 0;
6861 }
6862
Emeric Brunba841a12014-04-30 17:05:08 +02006863 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006864 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006865 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006866 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02006867 if (!crt)
6868 return 0;
6869
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006870 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6871 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02006872
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006873 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
6874 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02006875 /* SSL_get_peer_certificate increase X509 * ref count */
6876 if (cert_peer)
6877 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02006878 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02006879 }
Emeric Brun7f56e742012-10-19 18:15:40 +02006880
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006881 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006882 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006883 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02006884 /* SSL_get_peer_certificate increase X509 * ref count */
6885 if (cert_peer)
6886 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02006887
6888 return 1;
6889}
6890
Emeric Brunba841a12014-04-30 17:05:08 +02006891/* string, returns the certificate's key algorithm.
6892 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6893 * should be use.
6894 */
Emeric Brun521a0112012-10-22 12:22:55 +02006895static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006896smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02006897{
Emeric Brunba841a12014-04-30 17:05:08 +02006898 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02006899 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006900 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02006901 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006902 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006903 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02006904
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006905 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006906 if (!conn || conn->xprt != &ssl_sock)
6907 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006908 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006909
6910 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02006911 smp->flags |= SMP_F_MAY_CHANGE;
6912 return 0;
6913 }
6914
Emeric Brunba841a12014-04-30 17:05:08 +02006915 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006916 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006917 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006918 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02006919 if (!crt)
6920 return 0;
6921
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006922 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
6923 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02006924
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006925 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
6926 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02006927 /* SSL_get_peer_certificate increase X509 * ref count */
6928 if (cert_peer)
6929 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02006930 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02006931 }
Emeric Brun521a0112012-10-22 12:22:55 +02006932
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006933 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006934 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006935 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02006936 if (cert_peer)
6937 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02006938
6939 return 1;
6940}
6941
Emeric Brun645ae792014-04-30 14:21:06 +02006942/* boolean, returns true if front conn. transport layer is SSL.
6943 * This function is also usable on backend conn if the fetch keyword 5th
6944 * char is 'b'.
6945 */
Willy Tarreau7875d092012-09-10 08:20:03 +02006946static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006947smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02006948{
Emeric Bruneb8def92018-02-19 15:59:48 +01006949 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
6950 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006951
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006952 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006953 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02006954 return 1;
6955}
6956
Emeric Brun2525b6b2012-10-18 15:59:43 +02006957/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02006958static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006959smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02006960{
6961#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006962 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006963 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006964
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006965 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006966 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006967 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01006968 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02006969 return 1;
6970#else
6971 return 0;
6972#endif
6973}
6974
Emeric Brun74f7ffa2018-02-19 16:14:12 +01006975/* boolean, returns true if client session has been resumed.
6976 * This function is also usable on backend conn if the fetch keyword 5th
6977 * char is 'b'.
6978 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02006979static int
6980smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
6981{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01006982 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
6983 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006984 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01006985
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02006986
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006987 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006988 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02006989 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01006990 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02006991 return 1;
6992}
6993
Emeric Brun645ae792014-04-30 14:21:06 +02006994/* string, returns the used cipher if front conn. transport layer is SSL.
6995 * This function is also usable on backend conn if the fetch keyword 5th
6996 * char is 'b'.
6997 */
Emeric Brun589fcad2012-10-16 14:13:26 +02006998static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006999smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007000{
Emeric Bruneb8def92018-02-19 15:59:48 +01007001 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7002 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007003 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007004
Willy Tarreaube508f12016-03-10 11:47:01 +01007005 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007006 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007007 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007008 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007009
Olivier Houchard66ab4982019-02-26 18:37:15 +01007010 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007011 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007012 return 0;
7013
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007014 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007015 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007016 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007017
7018 return 1;
7019}
7020
Emeric Brun645ae792014-04-30 14:21:06 +02007021/* integer, returns the algoritm's keysize if front conn. transport layer
7022 * is SSL.
7023 * This function is also usable on backend conn if the fetch keyword 5th
7024 * char is 'b'.
7025 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007026static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007027smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007028{
Emeric Bruneb8def92018-02-19 15:59:48 +01007029 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7030 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007031 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007032 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01007033
Emeric Brun589fcad2012-10-16 14:13:26 +02007034 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007035 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007036 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007037 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007038
Olivier Houchard66ab4982019-02-26 18:37:15 +01007039 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007040 return 0;
7041
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007042 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007043 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007044
7045 return 1;
7046}
7047
Emeric Brun645ae792014-04-30 14:21:06 +02007048/* integer, returns the used keysize if front conn. transport layer is SSL.
7049 * This function is also usable on backend conn if the fetch keyword 5th
7050 * char is 'b'.
7051 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007052static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007053smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007054{
Emeric Bruneb8def92018-02-19 15:59:48 +01007055 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7056 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007057 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007058
Emeric Brun589fcad2012-10-16 14:13:26 +02007059 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007060 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7061 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007062 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007063
Olivier Houchard66ab4982019-02-26 18:37:15 +01007064 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007065 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02007066 return 0;
7067
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007068 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007069
7070 return 1;
7071}
7072
Bernard Spil13c53f82018-02-15 13:34:58 +01007073#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02007074static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007075smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007076{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007077 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007078 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007079
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007080 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007081 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007082
Olivier Houchard6b77f492018-11-22 18:18:29 +01007083 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7084 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007085 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7086 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007087 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007088
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007089 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007090 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007091 (const unsigned char **)&smp->data.u.str.area,
7092 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02007093
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007094 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007095 return 0;
7096
7097 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007098}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007099#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02007100
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007101#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007102static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007103smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02007104{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007105 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007106 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007107
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007108 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007109 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02007110
Olivier Houchard6b77f492018-11-22 18:18:29 +01007111 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7112 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7113
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007114 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02007115 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007116 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02007117
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007118 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007119 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007120 (const unsigned char **)&smp->data.u.str.area,
7121 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02007122
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007123 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02007124 return 0;
7125
7126 return 1;
7127}
7128#endif
7129
Emeric Brun645ae792014-04-30 14:21:06 +02007130/* string, returns the used protocol if front conn. transport layer is SSL.
7131 * This function is also usable on backend conn if the fetch keyword 5th
7132 * char is 'b'.
7133 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02007134static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007135smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007136{
Emeric Bruneb8def92018-02-19 15:59:48 +01007137 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7138 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007139 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007140
Emeric Brun589fcad2012-10-16 14:13:26 +02007141 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007142 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7143 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007144 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007145
Olivier Houchard66ab4982019-02-26 18:37:15 +01007146 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007147 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007148 return 0;
7149
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007150 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007151 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007152 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007153
7154 return 1;
7155}
7156
Willy Tarreau87b09662015-04-03 00:22:06 +02007157/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02007158 * This function is also usable on backend conn if the fetch keyword 5th
7159 * char is 'b'.
7160 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007161#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02007162static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007163smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02007164{
Emeric Bruneb8def92018-02-19 15:59:48 +01007165 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7166 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007167 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007168 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007169
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007170 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007171 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02007172
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007173 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7174 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007175 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007176
Olivier Houchard66ab4982019-02-26 18:37:15 +01007177 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02007178 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02007179 return 0;
7180
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007181 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
7182 (unsigned int *)&smp->data.u.str.data);
7183 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02007184 return 0;
7185
7186 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02007187}
Patrick Hemmer41966772018-04-28 19:15:48 -04007188#endif
7189
Emeric Brunfe68f682012-10-16 14:59:28 +02007190
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007191#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL)
Patrick Hemmere0275472018-04-28 19:15:51 -04007192static int
7193smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
7194{
7195 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7196 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7197 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02007198 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007199 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007200
7201 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7202 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007203 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007204
Olivier Houchard66ab4982019-02-26 18:37:15 +01007205 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04007206 if (!ssl_sess)
7207 return 0;
7208
7209 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007210 data->data = SSL_SESSION_get_master_key(ssl_sess,
7211 (unsigned char *) data->area,
7212 data->size);
7213 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04007214 return 0;
7215
7216 smp->flags = 0;
7217 smp->data.type = SMP_T_BIN;
7218 smp->data.u.str = *data;
7219
7220 return 1;
7221}
7222#endif
7223
Patrick Hemmer41966772018-04-28 19:15:48 -04007224#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02007225static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007226smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007227{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007228 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007229 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007230
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007231 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007232 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02007233
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007234 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007235 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7236 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007237 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007238
Olivier Houchard66ab4982019-02-26 18:37:15 +01007239 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007240 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02007241 return 0;
7242
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007243 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02007244 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02007245}
Patrick Hemmer41966772018-04-28 19:15:48 -04007246#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02007247
David Sc1ad52e2014-04-08 18:48:47 -04007248static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007249smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
7250{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007251 struct connection *conn;
7252 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007253 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007254
7255 conn = objt_conn(smp->sess->origin);
7256 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7257 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007258 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007259
Olivier Houchard66ab4982019-02-26 18:37:15 +01007260 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007261 if (!capture)
7262 return 0;
7263
7264 smp->flags = SMP_F_CONST;
7265 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007266 smp->data.u.str.area = capture->ciphersuite;
7267 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007268 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007269}
7270
7271static int
7272smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
7273{
Willy Tarreau83061a82018-07-13 11:56:34 +02007274 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007275
7276 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7277 return 0;
7278
7279 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007280 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007281 smp->data.type = SMP_T_BIN;
7282 smp->data.u.str = *data;
7283 return 1;
7284}
7285
7286static int
7287smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
7288{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007289 struct connection *conn;
7290 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007291 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007292
7293 conn = objt_conn(smp->sess->origin);
7294 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7295 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007296 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007297
Olivier Houchard66ab4982019-02-26 18:37:15 +01007298 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007299 if (!capture)
7300 return 0;
7301
7302 smp->data.type = SMP_T_SINT;
7303 smp->data.u.sint = capture->xxh64;
7304 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007305}
7306
7307static int
7308smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
7309{
Willy Tarreau5db847a2019-05-09 14:13:35 +02007310#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02007311 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007312 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007313
7314 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7315 return 0;
7316
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007317 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007318 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007319 const char *str;
7320 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007321 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007322 uint16_t id = (bin[0] << 8) | bin[1];
7323#if defined(OPENSSL_IS_BORINGSSL)
7324 cipher = SSL_get_cipher_by_value(id);
7325#else
Willy Tarreaub7290772018-10-15 11:01:59 +02007326 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007327 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7328 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007329#endif
7330 str = SSL_CIPHER_get_name(cipher);
7331 if (!str || strcmp(str, "(NONE)") == 0)
7332 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007333 else
7334 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
7335 }
7336 smp->data.type = SMP_T_STR;
7337 smp->data.u.str = *data;
7338 return 1;
7339#else
7340 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
7341#endif
7342}
7343
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007344#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007345static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007346smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04007347{
Emeric Bruneb8def92018-02-19 15:59:48 +01007348 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7349 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04007350 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02007351 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007352 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007353
7354 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04007355 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7356 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007357 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007358
7359 if (!(conn->flags & CO_FL_CONNECTED)) {
7360 smp->flags |= SMP_F_MAY_CHANGE;
7361 return 0;
7362 }
7363
7364 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01007365 if (!SSL_session_reused(ctx->ssl))
7366 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007367 finished_trash->area,
7368 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007369 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007370 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007371 finished_trash->area,
7372 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007373
7374 if (!finished_len)
7375 return 0;
7376
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007377 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007378 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007379 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04007380
7381 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04007382}
Patrick Hemmer41966772018-04-28 19:15:48 -04007383#endif
David Sc1ad52e2014-04-08 18:48:47 -04007384
Emeric Brun2525b6b2012-10-18 15:59:43 +02007385/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007386static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007387smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007388{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007389 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007390 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007391
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007392 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007393 if (!conn || conn->xprt != &ssl_sock)
7394 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007395 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007396
7397 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007398 smp->flags = SMP_F_MAY_CHANGE;
7399 return 0;
7400 }
7401
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007402 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007403 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007404 smp->flags = 0;
7405
7406 return 1;
7407}
7408
Emeric Brun2525b6b2012-10-18 15:59:43 +02007409/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007410static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007411smp_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 +02007412{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007413 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007414 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007415
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007416 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007417 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02007418 return 0;
7419
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007420 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007421 smp->flags = SMP_F_MAY_CHANGE;
7422 return 0;
7423 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007424 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02007425
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007426 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007427 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007428 smp->flags = 0;
7429
7430 return 1;
7431}
7432
Emeric Brun2525b6b2012-10-18 15:59:43 +02007433/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02007434static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007435smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007436{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007437 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007438 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007439
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007440 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007441 if (!conn || conn->xprt != &ssl_sock)
7442 return 0;
7443
7444 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007445 smp->flags = SMP_F_MAY_CHANGE;
7446 return 0;
7447 }
7448
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007449 ctx = conn->xprt_ctx;
7450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007451 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007452 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007453 smp->flags = 0;
7454
7455 return 1;
7456}
7457
Emeric Brun2525b6b2012-10-18 15:59:43 +02007458/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007459static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007460smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007461{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007462 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007463 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007464
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007465 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007466 if (!conn || conn->xprt != &ssl_sock)
7467 return 0;
7468
7469 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007470 smp->flags = SMP_F_MAY_CHANGE;
7471 return 0;
7472 }
7473
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007474 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007475 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007476 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007477
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007478 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007479 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007480 smp->flags = 0;
7481
7482 return 1;
7483}
7484
Emeric Brunfb510ea2012-10-05 12:00:26 +02007485/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007486static 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 +02007487{
7488 if (!*args[cur_arg + 1]) {
7489 if (err)
7490 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7491 return ERR_ALERT | ERR_FATAL;
7492 }
7493
Willy Tarreauef934602016-12-22 23:12:01 +01007494 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7495 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007496 else
7497 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007498
Emeric Brund94b3fe2012-09-20 18:23:56 +02007499 return 0;
7500}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007501static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7502{
7503 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
7504}
Emeric Brund94b3fe2012-09-20 18:23:56 +02007505
Christopher Faulet31af49d2015-06-09 17:29:50 +02007506/* parse the "ca-sign-file" bind keyword */
7507static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7508{
7509 if (!*args[cur_arg + 1]) {
7510 if (err)
7511 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7512 return ERR_ALERT | ERR_FATAL;
7513 }
7514
Willy Tarreauef934602016-12-22 23:12:01 +01007515 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7516 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007517 else
7518 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
7519
7520 return 0;
7521}
7522
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007523/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02007524static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7525{
7526 if (!*args[cur_arg + 1]) {
7527 if (err)
7528 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
7529 return ERR_ALERT | ERR_FATAL;
7530 }
7531 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
7532 return 0;
7533}
7534
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007535/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007536static 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 +02007537{
7538 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007539 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007540 return ERR_ALERT | ERR_FATAL;
7541 }
7542
Emeric Brun76d88952012-10-05 15:47:31 +02007543 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02007544 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007545 return 0;
7546}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007547static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7548{
7549 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
7550}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007551
Willy Tarreau5db847a2019-05-09 14:13:35 +02007552#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007553/* parse the "ciphersuites" bind keyword */
7554static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7555{
7556 if (!*args[cur_arg + 1]) {
7557 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
7558 return ERR_ALERT | ERR_FATAL;
7559 }
7560
7561 free(conf->ciphersuites);
7562 conf->ciphersuites = strdup(args[cur_arg + 1]);
7563 return 0;
7564}
7565static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7566{
7567 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
7568}
7569#endif
7570
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007571/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02007572static 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 +02007573{
Willy Tarreau38011032013-08-13 16:59:39 +02007574 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02007575
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007576 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007577 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007578 return ERR_ALERT | ERR_FATAL;
7579 }
7580
Willy Tarreauef934602016-12-22 23:12:01 +01007581 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
7582 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02007583 memprintf(err, "'%s' : path too long", args[cur_arg]);
7584 return ERR_ALERT | ERR_FATAL;
7585 }
Willy Tarreauef934602016-12-22 23:12:01 +01007586 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreau03209342016-12-22 17:08:28 +01007587 if (ssl_sock_load_cert(path, conf, err) > 0)
Emeric Brunc8e8d122012-10-02 18:42:10 +02007588 return ERR_ALERT | ERR_FATAL;
7589
7590 return 0;
7591 }
7592
Willy Tarreau03209342016-12-22 17:08:28 +01007593 if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007594 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02007595
7596 return 0;
7597}
7598
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007599/* parse the "crt-list" bind keyword */
7600static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7601{
7602 if (!*args[cur_arg + 1]) {
7603 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
7604 return ERR_ALERT | ERR_FATAL;
7605 }
7606
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007607 if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) {
Willy Tarreauad1731d2013-04-02 17:35:58 +02007608 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007609 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02007610 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007611
7612 return 0;
7613}
7614
Emeric Brunfb510ea2012-10-05 12:00:26 +02007615/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007616static 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 +02007617{
Emeric Brun051cdab2012-10-02 19:25:50 +02007618#ifndef X509_V_FLAG_CRL_CHECK
7619 if (err)
7620 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
7621 return ERR_ALERT | ERR_FATAL;
7622#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02007623 if (!*args[cur_arg + 1]) {
7624 if (err)
7625 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
7626 return ERR_ALERT | ERR_FATAL;
7627 }
Emeric Brun2b58d042012-09-20 17:10:03 +02007628
Willy Tarreauef934602016-12-22 23:12:01 +01007629 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7630 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007631 else
7632 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007633
Emeric Brun2b58d042012-09-20 17:10:03 +02007634 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02007635#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02007636}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007637static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7638{
7639 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
7640}
Emeric Brun2b58d042012-09-20 17:10:03 +02007641
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007642/* parse the "curves" bind keyword keyword */
7643static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7644{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007645#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007646 if (!*args[cur_arg + 1]) {
7647 if (err)
7648 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
7649 return ERR_ALERT | ERR_FATAL;
7650 }
7651 conf->curves = strdup(args[cur_arg + 1]);
7652 return 0;
7653#else
7654 if (err)
7655 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
7656 return ERR_ALERT | ERR_FATAL;
7657#endif
7658}
7659static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7660{
7661 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
7662}
7663
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007664/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007665static 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 +02007666{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007667#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Emeric Brun2b58d042012-09-20 17:10:03 +02007668 if (err)
7669 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
7670 return ERR_ALERT | ERR_FATAL;
7671#elif defined(OPENSSL_NO_ECDH)
7672 if (err)
7673 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
7674 return ERR_ALERT | ERR_FATAL;
7675#else
7676 if (!*args[cur_arg + 1]) {
7677 if (err)
7678 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
7679 return ERR_ALERT | ERR_FATAL;
7680 }
7681
7682 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007683
7684 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02007685#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007686}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007687static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7688{
7689 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
7690}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007691
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007692/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02007693static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7694{
7695 int code;
7696 char *p = args[cur_arg + 1];
7697 unsigned long long *ignerr = &conf->crt_ignerr;
7698
7699 if (!*p) {
7700 if (err)
7701 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
7702 return ERR_ALERT | ERR_FATAL;
7703 }
7704
7705 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
7706 ignerr = &conf->ca_ignerr;
7707
7708 if (strcmp(p, "all") == 0) {
7709 *ignerr = ~0ULL;
7710 return 0;
7711 }
7712
7713 while (p) {
7714 code = atoi(p);
7715 if ((code <= 0) || (code > 63)) {
7716 if (err)
7717 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
7718 args[cur_arg], code, args[cur_arg + 1]);
7719 return ERR_ALERT | ERR_FATAL;
7720 }
7721 *ignerr |= 1ULL << code;
7722 p = strchr(p, ',');
7723 if (p)
7724 p++;
7725 }
7726
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007727 return 0;
7728}
7729
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007730/* parse tls_method_options "no-xxx" and "force-xxx" */
7731static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007732{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007733 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007734 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007735 p = strchr(arg, '-');
7736 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007737 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007738 p++;
7739 if (!strcmp(p, "sslv3"))
7740 v = CONF_SSLV3;
7741 else if (!strcmp(p, "tlsv10"))
7742 v = CONF_TLSV10;
7743 else if (!strcmp(p, "tlsv11"))
7744 v = CONF_TLSV11;
7745 else if (!strcmp(p, "tlsv12"))
7746 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02007747 else if (!strcmp(p, "tlsv13"))
7748 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007749 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007750 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007751 if (!strncmp(arg, "no-", 3))
7752 methods->flags |= methodVersions[v].flag;
7753 else if (!strncmp(arg, "force-", 6))
7754 methods->min = methods->max = v;
7755 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007756 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02007757 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007758 fail:
7759 if (err)
7760 memprintf(err, "'%s' : option not implemented", arg);
7761 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02007762}
7763
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007764static 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 +02007765{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02007766 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007767}
7768
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007769static 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 +02007770{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007771 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
7772}
7773
7774/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
7775static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
7776{
7777 uint16_t i, v = 0;
7778 char *argv = args[cur_arg + 1];
7779 if (!*argv) {
7780 if (err)
7781 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
7782 return ERR_ALERT | ERR_FATAL;
7783 }
7784 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
7785 if (!strcmp(argv, methodVersions[i].name))
7786 v = i;
7787 if (!v) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007788 if (err)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007789 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007790 return ERR_ALERT | ERR_FATAL;
7791 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007792 if (!strcmp("ssl-min-ver", args[cur_arg]))
7793 methods->min = v;
7794 else if (!strcmp("ssl-max-ver", args[cur_arg]))
7795 methods->max = v;
7796 else {
7797 if (err)
7798 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
7799 return ERR_ALERT | ERR_FATAL;
7800 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007801 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007802}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007803
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02007804static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7805{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007806#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01007807 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 +02007808#endif
7809 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
7810}
7811
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007812static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7813{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02007814 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007815}
7816
7817static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7818{
7819 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
7820}
7821
Emeric Brun2d0c4822012-10-02 13:45:20 +02007822/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01007823static 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 +02007824{
Emeric Brun89675492012-10-05 13:48:26 +02007825 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02007826 return 0;
7827}
Emeric Brun2d0c4822012-10-02 13:45:20 +02007828
Olivier Houchardc2aae742017-09-22 18:26:28 +02007829/* parse the "allow-0rtt" bind keyword */
7830static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7831{
7832 conf->early_data = 1;
7833 return 0;
7834}
7835
7836static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7837{
Olivier Houchard9679ac92017-10-27 14:58:08 +02007838 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007839 return 0;
7840}
7841
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007842/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007843static 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 +02007844{
Bernard Spil13c53f82018-02-15 13:34:58 +01007845#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007846 char *p1, *p2;
7847
7848 if (!*args[cur_arg + 1]) {
7849 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
7850 return ERR_ALERT | ERR_FATAL;
7851 }
7852
7853 free(conf->npn_str);
7854
Willy Tarreau3724da12016-02-12 17:11:12 +01007855 /* the NPN string is built as a suite of (<len> <name>)*,
7856 * so we reuse each comma to store the next <len> and need
7857 * one more for the end of the string.
7858 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007859 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01007860 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007861 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
7862
7863 /* replace commas with the name length */
7864 p1 = conf->npn_str;
7865 p2 = p1 + 1;
7866 while (1) {
7867 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
7868 if (!p2)
7869 p2 = p1 + 1 + strlen(p1 + 1);
7870
7871 if (p2 - (p1 + 1) > 255) {
7872 *p2 = '\0';
7873 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
7874 return ERR_ALERT | ERR_FATAL;
7875 }
7876
7877 *p1 = p2 - (p1 + 1);
7878 p1 = p2;
7879
7880 if (!*p2)
7881 break;
7882
7883 *(p2++) = '\0';
7884 }
7885 return 0;
7886#else
7887 if (err)
7888 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
7889 return ERR_ALERT | ERR_FATAL;
7890#endif
7891}
7892
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007893static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7894{
7895 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
7896}
7897
Willy Tarreauab861d32013-04-02 02:30:41 +02007898/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007899static 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 +02007900{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007901#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007902 char *p1, *p2;
7903
7904 if (!*args[cur_arg + 1]) {
7905 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
7906 return ERR_ALERT | ERR_FATAL;
7907 }
7908
7909 free(conf->alpn_str);
7910
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01007911 /* the ALPN string is built as a suite of (<len> <name>)*,
7912 * so we reuse each comma to store the next <len> and need
7913 * one more for the end of the string.
7914 */
Willy Tarreauab861d32013-04-02 02:30:41 +02007915 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01007916 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02007917 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
7918
7919 /* replace commas with the name length */
7920 p1 = conf->alpn_str;
7921 p2 = p1 + 1;
7922 while (1) {
7923 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
7924 if (!p2)
7925 p2 = p1 + 1 + strlen(p1 + 1);
7926
7927 if (p2 - (p1 + 1) > 255) {
7928 *p2 = '\0';
7929 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
7930 return ERR_ALERT | ERR_FATAL;
7931 }
7932
7933 *p1 = p2 - (p1 + 1);
7934 p1 = p2;
7935
7936 if (!*p2)
7937 break;
7938
7939 *(p2++) = '\0';
7940 }
7941 return 0;
7942#else
7943 if (err)
7944 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
7945 return ERR_ALERT | ERR_FATAL;
7946#endif
7947}
7948
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007949static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7950{
7951 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
7952}
7953
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007954/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02007955static 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 +02007956{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01007957 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02007958 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02007959
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007960 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
7961 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007962#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007963 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
7964 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
7965#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01007966 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02007967 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
7968 if (!conf->ssl_conf.ssl_methods.min)
7969 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
7970 if (!conf->ssl_conf.ssl_methods.max)
7971 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02007972
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007973 return 0;
7974}
7975
Lukas Tribus53ae85c2017-05-04 15:45:40 +00007976/* parse the "prefer-client-ciphers" bind keyword */
7977static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7978{
7979 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
7980 return 0;
7981}
7982
Christopher Faulet31af49d2015-06-09 17:29:50 +02007983/* parse the "generate-certificates" bind keyword */
7984static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7985{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01007986#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02007987 conf->generate_certs = 1;
7988#else
7989 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
7990 err && *err ? *err : "");
7991#endif
7992 return 0;
7993}
7994
Emmanuel Hocdet65623372013-01-24 17:17:15 +01007995/* parse the "strict-sni" bind keyword */
7996static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7997{
7998 conf->strict_sni = 1;
7999 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008000}
8001
8002/* parse the "tls-ticket-keys" bind keyword */
8003static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8004{
8005#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
8006 FILE *f;
8007 int i = 0;
8008 char thisline[LINESIZE];
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008009 struct tls_keys_ref *keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008010
8011 if (!*args[cur_arg + 1]) {
8012 if (err)
8013 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
8014 return ERR_ALERT | ERR_FATAL;
8015 }
8016
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008017 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008018 if (keys_ref) {
8019 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008020 conf->keys_ref = keys_ref;
8021 return 0;
8022 }
8023
Vincent Bernat02779b62016-04-03 13:48:43 +02008024 keys_ref = malloc(sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01008025 if (!keys_ref) {
8026 if (err)
8027 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
8028 return ERR_ALERT | ERR_FATAL;
8029 }
8030
Emeric Brun9e754772019-01-10 17:51:55 +01008031 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01008032 if (!keys_ref->tlskeys) {
8033 free(keys_ref);
8034 if (err)
8035 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
8036 return ERR_ALERT | ERR_FATAL;
8037 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008038
8039 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Emeric Brun09852f72019-01-10 10:51:13 +01008040 free(keys_ref->tlskeys);
8041 free(keys_ref);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008042 if (err)
8043 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
8044 return ERR_ALERT | ERR_FATAL;
8045 }
8046
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008047 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01008048 if (!keys_ref->filename) {
8049 free(keys_ref->tlskeys);
8050 free(keys_ref);
8051 if (err)
8052 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
8053 return ERR_ALERT | ERR_FATAL;
8054 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008055
Emeric Brun9e754772019-01-10 17:51:55 +01008056 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008057 while (fgets(thisline, sizeof(thisline), f) != NULL) {
8058 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01008059 int dec_size;
8060
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008061 /* Strip newline characters from the end */
8062 if(thisline[len - 1] == '\n')
8063 thisline[--len] = 0;
8064
8065 if(thisline[len - 1] == '\r')
8066 thisline[--len] = 0;
8067
Emeric Brun9e754772019-01-10 17:51:55 +01008068 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
8069 if (dec_size < 0) {
Emeric Brun09852f72019-01-10 10:51:13 +01008070 free(keys_ref->filename);
8071 free(keys_ref->tlskeys);
8072 free(keys_ref);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008073 if (err)
8074 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
mildis16aa0152016-06-22 17:46:29 +02008075 fclose(f);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008076 return ERR_ALERT | ERR_FATAL;
8077 }
Emeric Brun9e754772019-01-10 17:51:55 +01008078 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
8079 keys_ref->key_size_bits = 128;
8080 }
8081 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
8082 keys_ref->key_size_bits = 256;
8083 }
8084 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
8085 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
8086 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
8087 free(keys_ref->filename);
8088 free(keys_ref->tlskeys);
8089 free(keys_ref);
8090 if (err)
8091 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
8092 fclose(f);
8093 return ERR_ALERT | ERR_FATAL;
8094 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008095 i++;
8096 }
8097
8098 if (i < TLS_TICKETS_NO) {
Emeric Brun09852f72019-01-10 10:51:13 +01008099 free(keys_ref->filename);
8100 free(keys_ref->tlskeys);
8101 free(keys_ref);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008102 if (err)
8103 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
mildis16aa0152016-06-22 17:46:29 +02008104 fclose(f);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008105 return ERR_ALERT | ERR_FATAL;
8106 }
8107
8108 fclose(f);
8109
8110 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01008111 i -= 2;
8112 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008113 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008114 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01008115 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008116 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008117
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008118 LIST_ADD(&tlskeys_reference, &keys_ref->list);
8119
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008120 return 0;
8121#else
8122 if (err)
8123 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
8124 return ERR_ALERT | ERR_FATAL;
8125#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008126}
8127
Emeric Brund94b3fe2012-09-20 18:23:56 +02008128/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008129static 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 +02008130{
8131 if (!*args[cur_arg + 1]) {
8132 if (err)
8133 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
8134 return ERR_ALERT | ERR_FATAL;
8135 }
8136
8137 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008138 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008139 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008140 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008141 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008142 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008143 else {
8144 if (err)
8145 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
8146 args[cur_arg], args[cur_arg + 1]);
8147 return ERR_ALERT | ERR_FATAL;
8148 }
8149
8150 return 0;
8151}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008152static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8153{
8154 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
8155}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008156
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02008157/* parse the "no-ca-names" bind keyword */
8158static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8159{
8160 conf->no_ca_names = 1;
8161 return 0;
8162}
8163static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8164{
8165 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
8166}
8167
Willy Tarreau92faadf2012-10-10 23:04:25 +02008168/************** "server" keywords ****************/
8169
Olivier Houchardc7566002018-11-20 23:33:50 +01008170/* parse the "npn" bind keyword */
8171static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8172{
8173#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
8174 char *p1, *p2;
8175
8176 if (!*args[*cur_arg + 1]) {
8177 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
8178 return ERR_ALERT | ERR_FATAL;
8179 }
8180
8181 free(newsrv->ssl_ctx.npn_str);
8182
8183 /* the NPN string is built as a suite of (<len> <name>)*,
8184 * so we reuse each comma to store the next <len> and need
8185 * one more for the end of the string.
8186 */
8187 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
8188 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
8189 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
8190 newsrv->ssl_ctx.npn_len);
8191
8192 /* replace commas with the name length */
8193 p1 = newsrv->ssl_ctx.npn_str;
8194 p2 = p1 + 1;
8195 while (1) {
8196 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
8197 newsrv->ssl_ctx.npn_len - (p1 + 1));
8198 if (!p2)
8199 p2 = p1 + 1 + strlen(p1 + 1);
8200
8201 if (p2 - (p1 + 1) > 255) {
8202 *p2 = '\0';
8203 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8204 return ERR_ALERT | ERR_FATAL;
8205 }
8206
8207 *p1 = p2 - (p1 + 1);
8208 p1 = p2;
8209
8210 if (!*p2)
8211 break;
8212
8213 *(p2++) = '\0';
8214 }
8215 return 0;
8216#else
8217 if (err)
8218 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
8219 return ERR_ALERT | ERR_FATAL;
8220#endif
8221}
8222
Olivier Houchard92150142018-12-21 19:47:01 +01008223/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01008224static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8225{
8226#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
8227 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01008228 char **alpn_str;
8229 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01008230
Olivier Houchard92150142018-12-21 19:47:01 +01008231 if (*args[*cur_arg] == 'c') {
8232 alpn_str = &newsrv->check.alpn_str;
8233 alpn_len = &newsrv->check.alpn_len;
8234 } else {
8235 alpn_str = &newsrv->ssl_ctx.alpn_str;
8236 alpn_len = &newsrv->ssl_ctx.alpn_len;
8237
8238 }
Olivier Houchardc7566002018-11-20 23:33:50 +01008239 if (!*args[*cur_arg + 1]) {
8240 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
8241 return ERR_ALERT | ERR_FATAL;
8242 }
8243
Olivier Houchard92150142018-12-21 19:47:01 +01008244 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01008245
8246 /* the ALPN string is built as a suite of (<len> <name>)*,
8247 * so we reuse each comma to store the next <len> and need
8248 * one more for the end of the string.
8249 */
Olivier Houchard92150142018-12-21 19:47:01 +01008250 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
8251 *alpn_str = calloc(1, *alpn_len + 1);
8252 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01008253
8254 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01008255 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01008256 p2 = p1 + 1;
8257 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01008258 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01008259 if (!p2)
8260 p2 = p1 + 1 + strlen(p1 + 1);
8261
8262 if (p2 - (p1 + 1) > 255) {
8263 *p2 = '\0';
8264 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8265 return ERR_ALERT | ERR_FATAL;
8266 }
8267
8268 *p1 = p2 - (p1 + 1);
8269 p1 = p2;
8270
8271 if (!*p2)
8272 break;
8273
8274 *(p2++) = '\0';
8275 }
8276 return 0;
8277#else
8278 if (err)
8279 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
8280 return ERR_ALERT | ERR_FATAL;
8281#endif
8282}
8283
Emeric Brunef42d922012-10-11 16:11:36 +02008284/* parse the "ca-file" server keyword */
8285static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8286{
8287 if (!*args[*cur_arg + 1]) {
8288 if (err)
8289 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
8290 return ERR_ALERT | ERR_FATAL;
8291 }
8292
Willy Tarreauef934602016-12-22 23:12:01 +01008293 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8294 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008295 else
8296 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
8297
8298 return 0;
8299}
8300
Olivier Houchard9130a962017-10-17 17:33:43 +02008301/* parse the "check-sni" server keyword */
8302static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8303{
8304 if (!*args[*cur_arg + 1]) {
8305 if (err)
8306 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
8307 return ERR_ALERT | ERR_FATAL;
8308 }
8309
8310 newsrv->check.sni = strdup(args[*cur_arg + 1]);
8311 if (!newsrv->check.sni) {
8312 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
8313 return ERR_ALERT | ERR_FATAL;
8314 }
8315 return 0;
8316
8317}
8318
Willy Tarreau92faadf2012-10-10 23:04:25 +02008319/* parse the "check-ssl" server keyword */
8320static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8321{
8322 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008323 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8324 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02008325#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008326 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8327 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8328#endif
Willy Tarreauef934602016-12-22 23:12:01 +01008329 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008330 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8331 if (!newsrv->ssl_ctx.methods.min)
8332 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8333 if (!newsrv->ssl_ctx.methods.max)
8334 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8335
Willy Tarreau92faadf2012-10-10 23:04:25 +02008336 return 0;
8337}
8338
8339/* parse the "ciphers" server keyword */
8340static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8341{
8342 if (!*args[*cur_arg + 1]) {
8343 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8344 return ERR_ALERT | ERR_FATAL;
8345 }
8346
8347 free(newsrv->ssl_ctx.ciphers);
8348 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
8349 return 0;
8350}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008351
Willy Tarreau5db847a2019-05-09 14:13:35 +02008352#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008353/* parse the "ciphersuites" server keyword */
8354static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8355{
8356 if (!*args[*cur_arg + 1]) {
8357 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8358 return ERR_ALERT | ERR_FATAL;
8359 }
8360
8361 free(newsrv->ssl_ctx.ciphersuites);
8362 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
8363 return 0;
8364}
8365#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008366
Emeric Brunef42d922012-10-11 16:11:36 +02008367/* parse the "crl-file" server keyword */
8368static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8369{
8370#ifndef X509_V_FLAG_CRL_CHECK
8371 if (err)
8372 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
8373 return ERR_ALERT | ERR_FATAL;
8374#else
8375 if (!*args[*cur_arg + 1]) {
8376 if (err)
8377 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
8378 return ERR_ALERT | ERR_FATAL;
8379 }
8380
Willy Tarreauef934602016-12-22 23:12:01 +01008381 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8382 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008383 else
8384 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
8385
8386 return 0;
8387#endif
8388}
8389
Emeric Bruna7aa3092012-10-26 12:58:00 +02008390/* parse the "crt" server keyword */
8391static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8392{
8393 if (!*args[*cur_arg + 1]) {
8394 if (err)
8395 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
8396 return ERR_ALERT | ERR_FATAL;
8397 }
8398
Willy Tarreauef934602016-12-22 23:12:01 +01008399 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01008400 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02008401 else
8402 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
8403
8404 return 0;
8405}
Emeric Brunef42d922012-10-11 16:11:36 +02008406
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008407/* parse the "no-check-ssl" server keyword */
8408static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8409{
8410 newsrv->check.use_ssl = 0;
8411 free(newsrv->ssl_ctx.ciphers);
8412 newsrv->ssl_ctx.ciphers = NULL;
8413 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
8414 return 0;
8415}
8416
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01008417/* parse the "no-send-proxy-v2-ssl" server keyword */
8418static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8419{
8420 newsrv->pp_opts &= ~SRV_PP_V2;
8421 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8422 return 0;
8423}
8424
8425/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
8426static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8427{
8428 newsrv->pp_opts &= ~SRV_PP_V2;
8429 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8430 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
8431 return 0;
8432}
8433
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008434/* parse the "no-ssl" server keyword */
8435static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8436{
8437 newsrv->use_ssl = 0;
8438 free(newsrv->ssl_ctx.ciphers);
8439 newsrv->ssl_ctx.ciphers = NULL;
8440 return 0;
8441}
8442
Olivier Houchard522eea72017-11-03 16:27:47 +01008443/* parse the "allow-0rtt" server keyword */
8444static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8445{
8446 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
8447 return 0;
8448}
8449
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01008450/* parse the "no-ssl-reuse" server keyword */
8451static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8452{
8453 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
8454 return 0;
8455}
8456
Emeric Brunf9c5c472012-10-11 15:28:34 +02008457/* parse the "no-tls-tickets" server keyword */
8458static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8459{
8460 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
8461 return 0;
8462}
David Safb76832014-05-08 23:42:08 -04008463/* parse the "send-proxy-v2-ssl" server keyword */
8464static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8465{
8466 newsrv->pp_opts |= SRV_PP_V2;
8467 newsrv->pp_opts |= SRV_PP_V2_SSL;
8468 return 0;
8469}
8470
8471/* parse the "send-proxy-v2-ssl-cn" server keyword */
8472static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8473{
8474 newsrv->pp_opts |= SRV_PP_V2;
8475 newsrv->pp_opts |= SRV_PP_V2_SSL;
8476 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
8477 return 0;
8478}
Emeric Brunf9c5c472012-10-11 15:28:34 +02008479
Willy Tarreau732eac42015-07-09 11:40:25 +02008480/* parse the "sni" server keyword */
8481static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8482{
8483#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
8484 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
8485 return ERR_ALERT | ERR_FATAL;
8486#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008487 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02008488
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008489 arg = args[*cur_arg + 1];
8490 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02008491 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
8492 return ERR_ALERT | ERR_FATAL;
8493 }
8494
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008495 free(newsrv->sni_expr);
8496 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02008497
Willy Tarreau732eac42015-07-09 11:40:25 +02008498 return 0;
8499#endif
8500}
8501
Willy Tarreau92faadf2012-10-10 23:04:25 +02008502/* parse the "ssl" server keyword */
8503static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8504{
8505 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008506 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8507 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02008508#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008509 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8510 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8511#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008512 return 0;
8513}
8514
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008515/* parse the "ssl-reuse" server keyword */
8516static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8517{
8518 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
8519 return 0;
8520}
8521
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008522/* parse the "tls-tickets" server keyword */
8523static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8524{
8525 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
8526 return 0;
8527}
8528
Emeric Brunef42d922012-10-11 16:11:36 +02008529/* parse the "verify" server keyword */
8530static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8531{
8532 if (!*args[*cur_arg + 1]) {
8533 if (err)
8534 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
8535 return ERR_ALERT | ERR_FATAL;
8536 }
8537
8538 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008539 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02008540 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008541 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02008542 else {
8543 if (err)
8544 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
8545 args[*cur_arg], args[*cur_arg + 1]);
8546 return ERR_ALERT | ERR_FATAL;
8547 }
8548
Evan Broderbe554312013-06-27 00:05:25 -07008549 return 0;
8550}
8551
8552/* parse the "verifyhost" server keyword */
8553static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8554{
8555 if (!*args[*cur_arg + 1]) {
8556 if (err)
8557 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
8558 return ERR_ALERT | ERR_FATAL;
8559 }
8560
Frédéric Lécaille273f3212017-03-13 15:52:01 +01008561 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07008562 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
8563
Emeric Brunef42d922012-10-11 16:11:36 +02008564 return 0;
8565}
8566
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008567/* parse the "ssl-default-bind-options" keyword in global section */
8568static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
8569 struct proxy *defpx, const char *file, int line,
8570 char **err) {
8571 int i = 1;
8572
8573 if (*(args[i]) == 0) {
8574 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8575 return -1;
8576 }
8577 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008578 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008579 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008580 else if (!strcmp(args[i], "prefer-client-ciphers"))
8581 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008582 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8583 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
8584 i++;
8585 else {
8586 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8587 return -1;
8588 }
8589 }
8590 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008591 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8592 return -1;
8593 }
8594 i++;
8595 }
8596 return 0;
8597}
8598
8599/* parse the "ssl-default-server-options" keyword in global section */
8600static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
8601 struct proxy *defpx, const char *file, int line,
8602 char **err) {
8603 int i = 1;
8604
8605 if (*(args[i]) == 0) {
8606 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8607 return -1;
8608 }
8609 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008610 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008611 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008612 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8613 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
8614 i++;
8615 else {
8616 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8617 return -1;
8618 }
8619 }
8620 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008621 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8622 return -1;
8623 }
8624 i++;
8625 }
8626 return 0;
8627}
8628
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008629/* parse the "ca-base" / "crt-base" keywords in global section.
8630 * Returns <0 on alert, >0 on warning, 0 on success.
8631 */
8632static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
8633 struct proxy *defpx, const char *file, int line,
8634 char **err)
8635{
8636 char **target;
8637
Willy Tarreauef934602016-12-22 23:12:01 +01008638 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008639
8640 if (too_many_args(1, args, err, NULL))
8641 return -1;
8642
8643 if (*target) {
8644 memprintf(err, "'%s' already specified.", args[0]);
8645 return -1;
8646 }
8647
8648 if (*(args[1]) == 0) {
8649 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
8650 return -1;
8651 }
8652 *target = strdup(args[1]);
8653 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008654}
8655
8656/* parse the "ssl-mode-async" keyword in global section.
8657 * Returns <0 on alert, >0 on warning, 0 on success.
8658 */
8659static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
8660 struct proxy *defpx, const char *file, int line,
8661 char **err)
8662{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008663#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008664 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01008665 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008666 return 0;
8667#else
8668 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
8669 return -1;
8670#endif
8671}
8672
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008673#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008674static int ssl_check_async_engine_count(void) {
8675 int err_code = 0;
8676
Emeric Brun3854e012017-05-17 20:42:48 +02008677 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008678 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008679 err_code = ERR_ABORT;
8680 }
8681 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008682}
8683
Grant Zhang872f9c22017-01-21 01:10:18 +00008684/* parse the "ssl-engine" keyword in global section.
8685 * Returns <0 on alert, >0 on warning, 0 on success.
8686 */
8687static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
8688 struct proxy *defpx, const char *file, int line,
8689 char **err)
8690{
8691 char *algo;
8692 int ret = -1;
8693
8694 if (*(args[1]) == 0) {
8695 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
8696 return ret;
8697 }
8698
8699 if (*(args[2]) == 0) {
8700 /* if no list of algorithms is given, it defaults to ALL */
8701 algo = strdup("ALL");
8702 goto add_engine;
8703 }
8704
8705 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
8706 if (strcmp(args[2], "algo") != 0) {
8707 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
8708 return ret;
8709 }
8710
8711 if (*(args[3]) == 0) {
8712 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
8713 return ret;
8714 }
8715 algo = strdup(args[3]);
8716
8717add_engine:
8718 if (ssl_init_single_engine(args[1], algo)==0) {
8719 openssl_engines_initialized++;
8720 ret = 0;
8721 }
8722 free(algo);
8723 return ret;
8724}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008725#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00008726
Willy Tarreauf22e9682016-12-21 23:23:19 +01008727/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
8728 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
8729 */
8730static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
8731 struct proxy *defpx, const char *file, int line,
8732 char **err)
8733{
8734 char **target;
8735
Willy Tarreauef934602016-12-22 23:12:01 +01008736 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01008737
8738 if (too_many_args(1, args, err, NULL))
8739 return -1;
8740
8741 if (*(args[1]) == 0) {
8742 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
8743 return -1;
8744 }
8745
8746 free(*target);
8747 *target = strdup(args[1]);
8748 return 0;
8749}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008750
Willy Tarreau5db847a2019-05-09 14:13:35 +02008751#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008752/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
8753 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
8754 */
8755static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
8756 struct proxy *defpx, const char *file, int line,
8757 char **err)
8758{
8759 char **target;
8760
8761 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
8762
8763 if (too_many_args(1, args, err, NULL))
8764 return -1;
8765
8766 if (*(args[1]) == 0) {
8767 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
8768 return -1;
8769 }
8770
8771 free(*target);
8772 *target = strdup(args[1]);
8773 return 0;
8774}
8775#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01008776
Willy Tarreau9ceda382016-12-21 23:13:03 +01008777/* parse various global tune.ssl settings consisting in positive integers.
8778 * Returns <0 on alert, >0 on warning, 0 on success.
8779 */
8780static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
8781 struct proxy *defpx, const char *file, int line,
8782 char **err)
8783{
8784 int *target;
8785
8786 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
8787 target = &global.tune.sslcachesize;
8788 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01008789 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01008790 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01008791 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01008792 else if (strcmp(args[0], "maxsslconn") == 0)
8793 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008794 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
8795 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01008796 else {
8797 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
8798 return -1;
8799 }
8800
8801 if (too_many_args(1, args, err, NULL))
8802 return -1;
8803
8804 if (*(args[1]) == 0) {
8805 memprintf(err, "'%s' expects an integer argument.", args[0]);
8806 return -1;
8807 }
8808
8809 *target = atoi(args[1]);
8810 if (*target < 0) {
8811 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
8812 return -1;
8813 }
8814 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008815}
8816
8817static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
8818 struct proxy *defpx, const char *file, int line,
8819 char **err)
8820{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008821 int ret;
8822
8823 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
8824 if (ret != 0)
8825 return ret;
8826
Willy Tarreaubafbe012017-11-24 17:34:44 +01008827 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008828 memprintf(err, "'%s' is already configured.", args[0]);
8829 return -1;
8830 }
8831
Willy Tarreaubafbe012017-11-24 17:34:44 +01008832 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
8833 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008834 memprintf(err, "Out of memory error.");
8835 return -1;
8836 }
8837 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01008838}
8839
8840/* parse "ssl.force-private-cache".
8841 * Returns <0 on alert, >0 on warning, 0 on success.
8842 */
8843static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
8844 struct proxy *defpx, const char *file, int line,
8845 char **err)
8846{
8847 if (too_many_args(0, args, err, NULL))
8848 return -1;
8849
Willy Tarreauef934602016-12-22 23:12:01 +01008850 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01008851 return 0;
8852}
8853
8854/* parse "ssl.lifetime".
8855 * Returns <0 on alert, >0 on warning, 0 on success.
8856 */
8857static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
8858 struct proxy *defpx, const char *file, int line,
8859 char **err)
8860{
8861 const char *res;
8862
8863 if (too_many_args(1, args, err, NULL))
8864 return -1;
8865
8866 if (*(args[1]) == 0) {
8867 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
8868 return -1;
8869 }
8870
Willy Tarreauef934602016-12-22 23:12:01 +01008871 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9ceda382016-12-21 23:13:03 +01008872 if (res) {
8873 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
8874 return -1;
8875 }
8876 return 0;
8877}
8878
8879#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01008880/* parse "ssl-dh-param-file".
8881 * Returns <0 on alert, >0 on warning, 0 on success.
8882 */
8883static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
8884 struct proxy *defpx, const char *file, int line,
8885 char **err)
8886{
8887 if (too_many_args(1, args, err, NULL))
8888 return -1;
8889
8890 if (*(args[1]) == 0) {
8891 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
8892 return -1;
8893 }
8894
8895 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
8896 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
8897 return -1;
8898 }
8899 return 0;
8900}
8901
Willy Tarreau9ceda382016-12-21 23:13:03 +01008902/* parse "ssl.default-dh-param".
8903 * Returns <0 on alert, >0 on warning, 0 on success.
8904 */
8905static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
8906 struct proxy *defpx, const char *file, int line,
8907 char **err)
8908{
8909 if (too_many_args(1, args, err, NULL))
8910 return -1;
8911
8912 if (*(args[1]) == 0) {
8913 memprintf(err, "'%s' expects an integer argument.", args[0]);
8914 return -1;
8915 }
8916
Willy Tarreauef934602016-12-22 23:12:01 +01008917 global_ssl.default_dh_param = atoi(args[1]);
8918 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01008919 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
8920 return -1;
8921 }
8922 return 0;
8923}
8924#endif
8925
8926
William Lallemand32af2032016-10-29 18:09:35 +02008927/* This function is used with TLS ticket keys management. It permits to browse
8928 * each reference. The variable <getnext> must contain the current node,
8929 * <end> point to the root node.
8930 */
8931#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
8932static inline
8933struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
8934{
8935 struct tls_keys_ref *ref = getnext;
8936
8937 while (1) {
8938
8939 /* Get next list entry. */
8940 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
8941
8942 /* If the entry is the last of the list, return NULL. */
8943 if (&ref->list == end)
8944 return NULL;
8945
8946 return ref;
8947 }
8948}
8949
8950static inline
8951struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
8952{
8953 int id;
8954 char *error;
8955
8956 /* If the reference starts by a '#', this is numeric id. */
8957 if (reference[0] == '#') {
8958 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
8959 id = strtol(reference + 1, &error, 10);
8960 if (*error != '\0')
8961 return NULL;
8962
8963 /* Perform the unique id lookup. */
8964 return tlskeys_ref_lookupid(id);
8965 }
8966
8967 /* Perform the string lookup. */
8968 return tlskeys_ref_lookup(reference);
8969}
8970#endif
8971
8972
8973#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
8974
8975static int cli_io_handler_tlskeys_files(struct appctx *appctx);
8976
8977static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
8978 return cli_io_handler_tlskeys_files(appctx);
8979}
8980
Willy Tarreauf5f26e82016-12-16 18:47:27 +01008981/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
8982 * (next index to be dumped), and cli.p0 (next key reference).
8983 */
William Lallemand32af2032016-10-29 18:09:35 +02008984static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
8985
8986 struct stream_interface *si = appctx->owner;
8987
8988 switch (appctx->st2) {
8989 case STAT_ST_INIT:
8990 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08008991 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02008992 * later and restart at the state "STAT_ST_INIT".
8993 */
8994 chunk_reset(&trash);
8995
8996 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
8997 chunk_appendf(&trash, "# id secret\n");
8998 else
8999 chunk_appendf(&trash, "# id (file)\n");
9000
Willy Tarreau06d80a92017-10-19 14:32:15 +02009001 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009002 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009003 return 0;
9004 }
9005
William Lallemand32af2032016-10-29 18:09:35 +02009006 /* Now, we start the browsing of the references lists.
9007 * Note that the following call to LIST_ELEM return bad pointer. The only
9008 * available field of this pointer is <list>. It is used with the function
9009 * tlskeys_list_get_next() for retruning the first available entry
9010 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009011 if (appctx->ctx.cli.p0 == NULL) {
9012 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9013 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009014 }
9015
9016 appctx->st2 = STAT_ST_LIST;
9017 /* fall through */
9018
9019 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009020 while (appctx->ctx.cli.p0) {
9021 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02009022
9023 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009024 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02009025 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009026
9027 if (appctx->ctx.cli.i1 == 0)
9028 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
9029
William Lallemand32af2032016-10-29 18:09:35 +02009030 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01009031 int head;
9032
9033 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
9034 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009035 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02009036 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02009037
9038 chunk_reset(t2);
9039 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01009040 if (ref->key_size_bits == 128) {
9041 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9042 sizeof(struct tls_sess_key_128),
9043 t2->area, t2->size);
9044 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9045 t2->area);
9046 }
9047 else if (ref->key_size_bits == 256) {
9048 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9049 sizeof(struct tls_sess_key_256),
9050 t2->area, t2->size);
9051 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9052 t2->area);
9053 }
9054 else {
9055 /* This case should never happen */
9056 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
9057 }
William Lallemand32af2032016-10-29 18:09:35 +02009058
Willy Tarreau06d80a92017-10-19 14:32:15 +02009059 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009060 /* let's try again later from this stream. We add ourselves into
9061 * this stream's users so that it can remove us upon termination.
9062 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01009063 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01009064 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009065 return 0;
9066 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009067 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02009068 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01009069 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009070 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009071 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02009072 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009073 /* let's try again later from this stream. We add ourselves into
9074 * this stream's users so that it can remove us upon termination.
9075 */
Willy Tarreaudb398432018-11-15 11:08:52 +01009076 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009077 return 0;
9078 }
9079
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009080 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02009081 break;
9082
9083 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009084 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009085 }
9086
9087 appctx->st2 = STAT_ST_FIN;
9088 /* fall through */
9089
9090 default:
9091 appctx->st2 = STAT_ST_FIN;
9092 return 1;
9093 }
9094 return 0;
9095}
9096
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009097/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009098static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009099{
William Lallemand32af2032016-10-29 18:09:35 +02009100 /* no parameter, shows only file list */
9101 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009102 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009103 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009104 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009105 }
9106
9107 if (args[2][0] == '*') {
9108 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009109 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009110 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009111 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
9112 if (!appctx->ctx.cli.p0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009113 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009114 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009115 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009116 return 1;
9117 }
9118 }
William Lallemand32af2032016-10-29 18:09:35 +02009119 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009120 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009121}
9122
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009123static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009124{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009125 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009126 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009127
William Lallemand32af2032016-10-29 18:09:35 +02009128 /* Expect two parameters: the filename and the new new TLS key in encoding */
9129 if (!*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009130 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009131 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009132 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009133 return 1;
9134 }
9135
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009136 ref = tlskeys_ref_lookup_ref(args[3]);
9137 if (!ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009138 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009139 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009140 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009141 return 1;
9142 }
9143
Willy Tarreau1c913e42018-08-22 05:26:57 +02009144 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Emeric Brun9e754772019-01-10 17:51:55 +01009145 if (ret < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009146 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009147 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009148 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009149 return 1;
9150 }
Emeric Brun9e754772019-01-10 17:51:55 +01009151
Willy Tarreau1c913e42018-08-22 05:26:57 +02009152 trash.data = ret;
Emeric Brun9e754772019-01-10 17:51:55 +01009153 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) {
9154 appctx->ctx.cli.severity = LOG_ERR;
9155 appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n";
9156 appctx->st0 = CLI_ST_PRINT;
9157 return 1;
9158 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009159 appctx->ctx.cli.severity = LOG_INFO;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01009160 appctx->ctx.cli.msg = "TLS ticket key updated!\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009161 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009162 return 1;
9163
9164}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009165#endif
William Lallemand32af2032016-10-29 18:09:35 +02009166
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009167static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009168{
9169#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
9170 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009171 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009172
9173 if (!payload)
9174 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +02009175
9176 /* Expect one parameter: the new response in base64 encoding */
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009177 if (!*payload) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009178 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009179 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009180 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009181 return 1;
9182 }
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009183
9184 /* remove \r and \n from the payload */
9185 for (i = 0, j = 0; payload[i]; i++) {
9186 if (payload[i] == '\r' || payload[i] == '\n')
9187 continue;
9188 payload[j++] = payload[i];
9189 }
9190 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009191
Willy Tarreau1c913e42018-08-22 05:26:57 +02009192 ret = base64dec(payload, j, trash.area, trash.size);
9193 if (ret < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009194 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009195 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009196 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009197 return 1;
9198 }
9199
Willy Tarreau1c913e42018-08-22 05:26:57 +02009200 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +02009201 if (ssl_sock_update_ocsp_response(&trash, &err)) {
9202 if (err) {
9203 memprintf(&err, "%s.\n", err);
9204 appctx->ctx.cli.err = err;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009205 appctx->st0 = CLI_ST_PRINT_FREE;
William Lallemand32af2032016-10-29 18:09:35 +02009206 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +02009207 else {
9208 appctx->ctx.cli.severity = LOG_ERR;
9209 appctx->ctx.cli.msg = "Failed to update OCSP response.\n";
9210 appctx->st0 = CLI_ST_PRINT;
9211 }
William Lallemand32af2032016-10-29 18:09:35 +02009212 return 1;
9213 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009214 appctx->ctx.cli.severity = LOG_INFO;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01009215 appctx->ctx.cli.msg = "OCSP Response updated!\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009216 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009217 return 1;
9218#else
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009219 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009220 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009221 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009222 return 1;
9223#endif
9224
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009225}
9226
Willy Tarreau86a394e2019-05-09 14:15:32 +02009227#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009228static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
9229{
9230 switch (arg->type) {
9231 case ARGT_STR:
9232 smp->data.type = SMP_T_STR;
9233 smp->data.u.str = arg->data.str;
9234 return 1;
9235 case ARGT_VAR:
9236 if (!vars_get_by_desc(&arg->data.var, smp))
9237 return 0;
9238 if (!sample_casts[smp->data.type][SMP_T_STR])
9239 return 0;
9240 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
9241 return 0;
9242 return 1;
9243 default:
9244 return 0;
9245 }
9246}
9247
9248static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
9249 const char *file, int line, char **err)
9250{
9251 switch(args[0].data.sint) {
9252 case 128:
9253 case 192:
9254 case 256:
9255 break;
9256 default:
9257 memprintf(err, "key size must be 128, 192 or 256 (bits).");
9258 return 0;
9259 }
9260 /* Try to decode a variable. */
9261 vars_check_arg(&args[1], NULL);
9262 vars_check_arg(&args[2], NULL);
9263 vars_check_arg(&args[3], NULL);
9264 return 1;
9265}
9266
9267/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
9268static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
9269{
9270 struct sample nonce, key, aead_tag;
9271 struct buffer *smp_trash, *smp_trash_alloc;
9272 EVP_CIPHER_CTX *ctx;
9273 int dec_size, ret;
9274
9275 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
9276 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
9277 return 0;
9278
9279 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
9280 if (!sample_conv_var2smp_str(&arg_p[2], &key))
9281 return 0;
9282
9283 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
9284 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
9285 return 0;
9286
9287 smp_trash = get_trash_chunk();
9288 smp_trash_alloc = alloc_trash_chunk();
9289 if (!smp_trash_alloc)
9290 return 0;
9291
9292 ctx = EVP_CIPHER_CTX_new();
9293
9294 if (!ctx)
9295 goto err;
9296
9297 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
9298 if (dec_size < 0)
9299 goto err;
9300 smp_trash->data = dec_size;
9301
9302 /* Set cipher type and mode */
9303 switch(arg_p[0].data.sint) {
9304 case 128:
9305 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
9306 break;
9307 case 192:
9308 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
9309 break;
9310 case 256:
9311 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
9312 break;
9313 }
9314
9315 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
9316
9317 /* Initialise IV */
9318 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
9319 goto err;
9320
9321 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
9322 if (dec_size < 0)
9323 goto err;
9324 smp_trash->data = dec_size;
9325
9326 /* Initialise key */
9327 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
9328 goto err;
9329
9330 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
9331 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
9332 goto err;
9333
9334 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
9335 if (dec_size < 0)
9336 goto err;
9337 smp_trash_alloc->data = dec_size;
9338 dec_size = smp_trash->data;
9339
9340 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
9341 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
9342
9343 if (ret <= 0)
9344 goto err;
9345
9346 smp->data.u.str.data = dec_size + smp_trash->data;
9347 smp->data.u.str.area = smp_trash->area;
9348 smp->data.type = SMP_T_BIN;
9349 smp->flags &= ~SMP_F_CONST;
9350 free_trash_chunk(smp_trash_alloc);
9351 return 1;
9352
9353err:
9354 free_trash_chunk(smp_trash_alloc);
9355 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009356}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009357# endif
William Lallemand32af2032016-10-29 18:09:35 +02009358
9359/* register cli keywords */
9360static struct cli_kw_list cli_kws = {{ },{
9361#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9362 { { "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 +02009363 { { "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 +02009364#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009365 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02009366 { { NULL }, NULL, NULL, NULL }
9367}};
9368
Willy Tarreau0108d902018-11-25 19:14:37 +01009369INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +02009370
Willy Tarreau7875d092012-09-10 08:20:03 +02009371/* Note: must not be declared <const> as its list will be overwritten.
9372 * Please take care of keeping this list alphabetically sorted.
9373 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009374static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +02009375 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009376 { "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 +01009377#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +01009378 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009379#endif
Emeric Brun645ae792014-04-30 14:21:06 +02009380 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009381#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9382 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
9383#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +01009384 { "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 +02009385 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +02009386 { "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 +02009387 { "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 +02009388#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +02009389 { "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 -04009390#endif
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009391#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL)
Patrick Hemmere0275472018-04-28 19:15:51 -04009392 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9393#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009394 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9395 { "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 +01009396 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009397 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009398 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9399 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9400 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9401 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9402 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9403 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9404 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9405 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009406 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009407 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9408 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +01009409 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009410 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9411 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9412 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9413 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9414 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9415 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9416 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +02009417 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009418 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009419 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009420 { "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 +01009421 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009422 { "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 +02009423 { "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 +01009424 { "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 +02009425 { "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 +01009426#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009427 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02009428#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009429#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009430 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +02009431#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009432 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009433#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +02009434 { "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 -04009435#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009436 { "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 +02009437#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009438 { "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 -04009439#endif
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009440#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL)
Patrick Hemmere0275472018-04-28 19:15:51 -04009441 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9442#endif
Patrick Hemmer41966772018-04-28 19:15:48 -04009443#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009444 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -04009445#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009446 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9447 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9448 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9449 { "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 +02009450 { NULL, NULL, 0, 0, 0 },
9451}};
9452
Willy Tarreau0108d902018-11-25 19:14:37 +01009453INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
9454
Willy Tarreau7875d092012-09-10 08:20:03 +02009455/* Note: must not be declared <const> as its list will be overwritten.
9456 * Please take care of keeping this list alphabetically sorted.
9457 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009458static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01009459 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
9460 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +01009461 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +02009462}};
9463
Willy Tarreau0108d902018-11-25 19:14:37 +01009464INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
9465
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009466/* Note: must not be declared <const> as its list will be overwritten.
9467 * Please take care of keeping this list alphabetically sorted, doing so helps
9468 * all code contributors.
9469 * Optional keywords are also declared with a NULL ->parse() function so that
9470 * the config parser can report an appropriate error when a known keyword was
9471 * not enabled.
9472 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009473static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009474 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009475 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9476 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9477 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Willy Tarreau5db847a2019-05-09 14:13:35 +02009478#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009479 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9480#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009481 { "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 +01009482 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009483 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009484 { "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 +01009485 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009486 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
9487 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009488 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
9489 { NULL, NULL, 0 },
9490};
9491
Willy Tarreau0108d902018-11-25 19:14:37 +01009492/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
9493
Willy Tarreau51fb7652012-09-18 18:24:39 +02009494static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009495 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009496 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9497 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9498 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
9499 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
9500 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
9501 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Willy Tarreau5db847a2019-05-09 14:13:35 +02009502#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009503 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9504#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009505 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
9506 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
9507 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
9508 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
9509 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
9510 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
9511 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
9512 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
9513 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
9514 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009515 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009516 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009517 { "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 +02009518 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
9519 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
9520 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
9521 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009522 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009523 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
9524 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009525 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
9526 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009527 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
9528 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
9529 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
9530 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
9531 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009532 { NULL, NULL, 0 },
9533}};
Emeric Brun46591952012-05-18 15:47:34 +02009534
Willy Tarreau0108d902018-11-25 19:14:37 +01009535INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
9536
Willy Tarreau92faadf2012-10-10 23:04:25 +02009537/* Note: must not be declared <const> as its list will be overwritten.
9538 * Please take care of keeping this list alphabetically sorted, doing so helps
9539 * all code contributors.
9540 * Optional keywords are also declared with a NULL ->parse() function so that
9541 * the config parser can report an appropriate error when a known keyword was
9542 * not enabled.
9543 */
9544static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +01009545 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +01009546 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009547 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +01009548 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +02009549 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009550 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
9551 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Willy Tarreau5db847a2019-05-09 14:13:35 +02009552#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009553 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
9554#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009555 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
9556 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
9557 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
9558 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
9559 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
9560 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
9561 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
9562 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
9563 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
9564 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
9565 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
9566 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
9567 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
9568 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
9569 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
9570 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
9571 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
9572 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +01009573 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009574 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
9575 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
9576 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
9577 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
9578 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
9579 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
9580 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
9581 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
9582 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
9583 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +02009584 { NULL, NULL, 0, 0 },
9585}};
9586
Willy Tarreau0108d902018-11-25 19:14:37 +01009587INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
9588
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009589static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009590 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
9591 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009592 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009593 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
9594 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +01009595#ifndef OPENSSL_NO_DH
9596 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
9597#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009598 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009599#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00009600 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009601#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +01009602 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
9603#ifndef OPENSSL_NO_DH
9604 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
9605#endif
9606 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
9607 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
9608 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
9609 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009610 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +01009611 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
9612 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Willy Tarreau5db847a2019-05-09 14:13:35 +02009613#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009614 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
9615 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
9616#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009617 { 0, NULL, NULL },
9618}};
9619
Willy Tarreau0108d902018-11-25 19:14:37 +01009620INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
9621
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009622/* Note: must not be declared <const> as its list will be overwritten */
9623static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +02009624#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009625 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
9626#endif
9627 { NULL, NULL, 0, 0, 0 },
9628}};
9629
9630INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
9631
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02009632/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +01009633static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02009634 .snd_buf = ssl_sock_from_buf,
9635 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +01009636 .subscribe = ssl_subscribe,
9637 .unsubscribe = ssl_unsubscribe,
Emeric Brun46591952012-05-18 15:47:34 +02009638 .rcv_pipe = NULL,
9639 .snd_pipe = NULL,
9640 .shutr = NULL,
9641 .shutw = ssl_sock_shutw,
9642 .close = ssl_sock_close,
9643 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +01009644 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +01009645 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +01009646 .prepare_srv = ssl_sock_prepare_srv_ctx,
9647 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +01009648 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01009649 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +02009650};
9651
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009652enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
9653 struct session *sess, struct stream *s, int flags)
9654{
9655 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009656 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009657
9658 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009659 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009660
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009661 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009662 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009663 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009664 s->req.flags |= CF_READ_NULL;
9665 return ACT_RET_YIELD;
9666 }
9667 }
9668 return (ACT_RET_CONT);
9669}
9670
9671static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
9672{
9673 rule->action_ptr = ssl_action_wait_for_hs;
9674
9675 return ACT_RET_PRS_OK;
9676}
9677
9678static struct action_kw_list http_req_actions = {ILH, {
9679 { "wait-for-handshake", ssl_parse_wait_for_hs },
9680 { /* END */ }
9681}};
9682
Willy Tarreau0108d902018-11-25 19:14:37 +01009683INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
9684
Willy Tarreau5db847a2019-05-09 14:13:35 +02009685#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01009686
9687static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
9688{
9689 if (ptr) {
9690 chunk_destroy(ptr);
9691 free(ptr);
9692 }
9693}
9694
9695#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01009696static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
9697{
Willy Tarreaubafbe012017-11-24 17:34:44 +01009698 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01009699}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01009700
Emeric Brun46591952012-05-18 15:47:34 +02009701__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02009702static void __ssl_sock_init(void)
9703{
Emeric Brun46591952012-05-18 15:47:34 +02009704 STACK_OF(SSL_COMP)* cm;
9705
Willy Tarreauef934602016-12-22 23:12:01 +01009706 if (global_ssl.listen_default_ciphers)
9707 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
9708 if (global_ssl.connect_default_ciphers)
9709 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Willy Tarreau5db847a2019-05-09 14:13:35 +02009710#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009711 if (global_ssl.listen_default_ciphersuites)
9712 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9713 if (global_ssl.connect_default_ciphersuites)
9714 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9715#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +01009716
Willy Tarreau13e14102016-12-22 20:25:26 +01009717 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009718#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +02009719 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -08009720#endif
Emeric Brun46591952012-05-18 15:47:34 +02009721 cm = SSL_COMP_get_compression_methods();
9722 sk_SSL_COMP_zero(cm);
Willy Tarreau5db847a2019-05-09 14:13:35 +02009723#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +02009724 ssl_locking_init();
9725#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +02009726#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01009727 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
9728#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +02009729 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +02009730 ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01009731 ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009732#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00009733 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009734 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009735#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +01009736#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9737 hap_register_post_check(tlskeys_finalize_config);
9738#endif
Willy Tarreau80713382018-11-26 10:19:54 +01009739
9740 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
9741 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
9742
9743#ifndef OPENSSL_NO_DH
9744 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
9745 hap_register_post_deinit(ssl_free_dh);
9746#endif
9747#ifndef OPENSSL_NO_ENGINE
9748 hap_register_post_deinit(ssl_free_engines);
9749#endif
9750 /* Load SSL string for the verbose & debug mode. */
9751 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +02009752 ha_meth = BIO_meth_new(0x666, "ha methods");
9753 BIO_meth_set_write(ha_meth, ha_ssl_write);
9754 BIO_meth_set_read(ha_meth, ha_ssl_read);
9755 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
9756 BIO_meth_set_create(ha_meth, ha_ssl_new);
9757 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
9758 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
9759 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
Willy Tarreau80713382018-11-26 10:19:54 +01009760}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +01009761
Willy Tarreau80713382018-11-26 10:19:54 +01009762/* Compute and register the version string */
9763static void ssl_register_build_options()
9764{
9765 char *ptr = NULL;
9766 int i;
9767
Willy Tarreauc2c0b612016-12-21 19:23:20 +01009768 memprintf(&ptr, "Built with OpenSSL version : "
9769#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01009770 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01009771#else /* OPENSSL_IS_BORINGSSL */
9772 OPENSSL_VERSION_TEXT
9773 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -08009774 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +02009775 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01009776#endif
9777 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009778#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +01009779 "no (library version too old)"
9780#elif defined(OPENSSL_NO_TLSEXT)
9781 "no (disabled via OPENSSL_NO_TLSEXT)"
9782#else
9783 "yes"
9784#endif
9785 "", ptr);
9786
9787 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
9788#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
9789 "yes"
9790#else
9791#ifdef OPENSSL_NO_TLSEXT
9792 "no (because of OPENSSL_NO_TLSEXT)"
9793#else
9794 "no (version might be too old, 0.9.8f min needed)"
9795#endif
9796#endif
9797 "", ptr);
9798
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +02009799 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
9800 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9801 if (methodVersions[i].option)
9802 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01009803
Willy Tarreauc2c0b612016-12-21 19:23:20 +01009804 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +01009805}
Willy Tarreauc2c0b612016-12-21 19:23:20 +01009806
Willy Tarreau80713382018-11-26 10:19:54 +01009807INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +02009808
Emeric Brun46591952012-05-18 15:47:34 +02009809
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009810#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00009811void ssl_free_engines(void) {
9812 struct ssl_engine_list *wl, *wlb;
9813 /* free up engine list */
9814 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
9815 ENGINE_finish(wl->e);
9816 ENGINE_free(wl->e);
9817 LIST_DEL(&wl->list);
9818 free(wl);
9819 }
9820}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009821#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +02009822
Remi Gacogned3a23c32015-05-28 16:39:47 +02009823#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +00009824void ssl_free_dh(void) {
9825 if (local_dh_1024) {
9826 DH_free(local_dh_1024);
9827 local_dh_1024 = NULL;
9828 }
9829 if (local_dh_2048) {
9830 DH_free(local_dh_2048);
9831 local_dh_2048 = NULL;
9832 }
9833 if (local_dh_4096) {
9834 DH_free(local_dh_4096);
9835 local_dh_4096 = NULL;
9836 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02009837 if (global_dh) {
9838 DH_free(global_dh);
9839 global_dh = NULL;
9840 }
Grant Zhang872f9c22017-01-21 01:10:18 +00009841}
9842#endif
9843
9844__attribute__((destructor))
9845static void __ssl_sock_deinit(void)
9846{
9847#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02009848 if (ssl_ctx_lru_tree) {
9849 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01009850 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +02009851 }
Remi Gacogned3a23c32015-05-28 16:39:47 +02009852#endif
9853
Willy Tarreau5db847a2019-05-09 14:13:35 +02009854#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +02009855 ERR_remove_state(0);
9856 ERR_free_strings();
9857
9858 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -08009859#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +02009860
Willy Tarreau5db847a2019-05-09 14:13:35 +02009861#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +02009862 CRYPTO_cleanup_all_ex_data();
9863#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +02009864 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +02009865}
9866
9867
Emeric Brun46591952012-05-18 15:47:34 +02009868/*
9869 * Local variables:
9870 * c-indent-level: 8
9871 * c-basic-offset: 8
9872 * End:
9873 */