blob: c6878e4608aee04caffd96fff9a8eb87eaa987ae [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
William Lallemand32af2032016-10-29 18:09:35 +020062#include <types/applet.h>
63#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020064#include <types/global.h>
65#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020066#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020067
Willy Tarreau7875d092012-09-10 08:20:03 +020068#include <proto/acl.h>
69#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020070#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020072#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/fd.h>
74#include <proto/freq_ctr.h>
75#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020076#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020077#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010078#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020079#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020080#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020081#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020082#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020083#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020084#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020085#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020086#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020087#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010089#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020090
Willy Tarreau9356dac2019-05-10 09:22:53 +020091/* ***** READ THIS before adding code here! *****
92 *
93 * Due to API incompatibilities between multiple OpenSSL versions and their
94 * derivatives, it's often tempting to add macros to (re-)define certain
95 * symbols. Please do not do this here, and do it in common/openssl-compat.h
96 * exclusively so that the whole code consistently uses the same macros.
97 *
98 * Whenever possible if a macro is missing in certain versions, it's better
99 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
100 */
101
Willy Tarreau518cedd2014-02-17 15:43:01 +0100102/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200103#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100104#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100105#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200106#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
107
Emeric Brunf282a812012-09-21 15:27:54 +0200108/* bits 0xFFFF0000 are reserved to store verify errors */
109
110/* Verify errors macros */
111#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
112#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
113#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
114
115#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
116#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
117#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119/* ssl_methods flags for ssl options */
120#define MC_SSL_O_ALL 0x0000
121#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
122#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
123#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
124#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200125#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200126
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;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200160#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
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
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200182#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
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 Houchard5149b592019-05-23 17:47:36 +0200214 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100215 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200216 struct wait_event wait_event;
217 struct wait_event *recv_wait;
218 struct wait_event *send_wait;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100219 int xprt_st; /* transport layer state, initialized to zero */
220 int tmp_early_data; /* 1st byte of early data, if any */
221 int sent_early_data; /* Amount of early data we sent so far */
222
Olivier Houchard66ab4982019-02-26 18:37:15 +0100223};
224
225DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
226
Olivier Houchardea8dd942019-05-20 14:02:16 +0200227static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200228static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200229
Olivier Houcharda8955d52019-04-07 22:00:38 +0200230/* Methods to implement OpenSSL BIO */
231static int ha_ssl_write(BIO *h, const char *buf, int num)
232{
233 struct buffer tmpbuf;
234 struct ssl_sock_ctx *ctx;
235 int ret;
236
237 ctx = BIO_get_data(h);
238 tmpbuf.size = num;
239 tmpbuf.area = (void *)(uintptr_t)buf;
240 tmpbuf.data = num;
241 tmpbuf.head = 0;
242 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200243 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200244 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200245 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200246 } else if (ret == 0)
247 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200248 return ret;
249}
250
251static int ha_ssl_gets(BIO *h, char *buf, int size)
252{
253
254 return 0;
255}
256
257static int ha_ssl_puts(BIO *h, const char *str)
258{
259
260 return ha_ssl_write(h, str, strlen(str));
261}
262
263static int ha_ssl_read(BIO *h, char *buf, int size)
264{
265 struct buffer tmpbuf;
266 struct ssl_sock_ctx *ctx;
267 int ret;
268
269 ctx = BIO_get_data(h);
270 tmpbuf.size = size;
271 tmpbuf.area = buf;
272 tmpbuf.data = 0;
273 tmpbuf.head = 0;
274 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200275 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200276 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200277 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200278 } else if (ret == 0)
279 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200280
281 return ret;
282}
283
284static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
285{
286 int ret = 0;
287 switch (cmd) {
288 case BIO_CTRL_DUP:
289 case BIO_CTRL_FLUSH:
290 ret = 1;
291 break;
292 }
293 return ret;
294}
295
296static int ha_ssl_new(BIO *h)
297{
298 BIO_set_init(h, 1);
299 BIO_set_data(h, NULL);
300 BIO_clear_flags(h, ~0);
301 return 1;
302}
303
304static int ha_ssl_free(BIO *data)
305{
306
307 return 1;
308}
309
310
Willy Tarreau5db847a2019-05-09 14:13:35 +0200311#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100312
Emeric Brun821bb9b2017-06-15 16:37:39 +0200313static HA_RWLOCK_T *ssl_rwlocks;
314
315
316unsigned long ssl_id_function(void)
317{
318 return (unsigned long)tid;
319}
320
321void ssl_locking_function(int mode, int n, const char * file, int line)
322{
323 if (mode & CRYPTO_LOCK) {
324 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100325 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200326 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200328 }
329 else {
330 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200332 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200334 }
335}
336
337static int ssl_locking_init(void)
338{
339 int i;
340
341 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
342 if (!ssl_rwlocks)
343 return -1;
344
345 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200347
348 CRYPTO_set_id_callback(ssl_id_function);
349 CRYPTO_set_locking_callback(ssl_locking_function);
350
351 return 0;
352}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100353
Emeric Brun821bb9b2017-06-15 16:37:39 +0200354#endif
355
William Lallemand150bfa82019-09-19 17:12:49 +0200356__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200357
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100358/* Uncommitted CKCH transaction */
359
360static struct {
361 struct ckch_store *new_ckchs;
362 struct ckch_store *old_ckchs;
363 char *path;
364} ckchs_transaction;
365
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100366/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100367struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100368 unsigned long long int xxh64;
369 unsigned char ciphersuite_len;
370 char ciphersuite[0];
371};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100372struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100373static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200374static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100375
Emmanuel Hocdet96b78342017-10-31 15:46:07 +0100376static int ssl_pkey_info_index = -1;
377
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200378#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
379struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
380#endif
381
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200382#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000383static unsigned int openssl_engines_initialized;
384struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
385struct ssl_engine_list {
386 struct list list;
387 ENGINE *e;
388};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200389#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000390
Remi Gacogne8de54152014-07-15 11:36:40 +0200391#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200392static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200393static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200394static DH *local_dh_1024 = NULL;
395static DH *local_dh_2048 = NULL;
396static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100397static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200398#endif /* OPENSSL_NO_DH */
399
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100400#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200401/* X509V3 Extensions that will be added on generated certificates */
402#define X509V3_EXT_SIZE 5
403static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
404 "basicConstraints",
405 "nsComment",
406 "subjectKeyIdentifier",
407 "authorityKeyIdentifier",
408 "keyUsage",
409};
410static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
411 "CA:FALSE",
412 "\"OpenSSL Generated Certificate\"",
413 "hash",
414 "keyid,issuer:always",
415 "nonRepudiation,digitalSignature,keyEncipherment"
416};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200417/* LRU cache to store generated certificate */
418static struct lru64_head *ssl_ctx_lru_tree = NULL;
419static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200420static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100421__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200422
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200423#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
424
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100425static struct ssl_bind_kw ssl_bind_kws[];
426
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200427#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500428/* The order here matters for picking a default context,
429 * keep the most common keytype at the bottom of the list
430 */
431const char *SSL_SOCK_KEYTYPE_NAMES[] = {
432 "dsa",
433 "ecdsa",
434 "rsa"
435};
436#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100437#else
438#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500439#endif
440
William Lallemandc3cd35f2017-11-28 11:04:43 +0100441static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100442static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
443
444#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
445
446#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
447 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
448
449#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
450 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200451
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100452/*
453 * This function gives the detail of the SSL error. It is used only
454 * if the debug mode and the verbose mode are activated. It dump all
455 * the SSL error until the stack was empty.
456 */
457static forceinline void ssl_sock_dump_errors(struct connection *conn)
458{
459 unsigned long ret;
460
461 if (unlikely(global.mode & MODE_DEBUG)) {
462 while(1) {
463 ret = ERR_get_error();
464 if (ret == 0)
465 return;
466 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200467 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100468 ERR_func_error_string(ret), ERR_reason_error_string(ret));
469 }
470 }
471}
472
yanbzhube2774d2015-12-10 15:07:30 -0500473
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200474#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000475static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
476{
477 int err_code = ERR_ABORT;
478 ENGINE *engine;
479 struct ssl_engine_list *el;
480
481 /* grab the structural reference to the engine */
482 engine = ENGINE_by_id(engine_id);
483 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100484 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000485 goto fail_get;
486 }
487
488 if (!ENGINE_init(engine)) {
489 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100490 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000491 goto fail_init;
492 }
493
494 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100495 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000496 goto fail_set_method;
497 }
498
499 el = calloc(1, sizeof(*el));
500 el->e = engine;
501 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100502 nb_engines++;
503 if (global_ssl.async)
504 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000505 return 0;
506
507fail_set_method:
508 /* release the functional reference from ENGINE_init() */
509 ENGINE_finish(engine);
510
511fail_init:
512 /* release the structural reference from ENGINE_by_id() */
513 ENGINE_free(engine);
514
515fail_get:
516 return err_code;
517}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200518#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000519
Willy Tarreau5db847a2019-05-09 14:13:35 +0200520#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200521/*
522 * openssl async fd handler
523 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200524void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000525{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200526 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000527
Emeric Brun3854e012017-05-17 20:42:48 +0200528 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000529 * to poll this fd until it is requested
530 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000531 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000532 fd_cant_recv(fd);
533
534 /* crypto engine is available, let's notify the associated
535 * connection that it can pursue its processing.
536 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200537 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000538}
539
Emeric Brun3854e012017-05-17 20:42:48 +0200540/*
541 * openssl async delayed SSL_free handler
542 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200543void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000544{
545 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200546 OSSL_ASYNC_FD all_fd[32];
547 size_t num_all_fds = 0;
548 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000549
Emeric Brun3854e012017-05-17 20:42:48 +0200550 /* We suppose that the async job for a same SSL *
551 * are serialized. So if we are awake it is
552 * because the running job has just finished
553 * and we can remove all async fds safely
554 */
555 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
556 if (num_all_fds > 32) {
557 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
558 return;
559 }
560
561 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
562 for (i=0 ; i < num_all_fds ; i++)
563 fd_remove(all_fd[i]);
564
565 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000566 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100567 _HA_ATOMIC_SUB(&sslconns, 1);
568 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000569}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000570/*
Emeric Brun3854e012017-05-17 20:42:48 +0200571 * function used to manage a returned SSL_ERROR_WANT_ASYNC
572 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000573 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200574static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000575{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100576 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200577 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200578 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000579 size_t num_add_fds = 0;
580 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200581 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000582
583 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
584 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200585 if (num_add_fds > 32 || num_del_fds > 32) {
586 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 +0000587 return;
588 }
589
Emeric Brun3854e012017-05-17 20:42:48 +0200590 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000591
Emeric Brun3854e012017-05-17 20:42:48 +0200592 /* We remove unused fds from the fdtab */
593 for (i=0 ; i < num_del_fds ; i++)
594 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000595
Emeric Brun3854e012017-05-17 20:42:48 +0200596 /* We add new fds to the fdtab */
597 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200598 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000599 }
600
Emeric Brun3854e012017-05-17 20:42:48 +0200601 num_add_fds = 0;
602 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
603 if (num_add_fds > 32) {
604 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
605 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000606 }
Emeric Brun3854e012017-05-17 20:42:48 +0200607
608 /* We activate the polling for all known async fds */
609 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000610 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200611 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000612 /* To ensure that the fd cache won't be used
613 * We'll prefer to catch a real RD event
614 * because handling an EAGAIN on this fd will
615 * result in a context switch and also
616 * some engines uses a fd in blocking mode.
617 */
618 fd_cant_recv(add_fd[i]);
619 }
Emeric Brun3854e012017-05-17 20:42:48 +0200620
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000621}
622#endif
623
William Lallemand104a7a62019-10-14 14:14:59 +0200624#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200625/*
626 * This function returns the number of seconds elapsed
627 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
628 * date presented un ASN1_GENERALIZEDTIME.
629 *
630 * In parsing error case, it returns -1.
631 */
632static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
633{
634 long epoch;
635 char *p, *end;
636 const unsigned short month_offset[12] = {
637 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
638 };
639 int year, month;
640
641 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
642
643 p = (char *)d->data;
644 end = p + d->length;
645
646 if (end - p < 4) return -1;
647 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
648 p += 4;
649 if (end - p < 2) return -1;
650 month = 10 * (p[0] - '0') + p[1] - '0';
651 if (month < 1 || month > 12) return -1;
652 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
653 We consider leap years and the current month (<marsh or not) */
654 epoch = ( ((year - 1970) * 365)
655 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
656 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
657 + month_offset[month-1]
658 ) * 24 * 60 * 60;
659 p += 2;
660 if (end - p < 2) return -1;
661 /* Add the number of seconds of completed days of current month */
662 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
663 p += 2;
664 if (end - p < 2) return -1;
665 /* Add the completed hours of the current day */
666 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
667 p += 2;
668 if (end - p < 2) return -1;
669 /* Add the completed minutes of the current hour */
670 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
671 p += 2;
672 if (p == end) return -1;
673 /* Test if there is available seconds */
674 if (p[0] < '0' || p[0] > '9')
675 goto nosec;
676 if (end - p < 2) return -1;
677 /* Add the seconds of the current minute */
678 epoch += 10 * (p[0] - '0') + p[1] - '0';
679 p += 2;
680 if (p == end) return -1;
681 /* Ignore seconds float part if present */
682 if (p[0] == '.') {
683 do {
684 if (++p == end) return -1;
685 } while (p[0] >= '0' && p[0] <= '9');
686 }
687
688nosec:
689 if (p[0] == 'Z') {
690 if (end - p != 1) return -1;
691 return epoch;
692 }
693 else if (p[0] == '+') {
694 if (end - p != 5) return -1;
695 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700696 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 +0200697 }
698 else if (p[0] == '-') {
699 if (end - p != 5) return -1;
700 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700701 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 +0200702 }
703
704 return -1;
705}
706
William Lallemand104a7a62019-10-14 14:14:59 +0200707/*
708 * struct alignment works here such that the key.key is the same as key_data
709 * Do not change the placement of key_data
710 */
711struct certificate_ocsp {
712 struct ebmb_node key;
713 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
714 struct buffer response;
715 long expire;
716};
717
718struct ocsp_cbk_arg {
719 int is_single;
720 int single_kt;
721 union {
722 struct certificate_ocsp *s_ocsp;
723 /*
724 * m_ocsp will have multiple entries dependent on key type
725 * Entry 0 - DSA
726 * Entry 1 - ECDSA
727 * Entry 2 - RSA
728 */
729 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
730 };
731};
732
Emeric Brun1d3865b2014-06-20 15:37:32 +0200733static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200734
735/* This function starts to check if the OCSP response (in DER format) contained
736 * in chunk 'ocsp_response' is valid (else exits on error).
737 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
738 * contained in the OCSP Response and exits on error if no match.
739 * If it's a valid OCSP Response:
740 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
741 * pointed by 'ocsp'.
742 * If 'ocsp' is NULL, the function looks up into the OCSP response's
743 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
744 * from the response) and exits on error if not found. Finally, If an OCSP response is
745 * already present in the container, it will be overwritten.
746 *
747 * Note: OCSP response containing more than one OCSP Single response is not
748 * considered valid.
749 *
750 * Returns 0 on success, 1 in error case.
751 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200752static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
753 struct certificate_ocsp *ocsp,
754 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200755{
756 OCSP_RESPONSE *resp;
757 OCSP_BASICRESP *bs = NULL;
758 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200759 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200761 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200762 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200763 int reason;
764 int ret = 1;
765
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200766 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
767 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200768 if (!resp) {
769 memprintf(err, "Unable to parse OCSP response");
770 goto out;
771 }
772
773 rc = OCSP_response_status(resp);
774 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
775 memprintf(err, "OCSP response status not successful");
776 goto out;
777 }
778
779 bs = OCSP_response_get1_basic(resp);
780 if (!bs) {
781 memprintf(err, "Failed to get basic response from OCSP Response");
782 goto out;
783 }
784
785 count_sr = OCSP_resp_count(bs);
786 if (count_sr > 1) {
787 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
788 goto out;
789 }
790
791 sr = OCSP_resp_get0(bs, 0);
792 if (!sr) {
793 memprintf(err, "Failed to get OCSP single response");
794 goto out;
795 }
796
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200797 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
798
Emeric Brun4147b2e2014-06-16 18:36:30 +0200799 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200800 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200801 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200802 goto out;
803 }
804
Emeric Brun13a6b482014-06-20 15:44:34 +0200805 if (!nextupd) {
806 memprintf(err, "OCSP single response: missing nextupdate");
807 goto out;
808 }
809
Emeric Brunc8b27b62014-06-19 14:16:17 +0200810 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200811 if (!rc) {
812 memprintf(err, "OCSP single response: no longer valid.");
813 goto out;
814 }
815
816 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200817 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200818 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
819 goto out;
820 }
821 }
822
823 if (!ocsp) {
824 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
825 unsigned char *p;
826
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200827 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200828 if (!rc) {
829 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
830 goto out;
831 }
832
833 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
834 memprintf(err, "OCSP single response: Certificate ID too long");
835 goto out;
836 }
837
838 p = key;
839 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200840 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200841 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
842 if (!ocsp) {
843 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
844 goto out;
845 }
846 }
847
848 /* According to comments on "chunk_dup", the
849 previous chunk buffer will be freed */
850 if (!chunk_dup(&ocsp->response, ocsp_response)) {
851 memprintf(err, "OCSP response: Memory allocation error");
852 goto out;
853 }
854
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200855 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
856
Emeric Brun4147b2e2014-06-16 18:36:30 +0200857 ret = 0;
858out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100859 ERR_clear_error();
860
Emeric Brun4147b2e2014-06-16 18:36:30 +0200861 if (bs)
862 OCSP_BASICRESP_free(bs);
863
864 if (resp)
865 OCSP_RESPONSE_free(resp);
866
867 return ret;
868}
869/*
870 * External function use to update the OCSP response in the OCSP response's
871 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
872 * to update in DER format.
873 *
874 * Returns 0 on success, 1 in error case.
875 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200876int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200877{
878 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
879}
880
William Lallemand4a660132019-10-14 14:51:41 +0200881#endif
882
883#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200884/*
885 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +0200886 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +0200887 *
888 * Returns 0 on success, 1 in error case.
889 */
William Lallemand3b5f3602019-10-16 18:05:05 +0200890static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200891{
892 int fd = -1;
893 int r = 0;
894 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +0200895 struct buffer *ocsp_response;
896 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200897
William Lallemand3b5f3602019-10-16 18:05:05 +0200898 if (buf) {
899 int i, j;
900 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +0200901
William Lallemand3b5f3602019-10-16 18:05:05 +0200902 /* remove \r and \n from the payload */
903 for (i = 0, j = 0; buf[i]; i++) {
904 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +0200905 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +0200906 buf[j++] = buf[i];
907 }
908 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200909
William Lallemand3b5f3602019-10-16 18:05:05 +0200910 ret = base64dec(buf, j, trash.area, trash.size);
911 if (ret < 0) {
912 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200913 goto end;
914 }
William Lallemand3b5f3602019-10-16 18:05:05 +0200915 trash.data = ret;
916 src = &trash;
917 } else {
918 fd = open(ocsp_path, O_RDONLY);
919 if (fd == -1) {
920 memprintf(err, "Error opening OCSP response file");
921 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200922 }
William Lallemand3b5f3602019-10-16 18:05:05 +0200923
924 trash.data = 0;
925 while (trash.data < trash.size) {
926 r = read(fd, trash.area + trash.data, trash.size - trash.data);
927 if (r < 0) {
928 if (errno == EINTR)
929 continue;
930
931 memprintf(err, "Error reading OCSP response from file");
932 goto end;
933 }
934 else if (r == 0) {
935 break;
936 }
937 trash.data += r;
938 }
939 close(fd);
940 fd = -1;
941 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200942 }
943
William Lallemand3b5f3602019-10-16 18:05:05 +0200944 ocsp_response = calloc(1, sizeof(*ocsp_response));
945 if (!chunk_dup(ocsp_response, src)) {
946 free(ocsp_response);
947 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +0200948 goto end;
949 }
950
William Lallemand3b5f3602019-10-16 18:05:05 +0200951 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +0200952 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200953end:
954 if (fd != -1)
955 close(fd);
956
957 return ret;
958}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100959#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +0200960
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100961#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
962static 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)
963{
Christopher Faulet16f45c82018-02-16 11:23:49 +0100964 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +0100965 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100966 struct connection *conn;
967 int head;
968 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100969 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100970
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200971 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +0200972 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100973 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
974
975 keys = ref->tlskeys;
976 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100977
978 if (enc) {
979 memcpy(key_name, keys[head].name, 16);
980
981 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +0100982 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100983
Emeric Brun9e754772019-01-10 17:51:55 +0100984 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100985
Emeric Brun9e754772019-01-10 17:51:55 +0100986 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
987 goto end;
988
Willy Tarreau9356dac2019-05-10 09:22:53 +0200989 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100990 ret = 1;
991 }
992 else if (ref->key_size_bits == 256 ) {
993
994 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
995 goto end;
996
Willy Tarreau9356dac2019-05-10 09:22:53 +0200997 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100998 ret = 1;
999 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001000 } else {
1001 for (i = 0; i < TLS_TICKETS_NO; i++) {
1002 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1003 goto found;
1004 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001005 ret = 0;
1006 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001007
Christopher Faulet16f45c82018-02-16 11:23:49 +01001008 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001009 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001010 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 +01001011 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1012 goto end;
1013 /* 2 for key renewal, 1 if current key is still valid */
1014 ret = i ? 2 : 1;
1015 }
1016 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001017 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 +01001018 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1019 goto end;
1020 /* 2 for key renewal, 1 if current key is still valid */
1021 ret = i ? 2 : 1;
1022 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001023 }
Emeric Brun9e754772019-01-10 17:51:55 +01001024
Christopher Faulet16f45c82018-02-16 11:23:49 +01001025 end:
1026 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1027 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001028}
1029
1030struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1031{
1032 struct tls_keys_ref *ref;
1033
1034 list_for_each_entry(ref, &tlskeys_reference, list)
1035 if (ref->filename && strcmp(filename, ref->filename) == 0)
1036 return ref;
1037 return NULL;
1038}
1039
1040struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1041{
1042 struct tls_keys_ref *ref;
1043
1044 list_for_each_entry(ref, &tlskeys_reference, list)
1045 if (ref->unique_id == unique_id)
1046 return ref;
1047 return NULL;
1048}
1049
Emeric Brun9e754772019-01-10 17:51:55 +01001050/* Update the key into ref: if keysize doesnt
1051 * match existing ones, this function returns -1
1052 * else it returns 0 on success.
1053 */
1054int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001055 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001056{
Emeric Brun9e754772019-01-10 17:51:55 +01001057 if (ref->key_size_bits == 128) {
1058 if (tlskey->data != sizeof(struct tls_sess_key_128))
1059 return -1;
1060 }
1061 else if (ref->key_size_bits == 256) {
1062 if (tlskey->data != sizeof(struct tls_sess_key_256))
1063 return -1;
1064 }
1065 else
1066 return -1;
1067
Christopher Faulet16f45c82018-02-16 11:23:49 +01001068 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001069 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1070 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001071 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1072 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001073
1074 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001075}
1076
Willy Tarreau83061a82018-07-13 11:56:34 +02001077int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001078{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001079 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1080
1081 if(!ref) {
1082 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1083 return 1;
1084 }
Emeric Brun9e754772019-01-10 17:51:55 +01001085 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1086 memprintf(err, "Invalid key size");
1087 return 1;
1088 }
1089
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001090 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001091}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001092
1093/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001094 * automatic ids. It's called just after the basic checks. It returns
1095 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001096 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001097static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001098{
1099 int i = 0;
1100 struct tls_keys_ref *ref, *ref2, *ref3;
1101 struct list tkr = LIST_HEAD_INIT(tkr);
1102
1103 list_for_each_entry(ref, &tlskeys_reference, list) {
1104 if (ref->unique_id == -1) {
1105 /* Look for the first free id. */
1106 while (1) {
1107 list_for_each_entry(ref2, &tlskeys_reference, list) {
1108 if (ref2->unique_id == i) {
1109 i++;
1110 break;
1111 }
1112 }
1113 if (&ref2->list == &tlskeys_reference)
1114 break;
1115 }
1116
1117 /* Uses the unique id and increment it for the next entry. */
1118 ref->unique_id = i;
1119 i++;
1120 }
1121 }
1122
1123 /* This sort the reference list by id. */
1124 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1125 LIST_DEL(&ref->list);
1126 list_for_each_entry(ref3, &tkr, list) {
1127 if (ref->unique_id < ref3->unique_id) {
1128 LIST_ADDQ(&ref3->list, &ref->list);
1129 break;
1130 }
1131 }
1132 if (&ref3->list == &tkr)
1133 LIST_ADDQ(&tkr, &ref->list);
1134 }
1135
1136 /* swap root */
1137 LIST_ADD(&tkr, &tlskeys_reference);
1138 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001139 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001140}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001141#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1142
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001143#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001144int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1145{
1146 switch (evp_keytype) {
1147 case EVP_PKEY_RSA:
1148 return 2;
1149 case EVP_PKEY_DSA:
1150 return 0;
1151 case EVP_PKEY_EC:
1152 return 1;
1153 }
1154
1155 return -1;
1156}
1157
Emeric Brun4147b2e2014-06-16 18:36:30 +02001158/*
1159 * Callback used to set OCSP status extension content in server hello.
1160 */
1161int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1162{
yanbzhube2774d2015-12-10 15:07:30 -05001163 struct certificate_ocsp *ocsp;
1164 struct ocsp_cbk_arg *ocsp_arg;
1165 char *ssl_buf;
1166 EVP_PKEY *ssl_pkey;
1167 int key_type;
1168 int index;
1169
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001170 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001171
1172 ssl_pkey = SSL_get_privatekey(ssl);
1173 if (!ssl_pkey)
1174 return SSL_TLSEXT_ERR_NOACK;
1175
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001176 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001177
1178 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1179 ocsp = ocsp_arg->s_ocsp;
1180 else {
1181 /* For multiple certs per context, we have to find the correct OCSP response based on
1182 * the certificate type
1183 */
1184 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1185
1186 if (index < 0)
1187 return SSL_TLSEXT_ERR_NOACK;
1188
1189 ocsp = ocsp_arg->m_ocsp[index];
1190
1191 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001192
1193 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001194 !ocsp->response.area ||
1195 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001196 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001197 return SSL_TLSEXT_ERR_NOACK;
1198
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001199 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001200 if (!ssl_buf)
1201 return SSL_TLSEXT_ERR_NOACK;
1202
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001203 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1204 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001205
1206 return SSL_TLSEXT_ERR_OK;
1207}
1208
William Lallemand4a660132019-10-14 14:51:41 +02001209#endif
1210
1211#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001212/*
1213 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001214 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1215 * status extension, the issuer's certificate is mandatory. It should be
1216 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001217 *
William Lallemand246c0242019-10-11 08:59:13 +02001218 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1219 * OCSP response. If file is empty or content is not a valid OCSP response,
1220 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1221 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001222 *
1223 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001224 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001225 */
William Lallemand4a660132019-10-14 14:51:41 +02001226#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001227static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001228{
William Lallemand246c0242019-10-11 08:59:13 +02001229 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001230 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001231 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001232 struct certificate_ocsp *ocsp = NULL, *iocsp;
1233 char *warn = NULL;
1234 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001235 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001236
Emeric Brun4147b2e2014-06-16 18:36:30 +02001237
William Lallemand246c0242019-10-11 08:59:13 +02001238 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001239 if (!x)
1240 goto out;
1241
William Lallemand246c0242019-10-11 08:59:13 +02001242 issuer = ckch->ocsp_issuer;
1243 if (!issuer)
1244 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001245
1246 cid = OCSP_cert_to_id(0, x, issuer);
1247 if (!cid)
1248 goto out;
1249
1250 i = i2d_OCSP_CERTID(cid, NULL);
1251 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1252 goto out;
1253
Vincent Bernat02779b62016-04-03 13:48:43 +02001254 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001255 if (!ocsp)
1256 goto out;
1257
1258 p = ocsp->key_data;
1259 i2d_OCSP_CERTID(cid, &p);
1260
1261 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1262 if (iocsp == ocsp)
1263 ocsp = NULL;
1264
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001265#ifndef SSL_CTX_get_tlsext_status_cb
1266# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1267 *cb = (void (*) (void))ctx->tlsext_status_cb;
1268#endif
1269 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1270
1271 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001272 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001273 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001274
1275 cb_arg->is_single = 1;
1276 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001277
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001278 pkey = X509_get_pubkey(x);
1279 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1280 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001281
1282 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1283 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1284 } else {
1285 /*
1286 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1287 * Update that cb_arg with the new cert's staple
1288 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001289 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001290 struct certificate_ocsp *tmp_ocsp;
1291 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001292 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001293 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001294
1295#ifdef SSL_CTX_get_tlsext_status_arg
1296 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1297#else
1298 cb_arg = ctx->tlsext_status_arg;
1299#endif
yanbzhube2774d2015-12-10 15:07:30 -05001300
1301 /*
1302 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1303 * the order of operations below matter, take care when changing it
1304 */
1305 tmp_ocsp = cb_arg->s_ocsp;
1306 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1307 cb_arg->s_ocsp = NULL;
1308 cb_arg->m_ocsp[index] = tmp_ocsp;
1309 cb_arg->is_single = 0;
1310 cb_arg->single_kt = 0;
1311
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001312 pkey = X509_get_pubkey(x);
1313 key_type = EVP_PKEY_base_id(pkey);
1314 EVP_PKEY_free(pkey);
1315
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001316 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001317 if (index >= 0 && !cb_arg->m_ocsp[index])
1318 cb_arg->m_ocsp[index] = iocsp;
1319
1320 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001321
1322 ret = 0;
1323
1324 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001325 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001326 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001327 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001328 }
1329
1330out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001331 if (cid)
1332 OCSP_CERTID_free(cid);
1333
1334 if (ocsp)
1335 free(ocsp);
1336
1337 if (warn)
1338 free(warn);
1339
Emeric Brun4147b2e2014-06-16 18:36:30 +02001340 return ret;
1341}
William Lallemand4a660132019-10-14 14:51:41 +02001342#else /* OPENSSL_IS_BORINGSSL */
1343static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001344{
William Lallemand4a660132019-10-14 14:51:41 +02001345 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001346}
1347#endif
1348
William Lallemand4a660132019-10-14 14:51:41 +02001349#endif
1350
1351
Willy Tarreau5db847a2019-05-09 14:13:35 +02001352#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001353
1354#define CT_EXTENSION_TYPE 18
1355
1356static int sctl_ex_index = -1;
1357
1358/*
1359 * Try to parse Signed Certificate Timestamp List structure. This function
1360 * makes only basic test if the data seems like SCTL. No signature validation
1361 * is performed.
1362 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001363static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001364{
1365 int ret = 1;
1366 int len, pos, sct_len;
1367 unsigned char *data;
1368
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001369 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001370 goto out;
1371
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001372 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001373 len = (data[0] << 8) | data[1];
1374
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001376 goto out;
1377
1378 data = data + 2;
1379 pos = 0;
1380 while (pos < len) {
1381 if (len - pos < 2)
1382 goto out;
1383
1384 sct_len = (data[pos] << 8) | data[pos + 1];
1385 if (pos + sct_len + 2 > len)
1386 goto out;
1387
1388 pos += sct_len + 2;
1389 }
1390
1391 ret = 0;
1392
1393out:
1394 return ret;
1395}
1396
William Lallemand0dfae6c2019-10-16 18:06:58 +02001397/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1398 * It fills the ckch->sctl buffer
1399 * return 0 on success or != 0 on failure */
1400static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001401{
1402 int fd = -1;
1403 int r = 0;
1404 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001405 struct buffer tmp;
1406 struct buffer *src;
1407 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001408
William Lallemand0dfae6c2019-10-16 18:06:58 +02001409 if (buf) {
1410 tmp.area = buf;
1411 tmp.data = strlen(buf);
1412 tmp.size = tmp.data + 1;
1413 src = &tmp;
1414 } else {
1415 fd = open(sctl_path, O_RDONLY);
1416 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001417 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001418
1419 trash.data = 0;
1420 while (trash.data < trash.size) {
1421 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1422 if (r < 0) {
1423 if (errno == EINTR)
1424 continue;
1425 goto end;
1426 }
1427 else if (r == 0) {
1428 break;
1429 }
1430 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001431 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001432 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001433 }
1434
William Lallemand0dfae6c2019-10-16 18:06:58 +02001435 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001436 if (ret)
1437 goto end;
1438
William Lallemand0dfae6c2019-10-16 18:06:58 +02001439 sctl = calloc(1, sizeof(*sctl));
1440 if (!chunk_dup(sctl, src)) {
1441 free(sctl);
1442 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001443 goto end;
1444 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001445 ret = 0;
1446 /* TODO: free the previous SCTL in the ckch */
1447 ckch->sctl = sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001448
1449end:
1450 if (fd != -1)
1451 close(fd);
1452
1453 return ret;
1454}
1455
1456int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1457{
Willy Tarreau83061a82018-07-13 11:56:34 +02001458 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001459
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001460 *out = (unsigned char *) sctl->area;
1461 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001462
1463 return 1;
1464}
1465
1466int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1467{
1468 return 1;
1469}
1470
William Lallemanda17f4112019-10-10 15:16:44 +02001471static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001472{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001473 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001474
William Lallemanda17f4112019-10-10 15:16:44 +02001475 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL))
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001476 goto out;
1477
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001478 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1479
1480 ret = 0;
1481
1482out:
1483 return ret;
1484}
1485
1486#endif
1487
Emeric Brune1f38db2012-09-03 20:36:47 +02001488void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1489{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001490 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001491 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001492 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001493 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001494
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001495#ifndef SSL_OP_NO_RENEGOTIATION
1496 /* Please note that BoringSSL defines this macro to zero so don't
1497 * change this to #if and do not assign a default value to this macro!
1498 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001499 if (where & SSL_CB_HANDSHAKE_START) {
1500 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001501 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 +02001502 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001503 conn->err_code = CO_ER_SSL_RENEG;
1504 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001505 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001506#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001507
1508 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001509 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001510 /* Long certificate chains optimz
1511 If write and read bios are differents, we
1512 consider that the buffering was activated,
1513 so we rise the output buffer size from 4k
1514 to 16k */
1515 write_bio = SSL_get_wbio(ssl);
1516 if (write_bio != SSL_get_rbio(ssl)) {
1517 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001518 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001519 }
1520 }
1521 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001522}
1523
Emeric Brune64aef12012-09-21 13:15:06 +02001524/* Callback is called for each certificate of the chain during a verify
1525 ok is set to 1 if preverify detect no error on current certificate.
1526 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001527int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001528{
1529 SSL *ssl;
1530 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001531 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001532 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001533
1534 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001535 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001536
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001537 ctx = conn->xprt_ctx;
1538
1539 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001540
Emeric Brun81c00f02012-09-21 14:31:21 +02001541 if (ok) /* no errors */
1542 return ok;
1543
1544 depth = X509_STORE_CTX_get_error_depth(x_store);
1545 err = X509_STORE_CTX_get_error(x_store);
1546
1547 /* check if CA error needs to be ignored */
1548 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001549 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1550 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1551 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001552 }
1553
Willy Tarreau07d94e42018-09-20 10:57:52 +02001554 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001555 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001556 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001557 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001558 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001559
Willy Tarreau20879a02012-12-03 16:32:10 +01001560 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001561 return 0;
1562 }
1563
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001564 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1565 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001566
Emeric Brun81c00f02012-09-21 14:31:21 +02001567 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001568 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001569 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001570 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001571 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001572 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001573
Willy Tarreau20879a02012-12-03 16:32:10 +01001574 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001575 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001576}
1577
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001578static inline
1579void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001580 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001581{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001582 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001583 unsigned char *msg;
1584 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001585 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001586
1587 /* This function is called for "from client" and "to server"
1588 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001589 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001590 */
1591
1592 /* "write_p" is set to 0 is the bytes are received messages,
1593 * otherwise it is set to 1.
1594 */
1595 if (write_p != 0)
1596 return;
1597
1598 /* content_type contains the type of message received or sent
1599 * according with the SSL/TLS protocol spec. This message is
1600 * encoded with one byte. The value 256 (two bytes) is used
1601 * for designing the SSL/TLS record layer. According with the
1602 * rfc6101, the expected message (other than 256) are:
1603 * - change_cipher_spec(20)
1604 * - alert(21)
1605 * - handshake(22)
1606 * - application_data(23)
1607 * - (255)
1608 * We are interessed by the handshake and specially the client
1609 * hello.
1610 */
1611 if (content_type != 22)
1612 return;
1613
1614 /* The message length is at least 4 bytes, containing the
1615 * message type and the message length.
1616 */
1617 if (len < 4)
1618 return;
1619
1620 /* First byte of the handshake message id the type of
1621 * message. The konwn types are:
1622 * - hello_request(0)
1623 * - client_hello(1)
1624 * - server_hello(2)
1625 * - certificate(11)
1626 * - server_key_exchange (12)
1627 * - certificate_request(13)
1628 * - server_hello_done(14)
1629 * We are interested by the client hello.
1630 */
1631 msg = (unsigned char *)buf;
1632 if (msg[0] != 1)
1633 return;
1634
1635 /* Next three bytes are the length of the message. The total length
1636 * must be this decoded length + 4. If the length given as argument
1637 * is not the same, we abort the protocol dissector.
1638 */
1639 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1640 if (len < rec_len + 4)
1641 return;
1642 msg += 4;
1643 end = msg + rec_len;
1644 if (end < msg)
1645 return;
1646
1647 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1648 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001649 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1650 */
1651 msg += 1 + 1 + 4 + 28;
1652 if (msg > end)
1653 return;
1654
1655 /* Next, is session id:
1656 * if present, we have to jump by length + 1 for the size information
1657 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001658 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001659 if (msg[0] > 0)
1660 msg += msg[0];
1661 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001662 if (msg > end)
1663 return;
1664
1665 /* Next two bytes are the ciphersuite length. */
1666 if (msg + 2 > end)
1667 return;
1668 rec_len = (msg[0] << 8) + msg[1];
1669 msg += 2;
1670 if (msg + rec_len > end || msg + rec_len < msg)
1671 return;
1672
Willy Tarreaubafbe012017-11-24 17:34:44 +01001673 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001674 if (!capture)
1675 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001676 /* Compute the xxh64 of the ciphersuite. */
1677 capture->xxh64 = XXH64(msg, rec_len, 0);
1678
1679 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001680 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1681 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001682 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001683
1684 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001685}
1686
Emeric Brun29f037d2014-04-25 19:05:36 +02001687/* Callback is called for ssl protocol analyse */
1688void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1689{
Emeric Brun29f037d2014-04-25 19:05:36 +02001690#ifdef TLS1_RT_HEARTBEAT
1691 /* test heartbeat received (write_p is set to 0
1692 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001693 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001694 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001695 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001696 const unsigned char *p = buf;
1697 unsigned int payload;
1698
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001699 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001700
1701 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1702 if (*p != TLS1_HB_REQUEST)
1703 return;
1704
Willy Tarreauaeed6722014-04-25 23:59:58 +02001705 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001706 goto kill_it;
1707
1708 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001709 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001710 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001711 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001712 /* We have a clear heartbleed attack (CVE-2014-0160), the
1713 * advertised payload is larger than the advertised packet
1714 * length, so we have garbage in the buffer between the
1715 * payload and the end of the buffer (p+len). We can't know
1716 * if the SSL stack is patched, and we don't know if we can
1717 * safely wipe out the area between p+3+len and payload.
1718 * So instead, we prevent the response from being sent by
1719 * setting the max_send_fragment to 0 and we report an SSL
1720 * error, which will kill this connection. It will be reported
1721 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001722 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1723 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001724 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001725 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1726 return;
1727 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001728#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001729 if (global_ssl.capture_cipherlist > 0)
1730 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001731}
1732
Bernard Spil13c53f82018-02-15 13:34:58 +01001733#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001734static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1735 const unsigned char *in, unsigned int inlen,
1736 void *arg)
1737{
1738 struct server *srv = arg;
1739
1740 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1741 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1742 return SSL_TLSEXT_ERR_OK;
1743 return SSL_TLSEXT_ERR_NOACK;
1744}
1745#endif
1746
1747#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001748/* This callback is used so that the server advertises the list of
1749 * negociable protocols for NPN.
1750 */
1751static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1752 unsigned int *len, void *arg)
1753{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001754 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001755
1756 *data = (const unsigned char *)conf->npn_str;
1757 *len = conf->npn_len;
1758 return SSL_TLSEXT_ERR_OK;
1759}
1760#endif
1761
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001762#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001763/* This callback is used so that the server advertises the list of
1764 * negociable protocols for ALPN.
1765 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001766static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1767 unsigned char *outlen,
1768 const unsigned char *server,
1769 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001770{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001771 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001772
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001773 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1774 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1775 return SSL_TLSEXT_ERR_NOACK;
1776 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001777 return SSL_TLSEXT_ERR_OK;
1778}
1779#endif
1780
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001781#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001782#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001783
Christopher Faulet30548802015-06-11 13:39:32 +02001784/* Create a X509 certificate with the specified servername and serial. This
1785 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001786static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001787ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001788{
Christopher Faulet7969a332015-10-09 11:15:03 +02001789 X509 *cacert = bind_conf->ca_sign_cert;
1790 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001791 SSL_CTX *ssl_ctx = NULL;
1792 X509 *newcrt = NULL;
1793 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001794 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001795 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001796 X509_NAME *name;
1797 const EVP_MD *digest;
1798 X509V3_CTX ctx;
1799 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001800 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001801
Christopher Faulet48a83322017-07-28 16:56:09 +02001802 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001803#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001804 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1805#else
1806 tmp_ssl = SSL_new(bind_conf->default_ctx);
1807 if (tmp_ssl)
1808 pkey = SSL_get_privatekey(tmp_ssl);
1809#endif
1810 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001811 goto mkcert_error;
1812
1813 /* Create the certificate */
1814 if (!(newcrt = X509_new()))
1815 goto mkcert_error;
1816
1817 /* Set version number for the certificate (X509v3) and the serial
1818 * number */
1819 if (X509_set_version(newcrt, 2L) != 1)
1820 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001821 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001822
1823 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001824 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1825 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001826 goto mkcert_error;
1827
1828 /* set public key in the certificate */
1829 if (X509_set_pubkey(newcrt, pkey) != 1)
1830 goto mkcert_error;
1831
1832 /* Set issuer name from the CA */
1833 if (!(name = X509_get_subject_name(cacert)))
1834 goto mkcert_error;
1835 if (X509_set_issuer_name(newcrt, name) != 1)
1836 goto mkcert_error;
1837
1838 /* Set the subject name using the same, but the CN */
1839 name = X509_NAME_dup(name);
1840 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1841 (const unsigned char *)servername,
1842 -1, -1, 0) != 1) {
1843 X509_NAME_free(name);
1844 goto mkcert_error;
1845 }
1846 if (X509_set_subject_name(newcrt, name) != 1) {
1847 X509_NAME_free(name);
1848 goto mkcert_error;
1849 }
1850 X509_NAME_free(name);
1851
1852 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001853 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001854 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1855 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1856 X509_EXTENSION *ext;
1857
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001858 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001859 goto mkcert_error;
1860 if (!X509_add_ext(newcrt, ext, -1)) {
1861 X509_EXTENSION_free(ext);
1862 goto mkcert_error;
1863 }
1864 X509_EXTENSION_free(ext);
1865 }
1866
1867 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001868
1869 key_type = EVP_PKEY_base_id(capkey);
1870
1871 if (key_type == EVP_PKEY_DSA)
1872 digest = EVP_sha1();
1873 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001874 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001875 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001876 digest = EVP_sha256();
1877 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001878#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001879 int nid;
1880
1881 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1882 goto mkcert_error;
1883 if (!(digest = EVP_get_digestbynid(nid)))
1884 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001885#else
1886 goto mkcert_error;
1887#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001888 }
1889
Christopher Faulet31af49d2015-06-09 17:29:50 +02001890 if (!(X509_sign(newcrt, capkey, digest)))
1891 goto mkcert_error;
1892
1893 /* Create and set the new SSL_CTX */
1894 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1895 goto mkcert_error;
1896 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1897 goto mkcert_error;
1898 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1899 goto mkcert_error;
1900 if (!SSL_CTX_check_private_key(ssl_ctx))
1901 goto mkcert_error;
1902
1903 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001904
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001905#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001906 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001907#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001908#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1909 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001910 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001911 EC_KEY *ecc;
1912 int nid;
1913
1914 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1915 goto end;
1916 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1917 goto end;
1918 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1919 EC_KEY_free(ecc);
1920 }
1921#endif
1922 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001923 return ssl_ctx;
1924
1925 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001926 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001927 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001928 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1929 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001930 return NULL;
1931}
1932
Christopher Faulet7969a332015-10-09 11:15:03 +02001933SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001934ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001935{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001936 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001937 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001938
Olivier Houchard66ab4982019-02-26 18:37:15 +01001939 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001940}
1941
Christopher Faulet30548802015-06-11 13:39:32 +02001942/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001943 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001944SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001945ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001946{
1947 struct lru64 *lru = NULL;
1948
1949 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001950 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001951 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001952 if (lru && lru->domain) {
1953 if (ssl)
1954 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001955 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001956 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02001957 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001958 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001959 }
1960 return NULL;
1961}
1962
Emeric Brun821bb9b2017-06-15 16:37:39 +02001963/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
1964 * function is not thread-safe, it should only be used to check if a certificate
1965 * exists in the lru cache (with no warranty it will not be removed by another
1966 * thread). It is kept for backward compatibility. */
1967SSL_CTX *
1968ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
1969{
1970 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
1971}
1972
Christopher Fauletd2cab922015-07-28 16:03:47 +02001973/* Set a certificate int the LRU cache used to store generated
1974 * certificate. Return 0 on success, otherwise -1 */
1975int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001976ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02001977{
1978 struct lru64 *lru = NULL;
1979
1980 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001981 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001982 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001983 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001984 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001985 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02001986 }
Christopher Faulet30548802015-06-11 13:39:32 +02001987 if (lru->domain && lru->data)
1988 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02001989 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001990 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001991 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02001992 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02001993 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02001994}
1995
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001996/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02001997unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001998ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02001999{
2000 return XXH32(data, len, ssl_ctx_lru_seed);
2001}
2002
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002003/* Generate a cert and immediately assign it to the SSL session so that the cert's
2004 * refcount is maintained regardless of the cert's presence in the LRU cache.
2005 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002006static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002007ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002008{
2009 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002010 SSL_CTX *ssl_ctx = NULL;
2011 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002012 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002013
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002014 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002015 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002016 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002017 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002018 if (lru && lru->domain)
2019 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002020 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002021 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002022 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002023 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002024 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002026 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002027 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002028 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002029 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002030 SSL_set_SSL_CTX(ssl, ssl_ctx);
2031 /* No LRU cache, this CTX will be released as soon as the session dies */
2032 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002033 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002034 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002035 return 0;
2036}
2037static int
2038ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2039{
2040 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002041 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002042
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002043 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002044 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002045 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002046 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002047 }
2048 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002049}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002050#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002051
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002052#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002053typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2054
2055static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002056{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002057#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002058 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002059 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2060#endif
2061}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002062static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2063 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002064 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2065}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002066static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002067#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002068 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002069 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2070#endif
2071}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002072static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002073#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002074 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002075 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2076#endif
2077}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002078/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002079static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2080/* Unusable in this context. */
2081static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2082static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2083static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2084static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2085static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002086#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002087typedef enum { SET_MIN, SET_MAX } set_context_func;
2088
2089static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2090 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002091 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2092}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002093static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2094 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2095 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2096}
2097static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2098 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002099 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2100}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002101static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2102 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2103 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2104}
2105static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2106 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002107 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2108}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002109static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2110 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2111 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2112}
2113static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2114 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002115 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2116}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002117static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2118 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2119 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2120}
2121static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002122#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002123 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002124 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2125#endif
2126}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002127static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2128#if SSL_OP_NO_TLSv1_3
2129 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2130 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002131#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002132}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002133#endif
2134static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2135static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002136
2137static struct {
2138 int option;
2139 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002140 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2141 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002142 const char *name;
2143} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002144 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2145 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2146 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2147 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2148 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2149 {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 +02002150};
2151
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002152static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2153{
2154 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2155 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2156 SSL_set_SSL_CTX(ssl, ctx);
2157}
2158
Willy Tarreau5db847a2019-05-09 14:13:35 +02002159#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002160
2161static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2162{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002163 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002164 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002165
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002166 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2167 return SSL_TLSEXT_ERR_OK;
2168 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002169}
2170
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002171#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002172static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2173{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002174 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002175#else
2176static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2177{
2178#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002179 struct connection *conn;
2180 struct bind_conf *s;
2181 const uint8_t *extension_data;
2182 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002183 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002184
2185 char *wildp = NULL;
2186 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002187 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002188 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002189 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002190 int i;
2191
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002192 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002193 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002194
Olivier Houchard9679ac92017-10-27 14:58:08 +02002195 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002196 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002197#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002198 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2199 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002200#else
2201 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2202#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002203 /*
2204 * The server_name extension was given too much extensibility when it
2205 * was written, so parsing the normal case is a bit complex.
2206 */
2207 size_t len;
2208 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002209 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002210 /* Extract the length of the supplied list of names. */
2211 len = (*extension_data++) << 8;
2212 len |= *extension_data++;
2213 if (len + 2 != extension_len)
2214 goto abort;
2215 /*
2216 * The list in practice only has a single element, so we only consider
2217 * the first one.
2218 */
2219 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2220 goto abort;
2221 extension_len = len - 1;
2222 /* Now we can finally pull out the byte array with the actual hostname. */
2223 if (extension_len <= 2)
2224 goto abort;
2225 len = (*extension_data++) << 8;
2226 len |= *extension_data++;
2227 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2228 || memchr(extension_data, 0, len) != NULL)
2229 goto abort;
2230 servername = extension_data;
2231 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002232 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002233#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2234 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002235 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002236 }
2237#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002238 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002239 if (!s->strict_sni) {
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002240 ssl_sock_switchctx_set(ssl, s->default_ctx);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002241 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002242 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002243 goto abort;
2244 }
2245
2246 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002247#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002248 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002249#else
2250 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2251#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002252 uint8_t sign;
2253 size_t len;
2254 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002255 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002256 len = (*extension_data++) << 8;
2257 len |= *extension_data++;
2258 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002259 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002260 if (len % 2 != 0)
2261 goto abort;
2262 for (; len > 0; len -= 2) {
2263 extension_data++; /* hash */
2264 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002265 switch (sign) {
2266 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002267 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002268 break;
2269 case TLSEXT_signature_ecdsa:
2270 has_ecdsa_sig = 1;
2271 break;
2272 default:
2273 continue;
2274 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002275 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002276 break;
2277 }
2278 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002279 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002280 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002281 }
2282 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002283 const SSL_CIPHER *cipher;
2284 size_t len;
2285 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002286 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002287#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002288 len = ctx->cipher_suites_len;
2289 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002290#else
2291 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2292#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002293 if (len % 2 != 0)
2294 goto abort;
2295 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002296#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002297 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002298 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002299#else
2300 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2301#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002302 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002303 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002304 break;
2305 }
2306 }
2307 }
2308
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002309 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 trash.area[i] = tolower(servername[i]);
2311 if (!wildp && (trash.area[i] == '.'))
2312 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002313 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002314 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002315
William Lallemand150bfa82019-09-19 17:12:49 +02002316 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002317 /* lookup in full qualified names */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002318 node = ebst_lookup(&s->sni_ctx, trash.area);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002319
2320 /* lookup a not neg filter */
2321 for (n = node; n; n = ebmb_next_dup(n)) {
2322 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002323 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002324 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002325 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002326 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002327 break;
2328 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002329 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002330 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002331 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002332 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002333 if (!node_anonymous)
2334 node_anonymous = n;
2335 break;
2336 }
2337 }
2338 }
2339 if (wildp) {
2340 /* lookup in wildcards names */
2341 node = ebst_lookup(&s->sni_w_ctx, wildp);
2342 for (n = node; n; n = ebmb_next_dup(n)) {
2343 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002344 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002345 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002346 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002347 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002348 break;
2349 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002350 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002351 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002352 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002353 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002354 if (!node_anonymous)
2355 node_anonymous = n;
2356 break;
2357 }
2358 }
2359 }
2360 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002361 /* select by key_signature priority order */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002362 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2363 : ((has_rsa_sig && node_rsa) ? node_rsa
2364 : (node_anonymous ? node_anonymous
2365 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2366 : node_rsa /* no rsa signature case (far far away) */
2367 )));
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368 if (node) {
2369 /* switch ctx */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02002370 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002371 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002372 if (conf) {
2373 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2374 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2375 if (conf->early_data)
2376 allow_early = 1;
2377 }
William Lallemand02010472019-10-18 11:02:19 +02002378 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002379 goto allow_early;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002380 }
William Lallemand150bfa82019-09-19 17:12:49 +02002381
William Lallemand02010472019-10-18 11:02:19 +02002382 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002383#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002384 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002385 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002386 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002387 }
2388#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002389 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002390 /* no certificate match, is the default_ctx */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002391 ssl_sock_switchctx_set(ssl, s->default_ctx);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002392 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002393allow_early:
2394#ifdef OPENSSL_IS_BORINGSSL
2395 if (allow_early)
2396 SSL_set_early_data_enabled(ssl, 1);
2397#else
2398 if (!allow_early)
2399 SSL_set_max_early_data(ssl, 0);
2400#endif
2401 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002402 abort:
2403 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2404 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002405#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002406 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002407#else
2408 *al = SSL_AD_UNRECOGNIZED_NAME;
2409 return 0;
2410#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002411}
2412
2413#else /* OPENSSL_IS_BORINGSSL */
2414
Emeric Brunfc0421f2012-09-07 17:30:07 +02002415/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2416 * warning when no match is found, which implies the default (first) cert
2417 * will keep being used.
2418 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002419static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002420{
2421 const char *servername;
2422 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002423 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002424 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002425 int i;
2426 (void)al; /* shut gcc stupid warning */
2427
2428 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002429 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002430#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002431 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2432 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002433#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002434 if (s->strict_sni)
2435 return SSL_TLSEXT_ERR_ALERT_FATAL;
2436 ssl_sock_switchctx_set(ssl, s->default_ctx);
2437 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002438 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002439
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002440 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002441 if (!servername[i])
2442 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 trash.area[i] = tolower(servername[i]);
2444 if (!wildp && (trash.area[i] == '.'))
2445 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002446 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002447 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002448
William Lallemand150bfa82019-09-19 17:12:49 +02002449 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002450 /* lookup in full qualified names */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002451 node = ebst_lookup(&s->sni_ctx, trash.area);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002452
2453 /* lookup a not neg filter */
2454 for (n = node; n; n = ebmb_next_dup(n)) {
2455 if (!container_of(n, struct sni_ctx, name)->neg) {
2456 node = n;
2457 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002458 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002459 }
2460 if (!node && wildp) {
2461 /* lookup in wildcards names */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002462 node = ebst_lookup(&s->sni_w_ctx, wildp);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002463 }
2464 if (!node || container_of(node, struct sni_ctx, name)->neg) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002465#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002466 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2467 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002468 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002469 return SSL_TLSEXT_ERR_OK;
2470 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002471#endif
William Lallemand150bfa82019-09-19 17:12:49 +02002472 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002473 if (s->strict_sni)
2474 return SSL_TLSEXT_ERR_ALERT_FATAL;
2475 ssl_sock_switchctx_set(ssl, s->default_ctx);
2476 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002477 }
2478
2479 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002480 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002481 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002482 return SSL_TLSEXT_ERR_OK;
2483}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002484#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002485#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2486
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002487#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002488
2489static DH * ssl_get_dh_1024(void)
2490{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002491 static unsigned char dh1024_p[]={
2492 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2493 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2494 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2495 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2496 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2497 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2498 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2499 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2500 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2501 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2502 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2503 };
2504 static unsigned char dh1024_g[]={
2505 0x02,
2506 };
2507
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002508 BIGNUM *p;
2509 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002510 DH *dh = DH_new();
2511 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002512 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2513 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002514
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002515 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002516 DH_free(dh);
2517 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002518 } else {
2519 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002520 }
2521 }
2522 return dh;
2523}
2524
2525static DH *ssl_get_dh_2048(void)
2526{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002527 static unsigned char dh2048_p[]={
2528 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2529 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2530 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2531 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2532 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2533 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2534 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2535 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2536 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2537 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2538 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2539 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2540 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2541 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2542 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2543 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2544 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2545 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2546 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2547 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2548 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2549 0xB7,0x1F,0x77,0xF3,
2550 };
2551 static unsigned char dh2048_g[]={
2552 0x02,
2553 };
2554
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002555 BIGNUM *p;
2556 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002557 DH *dh = DH_new();
2558 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002559 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2560 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002561
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002562 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002563 DH_free(dh);
2564 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002565 } else {
2566 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002567 }
2568 }
2569 return dh;
2570}
2571
2572static DH *ssl_get_dh_4096(void)
2573{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002574 static unsigned char dh4096_p[]={
2575 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2576 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2577 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2578 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2579 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2580 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2581 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2582 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2583 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2584 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2585 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2586 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2587 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2588 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2589 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2590 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2591 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2592 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2593 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2594 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2595 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2596 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2597 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2598 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2599 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2600 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2601 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2602 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2603 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2604 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2605 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2606 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2607 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2608 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2609 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2610 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2611 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2612 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2613 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2614 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2615 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2616 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2617 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002618 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002619 static unsigned char dh4096_g[]={
2620 0x02,
2621 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002622
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002623 BIGNUM *p;
2624 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002625 DH *dh = DH_new();
2626 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002627 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2628 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002629
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002630 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002631 DH_free(dh);
2632 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002633 } else {
2634 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002635 }
2636 }
2637 return dh;
2638}
2639
2640/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002641 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002642static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2643{
2644 DH *dh = NULL;
2645 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002646 int type;
2647
2648 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002649
2650 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2651 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2652 */
2653 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2654 keylen = EVP_PKEY_bits(pkey);
2655 }
2656
Willy Tarreauef934602016-12-22 23:12:01 +01002657 if (keylen > global_ssl.default_dh_param) {
2658 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002659 }
2660
Remi Gacogned3a341a2015-05-29 16:26:17 +02002661 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002662 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002663 }
2664 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002665 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002666 }
2667 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002668 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002669 }
2670
2671 return dh;
2672}
2673
Remi Gacogne47783ef2015-05-29 15:53:22 +02002674static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002675{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002676 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002677 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002678
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002679 if (in == NULL)
2680 goto end;
2681
Remi Gacogne47783ef2015-05-29 15:53:22 +02002682 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002683 goto end;
2684
Remi Gacogne47783ef2015-05-29 15:53:22 +02002685 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2686
2687end:
2688 if (in)
2689 BIO_free(in);
2690
Emeric Brune1b4ed42018-08-16 15:14:12 +02002691 ERR_clear_error();
2692
Remi Gacogne47783ef2015-05-29 15:53:22 +02002693 return dh;
2694}
2695
2696int ssl_sock_load_global_dh_param_from_file(const char *filename)
2697{
2698 global_dh = ssl_sock_get_dh_from_file(filename);
2699
2700 if (global_dh) {
2701 return 0;
2702 }
2703
2704 return -1;
2705}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002706#endif
2707
William Lallemand9117de92019-10-04 00:29:42 +02002708/* Alloc and init a ckch_inst */
2709static struct ckch_inst *ckch_inst_new()
2710{
2711 struct ckch_inst *ckch_inst;
2712
2713 ckch_inst = calloc(1, sizeof *ckch_inst);
2714 if (ckch_inst)
2715 LIST_INIT(&ckch_inst->sni_ctx);
2716
2717 return ckch_inst;
2718}
2719
2720
2721/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002722static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002723 struct bind_conf *s, struct ssl_bind_conf *conf,
2724 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002725{
2726 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002727 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002728
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002729 if (*name == '!') {
2730 neg = 1;
2731 name++;
2732 }
2733 if (*name == '*') {
2734 wild = 1;
2735 name++;
2736 }
2737 /* !* filter is a nop */
2738 if (neg && wild)
2739 return order;
2740 if (*name) {
2741 int j, len;
2742 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002743 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002744 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002745 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002746 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002747 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002748
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002749 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002750 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002751 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002752 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002753 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002754 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002755 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002756 sc->order = order++;
2757 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002758 sc->wild = wild;
2759 sc->name.node.leaf_p = NULL;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01002760 if (kinfo.sig != TLSEXT_signature_anonymous)
2761 SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo);
William Lallemand9117de92019-10-04 00:29:42 +02002762
William Lallemand1d29c742019-10-04 00:53:29 +02002763 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002764 }
2765 return order;
2766}
2767
William Lallemand6af03992019-07-23 15:00:54 +02002768/*
William Lallemand1d29c742019-10-04 00:53:29 +02002769 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2770 * This function can't return an error.
2771 *
2772 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2773 */
2774static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2775{
2776
2777 struct sni_ctx *sc0, *sc0b, *sc1;
2778 struct ebmb_node *node;
2779
2780 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2781
2782 /* ignore if sc0 was already inserted in a tree */
2783 if (sc0->name.node.leaf_p)
2784 continue;
2785
2786 /* Check for duplicates. */
2787 if (sc0->wild)
2788 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2789 else
2790 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2791
2792 for (; node; node = ebmb_next_dup(node)) {
2793 sc1 = ebmb_entry(node, struct sni_ctx, name);
2794 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2795 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2796 /* it's a duplicate, we should remove and free it */
2797 LIST_DEL(&sc0->by_ckch_inst);
2798 free(sc0);
2799 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002800 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002801 }
2802 }
2803
2804 /* if duplicate, ignore the insertion */
2805 if (!sc0)
2806 continue;
2807
2808 if (sc0->wild)
2809 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2810 else
2811 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
2812 }
2813}
2814
2815/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002816 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002817 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002818struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002819
William Lallemandfa892222019-07-23 16:06:08 +02002820
Emeric Brun7a883362019-10-17 13:27:40 +02002821/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2822 * If there is no DH paramater availaible in the ckchs, the global
2823 * DH parameter is loaded into the SSL_CTX and if there is no
2824 * DH parameter available in ckchs nor in global, the default
2825 * DH parameters are applied on the SSL_CTX.
2826 * Returns a bitfield containing the flags:
2827 * ERR_FATAL in any fatal error case
2828 * ERR_ALERT if a reason of the error is availabine in err
2829 * ERR_WARN if a warning is available into err
2830 * The value 0 means there is no error nor warning and
2831 * the operation succeed.
2832 */
William Lallemandfa892222019-07-23 16:06:08 +02002833#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02002834static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
2835 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02002836{
Emeric Brun7a883362019-10-17 13:27:40 +02002837 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02002838 DH *dh = NULL;
2839
William Lallemanda8c73742019-07-31 18:31:34 +02002840 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02002841 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02002842 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
2843 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
2844 err && *err ? *err : "", path);
2845#if defined(SSL_CTX_set_dh_auto)
2846 SSL_CTX_set_dh_auto(ctx, 1);
2847 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2848 err && *err ? *err : "");
2849#else
2850 memprintf(err, "%s, DH ciphers won't be available.\n",
2851 err && *err ? *err : "");
2852#endif
2853 ret |= ERR_WARN;
2854 goto end;
2855 }
William Lallemandfa892222019-07-23 16:06:08 +02002856
2857 if (ssl_dh_ptr_index >= 0) {
2858 /* store a pointer to the DH params to avoid complaining about
2859 ssl-default-dh-param not being set for this SSL_CTX */
2860 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2861 }
2862 }
2863 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02002864 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
2865 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
2866 err && *err ? *err : "", path);
2867#if defined(SSL_CTX_set_dh_auto)
2868 SSL_CTX_set_dh_auto(ctx, 1);
2869 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2870 err && *err ? *err : "");
2871#else
2872 memprintf(err, "%s, DH ciphers won't be available.\n",
2873 err && *err ? *err : "");
2874#endif
2875 ret |= ERR_WARN;
2876 goto end;
2877 }
William Lallemandfa892222019-07-23 16:06:08 +02002878 }
2879 else {
2880 /* Clear openssl global errors stack */
2881 ERR_clear_error();
2882
2883 if (global_ssl.default_dh_param <= 1024) {
2884 /* we are limited to DH parameter of 1024 bits anyway */
2885 if (local_dh_1024 == NULL)
2886 local_dh_1024 = ssl_get_dh_1024();
2887
Emeric Brun7a883362019-10-17 13:27:40 +02002888 if (local_dh_1024 == NULL) {
2889 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2890 err && *err ? *err : "", path);
2891 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02002892 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02002893 }
William Lallemandfa892222019-07-23 16:06:08 +02002894
Emeric Bruna9363eb2019-10-17 14:53:03 +02002895 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
2896 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2897 err && *err ? *err : "", path);
2898#if defined(SSL_CTX_set_dh_auto)
2899 SSL_CTX_set_dh_auto(ctx, 1);
2900 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2901 err && *err ? *err : "");
2902#else
2903 memprintf(err, "%s, DH ciphers won't be available.\n",
2904 err && *err ? *err : "");
2905#endif
2906 ret |= ERR_WARN;
2907 goto end;
2908 }
William Lallemandfa892222019-07-23 16:06:08 +02002909 }
2910 else {
2911 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2912 }
William Lallemandfa892222019-07-23 16:06:08 +02002913 }
2914
2915end:
William Lallemandfa892222019-07-23 16:06:08 +02002916 return ret;
2917}
2918#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05002919
yanbzhu488a4d22015-12-01 15:16:07 -05002920/* Frees the contents of a cert_key_and_chain
2921 */
2922static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
2923{
yanbzhu488a4d22015-12-01 15:16:07 -05002924 if (!ckch)
2925 return;
2926
2927 /* Free the certificate and set pointer to NULL */
2928 if (ckch->cert)
2929 X509_free(ckch->cert);
2930 ckch->cert = NULL;
2931
2932 /* Free the key and set pointer to NULL */
2933 if (ckch->key)
2934 EVP_PKEY_free(ckch->key);
2935 ckch->key = NULL;
2936
2937 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01002938 if (ckch->chain)
2939 sk_X509_pop_free(ckch->chain, X509_free);
2940 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05002941
William Lallemand455af502019-10-17 18:04:45 +02002942 if (ckch->dh)
2943 DH_free(ckch->dh);
2944 ckch->dh = NULL;
2945
2946 if (ckch->sctl) {
2947 free(ckch->sctl->area);
2948 ckch->sctl->area = NULL;
2949 free(ckch->sctl);
2950 ckch->sctl = NULL;
2951 }
2952
2953 if (ckch->ocsp_response) {
2954 free(ckch->ocsp_response->area);
2955 ckch->ocsp_response->area = NULL;
2956 free(ckch->ocsp_response);
2957 ckch->ocsp_response = NULL;
2958 }
yanbzhu488a4d22015-12-01 15:16:07 -05002959}
2960
William Lallemand8d0f8932019-10-17 18:03:58 +02002961/*
2962 *
2963 * This function copy a cert_key_and_chain in memory
2964 *
2965 * It's used to try to apply changes on a ckch before committing them, because
2966 * most of the time it's not possible to revert those changes
2967 *
2968 * Return a the dst or NULL
2969 */
2970static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
2971 struct cert_key_and_chain *dst)
2972{
2973 if (src->cert) {
2974 dst->cert = src->cert;
2975 X509_up_ref(src->cert);
2976 }
2977
2978 if (src->key) {
2979 dst->key = src->key;
2980 EVP_PKEY_up_ref(src->key);
2981 }
2982
2983 if (src->chain) {
2984 dst->chain = X509_chain_up_ref(src->chain);
2985 }
2986
2987 if (src->dh) {
2988 DH_up_ref(src->dh);
2989 dst->dh = src->dh;
2990 }
2991
2992 if (src->sctl) {
2993 struct buffer *sctl;
2994
2995 sctl = calloc(1, sizeof(*sctl));
2996 if (!chunk_dup(sctl, src->sctl)) {
2997 free(sctl);
2998 sctl = NULL;
2999 goto error;
3000 }
3001 dst->sctl = sctl;
3002 }
3003
3004 if (src->ocsp_response) {
3005 struct buffer *ocsp_response;
3006
3007 ocsp_response = calloc(1, sizeof(*ocsp_response));
3008 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3009 free(ocsp_response);
3010 ocsp_response = NULL;
3011 goto error;
3012 }
3013 dst->ocsp_response = ocsp_response;
3014 }
3015
3016 if (src->ocsp_issuer) {
3017 X509_up_ref(src->ocsp_issuer);
3018 dst->ocsp_issuer = src->ocsp_issuer;
3019 }
3020
3021 return dst;
3022
3023error:
3024
3025 /* free everything */
3026 ssl_sock_free_cert_key_and_chain_contents(dst);
3027
3028 return NULL;
3029}
3030
3031
yanbzhu488a4d22015-12-01 15:16:07 -05003032/* checks if a key and cert exists in the ckch
3033 */
William Lallemand1633e392019-09-30 12:58:13 +02003034#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003035static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3036{
3037 return (ckch->cert != NULL && ckch->key != NULL);
3038}
William Lallemand1633e392019-09-30 12:58:13 +02003039#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003040
William Lallemandf9568fc2019-10-16 18:27:58 +02003041/*
3042 * return 0 on success or != 0 on failure
3043 */
3044static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3045{
3046 int ret = 1;
3047 BIO *in = NULL;
3048 X509 *issuer;
3049
3050 if (buf) {
3051 /* reading from a buffer */
3052 in = BIO_new_mem_buf(buf, -1);
3053 if (in == NULL) {
3054 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3055 goto end;
3056 }
3057
3058 } else {
3059 /* reading from a file */
3060 in = BIO_new(BIO_s_file());
3061 if (in == NULL)
3062 goto end;
3063
3064 if (BIO_read_filename(in, path) <= 0)
3065 goto end;
3066 }
3067
3068 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3069 if (!issuer) {
3070 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
3071 *err ? *err : "", path);
3072 goto end;
3073 }
3074 ret = 0;
3075 ckch->ocsp_issuer = issuer;
3076
3077end:
3078
3079 ERR_clear_error();
3080 if (in)
3081 BIO_free(in);
3082
3083 return ret;
3084}
3085
William Lallemand96a9c972019-10-17 11:56:17 +02003086
3087/*
3088 * Try to load a PEM file from a <path> or a buffer <buf>
3089 * The PEM must contain at least a Private Key and a Certificate,
3090 * It could contain a DH and a certificate chain.
yanbzhu488a4d22015-12-01 15:16:07 -05003091 *
William Lallemand96a9c972019-10-17 11:56:17 +02003092 * If it failed you should not attempt to use the ckch but free it.
3093 *
3094 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003095 */
William Lallemand96a9c972019-10-17 11:56:17 +02003096static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
yanbzhu488a4d22015-12-01 15:16:07 -05003097{
William Lallemandf11365b2019-09-19 14:25:58 +02003098 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003099 int ret = 1;
William Lallemand96a9c972019-10-17 11:56:17 +02003100 X509 *ca = NULL;
3101 X509 *cert = NULL;
3102 EVP_PKEY *key = NULL;
3103 DH *dh;
3104
3105 if (buf) {
3106 /* reading from a buffer */
3107 in = BIO_new_mem_buf(buf, -1);
3108 if (in == NULL) {
3109 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3110 goto end;
3111 }
yanbzhu488a4d22015-12-01 15:16:07 -05003112
William Lallemand96a9c972019-10-17 11:56:17 +02003113 } else {
3114 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003115 in = BIO_new(BIO_s_file());
3116 if (in == NULL)
3117 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003118
William Lallemandf11365b2019-09-19 14:25:58 +02003119 if (BIO_read_filename(in, path) <= 0)
3120 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003121 }
yanbzhu488a4d22015-12-01 15:16:07 -05003122
yanbzhu488a4d22015-12-01 15:16:07 -05003123 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003124 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3125 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003126 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003127 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003128 goto end;
3129 }
3130
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003131#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003132 /* Seek back to beginning of file */
3133 if (BIO_reset(in) == -1) {
3134 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3135 err && *err ? *err : "", path);
3136 goto end;
3137 }
3138
William Lallemand96a9c972019-10-17 11:56:17 +02003139 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3140 /* no need to return an error there, dh is not mandatory */
3141
3142 if (dh) {
3143 if (ckch->dh)
3144 DH_free(ckch->dh);
3145 ckch->dh = dh;
3146 }
3147
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003148#endif
William Lallemandfa892222019-07-23 16:06:08 +02003149
Willy Tarreaubb137a82016-04-06 19:02:38 +02003150 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003151 if (BIO_reset(in) == -1) {
3152 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3153 err && *err ? *err : "", path);
3154 goto end;
3155 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003156
3157 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003158 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3159 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003160 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003161 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003162 goto end;
3163 }
3164
William Lallemand96a9c972019-10-17 11:56:17 +02003165 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003166 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003167 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003168 goto end;
3169 }
3170
William Lallemand96a9c972019-10-17 11:56:17 +02003171 /* Key and Cert are good, we can use them in the ckch */
3172 if (ckch->key) /* free the previous key */
3173 EVP_PKEY_free(ckch->key);
3174 ckch->key = key;
3175
3176 if (ckch->cert) /* free the previous cert */
3177 X509_free(ckch->cert);
3178 ckch->cert = cert;
3179
3180 /* Look for a Certificate Chain */
3181 ca = PEM_read_bio_X509(in, NULL, NULL, NULL);
3182 if (ca) {
3183 /* there is a chain a in the PEM, clean the previous one in the CKCH */
3184 if (ckch->chain) /* free the previous chain */
3185 sk_X509_pop_free(ckch->chain, X509_free);
3186 ckch->chain = sk_X509_new_null();
3187 if (!sk_X509_push(ckch->chain, ca)) {
3188 X509_free(ca);
3189 goto end;
3190 }
3191 }
3192 /* look for other crt in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003193 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL)))
3194 if (!sk_X509_push(ckch->chain, ca)) {
3195 X509_free(ca);
3196 goto end;
3197 }
yanbzhu488a4d22015-12-01 15:16:07 -05003198
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003199 /* no chain */
3200 if (ckch->chain == NULL) {
3201 ckch->chain = sk_X509_new_null();
3202 }
3203
yanbzhu488a4d22015-12-01 15:16:07 -05003204 ret = ERR_get_error();
3205 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3206 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003207 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003208 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003209 }
3210
3211 ret = 0;
3212
William Lallemand96a9c972019-10-17 11:56:17 +02003213end:
William Lallemand246c0242019-10-11 08:59:13 +02003214
3215 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003216 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003217 BIO_free(in);
William Lallemand96a9c972019-10-17 11:56:17 +02003218 if (ret != 0) {
3219 if (key)
3220 EVP_PKEY_free(key);
3221 if (cert)
3222 X509_free(cert);
yanbzhu488a4d22015-12-01 15:16:07 -05003223 }
William Lallemanda17f4112019-10-10 15:16:44 +02003224
William Lallemand96a9c972019-10-17 11:56:17 +02003225 return ret;
3226}
3227
3228/*
3229 * Try to load in a ckch every files related to a ckch.
3230 * (PEM, sctl, ocsp, issuer etc.)
3231 *
3232 * This function is only used to load files during the configuration parsing,
3233 * it is not used with the CLI.
3234 *
3235 * This allows us to carry the contents of the file without having to read the
3236 * file multiple times. The caller must call
3237 * ssl_sock_free_cert_key_and_chain_contents.
3238 *
3239 * returns:
3240 * 0 on Success
3241 * 1 on SSL Failure
3242 */
3243static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3244{
3245 int ret = 1;
3246
3247 /* try to load the PEM */
3248 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3249 goto end;
3250 }
3251
William Lallemanda17f4112019-10-10 15:16:44 +02003252#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3253 /* try to load the sctl file */
3254 {
3255 char fp[MAXPATHLEN+1];
3256 struct stat st;
3257
3258 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3259 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003260 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003261 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3262 *err ? *err : "", fp);
3263 ret = 1;
3264 goto end;
3265 }
3266 }
3267 }
3268#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003269
William Lallemand246c0242019-10-11 08:59:13 +02003270 /* try to load an ocsp response file */
3271 {
3272 char fp[MAXPATHLEN+1];
3273 struct stat st;
3274
3275 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3276 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003277 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003278 ret = 1;
3279 goto end;
3280 }
3281 }
3282 }
3283
3284 if (ckch->ocsp_response) {
3285 X509 *issuer;
3286 int i;
3287
3288 /* check if one of the certificate of the chain is the issuer */
3289 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3290 issuer = sk_X509_value(ckch->chain, i);
3291 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3292 ckch->ocsp_issuer = issuer;
3293 break;
3294 } else
3295 issuer = NULL;
3296 }
3297
3298 /* if no issuer was found, try to load an issuer from the .issuer */
3299 if (!issuer) {
3300 struct stat st;
3301 char fp[MAXPATHLEN+1];
3302
3303 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3304 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003305 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003306 ret = 1;
3307 goto end;
3308 }
3309
3310 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003311 memprintf(err, "%s '%s' is not an issuer'.\n",
William Lallemand246c0242019-10-11 08:59:13 +02003312 *err ? *err : "", fp);
3313 ret = 1;
3314 goto end;
3315 }
3316 } else {
3317 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
3318 *err ? *err : "");
3319 ret = 1;
3320 goto end;
3321 }
3322 }
3323 }
3324
yanbzhu488a4d22015-12-01 15:16:07 -05003325 ret = 0;
3326
3327end:
3328
3329 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003330
3331 /* Something went wrong in one of the reads */
3332 if (ret != 0)
3333 ssl_sock_free_cert_key_and_chain_contents(ckch);
3334
3335 return ret;
3336}
3337
3338/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003339 * Returns a bitfield containing the flags:
3340 * ERR_FATAL in any fatal error case
3341 * ERR_ALERT if the reason of the error is available in err
3342 * ERR_WARN if a warning is available into err
3343 * The value 0 means there is no error nor warning and
3344 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003345 */
3346static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3347{
Emeric Bruna96b5822019-10-17 13:25:14 +02003348 int errcode = 0;
3349
yanbzhu488a4d22015-12-01 15:16:07 -05003350 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3351 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3352 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003353 errcode |= ERR_ALERT | ERR_FATAL;
3354 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003355 }
3356
3357 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3358 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3359 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003360 errcode |= ERR_ALERT | ERR_FATAL;
3361 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003362 }
3363
yanbzhu488a4d22015-12-01 15:16:07 -05003364 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003365#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003366 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3367 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3368 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003369 errcode |= ERR_ALERT | ERR_FATAL;
3370 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003371 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003372#else
3373 { /* legacy compat (< openssl 1.0.2) */
3374 X509 *ca;
3375 while ((ca = sk_X509_shift(ckch->chain)))
3376 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3377 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3378 err && *err ? *err : "", path);
3379 X509_free(ca);
Emeric Bruna96b5822019-10-17 13:25:14 +02003380 errcode |= ERR_ALERT | ERR_FATAL;
3381 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003382 }
3383 }
3384#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003385
William Lallemandfa892222019-07-23 16:06:08 +02003386#ifndef OPENSSL_NO_DH
3387 /* store a NULL pointer to indicate we have not yet loaded
3388 a custom DH param file */
3389 if (ssl_dh_ptr_index >= 0) {
3390 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3391 }
3392
Emeric Brun7a883362019-10-17 13:27:40 +02003393 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3394 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003395 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3396 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003397 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003398 }
3399#endif
3400
William Lallemanda17f4112019-10-10 15:16:44 +02003401#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3402 if (sctl_ex_index >= 0 && ckch->sctl) {
3403 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3404 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3405 *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003406 errcode |= ERR_ALERT | ERR_FATAL;
3407 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003408 }
3409 }
3410#endif
3411
William Lallemand4a660132019-10-14 14:51:41 +02003412#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003413 /* Load OCSP Info into context */
3414 if (ckch->ocsp_response) {
3415 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
3416 if (err)
3417 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",
3418 *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003419 errcode |= ERR_ALERT | ERR_FATAL;
3420 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003421 }
3422 }
William Lallemand246c0242019-10-11 08:59:13 +02003423#endif
3424
Emeric Bruna96b5822019-10-17 13:25:14 +02003425 end:
3426 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003427}
3428
William Lallemandc4ecddf2019-07-31 16:50:08 +02003429#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003430
William Lallemand28a8fce2019-10-04 17:36:55 +02003431static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003432{
3433 struct sni_keytype *s_kt = NULL;
3434 struct ebmb_node *node;
3435 int i;
3436
3437 for (i = 0; i < trash.size; i++) {
3438 if (!str[i])
3439 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003440 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003441 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003442 trash.area[i] = 0;
3443 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003444 if (!node) {
3445 /* CN not found in tree */
3446 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3447 /* Using memcpy here instead of strncpy.
3448 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3449 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3450 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003451 if (!s_kt)
3452 return -1;
3453
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003454 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003455 s_kt->keytypes = 0;
3456 ebst_insert(sni_keytypes, &s_kt->name);
3457 } else {
3458 /* CN found in tree */
3459 s_kt = container_of(node, struct sni_keytype, name);
3460 }
3461
3462 /* Mark that this CN has the keytype of key_index via keytypes mask */
3463 s_kt->keytypes |= 1<<key_index;
3464
William Lallemand28a8fce2019-10-04 17:36:55 +02003465 return 0;
3466
yanbzhu08ce6ab2015-12-02 13:01:29 -05003467}
3468
William Lallemandc4ecddf2019-07-31 16:50:08 +02003469#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003470/*
3471 * Free a ckch_store and its ckch(s)
3472 * The linked ckch_inst are not free'd
3473 */
3474void ckchs_free(struct ckch_store *ckchs)
3475{
3476 if (!ckchs)
3477 return;
3478
3479#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3480 if (ckchs->multi) {
3481 int n;
3482
3483 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3484 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3485 } else
3486#endif
3487 {
3488 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3489 ckchs->ckch = NULL;
3490 }
3491
3492 free(ckchs);
3493}
3494
3495/* allocate and duplicate a ckch_store
3496 * Return a new ckch_store or NULL */
3497static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3498{
3499 struct ckch_store *dst;
3500 int pathlen;
3501
3502 pathlen = strlen(src->path);
3503 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3504 if (!dst)
3505 return NULL;
3506 /* copy previous key */
3507 memcpy(dst->path, src->path, pathlen + 1);
3508 dst->multi = src->multi;
3509 LIST_INIT(&dst->ckch_inst);
3510
3511 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3512 if (!dst->ckch)
3513 goto error;
3514
3515#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3516 if (src->multi) {
3517 int n;
3518
3519 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3520 if (&src->ckch[n]) {
3521 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3522 goto error;
3523 }
3524 }
3525 } else
3526#endif
3527 {
3528 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3529 goto error;
3530 }
3531
3532 return dst;
3533
3534error:
3535 ckchs_free(dst);
3536
3537 return NULL;
3538}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003539
William Lallemand36b84632019-07-18 19:28:17 +02003540/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003541 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003542 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003543static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003544{
3545 struct ebmb_node *eb;
3546
William Lallemande3af8fb2019-10-08 11:36:53 +02003547 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003548 if (!eb)
3549 return NULL;
3550
William Lallemande3af8fb2019-10-08 11:36:53 +02003551 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003552}
3553
3554/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003555 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003556 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003557static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003558{
William Lallemande3af8fb2019-10-08 11:36:53 +02003559 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003560
William Lallemande3af8fb2019-10-08 11:36:53 +02003561 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3562 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003563 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3564 goto end;
3565 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003566 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003567
William Lallemande3af8fb2019-10-08 11:36:53 +02003568 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003569 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3570 goto end;
3571 }
3572
William Lallemand9117de92019-10-04 00:29:42 +02003573 LIST_INIT(&ckchs->ckch_inst);
3574
William Lallemand36b84632019-07-18 19:28:17 +02003575 if (!multi) {
3576
William Lallemand96a9c972019-10-17 11:56:17 +02003577 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003578 goto end;
3579
William Lallemande3af8fb2019-10-08 11:36:53 +02003580 /* insert into the ckchs tree */
3581 memcpy(ckchs->path, path, strlen(path) + 1);
3582 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003583 } else {
3584 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003585#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3586 char fp[MAXPATHLEN+1] = {0};
3587 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003588
3589 /* Load all possible certs and keys */
3590 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3591 struct stat buf;
3592 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3593 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003594 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003595 goto end;
3596 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003597 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003598 }
3599 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003600#endif
William Lallemand36b84632019-07-18 19:28:17 +02003601
3602 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003603 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003604 goto end;
3605 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003606 /* insert into the ckchs tree */
3607 memcpy(ckchs->path, path, strlen(path) + 1);
3608 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003609 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003610 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003611
William Lallemand36b84632019-07-18 19:28:17 +02003612end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003613 if (ckchs) {
3614 free(ckchs->ckch);
3615 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003616 }
3617
William Lallemande3af8fb2019-10-08 11:36:53 +02003618 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003619
3620 return NULL;
3621}
3622
William Lallemandc4ecddf2019-07-31 16:50:08 +02003623#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3624
William Lallemand36b84632019-07-18 19:28:17 +02003625/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003626 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003627 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003628 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3629 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003630 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003631 *
Emeric Brun054563d2019-10-17 13:16:58 +02003632 * Returns a bitfield containing the flags:
3633 * ERR_FATAL in any fatal error case
3634 * ERR_ALERT if the reason of the error is available in err
3635 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003636 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003637 */
Emeric Brun054563d2019-10-17 13:16:58 +02003638static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3639 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3640 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003641{
William Lallemand36b84632019-07-18 19:28:17 +02003642 int i = 0, n = 0;
3643 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003644 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003645 struct ebmb_node *node;
3646 struct ebmb_node *next;
3647 /* Array of SSL_CTX pointers corresponding to each possible combo
3648 * of keytypes
3649 */
3650 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003651 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003652 X509_NAME *xname = NULL;
3653 char *str = NULL;
3654#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3655 STACK_OF(GENERAL_NAME) *names = NULL;
3656#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003657 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003658
Emeric Brun054563d2019-10-17 13:16:58 +02003659 *ckchi = NULL;
3660
William Lallemande3af8fb2019-10-08 11:36:53 +02003661 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003662 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3663 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003664 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003665 }
3666
3667 ckch_inst = ckch_inst_new();
3668 if (!ckch_inst) {
3669 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3670 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003671 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003672 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003673 }
3674
William Lallemande3af8fb2019-10-08 11:36:53 +02003675 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003676
William Lallemand150bfa82019-09-19 17:12:49 +02003677 /* at least one of the instances is using filters during the config
3678 * parsing, that's ok to inherit this during loading on CLI */
3679 ckchs->filters = !!fcount;
3680
yanbzhu08ce6ab2015-12-02 13:01:29 -05003681 /* Process each ckch and update keytypes for each CN/SAN
3682 * for example, if CN/SAN www.a.com is associated with
3683 * certs with keytype 0 and 2, then at the end of the loop,
3684 * www.a.com will have:
3685 * keyindex = 0 | 1 | 4 = 5
3686 */
3687 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003688 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003689
3690 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3691 continue;
3692
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003693 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003694 for (i = 0; i < fcount; i++) {
3695 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3696 if (ret < 0) {
3697 memprintf(err, "%sunable to allocate SSL context.\n",
3698 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003699 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003700 goto end;
3701 }
3702 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003703 } else {
3704 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3705 * so the line that contains logic is marked via comments
3706 */
3707 xname = X509_get_subject_name(certs_and_keys[n].cert);
3708 i = -1;
3709 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3710 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003711 ASN1_STRING *value;
3712 value = X509_NAME_ENTRY_get_data(entry);
3713 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003714 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003715 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003716
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003717 OPENSSL_free(str);
3718 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003719 if (ret < 0) {
3720 memprintf(err, "%sunable to allocate SSL context.\n",
3721 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003722 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003723 goto end;
3724 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003725 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003726 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003727
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003728 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003729#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003730 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3731 if (names) {
3732 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3733 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003734
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003735 if (name->type == GEN_DNS) {
3736 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3737 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003738 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003739
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003740 OPENSSL_free(str);
3741 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003742 if (ret < 0) {
3743 memprintf(err, "%sunable to allocate SSL context.\n",
3744 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003745 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003746 goto end;
3747 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003748 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003749 }
3750 }
3751 }
3752 }
3753#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3754 }
3755
3756 /* If no files found, return error */
3757 if (eb_is_empty(&sni_keytypes_map)) {
3758 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3759 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003760 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003761 goto end;
3762 }
3763
3764 /* We now have a map of CN/SAN to keytypes that are loaded in
3765 * Iterate through the map to create the SSL_CTX's (if needed)
3766 * and add each CTX to the SNI tree
3767 *
3768 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003769 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003770 * combination is denoted by the key in the map. Each key
3771 * has a value between 1 and 2^n - 1. Conveniently, the array
3772 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3773 * entry in the array to correspond to the unique combo (key)
3774 * associated with i. This unique key combo (i) will be associated
3775 * with combos[i-1]
3776 */
3777
3778 node = ebmb_first(&sni_keytypes_map);
3779 while (node) {
3780 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003781 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003782 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003783
3784 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3785 i = container_of(node, struct sni_keytype, name)->keytypes;
3786 cur_ctx = key_combos[i-1].ctx;
3787
3788 if (cur_ctx == NULL) {
3789 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003790 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003791 if (cur_ctx == NULL) {
3792 memprintf(err, "%sunable to allocate SSL context.\n",
3793 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003794 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003795 goto end;
3796 }
3797
yanbzhube2774d2015-12-10 15:07:30 -05003798 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003799 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3800 if (i & (1<<n)) {
3801 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003802 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003803 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3804 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003805 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003806 }
3807 }
3808
yanbzhu08ce6ab2015-12-02 13:01:29 -05003809 /* Update key_combos */
3810 key_combos[i-1].ctx = cur_ctx;
3811 }
3812
3813 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003814
William Lallemand1d29c742019-10-04 00:53:29 +02003815 key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf,
William Lallemandfe49bb32019-10-03 23:46:33 +02003816 kinfo, str, key_combos[i-1].order);
3817 if (key_combos[i-1].order < 0) {
3818 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003819 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003820 goto end;
3821 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003822 node = ebmb_next(node);
3823 }
3824
3825
3826 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3827 if (!bind_conf->default_ctx) {
3828 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3829 if (key_combos[i].ctx) {
3830 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003831 bind_conf->default_ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003832 break;
3833 }
3834 }
3835 }
3836
William Lallemand614ca0d2019-10-07 13:52:11 +02003837 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02003838 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003839end:
3840
3841 if (names)
3842 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3843
yanbzhu08ce6ab2015-12-02 13:01:29 -05003844 node = ebmb_first(&sni_keytypes_map);
3845 while (node) {
3846 next = ebmb_next(node);
3847 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02003848 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05003849 node = next;
3850 }
3851
Emeric Brun054563d2019-10-17 13:16:58 +02003852 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02003853 struct sni_ctx *sc0, *sc0b;
3854
3855 /* free the SSL_CTX in case of error */
3856 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
3857 if (key_combos[i].ctx)
3858 SSL_CTX_free(key_combos[i].ctx);
3859 }
3860
3861 /* free the sni_ctx in case of error */
3862 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
3863
3864 ebmb_delete(&sc0->name);
3865 LIST_DEL(&sc0->by_ckch_inst);
3866 free(sc0);
3867 }
William Lallemand614ca0d2019-10-07 13:52:11 +02003868 free(ckch_inst);
3869 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02003870 }
3871
Emeric Brun054563d2019-10-17 13:16:58 +02003872 *ckchi = ckch_inst;
3873 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003874}
3875#else
3876/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02003877static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3878 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3879 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003880{
3881 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3882 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02003883 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003884}
3885
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003886#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003887
William Lallemand614ca0d2019-10-07 13:52:11 +02003888/*
3889 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02003890 *
3891 * Returns a bitfield containing the flags:
3892 * ERR_FATAL in any fatal error case
3893 * ERR_ALERT if the reason of the error is available in err
3894 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02003895 */
Emeric Brun054563d2019-10-17 13:16:58 +02003896static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
3897 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003898{
William Lallemandc9402072019-05-15 15:33:54 +02003899 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02003900 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003901 int order = 0;
3902 X509_NAME *xname;
3903 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003904 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003905 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02003906#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3907 STACK_OF(GENERAL_NAME) *names;
3908#endif
William Lallemand36b84632019-07-18 19:28:17 +02003909 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02003910 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02003911 int errcode = 0;
3912
3913 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02003914
William Lallemande3af8fb2019-10-08 11:36:53 +02003915 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02003916 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003917
William Lallemande3af8fb2019-10-08 11:36:53 +02003918 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003919
William Lallemand150bfa82019-09-19 17:12:49 +02003920 /* at least one of the instances is using filters during the config
3921 * parsing, that's ok to inherit this during loading on CLI */
3922 ckchs->filters = !!fcount;
3923
William Lallemandc9402072019-05-15 15:33:54 +02003924 ctx = SSL_CTX_new(SSLv23_server_method());
3925 if (!ctx) {
3926 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3927 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003928 errcode |= ERR_ALERT | ERR_FATAL;
3929 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003930 }
3931
Emeric Bruna96b5822019-10-17 13:25:14 +02003932 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
3933 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02003934 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02003935
3936 ckch_inst = ckch_inst_new();
3937 if (!ckch_inst) {
3938 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3939 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003940 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003941 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003942 }
3943
William Lallemand36b84632019-07-18 19:28:17 +02003944 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003945 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003946 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003947 switch(EVP_PKEY_base_id(pkey)) {
3948 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003949 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003950 break;
3951 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003952 kinfo.sig = TLSEXT_signature_ecdsa;
3953 break;
3954 case EVP_PKEY_DSA:
3955 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003956 break;
3957 }
3958 EVP_PKEY_free(pkey);
3959 }
3960
Emeric Brun50bcecc2013-04-22 13:05:23 +02003961 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02003962 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02003963 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order);
William Lallemandfe49bb32019-10-03 23:46:33 +02003964 if (order < 0) {
3965 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003966 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003967 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003968 }
3969 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003970 }
3971 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003972#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02003973 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003974 if (names) {
3975 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3976 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3977 if (name->type == GEN_DNS) {
3978 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003979 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003980 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003981 if (order < 0) {
3982 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003983 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003984 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003985 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003986 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003987 }
3988 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003989 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003990 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003991#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02003992 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003993 i = -1;
3994 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3995 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003996 ASN1_STRING *value;
3997
3998 value = X509_NAME_ENTRY_get_data(entry);
3999 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004000 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004001 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004002 if (order < 0) {
4003 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004004 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004005 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004006 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004007 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004008 }
4009 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004010 /* we must not free the SSL_CTX anymore below, since it's already in
4011 * the tree, so it will be discovered and cleaned in time.
4012 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004013
Emeric Brunfc0421f2012-09-07 17:30:07 +02004014#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004015 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004016 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4017 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004018 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004019 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004020 }
4021#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004022 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004023 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004024 bind_conf->default_ssl_conf = ssl_conf;
4025 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004026
William Lallemand9117de92019-10-04 00:29:42 +02004027 /* everything succeed, the ckch instance can be used */
4028 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004029 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004030
Emeric Brun054563d2019-10-17 13:16:58 +02004031 *ckchi = ckch_inst;
4032 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004033
4034error:
4035 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004036 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004037 struct sni_ctx *sc0, *sc0b;
4038
4039 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4040
4041 ebmb_delete(&sc0->name);
4042 LIST_DEL(&sc0->by_ckch_inst);
4043 free(sc0);
4044 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004045 free(ckch_inst);
4046 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004047 }
4048 /* We only created 1 SSL_CTX so we can free it there */
4049 SSL_CTX_free(ctx);
4050
Emeric Brun054563d2019-10-17 13:16:58 +02004051 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004052}
4053
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004054/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004055static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4056 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4057 char **sni_filter, int fcount, char **err)
4058{
4059 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004060 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004061
4062 /* we found the ckchs in the tree, we can use it directly */
4063 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004064 errcode |= ckch_inst_new_load_multi_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, &ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02004065 else
Emeric Brun054563d2019-10-17 13:16:58 +02004066 errcode |= ckch_inst_new_load_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, &ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02004067
Emeric Brun054563d2019-10-17 13:16:58 +02004068 if (errcode & ERR_CODE)
4069 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004070
4071 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4072
4073 /* succeed, add the instance to the ckch_store's list of instance */
4074 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004075 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004076}
4077
4078
Willy Tarreaubbc91962019-10-16 16:42:19 +02004079/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004080int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004081{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004082 struct dirent **de_list;
4083 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004084 DIR *dir;
4085 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004086 char *end;
4087 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004088 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004089 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004090#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004091 int is_bundle;
4092 int j;
4093#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004094 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004095 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004096 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004097 }
4098
yanbzhu08ce6ab2015-12-02 13:01:29 -05004099 if (stat(path, &buf) == 0) {
4100 dir = opendir(path);
William Lallemand36b84632019-07-18 19:28:17 +02004101 if (!dir) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004102 ckchs = ckchs_load_cert_file(path, 0, err);
4103 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004104 return ERR_ALERT | ERR_FATAL;
4105
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004106 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004107 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004108
yanbzhu08ce6ab2015-12-02 13:01:29 -05004109 /* strip trailing slashes, including first one */
4110 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4111 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004112
yanbzhu08ce6ab2015-12-02 13:01:29 -05004113 n = scandir(path, &de_list, 0, alphasort);
4114 if (n < 0) {
4115 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4116 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004117 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004118 }
4119 else {
4120 for (i = 0; i < n; i++) {
4121 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004122
yanbzhu08ce6ab2015-12-02 13:01:29 -05004123 end = strrchr(de->d_name, '.');
4124 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4125 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004126
yanbzhu08ce6ab2015-12-02 13:01:29 -05004127 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4128 if (stat(fp, &buf) != 0) {
4129 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4130 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004131 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004132 goto ignore_entry;
4133 }
4134 if (!S_ISREG(buf.st_mode))
4135 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004136
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004137#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004138 is_bundle = 0;
4139 /* Check if current entry in directory is part of a multi-cert bundle */
4140
4141 if (end) {
4142 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4143 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4144 is_bundle = 1;
4145 break;
4146 }
4147 }
4148
4149 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004150 int dp_len;
4151
4152 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004153
4154 /* increment i and free de until we get to a non-bundle cert
4155 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004156 * this is important since ignore_entry will free de. This also
4157 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004158 */
Willy Tarreau05800522019-10-29 10:48:50 +01004159 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004160 free(de);
4161 i++;
4162 de = de_list[i];
4163 }
4164
Willy Tarreau05800522019-10-29 10:48:50 +01004165 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004166 if ((ckchs = ckchs_lookup(fp)) == NULL)
4167 ckchs = ckchs_load_cert_file(fp, 1, err);
4168 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004169 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004170 else
4171 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004172 /* Successfully processed the bundle */
4173 goto ignore_entry;
4174 }
4175 }
4176
4177#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004178 if ((ckchs = ckchs_lookup(fp)) == NULL)
4179 ckchs = ckchs_load_cert_file(fp, 0, err);
4180 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004181 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004182 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004183 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004184
yanbzhu08ce6ab2015-12-02 13:01:29 -05004185ignore_entry:
4186 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004187 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004188 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004189 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004190 closedir(dir);
4191 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004192 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004193
William Lallemande3af8fb2019-10-08 11:36:53 +02004194 ckchs = ckchs_load_cert_file(path, 1, err);
4195 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004196 return ERR_ALERT | ERR_FATAL;
4197
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004198 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004199
Emeric Brunfc0421f2012-09-07 17:30:07 +02004200 return cfgerr;
4201}
4202
Thierry Fournier383085f2013-01-24 14:15:43 +01004203/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4204 * done once. Zero is returned if the operation fails. No error is returned
4205 * if the random is said as not implemented, because we expect that openssl
4206 * will use another method once needed.
4207 */
4208static int ssl_initialize_random()
4209{
4210 unsigned char random;
4211 static int random_initialized = 0;
4212
4213 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4214 random_initialized = 1;
4215
4216 return random_initialized;
4217}
4218
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004219/* release ssl bind conf */
4220void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004221{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004222 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004223#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004224 free(conf->npn_str);
4225 conf->npn_str = NULL;
4226#endif
4227#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4228 free(conf->alpn_str);
4229 conf->alpn_str = NULL;
4230#endif
4231 free(conf->ca_file);
4232 conf->ca_file = NULL;
4233 free(conf->crl_file);
4234 conf->crl_file = NULL;
4235 free(conf->ciphers);
4236 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004237#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004238 free(conf->ciphersuites);
4239 conf->ciphersuites = NULL;
4240#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004241 free(conf->curves);
4242 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004243 free(conf->ecdhe);
4244 conf->ecdhe = NULL;
4245 }
4246}
4247
Willy Tarreaubbc91962019-10-16 16:42:19 +02004248/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004249int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4250{
4251 char thisline[CRT_LINESIZE];
4252 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004253 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004254 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004255 int linenum = 0;
4256 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004257 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004258
Willy Tarreauad1731d2013-04-02 17:35:58 +02004259 if ((f = fopen(file, "r")) == NULL) {
4260 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004261 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004262 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004263
4264 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004265 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004266 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004267 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004268 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004269 char *crt_path;
4270 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004271
4272 linenum++;
4273 end = line + strlen(line);
4274 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4275 /* Check if we reached the limit and the last char is not \n.
4276 * Watch out for the last line without the terminating '\n'!
4277 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004278 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4279 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004280 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004281 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004282 }
4283
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004284 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004285 newarg = 1;
4286 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004287 if (*line == '#' || *line == '\n' || *line == '\r') {
4288 /* end of string, end of loop */
4289 *line = 0;
4290 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004291 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004292 newarg = 1;
4293 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004294 } else if (*line == '[') {
4295 if (ssl_b) {
4296 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004297 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004298 break;
4299 }
4300 if (!arg) {
4301 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004302 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004303 break;
4304 }
4305 ssl_b = arg;
4306 newarg = 1;
4307 *line = 0;
4308 } else if (*line == ']') {
4309 if (ssl_e) {
4310 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004311 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004312 break;
4313 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004314 if (!ssl_b) {
4315 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004316 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004317 break;
4318 }
4319 ssl_e = arg;
4320 newarg = 1;
4321 *line = 0;
4322 } else if (newarg) {
4323 if (arg == MAX_CRT_ARGS) {
4324 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004325 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004326 break;
4327 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004328 newarg = 0;
4329 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004330 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004331 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004332 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004333 if (cfgerr)
4334 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004335 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004336
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004337 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004338 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004339 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004340
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004341 crt_path = args[0];
4342 if (*crt_path != '/' && global_ssl.crt_base) {
4343 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4344 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4345 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004346 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004347 break;
4348 }
4349 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4350 crt_path = path;
4351 }
4352
4353 ssl_conf = calloc(1, sizeof *ssl_conf);
4354 cur_arg = ssl_b ? ssl_b : 1;
4355 while (cur_arg < ssl_e) {
4356 newarg = 0;
4357 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4358 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4359 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004360 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004361 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4362 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4363 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004364 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004365 }
4366 cur_arg += 1 + ssl_bind_kws[i].skip;
4367 break;
4368 }
4369 }
4370 if (!cfgerr && !newarg) {
4371 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4372 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004373 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004374 break;
4375 }
4376 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004377
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004378 if (cfgerr) {
4379 ssl_sock_free_ssl_conf(ssl_conf);
4380 free(ssl_conf);
4381 ssl_conf = NULL;
4382 break;
4383 }
4384
William Lallemande3af8fb2019-10-08 11:36:53 +02004385 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004386 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004387 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004388 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004389 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004390 }
4391
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004392 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004393 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004394 else
4395 cfgerr |= ssl_sock_load_ckchs(crt_path, ckchs, bind_conf, ssl_conf, &args[cur_arg], arg - cur_arg - 1, err);
William Lallemandeed4bf22019-10-10 11:38:13 +02004396
Willy Tarreauad1731d2013-04-02 17:35:58 +02004397 if (cfgerr) {
4398 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004399 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004400 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004401 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004402 fclose(f);
4403 return cfgerr;
4404}
4405
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004406/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004407static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004408ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004409{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004410 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004411 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004412 SSL_OP_ALL | /* all known workarounds for bugs */
4413 SSL_OP_NO_SSLv2 |
4414 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004415 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004416 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004417 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004418 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004419 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004420 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004421 SSL_MODE_ENABLE_PARTIAL_WRITE |
4422 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004423 SSL_MODE_RELEASE_BUFFERS |
4424 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004425 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004426 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004427 int flags = MC_SSL_O_ALL;
4428 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004429
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004430 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004431 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004432
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004433 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004434 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4435 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4436 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004437 else
4438 flags = conf_ssl_methods->flags;
4439
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004440 min = conf_ssl_methods->min;
4441 max = conf_ssl_methods->max;
4442 /* start with TLSv10 to remove SSLv3 per default */
4443 if (!min && (!max || max >= CONF_TLSV10))
4444 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004445 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004446 if (min)
4447 flags |= (methodVersions[min].flag - 1);
4448 if (max)
4449 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004450 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004451 min = max = CONF_TLSV_NONE;
4452 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004453 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004454 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004455 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004456 if (min) {
4457 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004458 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4459 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4460 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4461 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004462 hole = 0;
4463 }
4464 max = i;
4465 }
4466 else {
4467 min = max = i;
4468 }
4469 }
4470 else {
4471 if (min)
4472 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004473 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004474 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004475 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4476 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004477 cfgerr += 1;
4478 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004479 /* save real min/max in bind_conf */
4480 conf_ssl_methods->min = min;
4481 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004482
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004483#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004484 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004485 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004486 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004487 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004488 else
4489 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4490 if (flags & methodVersions[i].flag)
4491 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004492#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004493 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004494 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4495 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004496#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004497
4498 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4499 options |= SSL_OP_NO_TICKET;
4500 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4501 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004502
4503#ifdef SSL_OP_NO_RENEGOTIATION
4504 options |= SSL_OP_NO_RENEGOTIATION;
4505#endif
4506
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004507 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004508
Willy Tarreau5db847a2019-05-09 14:13:35 +02004509#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004510 if (global_ssl.async)
4511 mode |= SSL_MODE_ASYNC;
4512#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004513 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004514 if (global_ssl.life_time)
4515 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004516
4517#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4518#ifdef OPENSSL_IS_BORINGSSL
4519 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4520 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02004521#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004522 if (bind_conf->ssl_conf.early_data) {
4523 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
4524 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
4525 }
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004526 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4527 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004528#else
4529 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004530#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004531 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004532#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004533 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004534}
4535
William Lallemand4f45bb92017-10-30 20:08:51 +01004536
4537static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4538{
4539 if (first == block) {
4540 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4541 if (first->len > 0)
4542 sh_ssl_sess_tree_delete(sh_ssl_sess);
4543 }
4544}
4545
4546/* return first block from sh_ssl_sess */
4547static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4548{
4549 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4550
4551}
4552
4553/* store a session into the cache
4554 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4555 * data: asn1 encoded session
4556 * data_len: asn1 encoded session length
4557 * Returns 1 id session was stored (else 0)
4558 */
4559static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4560{
4561 struct shared_block *first;
4562 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4563
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004564 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004565 if (!first) {
4566 /* Could not retrieve enough free blocks to store that session */
4567 return 0;
4568 }
4569
4570 /* STORE the key in the first elem */
4571 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4572 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4573 first->len = sizeof(struct sh_ssl_sess_hdr);
4574
4575 /* it returns the already existing node
4576 or current node if none, never returns null */
4577 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4578 if (oldsh_ssl_sess != sh_ssl_sess) {
4579 /* NOTE: Row couldn't be in use because we lock read & write function */
4580 /* release the reserved row */
4581 shctx_row_dec_hot(ssl_shctx, first);
4582 /* replace the previous session already in the tree */
4583 sh_ssl_sess = oldsh_ssl_sess;
4584 /* ignore the previous session data, only use the header */
4585 first = sh_ssl_sess_first_block(sh_ssl_sess);
4586 shctx_row_inc_hot(ssl_shctx, first);
4587 first->len = sizeof(struct sh_ssl_sess_hdr);
4588 }
4589
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004590 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004591 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004592 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004593 }
4594
4595 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004596
4597 return 1;
4598}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004599
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004600/* SSL callback used when a new session is created while connecting to a server */
4601static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4602{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004603 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004604 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004605
Willy Tarreau07d94e42018-09-20 10:57:52 +02004606 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004607
Olivier Houcharde6060c52017-11-16 17:42:52 +01004608 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4609 int len;
4610 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004611
Olivier Houcharde6060c52017-11-16 17:42:52 +01004612 len = i2d_SSL_SESSION(sess, NULL);
4613 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4614 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4615 } else {
4616 free(s->ssl_ctx.reused_sess[tid].ptr);
4617 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4618 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4619 }
4620 if (s->ssl_ctx.reused_sess[tid].ptr) {
4621 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4622 &ptr);
4623 }
4624 } else {
4625 free(s->ssl_ctx.reused_sess[tid].ptr);
4626 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4627 }
4628
4629 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004630}
4631
Olivier Houcharde6060c52017-11-16 17:42:52 +01004632
William Lallemanded0b5ad2017-10-30 19:36:36 +01004633/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004634int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004635{
4636 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4637 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4638 unsigned char *p;
4639 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004640 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004641 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004642
4643 /* Session id is already stored in to key and session id is known
4644 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004645 * note: SSL_SESSION_set1_id is using
4646 * a memcpy so we need to use a different pointer
4647 * than sid_data or sid_ctx_data to avoid valgrind
4648 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004649 */
4650
4651 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004652
4653 /* copy value in an other buffer */
4654 memcpy(encid, sid_data, sid_length);
4655
4656 /* pad with 0 */
4657 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4658 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4659
4660 /* force length to zero to avoid ASN1 encoding */
4661 SSL_SESSION_set1_id(sess, encid, 0);
4662
4663 /* force length to zero to avoid ASN1 encoding */
4664 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004665
4666 /* check if buffer is large enough for the ASN1 encoded session */
4667 data_len = i2d_SSL_SESSION(sess, NULL);
4668 if (data_len > SHSESS_MAX_DATA_LEN)
4669 goto err;
4670
4671 p = encsess;
4672
4673 /* process ASN1 session encoding before the lock */
4674 i2d_SSL_SESSION(sess, &p);
4675
William Lallemanded0b5ad2017-10-30 19:36:36 +01004676
William Lallemanda3c77cf2017-10-30 23:44:40 +01004677 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004678 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004679 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004680 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004681err:
4682 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004683 SSL_SESSION_set1_id(sess, encid, sid_length);
4684 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004685
4686 return 0; /* do not increment session reference count */
4687}
4688
4689/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004690SSL_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 +01004691{
William Lallemand4f45bb92017-10-30 20:08:51 +01004692 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004693 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4694 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004695 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004696 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004697
4698 global.shctx_lookups++;
4699
4700 /* allow the session to be freed automatically by openssl */
4701 *do_copy = 0;
4702
4703 /* tree key is zeros padded sessionid */
4704 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4705 memcpy(tmpkey, key, key_len);
4706 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4707 key = tmpkey;
4708 }
4709
4710 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004711 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004712
4713 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004714 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4715 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004716 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004717 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004718 global.shctx_misses++;
4719 return NULL;
4720 }
4721
William Lallemand4f45bb92017-10-30 20:08:51 +01004722 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4723 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004724
William Lallemand4f45bb92017-10-30 20:08:51 +01004725 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 +01004726
William Lallemanda3c77cf2017-10-30 23:44:40 +01004727 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004728
4729 /* decode ASN1 session */
4730 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004731 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004732 /* Reset session id and session id contenxt */
4733 if (sess) {
4734 SSL_SESSION_set1_id(sess, key, key_len);
4735 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4736 }
4737
4738 return sess;
4739}
4740
William Lallemand4f45bb92017-10-30 20:08:51 +01004741
William Lallemanded0b5ad2017-10-30 19:36:36 +01004742/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004743void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004744{
William Lallemand4f45bb92017-10-30 20:08:51 +01004745 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004746 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4747 unsigned int sid_length;
4748 const unsigned char *sid_data;
4749 (void)ctx;
4750
4751 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4752 /* tree key is zeros padded sessionid */
4753 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4754 memcpy(tmpkey, sid_data, sid_length);
4755 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4756 sid_data = tmpkey;
4757 }
4758
William Lallemanda3c77cf2017-10-30 23:44:40 +01004759 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004760
4761 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004762 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4763 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004764 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004765 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004766 }
4767
4768 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004769 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004770}
4771
4772/* Set session cache mode to server and disable openssl internal cache.
4773 * Set shared cache callbacks on an ssl context.
4774 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004775void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004776{
4777 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4778
4779 if (!ssl_shctx) {
4780 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4781 return;
4782 }
4783
4784 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4785 SSL_SESS_CACHE_NO_INTERNAL |
4786 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4787
4788 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004789 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4790 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4791 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004792}
4793
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004794int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx)
4795{
4796 struct proxy *curproxy = bind_conf->frontend;
4797 int cfgerr = 0;
4798 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004799 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004800 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004801#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004802 const char *conf_ciphersuites;
4803#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004804 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004805
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004806 if (ssl_conf) {
4807 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4808 int i, min, max;
4809 int flags = MC_SSL_O_ALL;
4810
4811 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004812 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4813 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004814 if (min)
4815 flags |= (methodVersions[min].flag - 1);
4816 if (max)
4817 flags |= ~((methodVersions[max].flag << 1) - 1);
4818 min = max = CONF_TLSV_NONE;
4819 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4820 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4821 if (min)
4822 max = i;
4823 else
4824 min = max = i;
4825 }
4826 /* save real min/max */
4827 conf_ssl_methods->min = min;
4828 conf_ssl_methods->max = max;
4829 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004830 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4831 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004832 cfgerr += 1;
4833 }
4834 }
4835
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004836 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004837 case SSL_SOCK_VERIFY_NONE:
4838 verify = SSL_VERIFY_NONE;
4839 break;
4840 case SSL_SOCK_VERIFY_OPTIONAL:
4841 verify = SSL_VERIFY_PEER;
4842 break;
4843 case SSL_SOCK_VERIFY_REQUIRED:
4844 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4845 break;
4846 }
4847 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4848 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004849 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
4850 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
4851 if (ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004852 /* load CAfile to verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004853 if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004854 ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
4855 curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004856 cfgerr++;
4857 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004858 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
4859 /* set CA names for client cert request, function returns void */
4860 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file));
4861 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004862 }
Emeric Brun850efd52014-01-29 12:24:34 +01004863 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004864 ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4865 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun850efd52014-01-29 12:24:34 +01004866 cfgerr++;
4867 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004868#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004869 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004870 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4871
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004872 if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004873 ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4874 curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004875 cfgerr++;
4876 }
Emeric Brun561e5742012-10-02 15:20:55 +02004877 else {
4878 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4879 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004880 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004881#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004882 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004883 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004884#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004885 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004886 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004887 ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4888 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004889 cfgerr++;
4890 }
4891 }
4892#endif
4893
William Lallemand4f45bb92017-10-30 20:08:51 +01004894 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004895 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4896 if (conf_ciphers &&
4897 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004898 ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4899 curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004900 cfgerr++;
4901 }
4902
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004903#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004904 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4905 if (conf_ciphersuites &&
4906 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
4907 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4908 curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
4909 cfgerr++;
4910 }
4911#endif
4912
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004913#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004914 /* If tune.ssl.default-dh-param has not been set,
4915 neither has ssl-default-dh-file and no static DH
4916 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004917 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004918 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004919 (ssl_dh_ptr_index == -1 ||
4920 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004921 STACK_OF(SSL_CIPHER) * ciphers = NULL;
4922 const SSL_CIPHER * cipher = NULL;
4923 char cipher_description[128];
4924 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
4925 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
4926 which is not ephemeral DH. */
4927 const char dhe_description[] = " Kx=DH ";
4928 const char dhe_export_description[] = " Kx=DH(";
4929 int idx = 0;
4930 int dhe_found = 0;
4931 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004932
Remi Gacogne23d5d372014-10-10 17:04:26 +02004933 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004934
Remi Gacogne23d5d372014-10-10 17:04:26 +02004935 if (ssl) {
4936 ciphers = SSL_get_ciphers(ssl);
4937
4938 if (ciphers) {
4939 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
4940 cipher = sk_SSL_CIPHER_value(ciphers, idx);
4941 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
4942 if (strstr(cipher_description, dhe_description) != NULL ||
4943 strstr(cipher_description, dhe_export_description) != NULL) {
4944 dhe_found = 1;
4945 break;
4946 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02004947 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004948 }
4949 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02004950 SSL_free(ssl);
4951 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004952 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004953
Lukas Tribus90132722014-08-18 00:56:33 +02004954 if (dhe_found) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004955 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 +02004956 }
4957
Willy Tarreauef934602016-12-22 23:12:01 +01004958 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004959 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004960
Willy Tarreauef934602016-12-22 23:12:01 +01004961 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004962 if (local_dh_1024 == NULL) {
4963 local_dh_1024 = ssl_get_dh_1024();
4964 }
Willy Tarreauef934602016-12-22 23:12:01 +01004965 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004966 if (local_dh_2048 == NULL) {
4967 local_dh_2048 = ssl_get_dh_2048();
4968 }
Willy Tarreauef934602016-12-22 23:12:01 +01004969 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004970 if (local_dh_4096 == NULL) {
4971 local_dh_4096 = ssl_get_dh_4096();
4972 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004973 }
4974 }
4975 }
4976#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004977
Emeric Brunfc0421f2012-09-07 17:30:07 +02004978 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004979#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004980 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004981#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004982
Bernard Spil13c53f82018-02-15 13:34:58 +01004983#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004984 ssl_conf_cur = NULL;
4985 if (ssl_conf && ssl_conf->npn_str)
4986 ssl_conf_cur = ssl_conf;
4987 else if (bind_conf->ssl_conf.npn_str)
4988 ssl_conf_cur = &bind_conf->ssl_conf;
4989 if (ssl_conf_cur)
4990 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004991#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004992#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004993 ssl_conf_cur = NULL;
4994 if (ssl_conf && ssl_conf->alpn_str)
4995 ssl_conf_cur = ssl_conf;
4996 else if (bind_conf->ssl_conf.alpn_str)
4997 ssl_conf_cur = &bind_conf->ssl_conf;
4998 if (ssl_conf_cur)
4999 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005000#endif
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005001#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005002 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5003 if (conf_curves) {
5004 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005005 ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5006 curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005007 cfgerr++;
5008 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005009#if defined(SSL_CTX_set_ecdh_auto)
5010 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
5011#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005012 }
5013#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005014#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005015 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005016 int i;
5017 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005018#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005019 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005020 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5021 NULL);
5022
5023 if (ecdhe == NULL) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02005024 SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005025 return cfgerr;
5026 }
5027#else
5028 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5029 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5030 ECDHE_DEFAULT_CURVE);
5031#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005032
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005033 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005034 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005035 ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5036 curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02005037 cfgerr++;
5038 }
5039 else {
5040 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5041 EC_KEY_free(ecdh);
5042 }
5043 }
5044#endif
5045
Emeric Brunfc0421f2012-09-07 17:30:07 +02005046 return cfgerr;
5047}
5048
Evan Broderbe554312013-06-27 00:05:25 -07005049static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5050{
5051 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5052 size_t prefixlen, suffixlen;
5053
5054 /* Trivial case */
5055 if (strcmp(pattern, hostname) == 0)
5056 return 1;
5057
Evan Broderbe554312013-06-27 00:05:25 -07005058 /* The rest of this logic is based on RFC 6125, section 6.4.3
5059 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5060
Emeric Bruna848dae2013-10-08 11:27:28 +02005061 pattern_wildcard = NULL;
5062 pattern_left_label_end = pattern;
5063 while (*pattern_left_label_end != '.') {
5064 switch (*pattern_left_label_end) {
5065 case 0:
5066 /* End of label not found */
5067 return 0;
5068 case '*':
5069 /* If there is more than one wildcards */
5070 if (pattern_wildcard)
5071 return 0;
5072 pattern_wildcard = pattern_left_label_end;
5073 break;
5074 }
5075 pattern_left_label_end++;
5076 }
5077
5078 /* If it's not trivial and there is no wildcard, it can't
5079 * match */
5080 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005081 return 0;
5082
5083 /* Make sure all labels match except the leftmost */
5084 hostname_left_label_end = strchr(hostname, '.');
5085 if (!hostname_left_label_end
5086 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5087 return 0;
5088
5089 /* Make sure the leftmost label of the hostname is long enough
5090 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005091 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005092 return 0;
5093
5094 /* Finally compare the string on either side of the
5095 * wildcard */
5096 prefixlen = pattern_wildcard - pattern;
5097 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005098 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5099 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005100 return 0;
5101
5102 return 1;
5103}
5104
5105static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5106{
5107 SSL *ssl;
5108 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005109 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005110 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005111 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005112
5113 int depth;
5114 X509 *cert;
5115 STACK_OF(GENERAL_NAME) *alt_names;
5116 int i;
5117 X509_NAME *cert_subject;
5118 char *str;
5119
5120 if (ok == 0)
5121 return ok;
5122
5123 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005124 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005125 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005126
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005127 /* We're checking if the provided hostnames match the desired one. The
5128 * desired hostname comes from the SNI we presented if any, or if not
5129 * provided then it may have been explicitly stated using a "verifyhost"
5130 * directive. If neither is set, we don't care about the name so the
5131 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005132 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005133 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005134 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005135 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005136 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005137 if (!servername)
5138 return ok;
5139 }
Evan Broderbe554312013-06-27 00:05:25 -07005140
5141 /* We only need to verify the CN on the actual server cert,
5142 * not the indirect CAs */
5143 depth = X509_STORE_CTX_get_error_depth(ctx);
5144 if (depth != 0)
5145 return ok;
5146
5147 /* At this point, the cert is *not* OK unless we can find a
5148 * hostname match */
5149 ok = 0;
5150
5151 cert = X509_STORE_CTX_get_current_cert(ctx);
5152 /* It seems like this might happen if verify peer isn't set */
5153 if (!cert)
5154 return ok;
5155
5156 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5157 if (alt_names) {
5158 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5159 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5160 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005161#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005162 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5163#else
Evan Broderbe554312013-06-27 00:05:25 -07005164 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005165#endif
Evan Broderbe554312013-06-27 00:05:25 -07005166 ok = ssl_sock_srv_hostcheck(str, servername);
5167 OPENSSL_free(str);
5168 }
5169 }
5170 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005171 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005172 }
5173
5174 cert_subject = X509_get_subject_name(cert);
5175 i = -1;
5176 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5177 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005178 ASN1_STRING *value;
5179 value = X509_NAME_ENTRY_get_data(entry);
5180 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005181 ok = ssl_sock_srv_hostcheck(str, servername);
5182 OPENSSL_free(str);
5183 }
5184 }
5185
Willy Tarreau71d058c2017-07-26 20:09:56 +02005186 /* report the mismatch and indicate if SNI was used or not */
5187 if (!ok && !conn->err_code)
5188 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005189 return ok;
5190}
5191
Emeric Brun94324a42012-10-11 14:00:19 +02005192/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005193int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005194{
Willy Tarreau03209342016-12-22 17:08:28 +01005195 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005196 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005197 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005198 SSL_OP_ALL | /* all known workarounds for bugs */
5199 SSL_OP_NO_SSLv2 |
5200 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005201 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005202 SSL_MODE_ENABLE_PARTIAL_WRITE |
5203 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005204 SSL_MODE_RELEASE_BUFFERS |
5205 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005206 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005207 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005208 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005209 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005210 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005211
Thierry Fournier383085f2013-01-24 14:15:43 +01005212 /* Make sure openssl opens /dev/urandom before the chroot */
5213 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005214 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005215 cfgerr++;
5216 }
5217
Willy Tarreaufce03112015-01-15 21:32:40 +01005218 /* Automatic memory computations need to know we use SSL there */
5219 global.ssl_used_backend = 1;
5220
5221 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005222 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005223 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005224 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5225 curproxy->id, srv->id,
5226 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005227 cfgerr++;
5228 return cfgerr;
5229 }
5230 }
Emeric Brun94324a42012-10-11 14:00:19 +02005231 if (srv->use_ssl)
5232 srv->xprt = &ssl_sock;
5233 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005234 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005235
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005236 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005237 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005238 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5239 proxy_type_str(curproxy), curproxy->id,
5240 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005241 cfgerr++;
5242 return cfgerr;
5243 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005244
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005245 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005246 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5247 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5248 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005249 else
5250 flags = conf_ssl_methods->flags;
5251
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005252 /* Real min and max should be determinate with configuration and openssl's capabilities */
5253 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005254 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005255 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005256 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005257
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005258 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005259 min = max = CONF_TLSV_NONE;
5260 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005261 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005262 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005263 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005264 if (min) {
5265 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005266 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5267 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5268 proxy_type_str(curproxy), curproxy->id, srv->id,
5269 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005270 hole = 0;
5271 }
5272 max = i;
5273 }
5274 else {
5275 min = max = i;
5276 }
5277 }
5278 else {
5279 if (min)
5280 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005281 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005282 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005283 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5284 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005285 cfgerr += 1;
5286 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005287
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005288#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005289 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005290 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005291 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005292 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005293 else
5294 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5295 if (flags & methodVersions[i].flag)
5296 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005297#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005298 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005299 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5300 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005301#endif
5302
5303 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5304 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005305 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005306
Willy Tarreau5db847a2019-05-09 14:13:35 +02005307#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005308 if (global_ssl.async)
5309 mode |= SSL_MODE_ASYNC;
5310#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005311 SSL_CTX_set_mode(ctx, mode);
5312 srv->ssl_ctx.ctx = ctx;
5313
Emeric Bruna7aa3092012-10-26 12:58:00 +02005314 if (srv->ssl_ctx.client_crt) {
5315 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 +01005316 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5317 proxy_type_str(curproxy), curproxy->id,
5318 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005319 cfgerr++;
5320 }
5321 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 +01005322 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5323 proxy_type_str(curproxy), curproxy->id,
5324 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005325 cfgerr++;
5326 }
5327 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005328 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5329 proxy_type_str(curproxy), curproxy->id,
5330 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005331 cfgerr++;
5332 }
5333 }
Emeric Brun94324a42012-10-11 14:00:19 +02005334
Emeric Brun850efd52014-01-29 12:24:34 +01005335 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5336 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005337 switch (srv->ssl_ctx.verify) {
5338 case SSL_SOCK_VERIFY_NONE:
5339 verify = SSL_VERIFY_NONE;
5340 break;
5341 case SSL_SOCK_VERIFY_REQUIRED:
5342 verify = SSL_VERIFY_PEER;
5343 break;
5344 }
Evan Broderbe554312013-06-27 00:05:25 -07005345 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005346 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005347 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005348 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005349 if (srv->ssl_ctx.ca_file) {
5350 /* load CAfile to verify */
5351 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005352 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
5353 curproxy->id, srv->id,
5354 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005355 cfgerr++;
5356 }
5357 }
Emeric Brun850efd52014-01-29 12:24:34 +01005358 else {
5359 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005360 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",
5361 curproxy->id, srv->id,
5362 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005363 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005364 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5365 curproxy->id, srv->id,
5366 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005367 cfgerr++;
5368 }
Emeric Brunef42d922012-10-11 16:11:36 +02005369#ifdef X509_V_FLAG_CRL_CHECK
5370 if (srv->ssl_ctx.crl_file) {
5371 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5372
5373 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005374 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5375 curproxy->id, srv->id,
5376 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005377 cfgerr++;
5378 }
5379 else {
5380 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5381 }
5382 }
5383#endif
5384 }
5385
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005386 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5387 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5388 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005389 if (srv->ssl_ctx.ciphers &&
5390 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005391 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5392 curproxy->id, srv->id,
5393 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005394 cfgerr++;
5395 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005396
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005397#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005398 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005399 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005400 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5401 curproxy->id, srv->id,
5402 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5403 cfgerr++;
5404 }
5405#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005406#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5407 if (srv->ssl_ctx.npn_str)
5408 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5409#endif
5410#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5411 if (srv->ssl_ctx.alpn_str)
5412 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5413#endif
5414
Emeric Brun94324a42012-10-11 14:00:19 +02005415
5416 return cfgerr;
5417}
5418
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005419/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005420 * be NULL, in which case nothing is done. Returns the number of errors
5421 * encountered.
5422 */
Willy Tarreau03209342016-12-22 17:08:28 +01005423int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005424{
5425 struct ebmb_node *node;
5426 struct sni_ctx *sni;
5427 int err = 0;
5428
Willy Tarreaufce03112015-01-15 21:32:40 +01005429 /* Automatic memory computations need to know we use SSL there */
5430 global.ssl_used_frontend = 1;
5431
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005432 /* Make sure openssl opens /dev/urandom before the chroot */
5433 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005434 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005435 err++;
5436 }
5437 /* Create initial_ctx used to start the ssl connection before do switchctx */
5438 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005439 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005440 /* It should not be necessary to call this function, but it's
5441 necessary first to check and move all initialisation related
5442 to initial_ctx in ssl_sock_initial_ctx. */
5443 err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx);
5444 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005445 if (bind_conf->default_ctx)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005446 err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx);
Emeric Brun0bed9942014-10-30 19:25:24 +01005447
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005448 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005449 while (node) {
5450 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005451 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5452 /* only initialize the CTX on its first occurrence and
5453 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005454 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005455 node = ebmb_next(node);
5456 }
5457
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005458 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005459 while (node) {
5460 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005461 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5462 /* only initialize the CTX on its first occurrence and
5463 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005464 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005465 node = ebmb_next(node);
5466 }
5467 return err;
5468}
5469
Willy Tarreau55d37912016-12-21 23:38:39 +01005470/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5471 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5472 * alerts are directly emitted since the rest of the stack does it below.
5473 */
5474int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5475{
5476 struct proxy *px = bind_conf->frontend;
5477 int alloc_ctx;
5478 int err;
5479
5480 if (!bind_conf->is_ssl) {
5481 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005482 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5483 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005484 }
5485 return 0;
5486 }
5487 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005488 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005489 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5490 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005491 }
5492 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005493 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5494 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005495 return -1;
5496 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005497 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005498 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005499 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005500 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005501 sizeof(*sh_ssl_sess_tree),
5502 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005503 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005504 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5505 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");
5506 else
5507 ha_alert("Unable to allocate SSL session cache.\n");
5508 return -1;
5509 }
5510 /* free block callback */
5511 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5512 /* init the root tree within the extra space */
5513 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5514 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005515 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005516 err = 0;
5517 /* initialize all certificate contexts */
5518 err += ssl_sock_prepare_all_ctx(bind_conf);
5519
5520 /* initialize CA variables if the certificates generation is enabled */
5521 err += ssl_sock_load_ca(bind_conf);
5522
5523 return -err;
5524}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005525
5526/* release ssl context allocated for servers. */
5527void ssl_sock_free_srv_ctx(struct server *srv)
5528{
Olivier Houchardc7566002018-11-20 23:33:50 +01005529#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5530 if (srv->ssl_ctx.alpn_str)
5531 free(srv->ssl_ctx.alpn_str);
5532#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005533#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005534 if (srv->ssl_ctx.npn_str)
5535 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005536#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005537 if (srv->ssl_ctx.ctx)
5538 SSL_CTX_free(srv->ssl_ctx.ctx);
5539}
5540
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005541/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005542 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5543 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005544void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005545{
5546 struct ebmb_node *node, *back;
5547 struct sni_ctx *sni;
5548
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005549 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005550 while (node) {
5551 sni = ebmb_entry(node, struct sni_ctx, name);
5552 back = ebmb_next(node);
5553 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005554 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005555 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005556 ssl_sock_free_ssl_conf(sni->conf);
5557 free(sni->conf);
5558 sni->conf = NULL;
5559 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005560 free(sni);
5561 node = back;
5562 }
5563
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005564 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005565 while (node) {
5566 sni = ebmb_entry(node, struct sni_ctx, name);
5567 back = ebmb_next(node);
5568 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005569 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005570 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005571 ssl_sock_free_ssl_conf(sni->conf);
5572 free(sni->conf);
5573 sni->conf = NULL;
5574 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005575 free(sni);
5576 node = back;
5577 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005578 SSL_CTX_free(bind_conf->initial_ctx);
5579 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005580 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005581 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005582}
5583
Willy Tarreau795cdab2016-12-22 17:30:54 +01005584/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5585void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5586{
5587 ssl_sock_free_ca(bind_conf);
5588 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005589 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005590 free(bind_conf->ca_sign_file);
5591 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005592 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005593 free(bind_conf->keys_ref->filename);
5594 free(bind_conf->keys_ref->tlskeys);
5595 LIST_DEL(&bind_conf->keys_ref->list);
5596 free(bind_conf->keys_ref);
5597 }
5598 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005599 bind_conf->ca_sign_pass = NULL;
5600 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005601}
5602
Christopher Faulet31af49d2015-06-09 17:29:50 +02005603/* Load CA cert file and private key used to generate certificates */
5604int
Willy Tarreau03209342016-12-22 17:08:28 +01005605ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005606{
Willy Tarreau03209342016-12-22 17:08:28 +01005607 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005608 FILE *fp;
5609 X509 *cacert = NULL;
5610 EVP_PKEY *capkey = NULL;
5611 int err = 0;
5612
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005613 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005614 return err;
5615
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005616#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005617 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005618 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005619 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005620 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005621 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005622#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005623
Christopher Faulet31af49d2015-06-09 17:29:50 +02005624 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005625 ha_alert("Proxy '%s': cannot enable certificate generation, "
5626 "no CA certificate File configured at [%s:%d].\n",
5627 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005628 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005629 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005630
5631 /* read in the CA certificate */
5632 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005633 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5634 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005635 goto load_error;
5636 }
5637 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005638 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5639 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005640 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005641 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005642 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005643 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005644 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5645 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005646 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005647 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005648
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005649 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005650 bind_conf->ca_sign_cert = cacert;
5651 bind_conf->ca_sign_pkey = capkey;
5652 return err;
5653
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005654 read_error:
5655 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005656 if (capkey) EVP_PKEY_free(capkey);
5657 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005658 load_error:
5659 bind_conf->generate_certs = 0;
5660 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005661 return err;
5662}
5663
5664/* Release CA cert and private key used to generate certificated */
5665void
5666ssl_sock_free_ca(struct bind_conf *bind_conf)
5667{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005668 if (bind_conf->ca_sign_pkey)
5669 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5670 if (bind_conf->ca_sign_cert)
5671 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005672 bind_conf->ca_sign_pkey = NULL;
5673 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005674}
5675
Emeric Brun46591952012-05-18 15:47:34 +02005676/*
5677 * This function is called if SSL * context is not yet allocated. The function
5678 * is designed to be called before any other data-layer operation and sets the
5679 * handshake flag on the connection. It is safe to call it multiple times.
5680 * It returns 0 on success and -1 in error case.
5681 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005682static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005683{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005684 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005685 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005686 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005687 return 0;
5688
Willy Tarreau3c728722014-01-23 13:50:42 +01005689 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005690 return 0;
5691
Olivier Houchard66ab4982019-02-26 18:37:15 +01005692 ctx = pool_alloc(ssl_sock_ctx_pool);
5693 if (!ctx) {
5694 conn->err_code = CO_ER_SSL_NO_MEM;
5695 return -1;
5696 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005697 ctx->wait_event.tasklet = tasklet_new();
5698 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005699 conn->err_code = CO_ER_SSL_NO_MEM;
5700 pool_free(ssl_sock_ctx_pool, ctx);
5701 return -1;
5702 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005703 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5704 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005705 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005706 ctx->sent_early_data = 0;
5707 ctx->tmp_early_data = -1;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005708 ctx->conn = conn;
Olivier Houchard81284e62019-06-06 13:21:23 +02005709 ctx->send_wait = NULL;
5710 ctx->recv_wait = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005711 ctx->xprt_st = 0;
5712 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005713
5714 /* Only work with sockets for now, this should be adapted when we'll
5715 * add QUIC support.
5716 */
5717 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005718 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005719 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5720 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005721 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005722
Willy Tarreau20879a02012-12-03 16:32:10 +01005723 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5724 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005725 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005726 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005727
Emeric Brun46591952012-05-18 15:47:34 +02005728 /* If it is in client mode initiate SSL session
5729 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005730 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005731 int may_retry = 1;
5732
5733 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005734 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005735 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5736 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005737 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005738 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005739 goto retry_connect;
5740 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005741 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005742 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005743 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005744 ctx->bio = BIO_new(ha_meth);
5745 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005746 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005747 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005748 goto retry_connect;
5749 }
Emeric Brun55476152014-11-12 17:35:37 +01005750 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005751 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005752 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005753 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005754 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005755
Evan Broderbe554312013-06-27 00:05:25 -07005756 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005757 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5758 SSL_free(ctx->ssl);
5759 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005760 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005761 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005762 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005763 goto retry_connect;
5764 }
Emeric Brun55476152014-11-12 17:35:37 +01005765 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005766 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005767 }
5768
Olivier Houchard66ab4982019-02-26 18:37:15 +01005769 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005770 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5771 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5772 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 +01005773 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005774 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005775 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5776 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005777 } else if (sess) {
5778 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005779 }
5780 }
Evan Broderbe554312013-06-27 00:05:25 -07005781
Emeric Brun46591952012-05-18 15:47:34 +02005782 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005783 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005784
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005785 _HA_ATOMIC_ADD(&sslconns, 1);
5786 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005787 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005788 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005789 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005790 if (conn->flags & CO_FL_ERROR)
5791 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005792 return 0;
5793 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005794 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005795 int may_retry = 1;
5796
5797 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005798 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005799 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5800 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005801 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005802 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005803 goto retry_accept;
5804 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005805 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005806 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005807 }
Emeric Brun46591952012-05-18 15:47:34 +02005808
Olivier Houcharda8955d52019-04-07 22:00:38 +02005809 ctx->bio = BIO_new(ha_meth);
5810 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005811 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005812 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005813 goto retry_accept;
5814 }
Emeric Brun55476152014-11-12 17:35:37 +01005815 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005816 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005817 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005818 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005819 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005820
Emeric Brune1f38db2012-09-03 20:36:47 +02005821 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005822 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5823 SSL_free(ctx->ssl);
5824 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005825 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005826 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005827 goto retry_accept;
5828 }
Emeric Brun55476152014-11-12 17:35:37 +01005829 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005830 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005831 }
5832
Olivier Houchard66ab4982019-02-26 18:37:15 +01005833 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005834
Emeric Brun46591952012-05-18 15:47:34 +02005835 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005836 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005837#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005838 conn->flags |= CO_FL_EARLY_SSL_HS;
5839#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005840
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005841 _HA_ATOMIC_ADD(&sslconns, 1);
5842 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005843 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005844 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005845 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005846 if (conn->flags & CO_FL_ERROR)
5847 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005848 return 0;
5849 }
5850 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005851 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005852err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005853 if (ctx && ctx->wait_event.tasklet)
5854 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005855 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005856 return -1;
5857}
5858
5859
5860/* This is the callback which is used when an SSL handshake is pending. It
5861 * updates the FD status if it wants some polling before being called again.
5862 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5863 * otherwise it returns non-zero and removes itself from the connection's
5864 * flags (the bit is provided in <flag> by the caller).
5865 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005866static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005867{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005868 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005869 int ret;
5870
Willy Tarreau3c728722014-01-23 13:50:42 +01005871 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005872 return 0;
5873
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005874 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005875 goto out_error;
5876
Willy Tarreau5db847a2019-05-09 14:13:35 +02005877#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005878 /*
5879 * Check if we have early data. If we do, we have to read them
5880 * before SSL_do_handshake() is called, And there's no way to
5881 * detect early data, except to try to read them
5882 */
5883 if (conn->flags & CO_FL_EARLY_SSL_HS) {
5884 size_t read_data;
5885
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005886 ret = SSL_read_early_data(ctx->ssl, &ctx->tmp_early_data,
Olivier Houchardc2aae742017-09-22 18:26:28 +02005887 1, &read_data);
5888 if (ret == SSL_READ_EARLY_DATA_ERROR)
5889 goto check_error;
5890 if (ret == SSL_READ_EARLY_DATA_SUCCESS) {
5891 conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN);
5892 return 1;
5893 } else
5894 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5895 }
5896#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005897 /* If we use SSL_do_handshake to process a reneg initiated by
5898 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5899 * Usually SSL_write and SSL_read are used and process implicitly
5900 * the reneg handshake.
5901 * Here we use SSL_peek as a workaround for reneg.
5902 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005903 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005904 char c;
5905
Olivier Houchard66ab4982019-02-26 18:37:15 +01005906 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005907 if (ret <= 0) {
5908 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005909 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005910
Emeric Brun674b7432012-11-08 19:21:55 +01005911 if (ret == SSL_ERROR_WANT_WRITE) {
5912 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005913 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005914 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005915 return 0;
5916 }
5917 else if (ret == SSL_ERROR_WANT_READ) {
5918 /* handshake may have been completed but we have
5919 * no more data to read.
5920 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005921 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005922 ret = 1;
5923 goto reneg_ok;
5924 }
5925 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005926 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005927 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005928 return 0;
5929 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005930#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005931 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005932 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005933 return 0;
5934 }
5935#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005936 else if (ret == SSL_ERROR_SYSCALL) {
5937 /* if errno is null, then connection was successfully established */
5938 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5939 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005940 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005941#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5942 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005943 conn->err_code = CO_ER_SSL_HANDSHAKE;
5944#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005945 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005946#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005947 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005948 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005949 empty_handshake = state == TLS_ST_BEFORE;
5950#else
Lukas Tribus49799162019-07-08 14:29:15 +02005951 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5952 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005953#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005954 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005955 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005956 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005957 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5958 else
5959 conn->err_code = CO_ER_SSL_EMPTY;
5960 }
5961 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005962 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005963 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5964 else
5965 conn->err_code = CO_ER_SSL_ABORT;
5966 }
5967 }
5968 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005969 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005970 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005971 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005972 conn->err_code = CO_ER_SSL_HANDSHAKE;
5973 }
Lukas Tribus49799162019-07-08 14:29:15 +02005974#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005975 }
Emeric Brun674b7432012-11-08 19:21:55 +01005976 goto out_error;
5977 }
5978 else {
5979 /* Fail on all other handshake errors */
5980 /* Note: OpenSSL may leave unread bytes in the socket's
5981 * buffer, causing an RST to be emitted upon close() on
5982 * TCP sockets. We first try to drain possibly pending
5983 * data to avoid this as much as possible.
5984 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005985 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005986 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005987 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005988 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005989 goto out_error;
5990 }
5991 }
5992 /* read some data: consider handshake completed */
5993 goto reneg_ok;
5994 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005995 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005996check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005997 if (ret != 1) {
5998 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005999 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006000
6001 if (ret == SSL_ERROR_WANT_WRITE) {
6002 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006003 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006004 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006005 return 0;
6006 }
6007 else if (ret == SSL_ERROR_WANT_READ) {
6008 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006009 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006010 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6011 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006012 return 0;
6013 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006014#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006015 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006016 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006017 return 0;
6018 }
6019#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006020 else if (ret == SSL_ERROR_SYSCALL) {
6021 /* if errno is null, then connection was successfully established */
6022 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6023 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006024 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006025#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6026 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006027 conn->err_code = CO_ER_SSL_HANDSHAKE;
6028#else
6029 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006030#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006031 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006032 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006033 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006034#else
Lukas Tribus49799162019-07-08 14:29:15 +02006035 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6036 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006037#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006038 if (empty_handshake) {
6039 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006040 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006041 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6042 else
6043 conn->err_code = CO_ER_SSL_EMPTY;
6044 }
6045 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006046 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006047 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6048 else
6049 conn->err_code = CO_ER_SSL_ABORT;
6050 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006051 }
6052 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006053 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006054 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6055 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006056 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006057 }
Lukas Tribus49799162019-07-08 14:29:15 +02006058#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006059 }
Willy Tarreau89230192012-09-28 20:22:13 +02006060 goto out_error;
6061 }
Emeric Brun46591952012-05-18 15:47:34 +02006062 else {
6063 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006064 /* Note: OpenSSL may leave unread bytes in the socket's
6065 * buffer, causing an RST to be emitted upon close() on
6066 * TCP sockets. We first try to drain possibly pending
6067 * data to avoid this as much as possible.
6068 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006069 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006070 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006071 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006072 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006073 goto out_error;
6074 }
6075 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006076#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006077 else {
6078 /*
6079 * If the server refused the early data, we have to send a
6080 * 425 to the client, as we no longer have the data to sent
6081 * them again.
6082 */
6083 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006084 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006085 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6086 goto out_error;
6087 }
6088 }
6089 }
6090#endif
6091
Emeric Brun46591952012-05-18 15:47:34 +02006092
Emeric Brun674b7432012-11-08 19:21:55 +01006093reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006094
Willy Tarreau5db847a2019-05-09 14:13:35 +02006095#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006096 /* ASYNC engine API doesn't support moving read/write
6097 * buffers. So we disable ASYNC mode right after
6098 * the handshake to avoid buffer oveflows.
6099 */
6100 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006101 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006102#endif
Emeric Brun46591952012-05-18 15:47:34 +02006103 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006104 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006105 if (objt_server(conn->target)) {
6106 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6107 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6108 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006109 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006110 else {
6111 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6112 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6113 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6114 }
Emeric Brun46591952012-05-18 15:47:34 +02006115 }
6116
6117 /* The connection is now established at both layers, it's time to leave */
6118 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6119 return 1;
6120
6121 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006122 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006123 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006124 ERR_clear_error();
6125
Emeric Brun9fa89732012-10-04 17:09:56 +02006126 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006127 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6128 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6129 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006130 }
6131
Emeric Brun46591952012-05-18 15:47:34 +02006132 /* Fail on all other handshake errors */
6133 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006134 if (!conn->err_code)
6135 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006136 return 0;
6137}
6138
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006139static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01006140{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006141 struct wait_event *sw;
6142 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006143
Olivier Houchard0ff28652019-06-24 18:57:39 +02006144 if (!ctx)
6145 return -1;
6146
Olivier Houchardea8dd942019-05-20 14:02:16 +02006147 if (event_type & SUB_RETRY_RECV) {
6148 sw = param;
6149 BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
6150 sw->events |= SUB_RETRY_RECV;
6151 ctx->recv_wait = sw;
6152 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
6153 !(ctx->wait_event.events & SUB_RETRY_RECV))
6154 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
6155 event_type &= ~SUB_RETRY_RECV;
6156 }
6157 if (event_type & SUB_RETRY_SEND) {
6158sw = param;
6159 BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
6160 sw->events |= SUB_RETRY_SEND;
6161 ctx->send_wait = sw;
6162 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
6163 !(ctx->wait_event.events & SUB_RETRY_SEND))
6164 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
6165 event_type &= ~SUB_RETRY_SEND;
6166
6167 }
6168 if (event_type != 0)
6169 return -1;
6170 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006171}
6172
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006173static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01006174{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006175 struct wait_event *sw;
6176 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006177
Olivier Houchardea8dd942019-05-20 14:02:16 +02006178 if (event_type & SUB_RETRY_RECV) {
6179 sw = param;
6180 BUG_ON(ctx->recv_wait != sw);
6181 ctx->recv_wait = NULL;
6182 sw->events &= ~SUB_RETRY_RECV;
6183 /* If we subscribed, and we're not doing the handshake,
6184 * then we subscribed because the upper layer asked for it,
6185 * as the upper layer is no longer interested, we can
6186 * unsubscribe too.
6187 */
6188 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
6189 (ctx->wait_event.events & SUB_RETRY_RECV))
6190 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV,
6191 &ctx->wait_event);
6192 }
6193 if (event_type & SUB_RETRY_SEND) {
6194 sw = param;
6195 BUG_ON(ctx->send_wait != sw);
6196 ctx->send_wait = NULL;
6197 sw->events &= ~SUB_RETRY_SEND;
6198 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
6199 (ctx->wait_event.events & SUB_RETRY_SEND))
6200 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND,
6201 &ctx->wait_event);
6202
6203 }
6204
6205 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006206}
6207
Olivier Houchard2e055482019-05-27 19:50:12 +02006208/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6209 * Returns 0 on success, and non-zero on failure.
6210 */
6211static int ssl_add_xprt(struct connection *conn, void *xprt_ctx, void *toadd_ctx, const struct xprt_ops *toadd_ops, void **oldxprt_ctx, const struct xprt_ops **oldxprt_ops)
6212{
6213 struct ssl_sock_ctx *ctx = xprt_ctx;
6214
6215 if (oldxprt_ops != NULL)
6216 *oldxprt_ops = ctx->xprt;
6217 if (oldxprt_ctx != NULL)
6218 *oldxprt_ctx = ctx->xprt_ctx;
6219 ctx->xprt = toadd_ops;
6220 ctx->xprt_ctx = toadd_ctx;
6221 return 0;
6222}
6223
Olivier Houchard5149b592019-05-23 17:47:36 +02006224/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6225 * return 0, otherwise just call the remove_xprt method from the underlying
6226 * XPRT.
6227 */
6228static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6229{
6230 struct ssl_sock_ctx *ctx = xprt_ctx;
6231
6232 if (ctx->xprt_ctx == toremove_ctx) {
6233 ctx->xprt_ctx = newctx;
6234 ctx->xprt = newops;
6235 return 0;
6236 }
6237 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6238}
6239
Olivier Houchardea8dd942019-05-20 14:02:16 +02006240static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6241{
6242 struct ssl_sock_ctx *ctx = context;
6243
6244 /* First if we're doing an handshake, try that */
6245 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6246 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6247 /* If we had an error, or the handshake is done and I/O is available,
6248 * let the upper layer know.
6249 * If no mux was set up yet, and nobody subscribed, then call
6250 * xprt_done_cb() ourself if it's set, or destroy the connection,
6251 * we can't be sure conn_fd_handler() will be called again.
6252 */
6253 if ((ctx->conn->flags & CO_FL_ERROR) ||
6254 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6255 int ret = 0;
6256 int woke = 0;
6257
6258 /* On error, wake any waiter */
6259 if (ctx->recv_wait) {
6260 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006261 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006262 ctx->recv_wait = NULL;
6263 woke = 1;
6264 }
6265 if (ctx->send_wait) {
6266 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006267 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006268 ctx->send_wait = NULL;
6269 woke = 1;
6270 }
6271 /* If we're the first xprt for the connection, let the
6272 * upper layers know. If xprt_done_cb() is set, call it,
6273 * otherwise, we should have a mux, so call its wake
6274 * method if we didn't woke a tasklet already.
6275 */
6276 if (ctx->conn->xprt_ctx == ctx) {
6277 if (ctx->conn->xprt_done_cb)
6278 ret = ctx->conn->xprt_done_cb(ctx->conn);
6279 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6280 ctx->conn->mux->wake(ctx->conn);
6281 return NULL;
6282 }
6283 }
6284 return NULL;
6285}
6286
Emeric Brun46591952012-05-18 15:47:34 +02006287/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006288 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006289 * buffer wraps, in which case a second call may be performed. The connection's
6290 * flags are updated with whatever special event is detected (error, read0,
6291 * empty). The caller is responsible for taking care of those events and
6292 * avoiding the call if inappropriate. The function does not call the
6293 * connection's polling update function, so the caller is responsible for this.
6294 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006295static 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 +02006296{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006297 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006298 ssize_t ret;
6299 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006300
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006301 conn_refresh_polling_flags(conn);
6302
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006303 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006304 goto out_error;
6305
6306 if (conn->flags & CO_FL_HANDSHAKE)
6307 /* a handshake was requested */
6308 return 0;
6309
Emeric Brun46591952012-05-18 15:47:34 +02006310 /* read the largest possible block. For this, we perform only one call
6311 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6312 * in which case we accept to do it once again. A new attempt is made on
6313 * EINTR too.
6314 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006315 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006316 int need_out = 0;
6317
Willy Tarreau591d4452018-06-15 17:21:00 +02006318 try = b_contig_space(buf);
6319 if (!try)
6320 break;
6321
Willy Tarreauabf08d92014-01-14 11:31:27 +01006322 if (try > count)
6323 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006324
Olivier Houchardc2aae742017-09-22 18:26:28 +02006325 if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006326 ctx->tmp_early_data != -1) {
6327 *b_tail(buf) = ctx->tmp_early_data;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006328 done++;
6329 try--;
6330 count--;
Olivier Houchardacd14032018-06-28 18:17:23 +02006331 b_add(buf, 1);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006332 ctx->tmp_early_data = -1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006333 continue;
6334 }
Willy Tarreauabf08d92014-01-14 11:31:27 +01006335
Willy Tarreau5db847a2019-05-09 14:13:35 +02006336#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006337 if (conn->flags & CO_FL_EARLY_SSL_HS) {
6338 size_t read_length;
6339
Olivier Houchard66ab4982019-02-26 18:37:15 +01006340 ret = SSL_read_early_data(ctx->ssl,
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02006341 b_tail(buf), try, &read_length);
Olivier Houchard522eea72017-11-03 16:27:47 +01006342 if (ret == SSL_READ_EARLY_DATA_SUCCESS &&
6343 read_length > 0)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006344 conn->flags |= CO_FL_EARLY_DATA;
6345 if (ret == SSL_READ_EARLY_DATA_SUCCESS ||
6346 ret == SSL_READ_EARLY_DATA_FINISH) {
6347 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6348 /*
6349 * We're done reading the early data,
6350 * let's make the handshake
6351 */
6352 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6353 conn->flags |= CO_FL_SSL_WAIT_HS;
6354 need_out = 1;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006355 /* Now initiate the handshake */
6356 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006357 if (read_length == 0)
6358 break;
6359 }
6360 ret = read_length;
6361 }
6362 } else
6363#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006364 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006365
Emeric Brune1f38db2012-09-03 20:36:47 +02006366 if (conn->flags & CO_FL_ERROR) {
6367 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006368 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006369 }
Emeric Brun46591952012-05-18 15:47:34 +02006370 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006371 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006372 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006373 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006374 }
Emeric Brun46591952012-05-18 15:47:34 +02006375 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006376 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006377 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006378 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006379 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006380 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006381#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006382 /* Async mode can be re-enabled, because we're leaving data state.*/
6383 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006384 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006385#endif
Emeric Brun46591952012-05-18 15:47:34 +02006386 break;
6387 }
6388 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006389 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006390 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6391 SUB_RETRY_RECV,
6392 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006393 /* handshake is running, and it may need to re-enable read */
6394 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006395#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006396 /* Async mode can be re-enabled, because we're leaving data state.*/
6397 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006398 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006399#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006400 break;
6401 }
Emeric Brun46591952012-05-18 15:47:34 +02006402 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006403 } else if (ret == SSL_ERROR_ZERO_RETURN)
6404 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006405 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6406 * stack before shutting down the connection for
6407 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006408 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6409 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006410 /* otherwise it's a real error */
6411 goto out_error;
6412 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006413 if (need_out)
6414 break;
Emeric Brun46591952012-05-18 15:47:34 +02006415 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006416 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006417 return done;
6418
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006419 clear_ssl_error:
6420 /* Clear openssl global errors stack */
6421 ssl_sock_dump_errors(conn);
6422 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006423 read0:
6424 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006425 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006426
Emeric Brun46591952012-05-18 15:47:34 +02006427 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006428 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006429 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006430 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006431 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006432 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006433}
6434
6435
Willy Tarreau787db9a2018-06-14 18:31:46 +02006436/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6437 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6438 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006439 * Only one call to send() is performed, unless the buffer wraps, in which case
6440 * a second call may be performed. The connection's flags are updated with
6441 * whatever special event is detected (error, empty). The caller is responsible
6442 * for taking care of those events and avoiding the call if inappropriate. The
6443 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006444 * is responsible for this. The buffer's output is not adjusted, it's up to the
6445 * caller to take care of this. It's up to the caller to update the buffer's
6446 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006447 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006448static 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 +02006449{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006450 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006451 ssize_t ret;
6452 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006453
6454 done = 0;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006455 conn_refresh_polling_flags(conn);
Emeric Brun46591952012-05-18 15:47:34 +02006456
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006457 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006458 goto out_error;
6459
Olivier Houchard010941f2019-05-03 20:56:19 +02006460 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006461 /* a handshake was requested */
6462 return 0;
6463
6464 /* send the largest possible block. For this we perform only one call
6465 * to send() unless the buffer wraps and we exactly fill the first hunk,
6466 * in which case we accept to do it once again.
6467 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006468 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006469#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006470 size_t written_data;
6471#endif
6472
Willy Tarreau787db9a2018-06-14 18:31:46 +02006473 try = b_contig_data(buf, done);
6474 if (try > count)
6475 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006476
Willy Tarreau7bed9452014-02-02 02:00:24 +01006477 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006478 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006479 global_ssl.max_record && try > global_ssl.max_record) {
6480 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006481 }
6482 else {
6483 /* we need to keep the information about the fact that
6484 * we're not limiting the upcoming send(), because if it
6485 * fails, we'll have to retry with at least as many data.
6486 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006487 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006488 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006489
Willy Tarreau5db847a2019-05-09 14:13:35 +02006490#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006491 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006492 unsigned int max_early;
6493
Olivier Houchard522eea72017-11-03 16:27:47 +01006494 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006495 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006496 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006497 if (SSL_get0_session(ctx->ssl))
6498 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006499 else
6500 max_early = 0;
6501 }
6502
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006503 if (try + ctx->sent_early_data > max_early) {
6504 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006505 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006506 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006507 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006508 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006509 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006510 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006511 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006512 if (ret == 1) {
6513 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006514 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006515 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006516 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006517 /* Initiate the handshake, now */
6518 tasklet_wakeup(ctx->wait_event.tasklet);
6519 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006520
Olivier Houchardc2aae742017-09-22 18:26:28 +02006521 }
6522
6523 } else
6524#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006525 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006526
Emeric Brune1f38db2012-09-03 20:36:47 +02006527 if (conn->flags & CO_FL_ERROR) {
6528 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006529 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006530 }
Emeric Brun46591952012-05-18 15:47:34 +02006531 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006532 /* A send succeeded, so we can consier ourself connected */
6533 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006534 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006535 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006536 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006537 }
6538 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006539 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006540
Emeric Brun46591952012-05-18 15:47:34 +02006541 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006542 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006543 /* handshake is running, and it may need to re-enable write */
6544 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006545 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006546#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006547 /* Async mode can be re-enabled, because we're leaving data state.*/
6548 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006549 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006550#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006551 break;
6552 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006553
Emeric Brun46591952012-05-18 15:47:34 +02006554 break;
6555 }
6556 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006557 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006558 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006559 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6560 SUB_RETRY_RECV,
6561 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006562#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006563 /* Async mode can be re-enabled, because we're leaving data state.*/
6564 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006565 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006566#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006567 break;
6568 }
Emeric Brun46591952012-05-18 15:47:34 +02006569 goto out_error;
6570 }
6571 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006572 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006573 return done;
6574
6575 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006576 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006577 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006578 ERR_clear_error();
6579
Emeric Brun46591952012-05-18 15:47:34 +02006580 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006581 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006582}
6583
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006584static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006585
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006586 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006587
Olivier Houchardea8dd942019-05-20 14:02:16 +02006588
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006589 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006590 if (ctx->wait_event.events != 0)
6591 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6592 ctx->wait_event.events,
6593 &ctx->wait_event);
6594 if (ctx->send_wait) {
6595 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006596 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006597 }
6598 if (ctx->recv_wait) {
6599 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006600 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006601 }
Olivier Houchard692c1d02019-05-23 18:41:47 +02006602 if (ctx->xprt->close)
6603 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006604#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006605 if (global_ssl.async) {
6606 OSSL_ASYNC_FD all_fd[32], afd;
6607 size_t num_all_fds = 0;
6608 int i;
6609
Olivier Houchard66ab4982019-02-26 18:37:15 +01006610 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006611 if (num_all_fds > 32) {
6612 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6613 return;
6614 }
6615
Olivier Houchard66ab4982019-02-26 18:37:15 +01006616 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006617
6618 /* If an async job is pending, we must try to
6619 to catch the end using polling before calling
6620 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006621 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006622 for (i=0 ; i < num_all_fds ; i++) {
6623 /* switch on an handler designed to
6624 * handle the SSL_free
6625 */
6626 afd = all_fd[i];
6627 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006628 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006629 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006630 /* To ensure that the fd cache won't be used
6631 * and we'll catch a real RD event.
6632 */
6633 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006634 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006635 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006636 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006637 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006638 return;
6639 }
Emeric Brun3854e012017-05-17 20:42:48 +02006640 /* Else we can remove the fds from the fdtab
6641 * and call SSL_free.
6642 * note: we do a fd_remove and not a delete
6643 * because the fd is owned by the engine.
6644 * the engine is responsible to close
6645 */
6646 for (i=0 ; i < num_all_fds ; i++)
6647 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006648 }
6649#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006650 SSL_free(ctx->ssl);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006651 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006652 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006653 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006654 }
Emeric Brun46591952012-05-18 15:47:34 +02006655}
6656
6657/* This function tries to perform a clean shutdown on an SSL connection, and in
6658 * any case, flags the connection as reusable if no handshake was in progress.
6659 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006660static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006661{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006662 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006663
Emeric Brun46591952012-05-18 15:47:34 +02006664 if (conn->flags & CO_FL_HANDSHAKE)
6665 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006666 if (!clean)
6667 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006668 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006669 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006670 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006671 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006672 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006673 ERR_clear_error();
6674 }
Emeric Brun46591952012-05-18 15:47:34 +02006675}
6676
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006677/* used for ppv2 pkey alog (can be used for logging) */
Willy Tarreau83061a82018-07-13 11:56:34 +02006678int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006679{
Christopher Faulet82004142019-09-10 10:12:03 +02006680 struct ssl_sock_ctx *ctx;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006681 struct pkey_info *pkinfo;
6682 int bits = 0;
6683 int sig = TLSEXT_signature_anonymous;
6684 int len = -1;
6685
6686 if (!ssl_sock_is_ssl(conn))
6687 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006688 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006689 pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006690 if (pkinfo) {
6691 sig = pkinfo->sig;
6692 bits = pkinfo->bits;
6693 } else {
6694 /* multicert and generated cert have no pkey info */
6695 X509 *crt;
6696 EVP_PKEY *pkey;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006697 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006698 if (!crt)
6699 return 0;
6700 pkey = X509_get_pubkey(crt);
6701 if (pkey) {
6702 bits = EVP_PKEY_bits(pkey);
6703 switch(EVP_PKEY_base_id(pkey)) {
6704 case EVP_PKEY_RSA:
6705 sig = TLSEXT_signature_rsa;
6706 break;
6707 case EVP_PKEY_EC:
6708 sig = TLSEXT_signature_ecdsa;
6709 break;
6710 case EVP_PKEY_DSA:
6711 sig = TLSEXT_signature_dsa;
6712 break;
6713 }
6714 EVP_PKEY_free(pkey);
6715 }
6716 }
6717
6718 switch(sig) {
6719 case TLSEXT_signature_rsa:
6720 len = chunk_printf(out, "RSA%d", bits);
6721 break;
6722 case TLSEXT_signature_ecdsa:
6723 len = chunk_printf(out, "EC%d", bits);
6724 break;
6725 case TLSEXT_signature_dsa:
6726 len = chunk_printf(out, "DSA%d", bits);
6727 break;
6728 default:
6729 return 0;
6730 }
6731 if (len < 0)
6732 return 0;
6733 return 1;
6734}
6735
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006736/* used for ppv2 cert signature (can be used for logging) */
6737const char *ssl_sock_get_cert_sig(struct connection *conn)
6738{
Christopher Faulet82004142019-09-10 10:12:03 +02006739 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006740
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006741 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6742 X509 *crt;
6743
6744 if (!ssl_sock_is_ssl(conn))
6745 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006746 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006747 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006748 if (!crt)
6749 return NULL;
6750 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6751 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6752}
6753
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006754/* used for ppv2 authority */
6755const char *ssl_sock_get_sni(struct connection *conn)
6756{
6757#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006758 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006759
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006760 if (!ssl_sock_is_ssl(conn))
6761 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006762 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006763 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006764#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006765 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006766#endif
6767}
6768
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006769/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006770const char *ssl_sock_get_cipher_name(struct connection *conn)
6771{
Christopher Faulet82004142019-09-10 10:12:03 +02006772 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006773
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006774 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006775 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006776 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006777 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006778}
6779
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006780/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006781const char *ssl_sock_get_proto_version(struct connection *conn)
6782{
Christopher Faulet82004142019-09-10 10:12:03 +02006783 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006784
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006785 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006786 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006787 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006788 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006789}
6790
Willy Tarreau8d598402012-10-22 17:58:39 +02006791/* Extract a serial from a cert, and copy it to a chunk.
6792 * Returns 1 if serial is found and copied, 0 if no serial found and
6793 * -1 if output is not large enough.
6794 */
6795static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006796ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006797{
6798 ASN1_INTEGER *serial;
6799
6800 serial = X509_get_serialNumber(crt);
6801 if (!serial)
6802 return 0;
6803
6804 if (out->size < serial->length)
6805 return -1;
6806
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006807 memcpy(out->area, serial->data, serial->length);
6808 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006809 return 1;
6810}
6811
Emeric Brun43e79582014-10-29 19:03:26 +01006812/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006813 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6814 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006815 */
6816static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006817ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006818{
6819 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006820 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006821
6822 len =i2d_X509(crt, NULL);
6823 if (len <= 0)
6824 return 1;
6825
6826 if (out->size < len)
6827 return -1;
6828
6829 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006830 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006831 return 1;
6832}
6833
Emeric Brunce5ad802012-10-22 14:11:22 +02006834
Willy Tarreau83061a82018-07-13 11:56:34 +02006835/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006836 * Returns 1 if serial is found and copied, 0 if no valid time found
6837 * and -1 if output is not large enough.
6838 */
6839static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006840ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006841{
6842 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6843 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6844
6845 if (gentm->length < 12)
6846 return 0;
6847 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6848 return 0;
6849 if (out->size < gentm->length-2)
6850 return -1;
6851
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006852 memcpy(out->area, gentm->data+2, gentm->length-2);
6853 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02006854 return 1;
6855 }
6856 else if (tm->type == V_ASN1_UTCTIME) {
6857 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
6858
6859 if (utctm->length < 10)
6860 return 0;
6861 if (utctm->data[0] >= 0x35)
6862 return 0;
6863 if (out->size < utctm->length)
6864 return -1;
6865
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006866 memcpy(out->area, utctm->data, utctm->length);
6867 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02006868 return 1;
6869 }
6870
6871 return 0;
6872}
6873
Emeric Brun87855892012-10-17 17:39:35 +02006874/* Extract an entry from a X509_NAME and copy its value to an output chunk.
6875 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
6876 */
6877static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006878ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
6879 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006880{
6881 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006882 ASN1_OBJECT *obj;
6883 ASN1_STRING *data;
6884 const unsigned char *data_ptr;
6885 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006886 int i, j, n;
6887 int cur = 0;
6888 const char *s;
6889 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006890 int name_count;
6891
6892 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006893
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006894 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006895 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02006896 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006897 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02006898 else
6899 j = i;
6900
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006901 ne = X509_NAME_get_entry(a, j);
6902 obj = X509_NAME_ENTRY_get_object(ne);
6903 data = X509_NAME_ENTRY_get_data(ne);
6904 data_ptr = ASN1_STRING_get0_data(data);
6905 data_len = ASN1_STRING_length(data);
6906 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006907 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006908 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006909 s = tmp;
6910 }
6911
6912 if (chunk_strcasecmp(entry, s) != 0)
6913 continue;
6914
6915 if (pos < 0)
6916 cur--;
6917 else
6918 cur++;
6919
6920 if (cur != pos)
6921 continue;
6922
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006923 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02006924 return -1;
6925
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006926 memcpy(out->area, data_ptr, data_len);
6927 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006928 return 1;
6929 }
6930
6931 return 0;
6932
6933}
6934
6935/* Extract and format full DN from a X509_NAME and copy result into a chunk
6936 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
6937 */
6938static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006939ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006940{
6941 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006942 ASN1_OBJECT *obj;
6943 ASN1_STRING *data;
6944 const unsigned char *data_ptr;
6945 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006946 int i, n, ln;
6947 int l = 0;
6948 const char *s;
6949 char *p;
6950 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006951 int name_count;
6952
6953
6954 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006955
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006956 out->data = 0;
6957 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006958 for (i = 0; i < name_count; i++) {
6959 ne = X509_NAME_get_entry(a, i);
6960 obj = X509_NAME_ENTRY_get_object(ne);
6961 data = X509_NAME_ENTRY_get_data(ne);
6962 data_ptr = ASN1_STRING_get0_data(data);
6963 data_len = ASN1_STRING_length(data);
6964 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006965 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006966 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006967 s = tmp;
6968 }
6969 ln = strlen(s);
6970
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006971 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006972 if (l > out->size)
6973 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006974 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02006975
6976 *(p++)='/';
6977 memcpy(p, s, ln);
6978 p += ln;
6979 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006980 memcpy(p, data_ptr, data_len);
6981 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006982 }
6983
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006984 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02006985 return 0;
6986
6987 return 1;
6988}
6989
Olivier Houchardab28a322018-12-21 19:45:40 +01006990void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6991{
6992#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02006993 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006994
Olivier Houcharde488ea82019-06-28 14:10:33 +02006995 if (!ssl_sock_is_ssl(conn))
6996 return;
Christopher Faulet82004142019-09-10 10:12:03 +02006997 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006998 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006999#endif
7000}
7001
Willy Tarreau119a4082016-12-22 21:58:38 +01007002/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7003 * to disable SNI.
7004 */
Willy Tarreau63076412015-07-10 11:33:32 +02007005void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7006{
7007#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007008 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007009
Willy Tarreau119a4082016-12-22 21:58:38 +01007010 char *prev_name;
7011
Willy Tarreau63076412015-07-10 11:33:32 +02007012 if (!ssl_sock_is_ssl(conn))
7013 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007014 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007015
Willy Tarreau119a4082016-12-22 21:58:38 +01007016 /* if the SNI changes, we must destroy the reusable context so that a
7017 * new connection will present a new SNI. As an optimization we could
7018 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7019 * server.
7020 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007021 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007022 if ((!prev_name && hostname) ||
7023 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007024 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007025
Olivier Houchard66ab4982019-02-26 18:37:15 +01007026 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007027#endif
7028}
7029
Emeric Brun0abf8362014-06-24 18:26:41 +02007030/* Extract peer certificate's common name into the chunk dest
7031 * Returns
7032 * the len of the extracted common name
7033 * or 0 if no CN found in DN
7034 * or -1 on error case (i.e. no peer certificate)
7035 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007036int ssl_sock_get_remote_common_name(struct connection *conn,
7037 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007038{
Christopher Faulet82004142019-09-10 10:12:03 +02007039 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007040 X509 *crt = NULL;
7041 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007042 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007043 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007044 .area = (char *)&find_cn,
7045 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007046 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007047 int result = -1;
David Safb76832014-05-08 23:42:08 -04007048
7049 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007050 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007051 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007052
7053 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007054 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007055 if (!crt)
7056 goto out;
7057
7058 name = X509_get_subject_name(crt);
7059 if (!name)
7060 goto out;
David Safb76832014-05-08 23:42:08 -04007061
Emeric Brun0abf8362014-06-24 18:26:41 +02007062 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7063out:
David Safb76832014-05-08 23:42:08 -04007064 if (crt)
7065 X509_free(crt);
7066
7067 return result;
7068}
7069
Dave McCowan328fb582014-07-30 10:39:13 -04007070/* returns 1 if client passed a certificate for this session, 0 if not */
7071int ssl_sock_get_cert_used_sess(struct connection *conn)
7072{
Christopher Faulet82004142019-09-10 10:12:03 +02007073 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007074 X509 *crt = NULL;
7075
7076 if (!ssl_sock_is_ssl(conn))
7077 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007078 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007079
7080 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007081 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007082 if (!crt)
7083 return 0;
7084
7085 X509_free(crt);
7086 return 1;
7087}
7088
7089/* returns 1 if client passed a certificate for this connection, 0 if not */
7090int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007091{
Christopher Faulet82004142019-09-10 10:12:03 +02007092 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007093
David Safb76832014-05-08 23:42:08 -04007094 if (!ssl_sock_is_ssl(conn))
7095 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007096 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007097 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007098}
7099
7100/* returns result from SSL verify */
7101unsigned int ssl_sock_get_verify_result(struct connection *conn)
7102{
Christopher Faulet82004142019-09-10 10:12:03 +02007103 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007104
David Safb76832014-05-08 23:42:08 -04007105 if (!ssl_sock_is_ssl(conn))
7106 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007107 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007108 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007109}
7110
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007111/* Returns the application layer protocol name in <str> and <len> when known.
7112 * Zero is returned if the protocol name was not found, otherwise non-zero is
7113 * returned. The string is allocated in the SSL context and doesn't have to be
7114 * freed by the caller. NPN is also checked if available since older versions
7115 * of openssl (1.0.1) which are more common in field only support this one.
7116 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007117static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007118{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007119#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7120 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007121 struct ssl_sock_ctx *ctx = xprt_ctx;
7122 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007123 return 0;
7124
7125 *str = NULL;
7126
7127#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007128 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007129 if (*str)
7130 return 1;
7131#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007132#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007133 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007134 if (*str)
7135 return 1;
7136#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007137#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007138 return 0;
7139}
7140
Willy Tarreau7875d092012-09-10 08:20:03 +02007141/***** Below are some sample fetching functions for ACL/patterns *****/
7142
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007143static int
7144smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7145{
7146 struct connection *conn;
7147
7148 conn = objt_conn(smp->sess->origin);
7149 if (!conn || conn->xprt != &ssl_sock)
7150 return 0;
7151
7152 smp->flags = 0;
7153 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007154#ifdef OPENSSL_IS_BORINGSSL
7155 {
7156 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7157 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7158 SSL_early_data_accepted(ctx->ssl));
7159 }
7160#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007161 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
7162 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007163#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007164 return 1;
7165}
7166
Emeric Brune64aef12012-09-21 13:15:06 +02007167/* boolean, returns true if client cert was present */
7168static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007169smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007170{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007171 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007172 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007173
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007174 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007175 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007176 return 0;
7177
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007178 ctx = conn->xprt_ctx;
7179
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007180 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02007181 smp->flags |= SMP_F_MAY_CHANGE;
7182 return 0;
7183 }
7184
7185 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007186 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007187 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007188
7189 return 1;
7190}
7191
Emeric Brun43e79582014-10-29 19:03:26 +01007192/* binary, returns a certificate in a binary chunk (der/raw).
7193 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7194 * should be use.
7195 */
7196static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007197smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007198{
7199 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7200 X509 *crt = NULL;
7201 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007202 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007203 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007204 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007205
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007206 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007207 if (!conn || conn->xprt != &ssl_sock)
7208 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007209 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007210
7211 if (!(conn->flags & CO_FL_CONNECTED)) {
7212 smp->flags |= SMP_F_MAY_CHANGE;
7213 return 0;
7214 }
7215
7216 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007217 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007218 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007219 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007220
7221 if (!crt)
7222 goto out;
7223
7224 smp_trash = get_trash_chunk();
7225 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7226 goto out;
7227
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007228 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007229 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007230 ret = 1;
7231out:
7232 /* SSL_get_peer_certificate, it increase X509 * ref count */
7233 if (cert_peer && crt)
7234 X509_free(crt);
7235 return ret;
7236}
7237
Emeric Brunba841a12014-04-30 17:05:08 +02007238/* binary, returns serial of certificate in a binary chunk.
7239 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7240 * should be use.
7241 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007242static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007243smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007244{
Emeric Brunba841a12014-04-30 17:05:08 +02007245 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007246 X509 *crt = NULL;
7247 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007248 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007249 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007250 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007251
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007252 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007253 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007254 return 0;
7255
Olivier Houchard66ab4982019-02-26 18:37:15 +01007256 ctx = conn->xprt_ctx;
7257
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007258 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007259 smp->flags |= SMP_F_MAY_CHANGE;
7260 return 0;
7261 }
7262
Emeric Brunba841a12014-04-30 17:05:08 +02007263 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007264 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007265 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007266 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007267
Willy Tarreau8d598402012-10-22 17:58:39 +02007268 if (!crt)
7269 goto out;
7270
Willy Tarreau47ca5452012-12-23 20:22:19 +01007271 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007272 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7273 goto out;
7274
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007275 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007276 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007277 ret = 1;
7278out:
Emeric Brunba841a12014-04-30 17:05:08 +02007279 /* SSL_get_peer_certificate, it increase X509 * ref count */
7280 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007281 X509_free(crt);
7282 return ret;
7283}
Emeric Brune64aef12012-09-21 13:15:06 +02007284
Emeric Brunba841a12014-04-30 17:05:08 +02007285/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7286 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7287 * should be use.
7288 */
James Votha051b4a2013-05-14 20:37:59 +02007289static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007290smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007291{
Emeric Brunba841a12014-04-30 17:05:08 +02007292 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007293 X509 *crt = NULL;
7294 const EVP_MD *digest;
7295 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007296 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007297 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007298 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007299
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007300 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007301 if (!conn || conn->xprt != &ssl_sock)
7302 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007303 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007304
7305 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02007306 smp->flags |= SMP_F_MAY_CHANGE;
7307 return 0;
7308 }
7309
Emeric Brunba841a12014-04-30 17:05:08 +02007310 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007311 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007312 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007313 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007314 if (!crt)
7315 goto out;
7316
7317 smp_trash = get_trash_chunk();
7318 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007319 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7320 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007321
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007322 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007323 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007324 ret = 1;
7325out:
Emeric Brunba841a12014-04-30 17:05:08 +02007326 /* SSL_get_peer_certificate, it increase X509 * ref count */
7327 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007328 X509_free(crt);
7329 return ret;
7330}
7331
Emeric Brunba841a12014-04-30 17:05:08 +02007332/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7333 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7334 * should be use.
7335 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007336static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007337smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007338{
Emeric Brunba841a12014-04-30 17:05:08 +02007339 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007340 X509 *crt = NULL;
7341 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007342 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007343 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007344 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007345
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007346 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007347 if (!conn || conn->xprt != &ssl_sock)
7348 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007349 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007350
7351 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007352 smp->flags |= SMP_F_MAY_CHANGE;
7353 return 0;
7354 }
7355
Emeric Brunba841a12014-04-30 17:05:08 +02007356 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007357 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007358 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007359 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007360 if (!crt)
7361 goto out;
7362
Willy Tarreau47ca5452012-12-23 20:22:19 +01007363 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007364 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007365 goto out;
7366
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007367 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007368 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007369 ret = 1;
7370out:
Emeric Brunba841a12014-04-30 17:05:08 +02007371 /* SSL_get_peer_certificate, it increase X509 * ref count */
7372 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007373 X509_free(crt);
7374 return ret;
7375}
7376
Emeric Brunba841a12014-04-30 17:05:08 +02007377/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7378 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7379 * should be use.
7380 */
Emeric Brun87855892012-10-17 17:39:35 +02007381static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007382smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007383{
Emeric Brunba841a12014-04-30 17:05:08 +02007384 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007385 X509 *crt = NULL;
7386 X509_NAME *name;
7387 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007388 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007389 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007390 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +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 Houchard66ab4982019-02-26 18:37:15 +01007395 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007396
7397 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007398 smp->flags |= SMP_F_MAY_CHANGE;
7399 return 0;
7400 }
7401
Emeric Brunba841a12014-04-30 17:05:08 +02007402 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007403 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007404 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007405 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007406 if (!crt)
7407 goto out;
7408
7409 name = X509_get_issuer_name(crt);
7410 if (!name)
7411 goto out;
7412
Willy Tarreau47ca5452012-12-23 20:22:19 +01007413 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02007414 if (args && args[0].type == ARGT_STR) {
7415 int pos = 1;
7416
7417 if (args[1].type == ARGT_SINT)
7418 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007419
7420 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7421 goto out;
7422 }
7423 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7424 goto out;
7425
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007426 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007427 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007428 ret = 1;
7429out:
Emeric Brunba841a12014-04-30 17:05:08 +02007430 /* SSL_get_peer_certificate, it increase X509 * ref count */
7431 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007432 X509_free(crt);
7433 return ret;
7434}
7435
Emeric Brunba841a12014-04-30 17:05:08 +02007436/* string, returns notbefore date in ASN1_UTCTIME format.
7437 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7438 * should be use.
7439 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007440static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007441smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007442{
Emeric Brunba841a12014-04-30 17:05:08 +02007443 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007444 X509 *crt = NULL;
7445 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007446 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007447 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007448 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007449
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007450 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007451 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007452 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007453 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007454
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007455 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007456 smp->flags |= SMP_F_MAY_CHANGE;
7457 return 0;
7458 }
7459
Emeric Brunba841a12014-04-30 17:05:08 +02007460 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007461 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007462 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007463 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007464 if (!crt)
7465 goto out;
7466
Willy Tarreau47ca5452012-12-23 20:22:19 +01007467 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007468 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007469 goto out;
7470
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007471 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007472 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007473 ret = 1;
7474out:
Emeric Brunba841a12014-04-30 17:05:08 +02007475 /* SSL_get_peer_certificate, it increase X509 * ref count */
7476 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007477 X509_free(crt);
7478 return ret;
7479}
7480
Emeric Brunba841a12014-04-30 17:05:08 +02007481/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7482 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7483 * should be use.
7484 */
Emeric Brun87855892012-10-17 17:39:35 +02007485static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007486smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007487{
Emeric Brunba841a12014-04-30 17:05:08 +02007488 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007489 X509 *crt = NULL;
7490 X509_NAME *name;
7491 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007492 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007493 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007494 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007495
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007496 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007497 if (!conn || conn->xprt != &ssl_sock)
7498 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007499 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007500
7501 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007502 smp->flags |= SMP_F_MAY_CHANGE;
7503 return 0;
7504 }
7505
Emeric Brunba841a12014-04-30 17:05:08 +02007506 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007507 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007508 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007509 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007510 if (!crt)
7511 goto out;
7512
7513 name = X509_get_subject_name(crt);
7514 if (!name)
7515 goto out;
7516
Willy Tarreau47ca5452012-12-23 20:22:19 +01007517 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02007518 if (args && args[0].type == ARGT_STR) {
7519 int pos = 1;
7520
7521 if (args[1].type == ARGT_SINT)
7522 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007523
7524 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7525 goto out;
7526 }
7527 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7528 goto out;
7529
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007530 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007531 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007532 ret = 1;
7533out:
Emeric Brunba841a12014-04-30 17:05:08 +02007534 /* SSL_get_peer_certificate, it increase X509 * ref count */
7535 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007536 X509_free(crt);
7537 return ret;
7538}
Emeric Brun9143d372012-12-20 15:44:16 +01007539
7540/* integer, returns true if current session use a client certificate */
7541static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007542smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007543{
7544 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007545 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007546 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007547
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007548 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007549 if (!conn || conn->xprt != &ssl_sock)
7550 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007551 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007552
7553 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007554 smp->flags |= SMP_F_MAY_CHANGE;
7555 return 0;
7556 }
7557
7558 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007559 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007560 if (crt) {
7561 X509_free(crt);
7562 }
7563
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007564 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007565 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007566 return 1;
7567}
7568
Emeric Brunba841a12014-04-30 17:05:08 +02007569/* integer, returns the certificate version
7570 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7571 * should be use.
7572 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007573static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007574smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007575{
Emeric Brunba841a12014-04-30 17:05:08 +02007576 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007577 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007578 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007579 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007580
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007581 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007582 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007583 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007584 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007585
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007586 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007587 smp->flags |= SMP_F_MAY_CHANGE;
7588 return 0;
7589 }
7590
Emeric Brunba841a12014-04-30 17:05:08 +02007591 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007592 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007593 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007594 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007595 if (!crt)
7596 return 0;
7597
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007598 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007599 /* SSL_get_peer_certificate increase X509 * ref count */
7600 if (cert_peer)
7601 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007602 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007603
7604 return 1;
7605}
7606
Emeric Brunba841a12014-04-30 17:05:08 +02007607/* string, returns the certificate's signature algorithm.
7608 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7609 * should be use.
7610 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007611static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007612smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007613{
Emeric Brunba841a12014-04-30 17:05:08 +02007614 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007615 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007616 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007617 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007618 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007619 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007620
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007621 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007622 if (!conn || conn->xprt != &ssl_sock)
7623 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007624 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007625
7626 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007627 smp->flags |= SMP_F_MAY_CHANGE;
7628 return 0;
7629 }
7630
Emeric Brunba841a12014-04-30 17:05:08 +02007631 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007632 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007633 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007634 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007635 if (!crt)
7636 return 0;
7637
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007638 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7639 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007640
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007641 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7642 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007643 /* SSL_get_peer_certificate increase X509 * ref count */
7644 if (cert_peer)
7645 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007646 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007647 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007648
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007649 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007650 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007651 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007652 /* SSL_get_peer_certificate increase X509 * ref count */
7653 if (cert_peer)
7654 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007655
7656 return 1;
7657}
7658
Emeric Brunba841a12014-04-30 17:05:08 +02007659/* string, returns the certificate's key algorithm.
7660 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7661 * should be use.
7662 */
Emeric Brun521a0112012-10-22 12:22:55 +02007663static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007664smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007665{
Emeric Brunba841a12014-04-30 17:05:08 +02007666 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007667 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007668 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007669 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007670 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007671 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007672
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007673 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007674 if (!conn || conn->xprt != &ssl_sock)
7675 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007676 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007677
7678 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007679 smp->flags |= SMP_F_MAY_CHANGE;
7680 return 0;
7681 }
7682
Emeric Brunba841a12014-04-30 17:05:08 +02007683 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007684 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007685 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007686 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007687 if (!crt)
7688 return 0;
7689
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007690 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7691 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007692
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007693 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7694 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007695 /* SSL_get_peer_certificate increase X509 * ref count */
7696 if (cert_peer)
7697 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007698 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007699 }
Emeric Brun521a0112012-10-22 12:22:55 +02007700
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007701 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007702 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007703 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007704 if (cert_peer)
7705 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007706
7707 return 1;
7708}
7709
Emeric Brun645ae792014-04-30 14:21:06 +02007710/* boolean, returns true if front conn. transport layer is SSL.
7711 * This function is also usable on backend conn if the fetch keyword 5th
7712 * char is 'b'.
7713 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007714static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007715smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007716{
Emeric Bruneb8def92018-02-19 15:59:48 +01007717 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7718 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007719
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007720 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007721 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007722 return 1;
7723}
7724
Emeric Brun2525b6b2012-10-18 15:59:43 +02007725/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007726static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007727smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007728{
7729#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007730 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007731 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007732
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007733 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007734 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007735 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007736 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007737 return 1;
7738#else
7739 return 0;
7740#endif
7741}
7742
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007743/* boolean, returns true if client session has been resumed.
7744 * This function is also usable on backend conn if the fetch keyword 5th
7745 * char is 'b'.
7746 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007747static int
7748smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7749{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007750 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7751 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007752 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007753
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007754
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007755 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007756 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007757 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007758 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007759 return 1;
7760}
7761
Emeric Brun645ae792014-04-30 14:21:06 +02007762/* string, returns the used cipher if front conn. transport layer is SSL.
7763 * This function is also usable on backend conn if the fetch keyword 5th
7764 * char is 'b'.
7765 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007766static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007767smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007768{
Emeric Bruneb8def92018-02-19 15:59:48 +01007769 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7770 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007771 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007772
Willy Tarreaube508f12016-03-10 11:47:01 +01007773 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007774 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007775 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007776 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007777
Olivier Houchard66ab4982019-02-26 18:37:15 +01007778 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007779 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007780 return 0;
7781
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007782 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007783 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007784 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007785
7786 return 1;
7787}
7788
Emeric Brun645ae792014-04-30 14:21:06 +02007789/* integer, returns the algoritm's keysize if front conn. transport layer
7790 * is SSL.
7791 * This function is also usable on backend conn if the fetch keyword 5th
7792 * char is 'b'.
7793 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007794static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007795smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007796{
Emeric Bruneb8def92018-02-19 15:59:48 +01007797 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7798 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007799 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007800 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01007801
Emeric Brun589fcad2012-10-16 14:13:26 +02007802 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007803 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007804 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007805 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007806
Olivier Houchard66ab4982019-02-26 18:37:15 +01007807 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007808 return 0;
7809
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007810 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007811 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007812
7813 return 1;
7814}
7815
Emeric Brun645ae792014-04-30 14:21:06 +02007816/* integer, returns the used keysize if front conn. transport layer is SSL.
7817 * This function is also usable on backend conn if the fetch keyword 5th
7818 * char is 'b'.
7819 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007820static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007821smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007822{
Emeric Bruneb8def92018-02-19 15:59:48 +01007823 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7824 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007825 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007826
Emeric Brun589fcad2012-10-16 14:13:26 +02007827 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007828 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7829 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007830 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007831
Olivier Houchard66ab4982019-02-26 18:37:15 +01007832 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007833 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02007834 return 0;
7835
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007836 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007837
7838 return 1;
7839}
7840
Bernard Spil13c53f82018-02-15 13:34:58 +01007841#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02007842static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007843smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007844{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007845 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007846 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007847
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007848 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007849 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007850
Olivier Houchard6b77f492018-11-22 18:18:29 +01007851 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7852 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007853 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7854 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007855 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007856
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007857 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007858 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007859 (const unsigned char **)&smp->data.u.str.area,
7860 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02007861
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007862 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007863 return 0;
7864
7865 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007866}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007867#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02007868
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007869#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007871smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02007872{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007873 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007874 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007875
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007876 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007877 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02007878
Olivier Houchard6b77f492018-11-22 18:18:29 +01007879 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7880 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7881
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007882 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02007883 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007884 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02007885
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007886 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007887 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007888 (const unsigned char **)&smp->data.u.str.area,
7889 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02007890
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007891 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02007892 return 0;
7893
7894 return 1;
7895}
7896#endif
7897
Emeric Brun645ae792014-04-30 14:21:06 +02007898/* string, returns the used protocol if front conn. transport layer is SSL.
7899 * This function is also usable on backend conn if the fetch keyword 5th
7900 * char is 'b'.
7901 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02007902static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007903smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007904{
Emeric Bruneb8def92018-02-19 15:59:48 +01007905 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7906 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007907 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007908
Emeric Brun589fcad2012-10-16 14:13:26 +02007909 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007910 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7911 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007912 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007913
Olivier Houchard66ab4982019-02-26 18:37:15 +01007914 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007915 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007916 return 0;
7917
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007918 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007919 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007920 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007921
7922 return 1;
7923}
7924
Willy Tarreau87b09662015-04-03 00:22:06 +02007925/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02007926 * This function is also usable on backend conn if the fetch keyword 5th
7927 * char is 'b'.
7928 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007929#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02007930static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007931smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02007932{
Emeric Bruneb8def92018-02-19 15:59:48 +01007933 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7934 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007935 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007936 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007937
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007938 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007939 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02007940
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007941 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7942 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007943 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007944
Olivier Houchard66ab4982019-02-26 18:37:15 +01007945 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02007946 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02007947 return 0;
7948
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007949 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
7950 (unsigned int *)&smp->data.u.str.data);
7951 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02007952 return 0;
7953
7954 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02007955}
Patrick Hemmer41966772018-04-28 19:15:48 -04007956#endif
7957
Emeric Brunfe68f682012-10-16 14:59:28 +02007958
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007959#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04007960static int
Patrick Hemmer65674662019-06-04 08:13:03 -04007961smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
7962{
7963 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7964 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7965 struct buffer *data;
7966 struct ssl_sock_ctx *ctx;
7967
7968 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7969 return 0;
7970 ctx = conn->xprt_ctx;
7971
7972 data = get_trash_chunk();
7973 if (kw[7] == 'c')
7974 data->data = SSL_get_client_random(ctx->ssl,
7975 (unsigned char *) data->area,
7976 data->size);
7977 else
7978 data->data = SSL_get_server_random(ctx->ssl,
7979 (unsigned char *) data->area,
7980 data->size);
7981 if (!data->data)
7982 return 0;
7983
7984 smp->flags = 0;
7985 smp->data.type = SMP_T_BIN;
7986 smp->data.u.str = *data;
7987
7988 return 1;
7989}
7990
7991static int
Patrick Hemmere0275472018-04-28 19:15:51 -04007992smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
7993{
7994 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7995 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7996 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02007997 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007998 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007999
8000 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8001 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008002 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008003
Olivier Houchard66ab4982019-02-26 18:37:15 +01008004 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008005 if (!ssl_sess)
8006 return 0;
8007
8008 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008009 data->data = SSL_SESSION_get_master_key(ssl_sess,
8010 (unsigned char *) data->area,
8011 data->size);
8012 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008013 return 0;
8014
8015 smp->flags = 0;
8016 smp->data.type = SMP_T_BIN;
8017 smp->data.u.str = *data;
8018
8019 return 1;
8020}
8021#endif
8022
Patrick Hemmer41966772018-04-28 19:15:48 -04008023#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008024static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008025smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008026{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008027 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008028 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008029
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008030 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008031 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008032
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008033 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008034 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8035 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008036 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008037
Olivier Houchard66ab4982019-02-26 18:37:15 +01008038 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008039 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008040 return 0;
8041
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008042 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008043 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008044}
Patrick Hemmer41966772018-04-28 19:15:48 -04008045#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008046
David Sc1ad52e2014-04-08 18:48:47 -04008047static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008048smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8049{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008050 struct connection *conn;
8051 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008052 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008053
8054 conn = objt_conn(smp->sess->origin);
8055 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8056 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008057 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008058
Olivier Houchard66ab4982019-02-26 18:37:15 +01008059 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008060 if (!capture)
8061 return 0;
8062
8063 smp->flags = SMP_F_CONST;
8064 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008065 smp->data.u.str.area = capture->ciphersuite;
8066 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008067 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008068}
8069
8070static int
8071smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8072{
Willy Tarreau83061a82018-07-13 11:56:34 +02008073 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008074
8075 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8076 return 0;
8077
8078 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008079 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008080 smp->data.type = SMP_T_BIN;
8081 smp->data.u.str = *data;
8082 return 1;
8083}
8084
8085static int
8086smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8087{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008088 struct connection *conn;
8089 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008090 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008091
8092 conn = objt_conn(smp->sess->origin);
8093 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8094 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008095 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008096
Olivier Houchard66ab4982019-02-26 18:37:15 +01008097 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008098 if (!capture)
8099 return 0;
8100
8101 smp->data.type = SMP_T_SINT;
8102 smp->data.u.sint = capture->xxh64;
8103 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008104}
8105
8106static int
8107smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8108{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008109#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008110 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008111 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008112
8113 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8114 return 0;
8115
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008116 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008117 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008118 const char *str;
8119 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008120 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008121 uint16_t id = (bin[0] << 8) | bin[1];
8122#if defined(OPENSSL_IS_BORINGSSL)
8123 cipher = SSL_get_cipher_by_value(id);
8124#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008125 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008126 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8127 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008128#endif
8129 str = SSL_CIPHER_get_name(cipher);
8130 if (!str || strcmp(str, "(NONE)") == 0)
8131 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008132 else
8133 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8134 }
8135 smp->data.type = SMP_T_STR;
8136 smp->data.u.str = *data;
8137 return 1;
8138#else
8139 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8140#endif
8141}
8142
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008143#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008144static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008145smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008146{
Emeric Bruneb8def92018-02-19 15:59:48 +01008147 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8148 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008149 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008150 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008151 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008152
8153 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008154 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8155 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008156 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008157
8158 if (!(conn->flags & CO_FL_CONNECTED)) {
8159 smp->flags |= SMP_F_MAY_CHANGE;
8160 return 0;
8161 }
8162
8163 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008164 if (!SSL_session_reused(ctx->ssl))
8165 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008166 finished_trash->area,
8167 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008168 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008169 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008170 finished_trash->area,
8171 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008172
8173 if (!finished_len)
8174 return 0;
8175
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008176 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008177 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008178 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008179
8180 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008181}
Patrick Hemmer41966772018-04-28 19:15:48 -04008182#endif
David Sc1ad52e2014-04-08 18:48:47 -04008183
Emeric Brun2525b6b2012-10-18 15:59:43 +02008184/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008185static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008186smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008187{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008188 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008189 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008190
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008191 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008192 if (!conn || conn->xprt != &ssl_sock)
8193 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008194 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008195
8196 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008197 smp->flags = SMP_F_MAY_CHANGE;
8198 return 0;
8199 }
8200
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008201 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008202 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008203 smp->flags = 0;
8204
8205 return 1;
8206}
8207
Emeric Brun2525b6b2012-10-18 15:59:43 +02008208/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008209static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008210smp_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 +02008211{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008212 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008213 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008214
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008215 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008216 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008217 return 0;
8218
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008219 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008220 smp->flags = SMP_F_MAY_CHANGE;
8221 return 0;
8222 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008223 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008224
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008225 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008226 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008227 smp->flags = 0;
8228
8229 return 1;
8230}
8231
Emeric Brun2525b6b2012-10-18 15:59:43 +02008232/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008233static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008234smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008235{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008236 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008237 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008238
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008239 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008240 if (!conn || conn->xprt != &ssl_sock)
8241 return 0;
8242
8243 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008244 smp->flags = SMP_F_MAY_CHANGE;
8245 return 0;
8246 }
8247
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008248 ctx = conn->xprt_ctx;
8249
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008250 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008251 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008252 smp->flags = 0;
8253
8254 return 1;
8255}
8256
Emeric Brun2525b6b2012-10-18 15:59:43 +02008257/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008258static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008259smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008260{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008261 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008262 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008263
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008264 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008265 if (!conn || conn->xprt != &ssl_sock)
8266 return 0;
8267
8268 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008269 smp->flags = SMP_F_MAY_CHANGE;
8270 return 0;
8271 }
8272
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008273 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008274 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008275 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008276
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008277 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008278 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008279 smp->flags = 0;
8280
8281 return 1;
8282}
8283
Emeric Brunfb510ea2012-10-05 12:00:26 +02008284/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008285static 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 +02008286{
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(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008295 else
8296 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008297
Emeric Brund94b3fe2012-09-20 18:23:56 +02008298 return 0;
8299}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008300static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8301{
8302 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8303}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008304
Christopher Faulet31af49d2015-06-09 17:29:50 +02008305/* parse the "ca-sign-file" bind keyword */
8306static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8307{
8308 if (!*args[cur_arg + 1]) {
8309 if (err)
8310 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
8311 return ERR_ALERT | ERR_FATAL;
8312 }
8313
Willy Tarreauef934602016-12-22 23:12:01 +01008314 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8315 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008316 else
8317 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8318
8319 return 0;
8320}
8321
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008322/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008323static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8324{
8325 if (!*args[cur_arg + 1]) {
8326 if (err)
8327 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
8328 return ERR_ALERT | ERR_FATAL;
8329 }
8330 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8331 return 0;
8332}
8333
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008334/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008335static 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 +02008336{
8337 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008338 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008339 return ERR_ALERT | ERR_FATAL;
8340 }
8341
Emeric Brun76d88952012-10-05 15:47:31 +02008342 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008343 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008344 return 0;
8345}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008346static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8347{
8348 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8349}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008350
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008351#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008352/* parse the "ciphersuites" bind keyword */
8353static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8354{
8355 if (!*args[cur_arg + 1]) {
8356 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8357 return ERR_ALERT | ERR_FATAL;
8358 }
8359
8360 free(conf->ciphersuites);
8361 conf->ciphersuites = strdup(args[cur_arg + 1]);
8362 return 0;
8363}
8364static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8365{
8366 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8367}
8368#endif
8369
Willy Tarreaubbc91962019-10-16 16:42:19 +02008370/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008371static 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 +02008372{
Willy Tarreau38011032013-08-13 16:59:39 +02008373 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008374
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008375 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008376 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008377 return ERR_ALERT | ERR_FATAL;
8378 }
8379
Willy Tarreauef934602016-12-22 23:12:01 +01008380 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8381 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008382 memprintf(err, "'%s' : path too long", args[cur_arg]);
8383 return ERR_ALERT | ERR_FATAL;
8384 }
Willy Tarreauef934602016-12-22 23:12:01 +01008385 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008386 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008387 }
8388
Willy Tarreaubbc91962019-10-16 16:42:19 +02008389 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008390}
8391
Willy Tarreaubbc91962019-10-16 16:42:19 +02008392/* parse the "crt-list" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008393static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8394{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008395 int err_code;
8396
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008397 if (!*args[cur_arg + 1]) {
8398 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8399 return ERR_ALERT | ERR_FATAL;
8400 }
8401
Willy Tarreaubbc91962019-10-16 16:42:19 +02008402 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8403 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008404 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008405
Willy Tarreaubbc91962019-10-16 16:42:19 +02008406 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008407}
8408
Emeric Brunfb510ea2012-10-05 12:00:26 +02008409/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008410static 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 +02008411{
Emeric Brun051cdab2012-10-02 19:25:50 +02008412#ifndef X509_V_FLAG_CRL_CHECK
8413 if (err)
8414 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
8415 return ERR_ALERT | ERR_FATAL;
8416#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008417 if (!*args[cur_arg + 1]) {
8418 if (err)
8419 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
8420 return ERR_ALERT | ERR_FATAL;
8421 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008422
Willy Tarreauef934602016-12-22 23:12:01 +01008423 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8424 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008425 else
8426 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008427
Emeric Brun2b58d042012-09-20 17:10:03 +02008428 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008429#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008430}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008431static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8432{
8433 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8434}
Emeric Brun2b58d042012-09-20 17:10:03 +02008435
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008436/* parse the "curves" bind keyword keyword */
8437static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8438{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008439#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008440 if (!*args[cur_arg + 1]) {
8441 if (err)
8442 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
8443 return ERR_ALERT | ERR_FATAL;
8444 }
8445 conf->curves = strdup(args[cur_arg + 1]);
8446 return 0;
8447#else
8448 if (err)
8449 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
8450 return ERR_ALERT | ERR_FATAL;
8451#endif
8452}
8453static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8454{
8455 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8456}
8457
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008458/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008459static 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 +02008460{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008461#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Emeric Brun2b58d042012-09-20 17:10:03 +02008462 if (err)
8463 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
8464 return ERR_ALERT | ERR_FATAL;
8465#elif defined(OPENSSL_NO_ECDH)
8466 if (err)
8467 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
8468 return ERR_ALERT | ERR_FATAL;
8469#else
8470 if (!*args[cur_arg + 1]) {
8471 if (err)
8472 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
8473 return ERR_ALERT | ERR_FATAL;
8474 }
8475
8476 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008477
8478 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008479#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008480}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008481static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8482{
8483 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8484}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008485
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008486/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008487static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8488{
8489 int code;
8490 char *p = args[cur_arg + 1];
8491 unsigned long long *ignerr = &conf->crt_ignerr;
8492
8493 if (!*p) {
8494 if (err)
8495 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
8496 return ERR_ALERT | ERR_FATAL;
8497 }
8498
8499 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8500 ignerr = &conf->ca_ignerr;
8501
8502 if (strcmp(p, "all") == 0) {
8503 *ignerr = ~0ULL;
8504 return 0;
8505 }
8506
8507 while (p) {
8508 code = atoi(p);
8509 if ((code <= 0) || (code > 63)) {
8510 if (err)
8511 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8512 args[cur_arg], code, args[cur_arg + 1]);
8513 return ERR_ALERT | ERR_FATAL;
8514 }
8515 *ignerr |= 1ULL << code;
8516 p = strchr(p, ',');
8517 if (p)
8518 p++;
8519 }
8520
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008521 return 0;
8522}
8523
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008524/* parse tls_method_options "no-xxx" and "force-xxx" */
8525static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008526{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008527 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008528 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008529 p = strchr(arg, '-');
8530 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008531 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008532 p++;
8533 if (!strcmp(p, "sslv3"))
8534 v = CONF_SSLV3;
8535 else if (!strcmp(p, "tlsv10"))
8536 v = CONF_TLSV10;
8537 else if (!strcmp(p, "tlsv11"))
8538 v = CONF_TLSV11;
8539 else if (!strcmp(p, "tlsv12"))
8540 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008541 else if (!strcmp(p, "tlsv13"))
8542 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008543 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008544 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008545 if (!strncmp(arg, "no-", 3))
8546 methods->flags |= methodVersions[v].flag;
8547 else if (!strncmp(arg, "force-", 6))
8548 methods->min = methods->max = v;
8549 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008550 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008551 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008552 fail:
8553 if (err)
8554 memprintf(err, "'%s' : option not implemented", arg);
8555 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008556}
8557
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008558static 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 +02008559{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008560 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008561}
8562
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008563static 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 +02008564{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008565 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8566}
8567
8568/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8569static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8570{
8571 uint16_t i, v = 0;
8572 char *argv = args[cur_arg + 1];
8573 if (!*argv) {
8574 if (err)
8575 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
8576 return ERR_ALERT | ERR_FATAL;
8577 }
8578 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8579 if (!strcmp(argv, methodVersions[i].name))
8580 v = i;
8581 if (!v) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008582 if (err)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008583 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008584 return ERR_ALERT | ERR_FATAL;
8585 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008586 if (!strcmp("ssl-min-ver", args[cur_arg]))
8587 methods->min = v;
8588 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8589 methods->max = v;
8590 else {
8591 if (err)
8592 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
8593 return ERR_ALERT | ERR_FATAL;
8594 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008595 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008596}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008597
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008598static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8599{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008600#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008601 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 +02008602#endif
8603 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8604}
8605
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008606static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8607{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008608 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008609}
8610
8611static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8612{
8613 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8614}
8615
Emeric Brun2d0c4822012-10-02 13:45:20 +02008616/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008617static 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 +02008618{
Emeric Brun89675492012-10-05 13:48:26 +02008619 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008620 return 0;
8621}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008622
Olivier Houchardc2aae742017-09-22 18:26:28 +02008623/* parse the "allow-0rtt" bind keyword */
8624static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8625{
8626 conf->early_data = 1;
8627 return 0;
8628}
8629
8630static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8631{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008632 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008633 return 0;
8634}
8635
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008636/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008637static 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 +02008638{
Bernard Spil13c53f82018-02-15 13:34:58 +01008639#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008640 char *p1, *p2;
8641
8642 if (!*args[cur_arg + 1]) {
8643 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8644 return ERR_ALERT | ERR_FATAL;
8645 }
8646
8647 free(conf->npn_str);
8648
Willy Tarreau3724da12016-02-12 17:11:12 +01008649 /* the NPN string is built as a suite of (<len> <name>)*,
8650 * so we reuse each comma to store the next <len> and need
8651 * one more for the end of the string.
8652 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008653 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008654 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008655 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8656
8657 /* replace commas with the name length */
8658 p1 = conf->npn_str;
8659 p2 = p1 + 1;
8660 while (1) {
8661 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8662 if (!p2)
8663 p2 = p1 + 1 + strlen(p1 + 1);
8664
8665 if (p2 - (p1 + 1) > 255) {
8666 *p2 = '\0';
8667 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8668 return ERR_ALERT | ERR_FATAL;
8669 }
8670
8671 *p1 = p2 - (p1 + 1);
8672 p1 = p2;
8673
8674 if (!*p2)
8675 break;
8676
8677 *(p2++) = '\0';
8678 }
8679 return 0;
8680#else
8681 if (err)
8682 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
8683 return ERR_ALERT | ERR_FATAL;
8684#endif
8685}
8686
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008687static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8688{
8689 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8690}
8691
Willy Tarreauab861d32013-04-02 02:30:41 +02008692/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008693static 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 +02008694{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008695#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008696 char *p1, *p2;
8697
8698 if (!*args[cur_arg + 1]) {
8699 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8700 return ERR_ALERT | ERR_FATAL;
8701 }
8702
8703 free(conf->alpn_str);
8704
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008705 /* the ALPN string is built as a suite of (<len> <name>)*,
8706 * so we reuse each comma to store the next <len> and need
8707 * one more for the end of the string.
8708 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008709 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008710 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008711 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8712
8713 /* replace commas with the name length */
8714 p1 = conf->alpn_str;
8715 p2 = p1 + 1;
8716 while (1) {
8717 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8718 if (!p2)
8719 p2 = p1 + 1 + strlen(p1 + 1);
8720
8721 if (p2 - (p1 + 1) > 255) {
8722 *p2 = '\0';
8723 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8724 return ERR_ALERT | ERR_FATAL;
8725 }
8726
8727 *p1 = p2 - (p1 + 1);
8728 p1 = p2;
8729
8730 if (!*p2)
8731 break;
8732
8733 *(p2++) = '\0';
8734 }
8735 return 0;
8736#else
8737 if (err)
8738 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
8739 return ERR_ALERT | ERR_FATAL;
8740#endif
8741}
8742
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008743static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8744{
8745 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8746}
8747
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008748/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008749static 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 +02008750{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008751 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008752 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008753
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008754 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8755 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008756#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008757 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8758 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8759#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008760 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008761 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8762 if (!conf->ssl_conf.ssl_methods.min)
8763 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8764 if (!conf->ssl_conf.ssl_methods.max)
8765 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008766
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008767 return 0;
8768}
8769
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008770/* parse the "prefer-client-ciphers" bind keyword */
8771static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8772{
8773 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8774 return 0;
8775}
8776
Christopher Faulet31af49d2015-06-09 17:29:50 +02008777/* parse the "generate-certificates" bind keyword */
8778static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8779{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008780#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008781 conf->generate_certs = 1;
8782#else
8783 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8784 err && *err ? *err : "");
8785#endif
8786 return 0;
8787}
8788
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008789/* parse the "strict-sni" bind keyword */
8790static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8791{
8792 conf->strict_sni = 1;
8793 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008794}
8795
8796/* parse the "tls-ticket-keys" bind keyword */
8797static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8798{
8799#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02008800 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008801 int i = 0;
8802 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02008803 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008804
8805 if (!*args[cur_arg + 1]) {
8806 if (err)
8807 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008808 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008809 }
8810
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008811 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008812 if (keys_ref) {
8813 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008814 conf->keys_ref = keys_ref;
8815 return 0;
8816 }
8817
Christopher Faulete566f3d2019-10-21 09:55:49 +02008818 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01008819 if (!keys_ref) {
8820 if (err)
8821 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008822 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008823 }
8824
Emeric Brun9e754772019-01-10 17:51:55 +01008825 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01008826 if (!keys_ref->tlskeys) {
Emeric Brun09852f72019-01-10 10:51:13 +01008827 if (err)
8828 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008829 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008830 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008831
8832 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
8833 if (err)
8834 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008835 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008836 }
8837
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008838 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01008839 if (!keys_ref->filename) {
Emeric Brun09852f72019-01-10 10:51:13 +01008840 if (err)
8841 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008842 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008843 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008844
Emeric Brun9e754772019-01-10 17:51:55 +01008845 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008846 while (fgets(thisline, sizeof(thisline), f) != NULL) {
8847 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01008848 int dec_size;
8849
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008850 /* Strip newline characters from the end */
8851 if(thisline[len - 1] == '\n')
8852 thisline[--len] = 0;
8853
8854 if(thisline[len - 1] == '\r')
8855 thisline[--len] = 0;
8856
Emeric Brun9e754772019-01-10 17:51:55 +01008857 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
8858 if (dec_size < 0) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008859 if (err)
8860 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008861 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008862 }
Emeric Brun9e754772019-01-10 17:51:55 +01008863 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
8864 keys_ref->key_size_bits = 128;
8865 }
8866 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
8867 keys_ref->key_size_bits = 256;
8868 }
8869 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
8870 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
8871 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Emeric Brun9e754772019-01-10 17:51:55 +01008872 if (err)
8873 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008874 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01008875 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008876 i++;
8877 }
8878
8879 if (i < TLS_TICKETS_NO) {
8880 if (err)
8881 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008882 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008883 }
8884
8885 fclose(f);
8886
8887 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01008888 i -= 2;
8889 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008890 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008891 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01008892 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008893 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008894
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008895 LIST_ADD(&tlskeys_reference, &keys_ref->list);
8896
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008897 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02008898
8899 fail:
8900 if (f)
8901 fclose(f);
8902 if (keys_ref) {
8903 free(keys_ref->filename);
8904 free(keys_ref->tlskeys);
8905 free(keys_ref);
8906 }
8907 return ERR_ALERT | ERR_FATAL;
8908
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008909#else
8910 if (err)
8911 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
8912 return ERR_ALERT | ERR_FATAL;
8913#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008914}
8915
Emeric Brund94b3fe2012-09-20 18:23:56 +02008916/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008917static 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 +02008918{
8919 if (!*args[cur_arg + 1]) {
8920 if (err)
8921 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
8922 return ERR_ALERT | ERR_FATAL;
8923 }
8924
8925 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008926 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008927 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008928 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008929 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008930 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008931 else {
8932 if (err)
8933 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
8934 args[cur_arg], args[cur_arg + 1]);
8935 return ERR_ALERT | ERR_FATAL;
8936 }
8937
8938 return 0;
8939}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008940static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8941{
8942 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
8943}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008944
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02008945/* parse the "no-ca-names" bind keyword */
8946static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8947{
8948 conf->no_ca_names = 1;
8949 return 0;
8950}
8951static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8952{
8953 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
8954}
8955
Willy Tarreau92faadf2012-10-10 23:04:25 +02008956/************** "server" keywords ****************/
8957
Olivier Houchardc7566002018-11-20 23:33:50 +01008958/* parse the "npn" bind keyword */
8959static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8960{
8961#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
8962 char *p1, *p2;
8963
8964 if (!*args[*cur_arg + 1]) {
8965 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
8966 return ERR_ALERT | ERR_FATAL;
8967 }
8968
8969 free(newsrv->ssl_ctx.npn_str);
8970
8971 /* the NPN string is built as a suite of (<len> <name>)*,
8972 * so we reuse each comma to store the next <len> and need
8973 * one more for the end of the string.
8974 */
8975 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
8976 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
8977 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
8978 newsrv->ssl_ctx.npn_len);
8979
8980 /* replace commas with the name length */
8981 p1 = newsrv->ssl_ctx.npn_str;
8982 p2 = p1 + 1;
8983 while (1) {
8984 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
8985 newsrv->ssl_ctx.npn_len - (p1 + 1));
8986 if (!p2)
8987 p2 = p1 + 1 + strlen(p1 + 1);
8988
8989 if (p2 - (p1 + 1) > 255) {
8990 *p2 = '\0';
8991 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8992 return ERR_ALERT | ERR_FATAL;
8993 }
8994
8995 *p1 = p2 - (p1 + 1);
8996 p1 = p2;
8997
8998 if (!*p2)
8999 break;
9000
9001 *(p2++) = '\0';
9002 }
9003 return 0;
9004#else
9005 if (err)
9006 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
9007 return ERR_ALERT | ERR_FATAL;
9008#endif
9009}
9010
Olivier Houchard92150142018-12-21 19:47:01 +01009011/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009012static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9013{
9014#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9015 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009016 char **alpn_str;
9017 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009018
Olivier Houchard92150142018-12-21 19:47:01 +01009019 if (*args[*cur_arg] == 'c') {
9020 alpn_str = &newsrv->check.alpn_str;
9021 alpn_len = &newsrv->check.alpn_len;
9022 } else {
9023 alpn_str = &newsrv->ssl_ctx.alpn_str;
9024 alpn_len = &newsrv->ssl_ctx.alpn_len;
9025
9026 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009027 if (!*args[*cur_arg + 1]) {
9028 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9029 return ERR_ALERT | ERR_FATAL;
9030 }
9031
Olivier Houchard92150142018-12-21 19:47:01 +01009032 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009033
9034 /* the ALPN string is built as a suite of (<len> <name>)*,
9035 * so we reuse each comma to store the next <len> and need
9036 * one more for the end of the string.
9037 */
Olivier Houchard92150142018-12-21 19:47:01 +01009038 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9039 *alpn_str = calloc(1, *alpn_len + 1);
9040 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009041
9042 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009043 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009044 p2 = p1 + 1;
9045 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009046 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009047 if (!p2)
9048 p2 = p1 + 1 + strlen(p1 + 1);
9049
9050 if (p2 - (p1 + 1) > 255) {
9051 *p2 = '\0';
9052 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9053 return ERR_ALERT | ERR_FATAL;
9054 }
9055
9056 *p1 = p2 - (p1 + 1);
9057 p1 = p2;
9058
9059 if (!*p2)
9060 break;
9061
9062 *(p2++) = '\0';
9063 }
9064 return 0;
9065#else
9066 if (err)
9067 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
9068 return ERR_ALERT | ERR_FATAL;
9069#endif
9070}
9071
Emeric Brunef42d922012-10-11 16:11:36 +02009072/* parse the "ca-file" server keyword */
9073static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9074{
9075 if (!*args[*cur_arg + 1]) {
9076 if (err)
9077 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
9078 return ERR_ALERT | ERR_FATAL;
9079 }
9080
Willy Tarreauef934602016-12-22 23:12:01 +01009081 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9082 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009083 else
9084 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9085
9086 return 0;
9087}
9088
Olivier Houchard9130a962017-10-17 17:33:43 +02009089/* parse the "check-sni" server keyword */
9090static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9091{
9092 if (!*args[*cur_arg + 1]) {
9093 if (err)
9094 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
9095 return ERR_ALERT | ERR_FATAL;
9096 }
9097
9098 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9099 if (!newsrv->check.sni) {
9100 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9101 return ERR_ALERT | ERR_FATAL;
9102 }
9103 return 0;
9104
9105}
9106
Willy Tarreau92faadf2012-10-10 23:04:25 +02009107/* parse the "check-ssl" server keyword */
9108static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9109{
9110 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009111 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9112 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009113#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009114 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9115 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9116#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009117 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009118 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9119 if (!newsrv->ssl_ctx.methods.min)
9120 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9121 if (!newsrv->ssl_ctx.methods.max)
9122 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9123
Willy Tarreau92faadf2012-10-10 23:04:25 +02009124 return 0;
9125}
9126
9127/* parse the "ciphers" server keyword */
9128static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9129{
9130 if (!*args[*cur_arg + 1]) {
9131 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9132 return ERR_ALERT | ERR_FATAL;
9133 }
9134
9135 free(newsrv->ssl_ctx.ciphers);
9136 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9137 return 0;
9138}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009139
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009140#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009141/* parse the "ciphersuites" server keyword */
9142static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9143{
9144 if (!*args[*cur_arg + 1]) {
9145 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9146 return ERR_ALERT | ERR_FATAL;
9147 }
9148
9149 free(newsrv->ssl_ctx.ciphersuites);
9150 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9151 return 0;
9152}
9153#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009154
Emeric Brunef42d922012-10-11 16:11:36 +02009155/* parse the "crl-file" server keyword */
9156static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9157{
9158#ifndef X509_V_FLAG_CRL_CHECK
9159 if (err)
9160 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
9161 return ERR_ALERT | ERR_FATAL;
9162#else
9163 if (!*args[*cur_arg + 1]) {
9164 if (err)
9165 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
9166 return ERR_ALERT | ERR_FATAL;
9167 }
9168
Willy Tarreauef934602016-12-22 23:12:01 +01009169 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9170 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009171 else
9172 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9173
9174 return 0;
9175#endif
9176}
9177
Emeric Bruna7aa3092012-10-26 12:58:00 +02009178/* parse the "crt" server keyword */
9179static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9180{
9181 if (!*args[*cur_arg + 1]) {
9182 if (err)
9183 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
9184 return ERR_ALERT | ERR_FATAL;
9185 }
9186
Willy Tarreauef934602016-12-22 23:12:01 +01009187 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009188 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009189 else
9190 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9191
9192 return 0;
9193}
Emeric Brunef42d922012-10-11 16:11:36 +02009194
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009195/* parse the "no-check-ssl" server keyword */
9196static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9197{
9198 newsrv->check.use_ssl = 0;
9199 free(newsrv->ssl_ctx.ciphers);
9200 newsrv->ssl_ctx.ciphers = NULL;
9201 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9202 return 0;
9203}
9204
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009205/* parse the "no-send-proxy-v2-ssl" server keyword */
9206static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9207{
9208 newsrv->pp_opts &= ~SRV_PP_V2;
9209 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9210 return 0;
9211}
9212
9213/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9214static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9215{
9216 newsrv->pp_opts &= ~SRV_PP_V2;
9217 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9218 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9219 return 0;
9220}
9221
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009222/* parse the "no-ssl" server keyword */
9223static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9224{
9225 newsrv->use_ssl = 0;
9226 free(newsrv->ssl_ctx.ciphers);
9227 newsrv->ssl_ctx.ciphers = NULL;
9228 return 0;
9229}
9230
Olivier Houchard522eea72017-11-03 16:27:47 +01009231/* parse the "allow-0rtt" server keyword */
9232static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9233{
9234 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9235 return 0;
9236}
9237
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009238/* parse the "no-ssl-reuse" server keyword */
9239static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9240{
9241 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9242 return 0;
9243}
9244
Emeric Brunf9c5c472012-10-11 15:28:34 +02009245/* parse the "no-tls-tickets" server keyword */
9246static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9247{
9248 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9249 return 0;
9250}
David Safb76832014-05-08 23:42:08 -04009251/* parse the "send-proxy-v2-ssl" server keyword */
9252static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9253{
9254 newsrv->pp_opts |= SRV_PP_V2;
9255 newsrv->pp_opts |= SRV_PP_V2_SSL;
9256 return 0;
9257}
9258
9259/* parse the "send-proxy-v2-ssl-cn" server keyword */
9260static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9261{
9262 newsrv->pp_opts |= SRV_PP_V2;
9263 newsrv->pp_opts |= SRV_PP_V2_SSL;
9264 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9265 return 0;
9266}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009267
Willy Tarreau732eac42015-07-09 11:40:25 +02009268/* parse the "sni" server keyword */
9269static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9270{
9271#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9272 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9273 return ERR_ALERT | ERR_FATAL;
9274#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009275 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009276
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009277 arg = args[*cur_arg + 1];
9278 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009279 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9280 return ERR_ALERT | ERR_FATAL;
9281 }
9282
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009283 free(newsrv->sni_expr);
9284 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009285
Willy Tarreau732eac42015-07-09 11:40:25 +02009286 return 0;
9287#endif
9288}
9289
Willy Tarreau92faadf2012-10-10 23:04:25 +02009290/* parse the "ssl" server keyword */
9291static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9292{
9293 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009294 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9295 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009296#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009297 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9298 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9299#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009300 return 0;
9301}
9302
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009303/* parse the "ssl-reuse" server keyword */
9304static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9305{
9306 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9307 return 0;
9308}
9309
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009310/* parse the "tls-tickets" server keyword */
9311static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9312{
9313 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9314 return 0;
9315}
9316
Emeric Brunef42d922012-10-11 16:11:36 +02009317/* parse the "verify" server keyword */
9318static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9319{
9320 if (!*args[*cur_arg + 1]) {
9321 if (err)
9322 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
9323 return ERR_ALERT | ERR_FATAL;
9324 }
9325
9326 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009327 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009328 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009329 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009330 else {
9331 if (err)
9332 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9333 args[*cur_arg], args[*cur_arg + 1]);
9334 return ERR_ALERT | ERR_FATAL;
9335 }
9336
Evan Broderbe554312013-06-27 00:05:25 -07009337 return 0;
9338}
9339
9340/* parse the "verifyhost" server keyword */
9341static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9342{
9343 if (!*args[*cur_arg + 1]) {
9344 if (err)
9345 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
9346 return ERR_ALERT | ERR_FATAL;
9347 }
9348
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009349 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009350 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9351
Emeric Brunef42d922012-10-11 16:11:36 +02009352 return 0;
9353}
9354
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009355/* parse the "ssl-default-bind-options" keyword in global section */
9356static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9357 struct proxy *defpx, const char *file, int line,
9358 char **err) {
9359 int i = 1;
9360
9361 if (*(args[i]) == 0) {
9362 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9363 return -1;
9364 }
9365 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009366 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009367 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009368 else if (!strcmp(args[i], "prefer-client-ciphers"))
9369 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009370 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9371 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9372 i++;
9373 else {
9374 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9375 return -1;
9376 }
9377 }
9378 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009379 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9380 return -1;
9381 }
9382 i++;
9383 }
9384 return 0;
9385}
9386
9387/* parse the "ssl-default-server-options" keyword in global section */
9388static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9389 struct proxy *defpx, const char *file, int line,
9390 char **err) {
9391 int i = 1;
9392
9393 if (*(args[i]) == 0) {
9394 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9395 return -1;
9396 }
9397 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009398 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009399 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009400 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9401 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9402 i++;
9403 else {
9404 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9405 return -1;
9406 }
9407 }
9408 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009409 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9410 return -1;
9411 }
9412 i++;
9413 }
9414 return 0;
9415}
9416
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009417/* parse the "ca-base" / "crt-base" keywords in global section.
9418 * Returns <0 on alert, >0 on warning, 0 on success.
9419 */
9420static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9421 struct proxy *defpx, const char *file, int line,
9422 char **err)
9423{
9424 char **target;
9425
Willy Tarreauef934602016-12-22 23:12:01 +01009426 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009427
9428 if (too_many_args(1, args, err, NULL))
9429 return -1;
9430
9431 if (*target) {
9432 memprintf(err, "'%s' already specified.", args[0]);
9433 return -1;
9434 }
9435
9436 if (*(args[1]) == 0) {
9437 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9438 return -1;
9439 }
9440 *target = strdup(args[1]);
9441 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009442}
9443
9444/* parse the "ssl-mode-async" keyword in global section.
9445 * Returns <0 on alert, >0 on warning, 0 on success.
9446 */
9447static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9448 struct proxy *defpx, const char *file, int line,
9449 char **err)
9450{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009451#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009452 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009453 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009454 return 0;
9455#else
9456 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9457 return -1;
9458#endif
9459}
9460
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009461#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009462static int ssl_check_async_engine_count(void) {
9463 int err_code = 0;
9464
Emeric Brun3854e012017-05-17 20:42:48 +02009465 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009466 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009467 err_code = ERR_ABORT;
9468 }
9469 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009470}
9471
Grant Zhang872f9c22017-01-21 01:10:18 +00009472/* parse the "ssl-engine" keyword in global section.
9473 * Returns <0 on alert, >0 on warning, 0 on success.
9474 */
9475static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9476 struct proxy *defpx, const char *file, int line,
9477 char **err)
9478{
9479 char *algo;
9480 int ret = -1;
9481
9482 if (*(args[1]) == 0) {
9483 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9484 return ret;
9485 }
9486
9487 if (*(args[2]) == 0) {
9488 /* if no list of algorithms is given, it defaults to ALL */
9489 algo = strdup("ALL");
9490 goto add_engine;
9491 }
9492
9493 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9494 if (strcmp(args[2], "algo") != 0) {
9495 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9496 return ret;
9497 }
9498
9499 if (*(args[3]) == 0) {
9500 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9501 return ret;
9502 }
9503 algo = strdup(args[3]);
9504
9505add_engine:
9506 if (ssl_init_single_engine(args[1], algo)==0) {
9507 openssl_engines_initialized++;
9508 ret = 0;
9509 }
9510 free(algo);
9511 return ret;
9512}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009513#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009514
Willy Tarreauf22e9682016-12-21 23:23:19 +01009515/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9516 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9517 */
9518static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9519 struct proxy *defpx, const char *file, int line,
9520 char **err)
9521{
9522 char **target;
9523
Willy Tarreauef934602016-12-22 23:12:01 +01009524 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009525
9526 if (too_many_args(1, args, err, NULL))
9527 return -1;
9528
9529 if (*(args[1]) == 0) {
9530 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9531 return -1;
9532 }
9533
9534 free(*target);
9535 *target = strdup(args[1]);
9536 return 0;
9537}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009538
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009539#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009540/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9541 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9542 */
9543static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9544 struct proxy *defpx, const char *file, int line,
9545 char **err)
9546{
9547 char **target;
9548
9549 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9550
9551 if (too_many_args(1, args, err, NULL))
9552 return -1;
9553
9554 if (*(args[1]) == 0) {
9555 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9556 return -1;
9557 }
9558
9559 free(*target);
9560 *target = strdup(args[1]);
9561 return 0;
9562}
9563#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009564
Willy Tarreau9ceda382016-12-21 23:13:03 +01009565/* parse various global tune.ssl settings consisting in positive integers.
9566 * Returns <0 on alert, >0 on warning, 0 on success.
9567 */
9568static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9569 struct proxy *defpx, const char *file, int line,
9570 char **err)
9571{
9572 int *target;
9573
9574 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9575 target = &global.tune.sslcachesize;
9576 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009577 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009578 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009579 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009580 else if (strcmp(args[0], "maxsslconn") == 0)
9581 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009582 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9583 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009584 else {
9585 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9586 return -1;
9587 }
9588
9589 if (too_many_args(1, args, err, NULL))
9590 return -1;
9591
9592 if (*(args[1]) == 0) {
9593 memprintf(err, "'%s' expects an integer argument.", args[0]);
9594 return -1;
9595 }
9596
9597 *target = atoi(args[1]);
9598 if (*target < 0) {
9599 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9600 return -1;
9601 }
9602 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009603}
9604
9605static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9606 struct proxy *defpx, const char *file, int line,
9607 char **err)
9608{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009609 int ret;
9610
9611 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9612 if (ret != 0)
9613 return ret;
9614
Willy Tarreaubafbe012017-11-24 17:34:44 +01009615 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009616 memprintf(err, "'%s' is already configured.", args[0]);
9617 return -1;
9618 }
9619
Willy Tarreaubafbe012017-11-24 17:34:44 +01009620 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9621 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009622 memprintf(err, "Out of memory error.");
9623 return -1;
9624 }
9625 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009626}
9627
9628/* parse "ssl.force-private-cache".
9629 * Returns <0 on alert, >0 on warning, 0 on success.
9630 */
9631static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9632 struct proxy *defpx, const char *file, int line,
9633 char **err)
9634{
9635 if (too_many_args(0, args, err, NULL))
9636 return -1;
9637
Willy Tarreauef934602016-12-22 23:12:01 +01009638 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009639 return 0;
9640}
9641
9642/* parse "ssl.lifetime".
9643 * Returns <0 on alert, >0 on warning, 0 on success.
9644 */
9645static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9646 struct proxy *defpx, const char *file, int line,
9647 char **err)
9648{
9649 const char *res;
9650
9651 if (too_many_args(1, args, err, NULL))
9652 return -1;
9653
9654 if (*(args[1]) == 0) {
9655 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9656 return -1;
9657 }
9658
Willy Tarreauef934602016-12-22 23:12:01 +01009659 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009660 if (res == PARSE_TIME_OVER) {
9661 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9662 args[1], args[0]);
9663 return -1;
9664 }
9665 else if (res == PARSE_TIME_UNDER) {
9666 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9667 args[1], args[0]);
9668 return -1;
9669 }
9670 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009671 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9672 return -1;
9673 }
9674 return 0;
9675}
9676
9677#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009678/* parse "ssl-dh-param-file".
9679 * Returns <0 on alert, >0 on warning, 0 on success.
9680 */
9681static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9682 struct proxy *defpx, const char *file, int line,
9683 char **err)
9684{
9685 if (too_many_args(1, args, err, NULL))
9686 return -1;
9687
9688 if (*(args[1]) == 0) {
9689 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9690 return -1;
9691 }
9692
9693 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9694 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9695 return -1;
9696 }
9697 return 0;
9698}
9699
Willy Tarreau9ceda382016-12-21 23:13:03 +01009700/* parse "ssl.default-dh-param".
9701 * Returns <0 on alert, >0 on warning, 0 on success.
9702 */
9703static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9704 struct proxy *defpx, const char *file, int line,
9705 char **err)
9706{
9707 if (too_many_args(1, args, err, NULL))
9708 return -1;
9709
9710 if (*(args[1]) == 0) {
9711 memprintf(err, "'%s' expects an integer argument.", args[0]);
9712 return -1;
9713 }
9714
Willy Tarreauef934602016-12-22 23:12:01 +01009715 global_ssl.default_dh_param = atoi(args[1]);
9716 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009717 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9718 return -1;
9719 }
9720 return 0;
9721}
9722#endif
9723
9724
William Lallemand32af2032016-10-29 18:09:35 +02009725/* This function is used with TLS ticket keys management. It permits to browse
9726 * each reference. The variable <getnext> must contain the current node,
9727 * <end> point to the root node.
9728 */
9729#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9730static inline
9731struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9732{
9733 struct tls_keys_ref *ref = getnext;
9734
9735 while (1) {
9736
9737 /* Get next list entry. */
9738 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9739
9740 /* If the entry is the last of the list, return NULL. */
9741 if (&ref->list == end)
9742 return NULL;
9743
9744 return ref;
9745 }
9746}
9747
9748static inline
9749struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9750{
9751 int id;
9752 char *error;
9753
9754 /* If the reference starts by a '#', this is numeric id. */
9755 if (reference[0] == '#') {
9756 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9757 id = strtol(reference + 1, &error, 10);
9758 if (*error != '\0')
9759 return NULL;
9760
9761 /* Perform the unique id lookup. */
9762 return tlskeys_ref_lookupid(id);
9763 }
9764
9765 /* Perform the string lookup. */
9766 return tlskeys_ref_lookup(reference);
9767}
9768#endif
9769
9770
9771#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9772
9773static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9774
9775static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9776 return cli_io_handler_tlskeys_files(appctx);
9777}
9778
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009779/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9780 * (next index to be dumped), and cli.p0 (next key reference).
9781 */
William Lallemand32af2032016-10-29 18:09:35 +02009782static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9783
9784 struct stream_interface *si = appctx->owner;
9785
9786 switch (appctx->st2) {
9787 case STAT_ST_INIT:
9788 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009789 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009790 * later and restart at the state "STAT_ST_INIT".
9791 */
9792 chunk_reset(&trash);
9793
9794 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9795 chunk_appendf(&trash, "# id secret\n");
9796 else
9797 chunk_appendf(&trash, "# id (file)\n");
9798
Willy Tarreau06d80a92017-10-19 14:32:15 +02009799 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009800 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009801 return 0;
9802 }
9803
William Lallemand32af2032016-10-29 18:09:35 +02009804 /* Now, we start the browsing of the references lists.
9805 * Note that the following call to LIST_ELEM return bad pointer. The only
9806 * available field of this pointer is <list>. It is used with the function
9807 * tlskeys_list_get_next() for retruning the first available entry
9808 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009809 if (appctx->ctx.cli.p0 == NULL) {
9810 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9811 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009812 }
9813
9814 appctx->st2 = STAT_ST_LIST;
9815 /* fall through */
9816
9817 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009818 while (appctx->ctx.cli.p0) {
9819 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02009820
9821 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009822 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02009823 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009824
9825 if (appctx->ctx.cli.i1 == 0)
9826 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
9827
William Lallemand32af2032016-10-29 18:09:35 +02009828 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01009829 int head;
9830
9831 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
9832 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009833 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02009834 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02009835
9836 chunk_reset(t2);
9837 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01009838 if (ref->key_size_bits == 128) {
9839 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9840 sizeof(struct tls_sess_key_128),
9841 t2->area, t2->size);
9842 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9843 t2->area);
9844 }
9845 else if (ref->key_size_bits == 256) {
9846 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9847 sizeof(struct tls_sess_key_256),
9848 t2->area, t2->size);
9849 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9850 t2->area);
9851 }
9852 else {
9853 /* This case should never happen */
9854 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
9855 }
William Lallemand32af2032016-10-29 18:09:35 +02009856
Willy Tarreau06d80a92017-10-19 14:32:15 +02009857 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009858 /* let's try again later from this stream. We add ourselves into
9859 * this stream's users so that it can remove us upon termination.
9860 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01009861 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01009862 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009863 return 0;
9864 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009865 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02009866 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01009867 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009868 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009869 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02009870 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009871 /* let's try again later from this stream. We add ourselves into
9872 * this stream's users so that it can remove us upon termination.
9873 */
Willy Tarreaudb398432018-11-15 11:08:52 +01009874 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009875 return 0;
9876 }
9877
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009878 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02009879 break;
9880
9881 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009882 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009883 }
9884
9885 appctx->st2 = STAT_ST_FIN;
9886 /* fall through */
9887
9888 default:
9889 appctx->st2 = STAT_ST_FIN;
9890 return 1;
9891 }
9892 return 0;
9893}
9894
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009895/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009896static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009897{
William Lallemand32af2032016-10-29 18:09:35 +02009898 /* no parameter, shows only file list */
9899 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009900 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009901 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009902 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009903 }
9904
9905 if (args[2][0] == '*') {
9906 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009907 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009908 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009909 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +02009910 if (!appctx->ctx.cli.p0)
9911 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02009912 }
William Lallemand32af2032016-10-29 18:09:35 +02009913 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009914 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009915}
9916
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009917static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009918{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009919 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009920 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009921
William Lallemand32af2032016-10-29 18:09:35 +02009922 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +02009923 if (!*args[3] || !*args[4])
9924 return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009925
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009926 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02009927 if (!ref)
9928 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02009929
Willy Tarreau1c913e42018-08-22 05:26:57 +02009930 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +02009931 if (ret < 0)
9932 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +01009933
Willy Tarreau1c913e42018-08-22 05:26:57 +02009934 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +02009935 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
9936 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009937
Willy Tarreau9d008692019-08-09 11:21:01 +02009938 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +02009939}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009940#endif
William Lallemand32af2032016-10-29 18:09:35 +02009941
William Lallemand44b35322019-10-17 16:28:40 +02009942
9943/* Type of SSL payloads that can be updated over the CLI */
9944
9945enum {
9946 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +01009947#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +02009948 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +01009949#endif
William Lallemand44b35322019-10-17 16:28:40 +02009950 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +01009951#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +02009952 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +01009953#endif
William Lallemand44b35322019-10-17 16:28:40 +02009954 CERT_TYPE_MAX,
9955};
9956
9957struct {
9958 const char *ext;
9959 int type;
9960 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
9961 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +02009962} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +02009963 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +02009964#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +02009965 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +02009966#endif
9967#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +02009968 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +02009969#endif
William Lallemand44b35322019-10-17 16:28:40 +02009970 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +02009971 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +02009972};
9973
William Lallemand430413e2019-10-28 14:30:47 +01009974/* states of the CLI IO handler for 'set ssl cert' */
9975enum {
9976 SETCERT_ST_INIT = 0,
9977 SETCERT_ST_GEN,
9978 SETCERT_ST_INSERT,
9979 SETCERT_ST_FIN,
9980};
William Lallemand8f840d72019-10-23 10:53:05 +02009981
William Lallemand430413e2019-10-28 14:30:47 +01009982/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +01009983static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +02009984{
9985 struct ckch_store *new_ckchs;
9986 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +02009987
William Lallemand430413e2019-10-28 14:30:47 +01009988 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +02009989
William Lallemand430413e2019-10-28 14:30:47 +01009990 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +02009991 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
William Lallemandbeea2a42019-10-30 17:45:33 +01009992 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +02009993
William Lallemandbeea2a42019-10-30 17:45:33 +01009994 if (!new_ckchs)
9995 return;
William Lallemand8f840d72019-10-23 10:53:05 +02009996
William Lallemandbeea2a42019-10-30 17:45:33 +01009997 /* if the allocation failed, we need to free everything from the temporary list */
9998 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
9999 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010000
William Lallemandbeea2a42019-10-30 17:45:33 +010010001 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10002 if (sc0->order == 0) /* we only free if it's the first inserted */
10003 SSL_CTX_free(sc0->ctx);
10004 LIST_DEL(&sc0->by_ckch_inst);
10005 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010006 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010007 LIST_DEL(&ckchi->by_ckchs);
10008 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010009 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010010 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010011 }
10012}
10013
10014
10015/*
10016 * This function tries to create the new ckch_inst and their SNIs
10017 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010018static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010019{
10020 struct stream_interface *si = appctx->owner;
10021 int y = 0;
10022 char *err = NULL;
10023 int errcode = 0;
10024 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10025 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010026 struct buffer *trash = alloc_trash_chunk();
10027
William Lallemand33cc76f2019-10-31 11:43:45 +010010028 if (trash == NULL)
10029 goto error;
10030
William Lallemand8f840d72019-10-23 10:53:05 +020010031 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10032 goto error;
10033
William Lallemand430413e2019-10-28 14:30:47 +010010034 while (1) {
10035 switch (appctx->st2) {
10036 case SETCERT_ST_INIT:
10037 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010038 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010039 if (ci_putchk(si_ic(si), trash) == -1) {
10040 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010041 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010042 }
10043 appctx->st2 = SETCERT_ST_GEN;
10044 /* fallthrough */
10045 case SETCERT_ST_GEN:
10046 /*
10047 * This state generates the ckch instances with their
10048 * sni_ctxs and SSL_CTX.
10049 *
William Lallemand430413e2019-10-28 14:30:47 +010010050 * Since the SSL_CTX generation can be CPU consumer, we
10051 * yield every 10 instances.
10052 */
William Lallemand8f840d72019-10-23 10:53:05 +020010053
William Lallemandbeea2a42019-10-30 17:45:33 +010010054 old_ckchs = appctx->ctx.ssl.old_ckchs;
10055 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010056
William Lallemandbeea2a42019-10-30 17:45:33 +010010057 if (!new_ckchs)
10058 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010059
William Lallemandbeea2a42019-10-30 17:45:33 +010010060 /* get the next ckchi to regenerate */
10061 ckchi = appctx->ctx.ssl.next_ckchi;
10062 /* we didn't start yet, set it to the first elem */
10063 if (ckchi == NULL)
10064 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010065
William Lallemandbeea2a42019-10-30 17:45:33 +010010066 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10067 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10068 struct ckch_inst *new_inst;
William Lallemand8f840d72019-10-23 10:53:05 +020010069
William Lallemandbeea2a42019-10-30 17:45:33 +010010070 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10071 if (y >= 10) {
10072 /* save the next ckchi to compute */
10073 appctx->ctx.ssl.next_ckchi = ckchi;
10074 goto yield;
10075 }
William Lallemand8f840d72019-10-23 10:53:05 +020010076
William Lallemandbeea2a42019-10-30 17:45:33 +010010077 if (new_ckchs->multi)
10078 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10079 else
10080 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
William Lallemand8f840d72019-10-23 10:53:05 +020010081
William Lallemandbeea2a42019-10-30 17:45:33 +010010082 if (errcode & ERR_CODE)
10083 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010084
William Lallemandbeea2a42019-10-30 17:45:33 +010010085 /* display one dot per new instance */
10086 chunk_appendf(trash, ".");
10087 /* link the new ckch_inst to the duplicate */
10088 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10089 y++;
10090 }
William Lallemand430413e2019-10-28 14:30:47 +010010091 appctx->st2 = SETCERT_ST_INSERT;
10092 /* fallthrough */
10093 case SETCERT_ST_INSERT:
10094 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010095
William Lallemandbeea2a42019-10-30 17:45:33 +010010096 old_ckchs = appctx->ctx.ssl.old_ckchs;
10097 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010098
William Lallemandbeea2a42019-10-30 17:45:33 +010010099 if (!new_ckchs)
10100 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010101
William Lallemandbeea2a42019-10-30 17:45:33 +010010102 /* First, we insert every new SNIs in the trees */
10103 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10104 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10105 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10106 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10107 }
William Lallemand8f840d72019-10-23 10:53:05 +020010108
William Lallemandbeea2a42019-10-30 17:45:33 +010010109 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10110 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
10111 struct sni_ctx *sc0, *sc0s;
William Lallemand430413e2019-10-28 14:30:47 +010010112
William Lallemandbeea2a42019-10-30 17:45:33 +010010113 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10114 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10115 ebmb_delete(&sc0->name);
10116 LIST_DEL(&sc0->by_ckch_inst);
10117 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010118 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010119 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10120 LIST_DEL(&ckchi->by_ckchs);
10121 free(ckchi);
10122 }
William Lallemand8f840d72019-10-23 10:53:05 +020010123
William Lallemandbeea2a42019-10-30 17:45:33 +010010124 /* Replace the old ckchs by the new one */
10125 ebmb_delete(&old_ckchs->node);
10126 ckchs_free(old_ckchs);
10127 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010128 appctx->st2 = SETCERT_ST_FIN;
10129 /* fallthrough */
10130 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010131 /* we achieved the transaction, we can set everything to NULL */
10132 free(ckchs_transaction.path);
10133 ckchs_transaction.path = NULL;
10134 ckchs_transaction.new_ckchs = NULL;
10135 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010136 goto end;
10137 }
William Lallemand8f840d72019-10-23 10:53:05 +020010138 }
William Lallemand430413e2019-10-28 14:30:47 +010010139end:
William Lallemand8f840d72019-10-23 10:53:05 +020010140
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010141 chunk_appendf(trash, "\nSuccess!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010142 if (ci_putchk(si_ic(si), trash) == -1)
10143 si_rx_room_blk(si);
10144 free_trash_chunk(trash);
10145 /* success: call the release function and don't come back */
10146 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010147yield:
10148 /* store the state */
10149 if (ci_putchk(si_ic(si), trash) == -1)
10150 si_rx_room_blk(si);
10151 free_trash_chunk(trash);
10152 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010153 return 0; /* should come back */
10154
10155error:
10156 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010157 if (trash) {
10158 chunk_appendf(trash, "\n%sFailed!\n", err);
10159 if (ci_putchk(si_ic(si), trash) == -1)
10160 si_rx_room_blk(si);
10161 free_trash_chunk(trash);
10162 }
William Lallemand430413e2019-10-28 14:30:47 +010010163 /* error: call the release function and don't come back */
10164 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010165}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010166
10167/*
10168 * Parsing function of 'commit ssl cert'
10169 */
10170static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10171{
10172 char *err = NULL;
10173
10174 if (!*args[3])
10175 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10176
10177 /* The operations on the CKCH architecture are locked so we can
10178 * manipulate ckch_store and ckch_inst */
10179 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10180 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10181
10182 if (!ckchs_transaction.path) {
10183 memprintf(&err, "No ongoing transaction! !\n");
10184 goto error;
10185 }
10186
10187 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10188 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10189 goto error;
10190 }
10191
10192 /* init the appctx structure */
10193 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010194 appctx->ctx.ssl.next_ckchi = NULL;
10195 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10196 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10197
10198 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10199 return 0;
10200
10201error:
10202
10203 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10204 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10205
10206 return cli_dynerr(appctx, err);
10207}
10208
10209
William Lallemand8f840d72019-10-23 10:53:05 +020010210/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010211 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010212 */
William Lallemand150bfa82019-09-19 17:12:49 +020010213static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10214{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010215 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010216 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010217 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010218 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010219 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010220 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010221 char *end;
10222 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010223 struct cert_key_and_chain *ckch;
10224 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010225
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010226 if ((buf = alloc_trash_chunk()) == NULL)
10227 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010228
10229 if (!*args[3] || !payload)
10230 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10231
10232 /* The operations on the CKCH architecture are locked so we can
10233 * manipulate ckch_store and ckch_inst */
10234 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10235 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10236
William Lallemand8f840d72019-10-23 10:53:05 +020010237 if (!chunk_strcpy(buf, args[3])) {
10238 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10239 errcode |= ERR_ALERT | ERR_FATAL;
10240 goto end;
10241 }
10242
William Lallemand44b35322019-10-17 16:28:40 +020010243 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010244 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010245 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010246 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10247 *end = '\0';
10248 type = cert_exts[i].type;
10249 break;
10250 }
10251 }
10252
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010253 appctx->ctx.ssl.old_ckchs = NULL;
10254 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010255
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010256 /* if there is an ongoing transaction */
10257 if (ckchs_transaction.path) {
10258 /* if the ongoing transaction is a bundle, we need to find which part of the bundle need to be updated */
William Lallemand963b2e72019-10-14 11:38:36 +020010259#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010260 if (ckchs_transaction.new_ckchs->multi) {
William Lallemand963b2e72019-10-14 11:38:36 +020010261 char *end = NULL;
10262 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010263
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010264 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010265 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010266 end = strrchr(buf->area, '.');
William Lallemand963b2e72019-10-14 11:38:36 +020010267 for (j = 0; *end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
10268 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10269 bundle = j; /* keep the type of certificate so we insert it at the right place */
10270 *end = '\0'; /* it's a bundle let's end the string*/
10271 break;
10272 }
William Lallemand150bfa82019-09-19 17:12:49 +020010273 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010274 if (bundle < 0) {
10275 memprintf(&err, "The ongoing transaction is the '%s' bundle. You need to specify which part of the bundle you want to update ('%s.{rsa,ecdsa,dsa}')\n", ckchs_transaction.path, buf->area);
10276 errcode |= ERR_ALERT | ERR_FATAL;
10277 goto end;
10278 }
10279 }
10280#endif
10281
10282 /* if there is an ongoing transaction, check if this is the same file */
10283 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10284 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10285 errcode |= ERR_ALERT | ERR_FATAL;
10286 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010287 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010288
10289 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10290
10291 } else {
10292 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10293
10294 /* lookup for the certificate in the tree:
10295 * check if this is used as a bundle AND as a unique certificate */
10296 for (i = 0; i < 2; i++) {
10297
10298 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10299 /* only the bundle name is in the tree and you should
10300 * never update a bundle name, only a filename */
10301 if (bundle < 0 && find_ckchs[i]->multi) {
10302 /* we tried to look for a non-bundle and we found a bundle */
10303 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10304 err ? err : "", args[3], args[3]);
10305 errcode |= ERR_ALERT | ERR_FATAL;
10306 goto end;
10307 }
10308 /* If we want a bundle but this is not a bundle */
10309 /* note that it should never happen */
10310 if (bundle >= 0 && find_ckchs[i]->multi == 0)
10311 goto end;
10312 }
10313#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10314 {
10315 char *end = NULL;
10316 int j;
10317
10318 /* check if it was used in a bundle by removing the
10319 * .dsa/.rsa/.ecdsa at the end of the filename */
10320 end = strrchr(buf->area, '.');
10321 for (j = 0; *end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
10322 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10323 bundle = j; /* keep the type of certificate so we insert it at the right place */
10324 *end = '\0'; /* it's a bundle let's end the string*/
10325 break;
10326 }
10327 }
10328 }
William Lallemand963b2e72019-10-14 11:38:36 +020010329#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010330 /* bundles are not supported here, so we don't need to lookup again */
10331 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010332#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010333 }
10334
10335 if (find_ckchs[0] && find_ckchs[1]) {
10336 memprintf(&err, "%sUpdating a certificate which is used in the HAProxy configuration as a bundle and as a unique certificate is not supported. ('%s' and '%s')\n",
10337 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10338 errcode |= ERR_ALERT | ERR_FATAL;
10339 goto end;
10340 }
10341
10342 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
10343
10344 /* this is a new transaction, set the path of the transaction */
10345 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10346 if (!appctx->ctx.ssl.path) {
10347 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10348 errcode |= ERR_ALERT | ERR_FATAL;
10349 goto end;
10350 }
10351
William Lallemand150bfa82019-09-19 17:12:49 +020010352 }
10353
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010354 if (!appctx->ctx.ssl.old_ckchs) {
10355 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010356 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010357 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010358 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010359 }
10360
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010361
10362 old_ckchs = appctx->ctx.ssl.old_ckchs;
10363
10364 /* TODO: handle filters */
10365 if (old_ckchs->filters) {
10366 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10367 err ? err : "");
10368 errcode |= ERR_ALERT | ERR_FATAL;
10369 goto end;
10370 }
10371
10372 /* duplicate the ckch store */
10373 new_ckchs = ckchs_dup(old_ckchs);
10374 if (!new_ckchs) {
10375 memprintf(&err, "%sCannot allocate memory!\n",
10376 err ? err : "");
10377 errcode |= ERR_ALERT | ERR_FATAL;
10378 goto end;
10379 }
10380
10381 if (!new_ckchs->multi)
10382 ckch = new_ckchs->ckch;
10383 else
10384 ckch = &new_ckchs->ckch[bundle];
10385
10386 /* appply the change on the duplicate */
10387 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10388 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10389 errcode |= ERR_ALERT | ERR_FATAL;
10390 goto end;
10391 }
10392
10393 appctx->ctx.ssl.new_ckchs = new_ckchs;
10394
10395 /* we succeed, we can save the ckchs in the transaction */
10396
10397 /* if there wasn't a transaction, update the old ckchs */
10398 if (!ckchs_transaction.old_ckchs && !ckchs_transaction.old_ckchs) {
10399 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10400 ckchs_transaction.path = appctx->ctx.ssl.path;
10401 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10402 } else {
10403 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10404
10405 }
10406
10407 /* free the previous ckchs if there was a transaction */
10408 ckchs_free(ckchs_transaction.new_ckchs);
10409
10410 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10411
10412
William Lallemand8f840d72019-10-23 10:53:05 +020010413 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010414
William Lallemand8f840d72019-10-23 10:53:05 +020010415end:
10416 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010417
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010418 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010419
10420 ckchs_free(appctx->ctx.ssl.new_ckchs);
10421 appctx->ctx.ssl.new_ckchs = NULL;
10422
10423 appctx->ctx.ssl.old_ckchs = NULL;
10424
10425 free(appctx->ctx.ssl.path);
10426 appctx->ctx.ssl.path = NULL;
10427
10428 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010429 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010430 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010431
10432 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10433 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010434 }
William Lallemand8f840d72019-10-23 10:53:05 +020010435 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010436}
10437
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010438static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010439{
10440#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
10441 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010442 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010443
10444 if (!payload)
10445 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020010446
10447 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010448 if (!*payload)
10449 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010450
10451 /* remove \r and \n from the payload */
10452 for (i = 0, j = 0; payload[i]; i++) {
10453 if (payload[i] == '\r' || payload[i] == '\n')
10454 continue;
10455 payload[j++] = payload[i];
10456 }
10457 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010458
Willy Tarreau1c913e42018-08-22 05:26:57 +020010459 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010460 if (ret < 0)
10461 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010462
Willy Tarreau1c913e42018-08-22 05:26:57 +020010463 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020010464 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020010465 if (err)
10466 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
10467 else
10468 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010469 }
Willy Tarreau9d008692019-08-09 11:21:01 +020010470
10471 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010472#else
Willy Tarreau9d008692019-08-09 11:21:01 +020010473 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010474#endif
10475
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010476}
10477
Willy Tarreau86a394e2019-05-09 14:15:32 +020010478#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010479static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
10480{
10481 switch (arg->type) {
10482 case ARGT_STR:
10483 smp->data.type = SMP_T_STR;
10484 smp->data.u.str = arg->data.str;
10485 return 1;
10486 case ARGT_VAR:
10487 if (!vars_get_by_desc(&arg->data.var, smp))
10488 return 0;
10489 if (!sample_casts[smp->data.type][SMP_T_STR])
10490 return 0;
10491 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
10492 return 0;
10493 return 1;
10494 default:
10495 return 0;
10496 }
10497}
10498
10499static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
10500 const char *file, int line, char **err)
10501{
10502 switch(args[0].data.sint) {
10503 case 128:
10504 case 192:
10505 case 256:
10506 break;
10507 default:
10508 memprintf(err, "key size must be 128, 192 or 256 (bits).");
10509 return 0;
10510 }
10511 /* Try to decode a variable. */
10512 vars_check_arg(&args[1], NULL);
10513 vars_check_arg(&args[2], NULL);
10514 vars_check_arg(&args[3], NULL);
10515 return 1;
10516}
10517
10518/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
10519static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
10520{
10521 struct sample nonce, key, aead_tag;
10522 struct buffer *smp_trash, *smp_trash_alloc;
10523 EVP_CIPHER_CTX *ctx;
10524 int dec_size, ret;
10525
10526 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
10527 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
10528 return 0;
10529
10530 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
10531 if (!sample_conv_var2smp_str(&arg_p[2], &key))
10532 return 0;
10533
10534 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
10535 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
10536 return 0;
10537
10538 smp_trash = get_trash_chunk();
10539 smp_trash_alloc = alloc_trash_chunk();
10540 if (!smp_trash_alloc)
10541 return 0;
10542
10543 ctx = EVP_CIPHER_CTX_new();
10544
10545 if (!ctx)
10546 goto err;
10547
10548 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
10549 if (dec_size < 0)
10550 goto err;
10551 smp_trash->data = dec_size;
10552
10553 /* Set cipher type and mode */
10554 switch(arg_p[0].data.sint) {
10555 case 128:
10556 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
10557 break;
10558 case 192:
10559 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
10560 break;
10561 case 256:
10562 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
10563 break;
10564 }
10565
10566 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
10567
10568 /* Initialise IV */
10569 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
10570 goto err;
10571
10572 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
10573 if (dec_size < 0)
10574 goto err;
10575 smp_trash->data = dec_size;
10576
10577 /* Initialise key */
10578 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
10579 goto err;
10580
10581 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
10582 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
10583 goto err;
10584
10585 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
10586 if (dec_size < 0)
10587 goto err;
10588 smp_trash_alloc->data = dec_size;
10589 dec_size = smp_trash->data;
10590
10591 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
10592 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
10593
10594 if (ret <= 0)
10595 goto err;
10596
10597 smp->data.u.str.data = dec_size + smp_trash->data;
10598 smp->data.u.str.area = smp_trash->area;
10599 smp->data.type = SMP_T_BIN;
10600 smp->flags &= ~SMP_F_CONST;
10601 free_trash_chunk(smp_trash_alloc);
10602 return 1;
10603
10604err:
10605 free_trash_chunk(smp_trash_alloc);
10606 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010607}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010608# endif
William Lallemand32af2032016-10-29 18:09:35 +020010609
10610/* register cli keywords */
10611static struct cli_kw_list cli_kws = {{ },{
10612#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10613 { { "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 +020010614 { { "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 +020010615#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010616 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010617 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
10618 { { "commit", "ssl", "cert", NULL }, "commit ssl cert <certfile> : commit a certificate file", cli_parse_commit_cert, cli_io_handler_commit_cert, cli_release_commit_cert },
William Lallemand32af2032016-10-29 18:09:35 +020010619 { { NULL }, NULL, NULL, NULL }
10620}};
10621
Willy Tarreau0108d902018-11-25 19:14:37 +010010622INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020010623
Willy Tarreau7875d092012-09-10 08:20:03 +020010624/* Note: must not be declared <const> as its list will be overwritten.
10625 * Please take care of keeping this list alphabetically sorted.
10626 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010627static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020010628 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010629 { "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 +010010630#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010010631 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010010632#endif
Emeric Brun645ae792014-04-30 14:21:06 +020010633 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010010634#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
10635 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
10636#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010010637 { "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 +020010638 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020010639 { "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 +020010640 { "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 +020010641#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020010642 { "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 -040010643#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010644#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040010645 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
10646 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040010647 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
10648#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010649 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
10650 { "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 +010010651 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010652 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020010653 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
10654 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10655 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10656 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10657 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10658 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
10659 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
10660 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010010661 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010662 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
10663 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010010664 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020010665 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
10666 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10667 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10668 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10669 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10670 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
10671 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020010672 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010673 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010010674 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010675 { "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 +010010676 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010010677 { "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 +020010678 { "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 +010010679 { "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 +020010680 { "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 +010010681#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010682 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020010683#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010010684#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010685 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020010686#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010687 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010688#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020010689 { "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 -040010690#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020010691 { "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 +020010692#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010693 { "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 -040010694#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010695#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040010696 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
10697 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040010698 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
10699#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040010700#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010701 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040010702#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010703 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10704 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
10705 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
10706 { "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 +020010707 { NULL, NULL, 0, 0, 0 },
10708}};
10709
Willy Tarreau0108d902018-11-25 19:14:37 +010010710INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
10711
Willy Tarreau7875d092012-09-10 08:20:03 +020010712/* Note: must not be declared <const> as its list will be overwritten.
10713 * Please take care of keeping this list alphabetically sorted.
10714 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010715static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010716 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
10717 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010718 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020010719}};
10720
Willy Tarreau0108d902018-11-25 19:14:37 +010010721INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
10722
Willy Tarreau79eeafa2012-09-14 07:53:05 +020010723/* Note: must not be declared <const> as its list will be overwritten.
10724 * Please take care of keeping this list alphabetically sorted, doing so helps
10725 * all code contributors.
10726 * Optional keywords are also declared with a NULL ->parse() function so that
10727 * the config parser can report an appropriate error when a known keyword was
10728 * not enabled.
10729 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010010730static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020010731 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010010732 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
10733 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
10734 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010735#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010736 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
10737#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010010738 { "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 +010010739 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010010740 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020010741 { "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 +010010742 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020010743 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
10744 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010010745 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
10746 { NULL, NULL, 0 },
10747};
10748
Willy Tarreau0108d902018-11-25 19:14:37 +010010749/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
10750
Willy Tarreau51fb7652012-09-18 18:24:39 +020010751static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020010752 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010753 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
10754 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
10755 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
10756 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
10757 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
10758 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010759#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010760 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
10761#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010762 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
10763 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
10764 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
10765 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
10766 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
10767 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
10768 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
10769 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
10770 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
10771 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020010772 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010773 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020010774 { "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 +020010775 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
10776 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
10777 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
10778 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020010779 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010780 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
10781 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010782 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
10783 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010784 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
10785 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
10786 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
10787 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
10788 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020010789 { NULL, NULL, 0 },
10790}};
Emeric Brun46591952012-05-18 15:47:34 +020010791
Willy Tarreau0108d902018-11-25 19:14:37 +010010792INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
10793
Willy Tarreau92faadf2012-10-10 23:04:25 +020010794/* Note: must not be declared <const> as its list will be overwritten.
10795 * Please take care of keeping this list alphabetically sorted, doing so helps
10796 * all code contributors.
10797 * Optional keywords are also declared with a NULL ->parse() function so that
10798 * the config parser can report an appropriate error when a known keyword was
10799 * not enabled.
10800 */
10801static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010010802 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010010803 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010804 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010010805 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020010806 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010807 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
10808 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010809#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010810 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
10811#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010812 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
10813 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
10814 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
10815 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
10816 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
10817 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
10818 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
10819 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
10820 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
10821 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
10822 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
10823 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
10824 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
10825 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
10826 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
10827 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
10828 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
10829 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010010830 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010831 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
10832 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
10833 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
10834 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
10835 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
10836 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
10837 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
10838 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
10839 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
10840 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020010841 { NULL, NULL, 0, 0 },
10842}};
10843
Willy Tarreau0108d902018-11-25 19:14:37 +010010844INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
10845
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010846static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010847 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
10848 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010849 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010850 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
10851 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010010852#ifndef OPENSSL_NO_DH
10853 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
10854#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010855 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010856#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010857 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010858#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010010859 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
10860#ifndef OPENSSL_NO_DH
10861 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
10862#endif
10863 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
10864 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
10865 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
10866 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010867 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010010868 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
10869 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010870#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010871 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
10872 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
10873#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010874 { 0, NULL, NULL },
10875}};
10876
Willy Tarreau0108d902018-11-25 19:14:37 +010010877INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
10878
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010879/* Note: must not be declared <const> as its list will be overwritten */
10880static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020010881#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010882 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
10883#endif
10884 { NULL, NULL, 0, 0, 0 },
10885}};
10886
10887INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
10888
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020010889/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010010890static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020010891 .snd_buf = ssl_sock_from_buf,
10892 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010010893 .subscribe = ssl_subscribe,
10894 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020010895 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020010896 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020010897 .rcv_pipe = NULL,
10898 .snd_pipe = NULL,
10899 .shutr = NULL,
10900 .shutw = ssl_sock_shutw,
10901 .close = ssl_sock_close,
10902 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010010903 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010010904 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010010905 .prepare_srv = ssl_sock_prepare_srv_ctx,
10906 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010010907 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010010908 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020010909};
10910
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010911enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
10912 struct session *sess, struct stream *s, int flags)
10913{
10914 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010915 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010916
10917 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010918 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010919
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010920 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010921 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010922 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010923 s->req.flags |= CF_READ_NULL;
10924 return ACT_RET_YIELD;
10925 }
10926 }
10927 return (ACT_RET_CONT);
10928}
10929
10930static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
10931{
10932 rule->action_ptr = ssl_action_wait_for_hs;
10933
10934 return ACT_RET_PRS_OK;
10935}
10936
10937static struct action_kw_list http_req_actions = {ILH, {
10938 { "wait-for-handshake", ssl_parse_wait_for_hs },
10939 { /* END */ }
10940}};
10941
Willy Tarreau0108d902018-11-25 19:14:37 +010010942INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
10943
Willy Tarreau5db847a2019-05-09 14:13:35 +020010944#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010945
10946static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
10947{
10948 if (ptr) {
10949 chunk_destroy(ptr);
10950 free(ptr);
10951 }
10952}
10953
10954#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010010955static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
10956{
Willy Tarreaubafbe012017-11-24 17:34:44 +010010957 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010010958}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010959
Emeric Brun46591952012-05-18 15:47:34 +020010960__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020010961static void __ssl_sock_init(void)
10962{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010963#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020010964 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010965 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010966#endif
Emeric Brun46591952012-05-18 15:47:34 +020010967
Willy Tarreauef934602016-12-22 23:12:01 +010010968 if (global_ssl.listen_default_ciphers)
10969 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
10970 if (global_ssl.connect_default_ciphers)
10971 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010972#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010973 if (global_ssl.listen_default_ciphersuites)
10974 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
10975 if (global_ssl.connect_default_ciphersuites)
10976 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10977#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010010978
Willy Tarreau13e14102016-12-22 20:25:26 +010010979 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010980#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020010981 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080010982#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010983#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020010984 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010985 n = sk_SSL_COMP_num(cm);
10986 while (n--) {
10987 (void) sk_SSL_COMP_pop(cm);
10988 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010989#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010990
Willy Tarreau5db847a2019-05-09 14:13:35 +020010991#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020010992 ssl_locking_init();
10993#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020010994#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010995 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
10996#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020010997 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020010998 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 +010010999 ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011000#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011001 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011002 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011003#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011004#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11005 hap_register_post_check(tlskeys_finalize_config);
11006#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011007
11008 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11009 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11010
11011#ifndef OPENSSL_NO_DH
11012 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11013 hap_register_post_deinit(ssl_free_dh);
11014#endif
11015#ifndef OPENSSL_NO_ENGINE
11016 hap_register_post_deinit(ssl_free_engines);
11017#endif
11018 /* Load SSL string for the verbose & debug mode. */
11019 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011020 ha_meth = BIO_meth_new(0x666, "ha methods");
11021 BIO_meth_set_write(ha_meth, ha_ssl_write);
11022 BIO_meth_set_read(ha_meth, ha_ssl_read);
11023 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11024 BIO_meth_set_create(ha_meth, ha_ssl_new);
11025 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11026 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11027 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011028
11029 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011030}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011031
Willy Tarreau80713382018-11-26 10:19:54 +010011032/* Compute and register the version string */
11033static void ssl_register_build_options()
11034{
11035 char *ptr = NULL;
11036 int i;
11037
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011038 memprintf(&ptr, "Built with OpenSSL version : "
11039#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011040 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011041#else /* OPENSSL_IS_BORINGSSL */
11042 OPENSSL_VERSION_TEXT
11043 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011044 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011045 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011046#endif
11047 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011048#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011049 "no (library version too old)"
11050#elif defined(OPENSSL_NO_TLSEXT)
11051 "no (disabled via OPENSSL_NO_TLSEXT)"
11052#else
11053 "yes"
11054#endif
11055 "", ptr);
11056
11057 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11058#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11059 "yes"
11060#else
11061#ifdef OPENSSL_NO_TLSEXT
11062 "no (because of OPENSSL_NO_TLSEXT)"
11063#else
11064 "no (version might be too old, 0.9.8f min needed)"
11065#endif
11066#endif
11067 "", ptr);
11068
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011069 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11070 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11071 if (methodVersions[i].option)
11072 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011073
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011074 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011075}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011076
Willy Tarreau80713382018-11-26 10:19:54 +010011077INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011078
Emeric Brun46591952012-05-18 15:47:34 +020011079
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011080#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011081void ssl_free_engines(void) {
11082 struct ssl_engine_list *wl, *wlb;
11083 /* free up engine list */
11084 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11085 ENGINE_finish(wl->e);
11086 ENGINE_free(wl->e);
11087 LIST_DEL(&wl->list);
11088 free(wl);
11089 }
11090}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011091#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011092
Remi Gacogned3a23c32015-05-28 16:39:47 +020011093#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011094void ssl_free_dh(void) {
11095 if (local_dh_1024) {
11096 DH_free(local_dh_1024);
11097 local_dh_1024 = NULL;
11098 }
11099 if (local_dh_2048) {
11100 DH_free(local_dh_2048);
11101 local_dh_2048 = NULL;
11102 }
11103 if (local_dh_4096) {
11104 DH_free(local_dh_4096);
11105 local_dh_4096 = NULL;
11106 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011107 if (global_dh) {
11108 DH_free(global_dh);
11109 global_dh = NULL;
11110 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011111}
11112#endif
11113
11114__attribute__((destructor))
11115static void __ssl_sock_deinit(void)
11116{
11117#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011118 if (ssl_ctx_lru_tree) {
11119 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011120 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011121 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011122#endif
11123
Willy Tarreau5db847a2019-05-09 14:13:35 +020011124#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011125 ERR_remove_state(0);
11126 ERR_free_strings();
11127
11128 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011129#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011130
Willy Tarreau5db847a2019-05-09 14:13:35 +020011131#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011132 CRYPTO_cleanup_all_ex_data();
11133#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011134 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011135}
11136
11137
Emeric Brun46591952012-05-18 15:47:34 +020011138/*
11139 * Local variables:
11140 * c-indent-level: 8
11141 * c-basic-offset: 8
11142 * End:
11143 */